VersionedConnection getConnection() throws IOException { sendVersionHeader(); IoFuture<Void> futureWelcome = WelcomeMessageReceiver.awaitWelcomeMessage(channel); IoFuture.Status result = futureWelcome.await(timeoutSeconds, TimeUnit.SECONDS); switch (result) { case DONE: // Set this first as something will need to start handling the response messages. channel.receiveMessage(new MessageReceiver()); break; case FAILED: throw futureWelcome.getException(); default: throw new IOException("Unable to obtain connectionId, status=" + result.toString()); } sendKeyPairs(); String connectionId = begin(); ClientConnection cc = new ClientConnection(channel, environment, clientRequestManager, clientExecutorManager, connectionId); cc.start(); return cc; }
ClientConnection(final Channel channel, final Map<String, ?> environment, final ClientRequestManager clientRequestManager, final ClientExecutorManager clientExecutorManager, final String connectionId) { super(channel, environment); this.channel = channel; this.clientRequestManager = clientRequestManager; this.clientExecutorManager = clientExecutorManager; this.connectionId = connectionId; handlerRegistry = createHandlerRegistry(); }
@Override public void write(DataOutput output) throws IOException { output.writeByte(UNREGISTER_MBEAN); output.writeInt(correlationId); output.writeByte(OBJECT_NAME); Marshaller marshaller = prepareForMarshalling(output); marshaller.writeObject(name); marshaller.close(); } });
public String getDefaultDomain() throws IOException { VersionedIoFuture<TypeExceptionHolder<String>> future = new VersionedIoFuture<TypeExceptionHolder<String>>(); final int correlationId = clientRequestManager.reserveNextCorrelationId(future); try { write(new MessageWriter() { @Override public void write(DataOutput output) throws IOException { output.writeByte(GET_DEFAULT_DOMAIN); output.writeInt(correlationId); } }); log.tracef("[%d] getDefaultDomain - Request Sent", correlationId); IoFuture.Status result = future.await(timeoutSeconds, TimeUnit.SECONDS); switch (result) { case DONE: TypeExceptionHolder<String> response = future.get(); if (response.e == null) { return response.value; } jmRuntimeException(response.e); throw toIoException(response.e); case FAILED: throw future.getException(); default: throw new IOException("Unable to obtain DefaultDomain, status=" + result.toString()); } } finally { clientRequestManager.releaseCorrelationId(correlationId); } }
Socket s = serverSocket.accept(); ClientConnection temp = new ClientConnection(s); //Changed this line connectedClients.add(temp); System.out.println("Client connection caught and initialized. Client: " + s); System.out.println("Connection with " + s + " now listening for incoming packets."); ThreadAction(temp); CleanClientList();
@Override public void handle(DataInput input, int correlationId) throws IOException { log.trace("Notification"); byte paramType = input.readByte(); if (paramType != INTEGER) { throw new IOException("Unexpected paramType"); } int listenerId = input.readInt(); paramType = input.readByte(); if (paramType != NOTIFICATION) { throw new IOException("Unexpected paramType"); } try { Unmarshaller unmarshaller = prepareForUnMarshalling(input); Notification notification = unmarshaller.readObject(Notification.class); paramType = unmarshaller.readByte(); if (paramType != OBJECT) { throw new IOException("Unexpected paramType"); } Object handBack = unmarshaller.readObject(); localNotificationManager.notify(listenerId, notification, handBack); } catch (ClassNotFoundException cnfe) { throw new IOException(cnfe); } }
public Integer getMBeanCount() throws IOException { VersionedIoFuture<TypeExceptionHolder<Integer>> future = new VersionedIoFuture<TypeExceptionHolder<Integer>>(); final int correlationId = clientRequestManager.reserveNextCorrelationId(future); try { write(new MessageWriter() { @Override public void write(DataOutput output) throws IOException { output.writeByte(GET_MBEAN_COUNT); output.writeInt(correlationId); } }); log.tracef("[%d] getMBeanCount - Request Sent", correlationId); IoFuture.Status result = future.await(timeoutSeconds, TimeUnit.SECONDS); switch (result) { case DONE: TypeExceptionHolder<Integer> response = future.get(); if (response.e == null) { return response.value; } jmRuntimeException(response.e); throw toIoException(response.e); case FAILED: throw future.getException(); default: throw new IOException("Unable to obtain MBeanCount, status=" + result.toString()); } } finally { clientRequestManager.releaseCorrelationId(correlationId); } }
// Somewhat pseudo-code... Server server = new Server(); server.onStartedListening(new Runnable() { @Override public void run() { executor.execute(new ClientConnection()); } }); executor.execute(server);
@Override public void handle(DataInput input, int correlationId) throws IOException { log.trace("Notification"); byte paramType = input.readByte(); if (paramType != INTEGER) { throw new IOException("Unexpected paramType"); } int listenerId = input.readInt(); paramType = input.readByte(); if (paramType != NOTIFICATION) { throw new IOException("Unexpected paramType"); } try { Unmarshaller unmarshaller = prepareForUnMarshalling(input); Notification notification = unmarshaller.readObject(Notification.class); paramType = unmarshaller.readByte(); if (paramType != OBJECT) { throw new IOException("Unexpected paramType"); } Object handBack = unmarshaller.readObject(); localNotificationManager.notify(listenerId, notification, handBack); } catch (ClassNotFoundException cnfe) { throw new IOException(cnfe); } }
public String[] getDomains() throws IOException { VersionedIoFuture<TypeExceptionHolder<String[]>> future = new VersionedIoFuture<TypeExceptionHolder<String[]>>(); final int correlationId = clientRequestManager.reserveNextCorrelationId(future); try { write(new MessageWriter() { @Override public void write(DataOutput output) throws IOException { output.writeByte(GET_DOMAINS); output.writeInt(correlationId); } }); log.tracef("[%d] getDomains - Request Sent", correlationId); IoFuture.Status result = future.await(timeoutSeconds, TimeUnit.SECONDS); switch (result) { case DONE: TypeExceptionHolder<String[]> response = future.get(); if (response.e == null) { return response.value; } jmRuntimeException(response.e); throw toIoException(response.e); case FAILED: throw future.getException(); default: throw new IOException("Unable to obtain Domains, status=" + result.toString()); } } finally { clientRequestManager.releaseCorrelationId(correlationId); } }
@Override public void write(DataOutput output) throws IOException { output.writeByte(GET_MBEAN_INFO); output.writeInt(correlationId); output.writeByte(OBJECT_NAME); Marshaller marshaller = prepareForMarshalling(output); marshaller.writeObject(name); marshaller.close(); } });
VersionedConnection getConnection() throws IOException { sendVersionHeader(); IoFuture<Void> futureWelcome = WelcomeMessageReceiver.awaitWelcomeMessage(channel); IoFuture.Status result = futureWelcome.await(timeoutSeconds, TimeUnit.SECONDS); switch (result) { case DONE: // Set this first as something will need to start handling the response messages. channel.receiveMessage(new MessageReceiver()); break; case FAILED: throw futureWelcome.getException(); default: throw new IOException("Unable to obtain connectionId, status=" + result.toString()); } sendKeyPairs(); String connectionId = begin(); ClientConnection cc = new ClientConnection(channel, environment, clientRequestManager, clientExecutorManager, connectionId); cc.start(); return cc; }
while (true) { // respond to clients Socket receiverSocket = serverSocket.accept(); System.out.println("receiver client connected!"); Socket senderSocket = serverSocket.accept(); System.out.println("sender client connected!"); ClientConnection connection = new ClientConnection(senderSocket, receiverSocket); Thread connectionThread = new Thread(connection); connectionThread.start(); }
ClientConnection(final Channel channel, final Map<String, ?> environment, final ClientRequestManager clientRequestManager, final ClientExecutorManager clientExecutorManager, final String connectionId) { super(channel, environment); this.channel = channel; this.clientRequestManager = clientRequestManager; this.clientExecutorManager = clientExecutorManager; this.connectionId = connectionId; handlerRegistry = createHandlerRegistry(); }
@Override public void handle(DataInput input, int correlationId) throws IOException { log.trace("Notification"); byte paramType = input.readByte(); if (paramType != INTEGER) { throw new IOException("Unexpected paramType"); } int listenerId = input.readInt(); paramType = input.readByte(); if (paramType != NOTIFICATION) { throw new IOException("Unexpected paramType"); } try { Unmarshaller unmarshaller = prepareForUnMarshalling(input); Notification notification = unmarshaller.readObject(Notification.class); paramType = unmarshaller.readByte(); if (paramType != OBJECT) { throw new IOException("Unexpected paramType"); } Object handBack = unmarshaller.readObject(); localNotificationManager.notify(listenerId, notification, handBack); } catch (ClassNotFoundException cnfe) { throw new IOException(cnfe); } }
final int correlationId = clientRequestManager.reserveNextCorrelationId(future); try { write(new MessageWriter() { throw toIoException(response.e); default: throw new IOException("Unable to obtain isRegistered, status=" + result.toString());
@Override public void write(DataOutput output) throws IOException { output.writeByte(GET_OBJECT_INSTANCE); output.writeInt(correlationId); output.writeByte(OBJECT_NAME); Marshaller marshaller = prepareForMarshalling(output); marshaller.writeObject(name); marshaller.close(); } });
VersionedConnection getConnection() throws IOException { sendVersionHeader(); IoFuture<Void> futureWelcome = WelcomeMessageReceiver.awaitWelcomeMessage(channel); IoFuture.Status result = futureWelcome.await(timeoutSeconds, TimeUnit.SECONDS); switch (result) { case DONE: // Set this first as something will need to start handling the response messages. channel.receiveMessage(new MessageReceiver()); break; case FAILED: throw futureWelcome.getException(); default: throw new IOException("Unable to obtain connectionId, status=" + result.toString()); } sendKeyPairs(); String connectionId = begin(); ClientConnection cc = new ClientConnection(channel, environment, clientRequestManager, clientExecutorManager, connectionId); cc.start(); return cc; }
class ClientConnection{ private static ClientConnection conn = null; Socket mSocket; // your socket protected ClientConnection(){ mSocket = new Socket(); // mSocket.connect( bla bla bla ) } public Socket getSocket(){ return mSocket(); } public static ClientConnection getInstance(){ if(conn == null){ synchronized (LOCK) { if(conn == null){ conn = new ClientConnection(); } } } return conn; } }
ClientConnection(final Channel channel, final Map<String, ?> environment, final ClientRequestManager clientRequestManager, final ClientExecutorManager clientExecutorManager, final String connectionId) { super(channel, environment); this.channel = channel; this.clientRequestManager = clientRequestManager; this.clientExecutorManager = clientExecutorManager; this.connectionId = connectionId; handlerRegistry = createHandlerRegistry(); }