Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Oct 12th, 2018 61 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. public static boolean connect(Host host, String message, int mode) throws Exception {
  2.         EventLoopGroup group = new NioEventLoopGroup();
  3.         try {
  4.             Bootstrap clientBootstrap = new Bootstrap();
  5.  
  6.             clientBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 100);
  7.  
  8.             clientBootstrap.group(group);
  9.             clientBootstrap.channel(NioSocketChannel.class);
  10.             clientBootstrap.remoteAddress(new InetSocketAddress(host.getIp(), host.getPort()));
  11.  
  12.             clientBootstrap.handler(new ChannelInitializer<SocketChannel>() {
  13.                 protected void initChannel(SocketChannel socketChannel) {
  14.  
  15.                     //TODO, TIMEOUT BILGISI ILE DOLDUR BURAYI
  16.                     //socketChannel.pipeline().addLast(new ReadTimeoutHandler(1));
  17.                     //socketChannel.pipeline().addLast("idleStateHandler", new IdleStateHandler(2, 2, 4));
  18.  
  19.                     socketChannel.pipeline().addLast(new FalconClientHandler(host, message, mode));
  20.                 }
  21.             });
  22.  
  23.             ChannelFuture channelFuture = clientBootstrap.connect().sync();
  24.  
  25.             //TODO I NEED TO CATCH IT SOMEWHERE IN HERE
  26.             channelFuture.channel().closeFuture().sync();
  27.             return true;
  28.         } catch (Exception e) {
  29.             System.err.println("Connection timed out --> " + e);
  30.             host.setActive(false); //connection kurulamadı demektir. Bir sonraki mesaj geldiğinde bu hostun açılıp açılmadığı denenecek.
  31.             return false;
  32.         } finally {
  33.             group.shutdownGracefully().sync();
  34.             if (mode == 0) { //arka planda sunucu hep ayakta olmalı. Mode 0 da asla bağlantı kesilmemeli. Kesiliyorsa host kapanmıştır.
  35.                 host.setActive(false);
  36.                 return false;
  37.             }
  38.         }
  39.     }
  40.    
  41. @Override
  42.     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)throws Exception {
  43.         cause.printStackTrace();
  44.         //CLOSE CONNECTION AND I HAVE TO PASS THE INFORMATION WHY CONNECTION CLOSED
  45.         ctx.close();
  46.     }
  47.    
  48. @Override
  49.     public void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf in) {
  50.         InetSocketAddress socketAddress = (InetSocketAddress) channelHandlerContext.channel().remoteAddress();
  51.         InetAddress inetaddress = socketAddress.getAddress();
  52.         String ipAddress = inetaddress.getHostAddress(); // IP address of client
  53.  
  54.         TCPHandshakeMessage tcpMessage;
  55.         byte[] signal;
  56.         String input = in.toString(CharsetUtil.US_ASCII);
  57.  
  58.         /*
  59.          * mode 0 -> Amaç sunucuların ayakta olup olmadığını anlamak
  60.          * mode 1 -> Amaç sunuculara mesaj göndermek
  61.          * */
  62.         if(this.mode == 1){  //MODE 1 BAŞLANGICI
  63.             //I WRITE THIS ON PURPOSE
  64.             double x = 12 / 0;
  65.             System.err.println("MESSAGE SENT TO " + message + " IP : " + ipAddress);
  66.             this.host.setActive(true);
  67.             //TODO TCP MESSAGE SINIFINI KULLAN
  68.             signal = message.getBytes();
  69.             sendMessage(channelHandlerContext, signal);
  70.             /*try {
  71.  
  72.             }catch (Exception e){
  73.                 System.out.println("An Error Occured " + e);
  74.             }finally {
  75.                 channelHandlerContext.channel().close();
  76.             }*/
  77.         }
  78. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top