Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012 The Netty Project
   *
   * The Netty Project licenses this file to you under the Apache License,
   * version 2.0 (the "License"); you may not use this file except in compliance
   * with the License. You may obtain a copy of the License at:
   *
   *   http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  * License for the specific language governing permissions and limitations
  * under the License.
  */
 package divconq.io.test;
 
 
 
 
 public final class LocalEcho {
 	static public Channel chan = null;
 	
 	public static void start() {
 	        // Address to bind on / connect to.
 	        //final LocalAddress addr = new LocalAddress(PORT);
 
 	    	/*
 	    	XElement config = new XElement("Bus", 
 	    			new XElement("SslContext", 
 	    					new XAttribute("Password", "A1s2d3f4"),
 	    					new XAttribute("File", "./packages/dcTest/keys/backend.jks")
 	    			),
 	    			new XElement("Trust", new XAttribute("Thumbprint", "F7:15:BB:A0:68:01:B3:4A:C0:ED:19:58:26:77:1D:E8:98:13:27:AF"))
 	    	);		  
 	    	
 	    	SslContextFactory.init(config);
 	    	
 	        EventLoopGroup el = new NioEventLoopGroup();
 	        */
 	    	
 	        EventLoopGroup el = ..getEventLoopGroup();
 	    	
             // Note that we can use any event loop to ensure certain local channels
             // are handled by the same event loop thread which drives a certain socket channel
             // to reduce the communication latency between socket channels and local channels.
             ServerBootstrap sb = new ServerBootstrap();
             sb.group(el)
               .channel(NioServerSocketChannel.class)
               .childOption(.false)
               .handler(new ChannelInitializer<ServerSocketChannel>() {
                   @Override
                   public void initChannel(ServerSocketChannel chthrows Exception {
                       //ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO));
                   }
               })
               .childHandler(new ChannelInitializer<SocketChannel>() {
                   @Override
                   public void initChannel(SocketChannel chthrows Exception {
                       ch.pipeline().addLast(
                     		 new SslHandler(SslContextFactory.getServerEngine()),
                              //new LoggingHandler(LogLevel.INFO),
                     		 //new CtpMessageDecoder(),
                     		 new StreamInboundHandler()
                               //new LocalEchoServerHandler()
                    	);
                  }
              });
            Bootstrap cb = new Bootstrap();
            cb.group(el)
              .channel(NioSocketChannel.class)
              .handler(new ChannelInitializer<SocketChannel>() {
                  @Override
                  public void initChannel(SocketChannel chthrows Exception {
                      ch.pipeline().addLast(
                    		  new SslHandler(SslContextFactory.getClientEngine()),
                    		  
                              //new LoggingHandler(LogLevel.INFO),
                    		  new CtpMessageEncoder()
                    		  
                              //new LocalEchoClientHandler()
                    	);
                  }
              });
            try {
	            // Start the server.
	            sb.bind(8181).sync();
	            // Start the client.
	            Channel ch = cb.connect("localhost", 8181).sync().channel();
	            
	            . = ch;
	            ..writeAndFlush(new ClientHelloMessage());
            }
            catch (Exception x) {
            	..println("Test dcBus could not start!");
            }
	}
	public static void test1() {
        StreamMessage msg = new StreamMessage();
        
        msg.setPath("/one/two/three/done.txt");
        msg.setFileSize(909090);
        ..write(msg);
        
        msg = new StreamMessage();
        
        msg.setPath("/xyz/frog.txt");
        msg.setFileSize(4545);
        ..write(msg);
        
        ..flush();
	}
	public static void test2() {
		FileSystemFile src = StreamUtil.localFile(Paths.get("c:/temp/test/source"));
		//FileSystemFile src = StreamUtil.localFile(Paths.get("c:/temp/test/betty_2013-10-06_FULL_S1_R1.sig"));
		//FileSystemFile dest = StreamUtil.localFile(Paths.get("c:/temp/testtar/test-files-0.tar.gz"));
		Task t = new Task()
			.withTitle("Streaming Out Test")
		@SuppressWarnings("resource")
		TaskRun trun = StreamUtil.composeStream(t
				src.allocSrc(), 
				new TarStream().withNameHint("test-files-9"), 
			public void completed(TaskRun or) {
				..println("Transfer Out is complete!!");
			}
		});
	}
    static final String PORT = System.getProperty("port""test_port");
    public static void main(String[] argsthrows Exception {
        // Address to bind on / connect to.
        //final LocalAddress addr = new LocalAddress(PORT);
    	/*
    	XElement config = new XElement("Bus", 
    			new XElement("SslContext", 
    					new XAttribute("Password", "A1s2d3f4"),
    					new XAttribute("File", "./packages/dcTest/keys/backend.jks")
    			),
    			new XElement("Trust", new XAttribute("Thumbprint", "F7:15:BB:A0:68:01:B3:4A:C0:ED:19:58:26:77:1D:E8:98:13:27:AF"))
    	);		  
    	
    	SslContextFactory.init(config);
    	
        EventLoopGroup el = new NioEventLoopGroup();
        */
    	
        EventLoopGroup el = ..getEventLoopGroup();
    	
        try {
            // Note that we can use any event loop to ensure certain local channels
            // are handled by the same event loop thread which drives a certain socket channel
            // to reduce the communication latency between socket channels and local channels.
            ServerBootstrap sb = new ServerBootstrap();
            sb.group(el)
              .channel(NioServerSocketChannel.class)
              .childOption(.false)
              .handler(new ChannelInitializer<ServerSocketChannel>() {
                  @Override
                  public void initChannel(ServerSocketChannel chthrows Exception {
                      //ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO));
                  }
              })
              .childHandler(new ChannelInitializer<SocketChannel>() {
                  @Override
                  public void initChannel(SocketChannel chthrows Exception {
                      ch.pipeline().addLast(
                    		  new SslHandler(SslContextFactory.getServerEngine()) //,
                    		 //new CtpMessageDecoder() 
                              //new LoggingHandler(LogLevel.INFO),
                              //new LocalEchoServerHandler()
                    		 );
                  }
              });
            Bootstrap cb = new Bootstrap();
            cb.group(el)
              .channel(NioSocketChannel.class)
              .handler(new ChannelInitializer<SocketChannel>() {
                  @Override
                  public void initChannel(SocketChannel chthrows Exception {
                      ch.pipeline().addLast(
                    		  new SslHandler(SslContextFactory.getClientEngine()),
                    		  
                    		  new CtpMessageEncoder(),
                    		  
                              //new LoggingHandler(LogLevel.INFO),
                              new LocalEchoClientHandler());
                  }
              });
            // Start the server.
            sb.bind(8181).sync();
            // Start the client.
            Channel ch = cb.connect("localhost", 8181).sync().channel();
            // Read commands from the stdin.
            ..println("Enter text (quit to end)");
            
            /*
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            
            for (;;) {
                String line = in.readLine();
                if (line == null || "quit".equalsIgnoreCase(line)) {
                    break;
                }
                // Sends the received line to the server.
                lastWriteFuture = ch.writeAndFlush(line);
            }
            */
        	AtomicLong ctd = new AtomicLong(50000);
        	AtomicReference<ChannelFutureListenercfl = new AtomicReference<>();
        	AtomicReference<Runnablesr = new AtomicReference<>();
        	
        	cfl.set(new ChannelFutureListener() {					
				public void operationComplete(ChannelFuture futurethrows Exception {
					long v = ctd.decrementAndGet();
					//System.out.println("Sending: " + v);
					if (future.isSuccess()) {
						if (v > 0)
							/*
							ch.writeAndFlush(StringUtil.buildSecurityCode(512)).addListener(new ChannelFutureListener() {
								@Override
								public void operationComplete(
										ChannelFuture future) throws Exception {
									System.out.println("got there!");
								}
							});
							*/
							//future.channel().writeAndFlush(StringUtil.buildSecurityCode(512)).addListener(cfl.get());
							//ch.writeAndFlush(StringUtil.buildSecurityCode(512)).addListener(cfl.get());
							ch.eventLoop().execute(sr.get());
						else 
							..println("DONE sending! ");
					}
					else {
						..println("ERROR sending - DONE sending!  " + future.cause());
					}	
				}
			});
        	
        	sr.set(new Runnable() {				
				public void run() {
					String code = StringUtil.buildSecurityCode(512);
					ByteBuf b = Unpooled.wrappedBuffer(Utf8Encoder.encode(code));
				}
			});
        	
        	//ch.eventLoop().n
        	
        	//ch.
        	
            // Sends the random string to the server.
        	//ch.writeAndFlush(StringUtil.buildSecurityCode(512)).addListener(cfl.get());
        	//ch.writeAndFlush(StringUtil.buildSecurityCode(512));
        	//ch.writeAndFlush(StringUtil.buildSecurityCode(512));
        	//ch.writeAndFlush(StringUtil.buildSecurityCode(512));
        	
        	ch.eventLoop().execute(sr.get());
            BufferedReader in = new BufferedReader(new InputStreamReader(.));
            
            for (;;) {
                String line = in.readLine();
                if (line == null || "quit".equalsIgnoreCase(line)) {
                    break;
                }
                //System.out.println("Gotten: " + ((LocalEchoServerHandler) ch.pipeline().last()).gotten.get());
                
                // Sends the received line to the server.
                //lastWriteFuture = ch.writeAndFlush(line);
            }
        } 
        finally {
            el.shutdownGracefully();
        }
    }
New to GrepCode? Check out our FAQ X