Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //kode untuk menjalankan server
- public class CoreServer {
- private static final Logger LOGGER = LoggerFactory.getLogger(CoreServer.class);
- private Integer port;
- // @Getter
- // Channel channel;
- //
- // @Getter
- // ChannelFuture channelFuture;
- EventLoopGroup bossGroup = new NioEventLoopGroup(10);
- EventLoopGroup workerGroup = new NioEventLoopGroup(10);
- public CoreServer(int port) {
- this.port = port;
- }
- public void start() throws Exception {
- LOGGER.debug("\n\n------------------------------------------< STARTING SERVER >---------------------------------------");
- try {
- ServerBootstrap bootServer = new ServerBootstrap();
- bootServer.group(bossGroup, workerGroup)
- .handler(new LoggingHandler(LogLevel.DEBUG))
- .channel(NioServerSocketChannel.class)
- .childHandler(new ServerInitializer())
- .option(ChannelOption.SO_BACKLOG, 128)
- .childOption(ChannelOption.SO_KEEPALIVE, true);
- ChannelFuture cFuture = bootServer.bind(port).sync();
- cFuture.channel().closeFuture().sync();
- } catch (InterruptedException e) {
- e.printStackTrace();
- LOGGER.error("Error Menjalankan Server [{}]", e);
- System.out.println("Exception in server thread");
- } finally {
- shutdown();
- }
- }
- public void shutdown() {
- LOGGER.debug("\n------------------------------------------< SHUTDOWN SERVER >---------------------------------------");
- try {
- workerGroup.shutdownGracefully().sync();
- bossGroup.shutdownGracefully().sync();
- } catch (InterruptedException ex) {
- Exceptions.printStackTrace(ex);
- }
- }
- }
- //listener untuk menerima dan mengirim data
- @Sharable
- public class ServerListener extends ChannelInboundHandlerAdapter {
- private static final Logger LOGGER = LoggerFactory.getLogger(ServerListener.class);
- private ServerAction action = new ServerAction();
- @Override
- public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
- ctx.flush();
- }
- @Override
- public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
- if (msg != null) {
- action.readMessage(ctx.channel(), msg);
- }
- //ReferenceCountUtil.release(msg);
- }
- @Override
- public void channelActive(ChannelHandlerContext ctx) throws Exception {
- super.channelActive(ctx);
- //action.addChannel(ctx.channel());
- //action.viewConnectedChannel();
- LOGGER.info("Client {} now connected", ctx.channel().remoteAddress());
- }
- @Override
- public void channelInactive(ChannelHandlerContext ctx) throws Exception {
- super.channelInactive(ctx);
- //action.removeChannel(ctx.channel());
- //action.viewConnectedChannel();
- ctx.flush();
- ctx.close();
- LOGGER.info("Client {} disconnect", ctx.channel().remoteAddress());
- }
- }
- //server initializer
- public class ServerInitializer extends ChannelInitializer<SocketChannel> {
- @Override
- protected void initChannel(SocketChannel socketChannel) throws Exception {
- socketChannel.pipeline().addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
- socketChannel.pipeline().addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
- socketChannel.pipeline().addLast(new ServerListener());
- socketChannel.pipeline().addLast(new ChannelExceptionHandler());
- }
- }
- //action ketika ada pesan dari server
- public class ServerAction {
- private List<Channel> channels = new ArrayList<Channel>();
- private static final Logger LOGGER = LoggerFactory.getLogger(ServerAction.class);
- public void addChannel(Channel client) {
- channels.add(client);
- }
- public void viewConnectedChannel() {
- LOGGER.info("------------------------< LIST CONNECTED CLIENT >-------------");
- for (Channel channel : channels) {
- LOGGER.info("Channel {} connected", channel.remoteAddress());
- }
- LOGGER.info("--------------------------------------------------------------");
- }
- public void removeChannel(Channel client) {
- channels.remove(client);
- }
- public void readMessage(Channel client, Object msg) {
- messageChecker(client, msg);
- }
- private void messageChecker(Channel client, Object msg) {
- LOGGER.info("Message From client {} : {} ", client.remoteAddress(), msg.toString());
- JSONObject object = new JSONObject(msg.toString());
- String action = object.get("action").toString();
- if (action.equalsIgnoreCase(RequestAction.PRINTER.toString())) {
- sendResponse(client, ServerUtils.getListPrinter());
- } else if (action.equalsIgnoreCase(RequestAction.FLOOR.toString())) {
- sendResponse(client, ServerUtils.getListFloor());
- } else if (action.equalsIgnoreCase(RequestAction.TABLE.toString())) {
- sendResponse(client, ServerUtils.getListTable());
- } else if (action.equalsIgnoreCase(RequestAction.ITEM.toString())) {
- sendResponse(client, ServerUtils.getListItem());
- } else if (action.equalsIgnoreCase(RequestAction.UNIT.toString())) {
- sendResponse(client, ServerUtils.getListUnit());
- } else if (action.equalsIgnoreCase(RequestAction.PRICE.toString())) {
- sendResponse(client, ServerUtils.getListPrice());
- }
- }
- public void sendResponse(Channel client, String msg) {
- client.write(msg);
- LOGGER.info("Response for {} complete", client.remoteAddress());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement