You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by "Trustin Lee (JIRA)" <ji...@apache.org> on 2007/07/31 09:53:53 UTC

[jira] Issue Comment Edited: (DIRMINA-404) The IoSession returned by session = connectFuture.getSession() is different than the one passed to sessionOpened(IoSession session)

    [ https://issues.apache.org/jira/browse/DIRMINA-404?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12516640 ] 

Trustin Lee edited comment on DIRMINA-404 at 7/31/07 12:52 AM:
---------------------------------------------------------------

Please feel free to modify the attached test code to try to reproduce your problem.


 was:
Please feel free to modify the example code to try to reproduce your problem.

> The IoSession returned by session = connectFuture.getSession() is different than the one passed to sessionOpened(IoSession session)
> -----------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: DIRMINA-404
>                 URL: https://issues.apache.org/jira/browse/DIRMINA-404
>             Project: MINA
>          Issue Type: Bug
>    Affects Versions: 1.1.1
>         Environment: Windows XP
>            Reporter: Kevin Smeltzer
>            Assignee: Trustin Lee
>            Priority: Trivial
>         Attachments: Main.java
>
>
> // CLIENT MODULE
> public class bgMinaClient extends IoHandlerAdapter
> {
>     public static final int CONNECT_TIMEOUT = 10000;
>     private String host;
>     private int port;
>     private String tokenizer;
>     private SocketConnector connector;
>     private IoSession session;
>     private bgClientListener clientListener;
>     private ConnectFuture connectFuture;
>     
>     public bgMinaClient(String host, int port, String tokenizer, bgClientListener clientListener) {
>     	
>         this.host = host;
>         this.port = port;
>         this.tokenizer = tokenizer;
>         this.clientListener = clientListener;
>         connector = new SocketConnector();
>         connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new bgCodecFactory(true)));
>         
>     }
>     public void connect() {
>     	
>     	// try to connect 5 times before giving up
>     	int attempts = 5;
>     	do {
> 	        connectFuture = connector.connect(new InetSocketAddress(host,port), this);
>     	    connectFuture.join(CONNECT_TIMEOUT);
> 	        try
>     	    {
>         	
>             	session = connectFuture.getSession();
>             
>         	}
>         	catch (RuntimeIOException e)
>         	{
>         	
> 	            clientListener.onException( e );
>             
> 	        }
> 	        attempts--;
> 	        
>     	} while ( session == null && attempts > 0);
> 			
>     }
>     public void disconnect() {
>     	
>         if (session != null)
>         {
>         	
>             session.close().join(CONNECT_TIMEOUT);
>             session = null;
>             
>         }
>     }
>     public void sessionOpened(IoSession session) throws Exception {
>     	
>     	this.session = session;
> 		clientListener.doConnect( session );
>       
>     }
>     public void sessionClosed(IoSession session) throws Exception {
>     	
>         clientListener.doDisconnect();
>         
>     }
>     public void sendRequest( bgClientRequest clientRequest ) {
>         
>         if (session == null) {
>         		
>             clientListener.onException(new Throwable("not connected"));
>             
>         }
>         else
>         {
>         	
>             session.write(clientRequest);
>             
>         }
>     }
>     
>     public void messageReceived( IoSession session, Object message) throws Exception {
>         
>         bgServerResponse response = (bgServerResponse) message;
>         Object o;
>       o = response.getObject();
>     	
>       	clientListener.doServerAttached ( o );
>         	
>         }
>         
>     }
>     public void exceptionCaught( Throwable cause) throws Exception {
>     
>         clientListener.onException( cause );
>         
>     }
> }
> // SERVER MODULE
> public class bgMinaServer {
> 	// defines how messages are split into parts
> 	final String tokenizer;
> 			
> 	// commPort used for communications
> 	final int commPort;
> 	
> 	// the acceptor object	
> 	IoAcceptor acceptor = null;
> 	
> 	// the socket acceptor configuration
>     SocketAcceptorConfig cfg;
>     
>     // the server handler object
>     bgMinaServerHandler handler;
>     
>     bgServerListener serverListener;
> 	public bgMinaServer ( int commPort, String tokenizer, bgServerListener serverListener ) {
> 		
> 		this.serverListener = serverListener;
> 		this.commPort = commPort;
> 		this.tokenizer = tokenizer;
> 		
>         ByteBuffer.setUseDirectBuffers(false);
>        	ByteBuffer.setAllocator(new SimpleByteBufferAllocator());
> 			
>        	acceptor = new SocketAcceptor();
>        	cfg = new SocketAcceptorConfig();
>        	cfg.getSessionConfig().setReuseAddress( true );
>        	//cfg.getFilterChain().addLast( "logger", new LoggingFilter() );
>        	cfg.getFilterChain().addLast( "protocol", new ProtocolCodecFilter(new bgCodecFactory(false)));
> 			
> 	}
> 	
> 	public void start() {
> 		try {
> 			
> 			handler = new bgMinaServerHandler();
> 			handler.serverListener = this.serverListener;
> 			handler.tokenizer = this.tokenizer;
> 	        acceptor.bind( new InetSocketAddress( commPort ), handler, cfg);
>         	System.out.println("Game server started.");
>        
>   		} catch (IOException e) {
> 			
> 			e.printStackTrace();
> 			
> 		}
> 	}
> }
> public class bgMinaServerHandler extends IoHandlerAdapter {
> 	// the objectthat will listen to the server commands
> 	public bgServerListener serverListener;
> 	
> 	// defines how messages are split into parts
> 	String tokenizer;
> 	 
> 	public static final String CLIENT_KEY = bgMinaServerHandler.class.getName() + ".CLIENTID";
> 	
> 	public void exceptionCaught(IoSession session, Throwable t) throws Exception {
> 		// check to see if we are connected
> 		if ( !session.isConnected() ) {
> 			
> 			serverListener.doDisconnect( session );
> 			// we are no longer conncted so we need to take this client out of our
> 			// currently connected client list
> 			// but how?
> 			// we could set a session attribute when the client connects to 
> 			// equal his ID number
> 			// then when he dosconnects we can remove him easily!
> 			
> 		}
> 		session.close();
> 		
> 	}
> 	public void messageReceived(IoSession session, Object message) throws Exception {
> 		// get the message from the network
> 		bgClientRequest request = (bgClientRequest)message;
> 		
> 		// holds tokenized messages used in networking communications
> 		bgTokenizedMessage commMessage = new bgTokenizedMessage();
> 		commMessage.setMessage ( request.getRequest(), tokenizer );
> 		
> 		if ( !commMessage.hasMessage() )
> 			return;
> 		
> 		String command = commMessage.getNextToken();
> 		if ( command.equalsIgnoreCase("REQUEST") ) {
> 				
> 			serverListener.doClientRequest( session, commMessage );
> 						
> 		}
> 		if ( command.equalsIgnoreCase("ATTEMPT") ) {
> 				
> 			serverListener.doClientAttempt( session, commMessage );
> 						
> 		}
> 		if ( command.equalsIgnoreCase("VOTE") ) {
> 		
> 			serverListener.doClientVote( session, commMessage );
> 					
> 		}
>         
> 	}
>     public void sessionOpened(IoSession session) throws Exception {
> 		System.out.println("Client connected...");
> 		if( session.getTransportType() == TransportType.SOCKET )
> 			System.out.println("Receive buffer: " + ((SocketSessionConfig) session.getConfig() ).getReceiveBufferSize() );
>     }
> }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.