Closing the Client / Sender

Michael McGrady mmcgrady at topiatechnology.com
Tue Aug 11 14:38:01 EDT 2009


Sorry, forgot the text.  How do I close the client after notify(....)  
sends the data to the application logic without closing the server?

Mike

Mike McGrady
Principal Investigator AF081-028 AFRL SBIR
Senior Engineer
Topia Technology, Inc
1.253.720.3365
mmcgrady at topiatechnology.com


SERVER Manager Code

NettyKarmaReceiverAdapter receiver = new NettyKarmaReceiverAdapter(new  
InetSocketAddress("localhost", 8080)) ;
receiver.start() ;

CLIENT Manager Code

NettyKarmaSenderAdapter sender = new  
NettyKarmaSenderAdapter("localhost", 8080) ;
sender.start() ;
sender.notify("Hello, World!") ;
sender.stop() ;

RECEIVER ADAPTER Code

	
	public NettyKarmaReceiverAdapter(InetSocketAddress address) {
		this.address = address;
         this.factory =
             new NioServerSocketChannelFactory(
                     Executors.newCachedThreadPool(),
                     Executors.newCachedThreadPool());
         this.bootstrap = new ServerBootstrap(factory);
	}

	private void setup ( ) {
         this.bootstrap.setOption("child.tcpNoDelay", true);
         this.bootstrap.setOption("child.keepAlive", true);
         this.bootstrap.setPipelineFactory(new  
KarmaReceiverPipelineFactory(this));
	}

	public void start ( ) {
		setup() ;
         this.bootstrap.bind(address);	}

SENDER ADAPTER Code

	public NettyKarmaSenderAdapter ( String host, int port ) {
		this.host = host;
		this.port = port;
	}

	public void setup ( ) {		
		ChannelFactory factory =
			new NioClientSocketChannelFactory ( Executors
				.newCachedThreadPool ( ), Executors.newCachedThreadPool ( ) );
		this.bootstrap = new ClientBootstrap ( factory );
		
		this.bootstrap
			.setPipelineFactory ( new KarmaSenderPipelineFactory (this) );
		this.bootstrap.setOption ( "tcpNoDelay", true );
		this.bootstrap.setOption ( "keepAlive", true );
		ChannelFuture future =
			this.bootstrap.connect ( new InetSocketAddress ( this.host,  
this.port ) );
		
		// Wait until the connection attempt succeeds or fails.
		this.channel = future.awaitUninterruptibly ( ).getChannel ( );
		if ( !future.isSuccess ( ) ) {
			future.getCause ( ).printStackTrace ( );
			factory.releaseExternalResources ( );
			return;
		}
	}
	
	public void start ( ) {
		setup ( );
	}
	
	public int stop ( ) {
		????????????????????????????????????????
		return +2;
	}

	public void notify ( File file ) {
		try {
			KarmaChunkedFile chunks = new KarmaChunkedFile(file) ;
			try {
				while (chunks.hasNextChunk()) {
					Object object = chunks.nextChunk ( ) ;
					byte [] bytes = ByteObjectConverter.toBytes(object) ;
					
					ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(bytes) ;
					this.channel.write ( buffer );
				}
			} catch ( Exception e ) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch ( IOException e ) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void notify ( KarmaAumlet aumlet ) {
		byte [] bytes = ByteObjectConverter.toBytes(aumlet) ;
		ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(bytes) ;
		this.channel.write ( buffer );
	}
	
	public void notify ( String message ) {
		byte [] bytes = ByteObjectConverter.toBytes(new  
KarmaMessage(message)) ;
		ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(bytes) ;
		this.channel.write ( buffer );
	}

SENDER PIPELINE Code


public class KarmaSenderPipelineFactory implements  
ChannelPipelineFactory {
	private final NettyKarmaSenderAdapter adapter ;
	
	public KarmaSenderPipelineFactory (NettyKarmaSenderAdapter adapter) {
		this.adapter = adapter;
	}
	
	public ChannelPipeline getPipeline ( ) throws Exception {
		ChannelPipeline pipeline = pipeline ( );
		SSLEngine engine =
			KarmaSslContextFactory.getClientContext ( ).createSSLEngine ( );
		engine.setUseClientMode ( true );
		pipeline.addLast ( "ssl", new KarmaSslHandler ( engine ) );
		pipeline.addLast ( "universal", new KarmaSenderCodecHandler (
			adapter) );
		
		return pipeline;
	}
}

SENDER CODEC HANDLER Code

	

public class KarmaReceiverPipelineFactory implements  
ChannelPipelineFactory {
	private final NettyKarmaReceiverAdapter adapter ;
	
	public KarmaReceiverPipelineFactory ( NettyKarmaReceiverAdapter  
adapter ) {
		this.adapter = adapter;
	}
	
	public ChannelPipeline getPipeline ( ) throws Exception {
		ChannelPipeline pipeline = pipeline ( );
		SSLEngine engine =
			SslContextFactory.getServerContext ( ).createSSLEngine ( );
		engine.setUseClientMode ( false );
		pipeline.addLast ( "ssl", new SslHandler ( engine ) );
		pipeline.addLast ( "universal", new KarmaReceiverCodecHandler (
			adapter ) );
		
		return pipeline;
	}
}

RECEIVER PIPELINE Code

	

public class KarmaReceiverPipelineFactory implements  
ChannelPipelineFactory {
	private final NettyKarmaReceiverAdapter adapter ;
	
	public KarmaReceiverPipelineFactory ( NettyKarmaReceiverAdapter  
adapter ) {
		this.adapter = adapter;
	}
	
	public ChannelPipeline getPipeline ( ) throws Exception {
		ChannelPipeline pipeline = pipeline ( );
		SSLEngine engine =
			KarmaSslContextFactory.getServerContext ( ).createSSLEngine ( );
		engine.setUseClientMode ( false );
		pipeline.addLast ( "ssl", new SslHandler ( engine ) );
		pipeline.addLast ( "universal", new KarmaReceiverCodecHandler (
			adapter ) );
		
		return pipeline;
	}
}

RECEIVER CODEC HANDLER Code

	
	public KarmaReceiverCodecHandler ( KarmaReceiver receiver ) {
		this.receiver = receiver;
		this.charsetName = Charset.defaultCharset ( ).name ( );
	}

	public void messageReceivedUpstream (
		ChannelHandlerContext ctx, MessageEvent evt ) throws Exception {
		Object originalMessage = evt.getMessage ( );
		Object decodedMessage =
			decode ( ctx, evt.getChannel ( ), originalMessage );
		
		if ( originalMessage == decodedMessage ) {
			ctx.sendUpstream ( evt );
		} else {
			if ( ! ( ( evt ).getMessage ( ) instanceof ChannelBuffer ) ) {
				fireMessageReceived ( ctx, decodedMessage, evt.getRemoteAddress  
( ) );
			}
		}
	}
	
	private Object decode (
		ChannelHandlerContext ctx, Channel channel, Object msg )
		throws Exception {
		if ( ! ( msg instanceof ChannelBuffer ) ) {
			return msg;
		}
		
		ChannelBuffer buffer = ( ( ChannelBuffer ) msg ).copy ( );
		int readableBytes = buffer.readableBytes ( );
		byte [ ] bytes = new byte [ readableBytes ];
		buffer.readBytes ( bytes );
		Object object = ByteObjectConverter.toObject ( bytes );
		if (object instanceof KarmaMessage ) {
			this.receiver.update ( ((KarmaMessage)object).message ) ;
		} else if (object instanceof KarmaChunk ) {
			this.receiver.update ( ((KarmaChunk)object) ) ;
		}  else if (object instanceof Serializable ) {
			this.receiver.update ( ((KarmaAumlet)object) ) ;		}
		
		return ( ( ChannelBuffer ) msg ).toString ( charsetName );
	}






-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/netty-users/attachments/20090811/19172921/attachment-0001.html 


More information about the netty-users mailing list