protected void configureChannels( long tempId, int[] ports ) { try { for( int i = 0; i < ports.length; i++ ) { Connector c = connectorFactory.createConnector( i, ports[i] ); ConnectorAdapter ca = new ConnectorAdapter(c, dispatcher, dispatcher, true); int ch = channels.size(); channels.add( ca ); // Need to send the connection its hook-up registration // and start it. ca.start(); ClientRegistrationMessage reg; reg = new ClientRegistrationMessage(); reg.setId(tempId); reg.setReliable(true); send( ch, reg, false ); } } catch( IOException e ) { throw new RuntimeException( "Error configuring channels", e ); } }
if( ca == null ) continue; ca.close();
public void run() { MessageProtocol protocol = new MessageProtocol(); try { while( go.get() ) { ByteBuffer buffer = connector.read(); if( buffer == null ) { if( go.get() ) { throw new ConnectorException( "Connector closed." ); } else { // Just dump out because a null buffer is expected // from a closed/closing connector break; } } protocol.addBuffer( buffer ); Message m = null; while( (m = protocol.getMessage()) != null ) { m.setReliable( reliable ); dispatch( m ); } } } catch( Exception e ) { handleError( e ); } }
private void write( ByteBuffer data ) { try { connector.write(data); } catch( Exception e ) { handleError( e ); } }
protected void setPrimaryConnectors( Connector reliable, Connector fast, ConnectorFactory connectorFactory ) { if( reliable == null ) throw new IllegalArgumentException( "The reliable connector cannot be null." ); if( isRunning ) throw new IllegalStateException( "Client is already started." ); if( !channels.isEmpty() ) throw new IllegalStateException( "Channels already exist." ); this.connectorFactory = connectorFactory; channels.add(new ConnectorAdapter(reliable, dispatcher, dispatcher, true)); if( fast != null ) { channels.add(new ConnectorAdapter(fast, dispatcher, dispatcher, false)); } else { // Add the null adapter to keep the indexes right channels.add(null); } }
protected void send( int channel, Message message, boolean waitForConnected ) { checkRunning(); if( waitForConnected ) { // Make sure we aren't still connecting waitForConnected(); } ByteBuffer buffer = dataBuffer.get(); if( buffer == null ) { buffer = ByteBuffer.allocate( 65536 + 2 ); dataBuffer.set(buffer); } buffer.clear(); // Convert the message to bytes buffer = MessageProtocol.messageToBuffer(message, buffer); // Since we share the buffer between invocations, we will need to // copy this message's part out of it. This is because we actually // do the send on a background thread. byte[] temp = new byte[buffer.remaining()]; System.arraycopy(buffer.array(), buffer.position(), temp, 0, buffer.remaining()); buffer = ByteBuffer.wrap(temp); channels.get(channel).write(buffer); }
this.errorHandler = errorHandler; this.reliable = reliable; setDaemon(true);
if( ca == null ) continue; ca.start();
private void write( ByteBuffer data ) { try { connector.write(data); } catch( Exception e ) { handleError( e ); } }
protected void setConnectors( Connector reliable, Connector fast ) { if( reliable == null ) throw new IllegalArgumentException( "The reliable connector cannot be null." ); if( isRunning ) throw new IllegalStateException( "Client is already started." ); this.reliable = reliable; this.fast = fast; reliableAdapter = new ConnectorAdapter(reliable, dispatcher, dispatcher, true); if( fast != null ) { fastAdapter = new ConnectorAdapter(fast, dispatcher, dispatcher, false); } }
protected void send( int channel, Message message, boolean waitForConnected ) { checkRunning(); if( waitForConnected ) { // Make sure we aren't still connecting waitForConnected(); } ByteBuffer buffer = dataBuffer.get(); if( buffer == null ) { buffer = ByteBuffer.allocate( 65536 + 2 ); dataBuffer.set(buffer); } buffer.clear(); // Convert the message to bytes buffer = MessageProtocol.messageToBuffer(message, buffer); // Since we share the buffer between invocations, we will need to // copy this message's part out of it. This is because we actually // do the send on a background thread. byte[] temp = new byte[buffer.remaining()]; System.arraycopy(buffer.array(), buffer.position(), temp, 0, buffer.remaining()); buffer = ByteBuffer.wrap(temp); channels.get(channel).write(buffer); }
this.errorHandler = errorHandler; this.reliable = reliable; setDaemon(true);
public void run() { MessageProtocol protocol = new MessageProtocol(); try { while( go.get() ) { ByteBuffer buffer = connector.read(); if( buffer == null ) { if( go.get() ) { throw new ConnectorException( "Connector closed." ); } else { // Just dump out because a null buffer is expected // from a closed/closing connector break; } } protocol.addBuffer( buffer ); Message m = null; while( (m = protocol.getMessage()) != null ) { m.setReliable( reliable ); dispatch( m ); } } } catch( Exception e ) { handleError( e ); } }
protected void configureChannels( long tempId, int[] ports ) { try { for( int i = 0; i < ports.length; i++ ) { Connector c = connectorFactory.createConnector( i, ports[i] ); ConnectorAdapter ca = new ConnectorAdapter(c, dispatcher, dispatcher, true); int ch = channels.size(); channels.add( ca ); // Need to send the connection its hook-up registration // and start it. ca.start(); ClientRegistrationMessage reg; reg = new ClientRegistrationMessage(); reg.setId(tempId); reg.setReliable(true); send( ch, reg, false ); } } catch( IOException e ) { throw new RuntimeException( "Error configuring channels", e ); } }
private void write( ByteBuffer data ) { try { connector.write(data); } catch( Exception e ) { handleError( e ); } }
protected void setPrimaryConnectors( Connector reliable, Connector fast, ConnectorFactory connectorFactory ) { if( reliable == null ) throw new IllegalArgumentException( "The reliable connector cannot be null." ); if( isRunning ) throw new IllegalStateException( "Client is already started." ); if( !channels.isEmpty() ) throw new IllegalStateException( "Channels already exist." ); this.connectorFactory = connectorFactory; channels.add(new ConnectorAdapter(reliable, dispatcher, dispatcher, true)); if( fast != null ) { channels.add(new ConnectorAdapter(fast, dispatcher, dispatcher, false)); } else { // Add the null adapter to keep the indexes right channels.add(null); } }
protected void closeConnections( DisconnectInfo info ) { if( !isRunning ) return; // Send a close message // Tell the thread it's ok to die if( fastAdapter != null ) { fastAdapter.close(); } if( reliableAdapter != null ) { reliableAdapter.close(); } // Wait for the threads? // Just in case we never fully connected connecting.countDown(); fireDisconnected(info); isRunning = false; }
this.errorHandler = errorHandler; this.reliable = reliable; setDaemon(true);