Exemplo n.º 1
0
 protected synchronized Message receiveMessage() throws IOException {
   if (messageBuffer.size() > 0) {
     Message m = (Message) messageBuffer.get(0);
     messageBuffer.remove(0);
     return m;
   }
   try {
     InetSocketAddress remoteAddress = (InetSocketAddress) channel.receive(receiveBuffer);
     if (remoteAddress != null) {
       int len = receiveBuffer.position();
       receiveBuffer.rewind();
       receiveBuffer.get(buf, 0, len);
       try {
         IP address = IP.fromInetAddress(remoteAddress.getAddress());
         int port = remoteAddress.getPort();
         extractor.appendData(buf, 0, len, new SocketDescriptor(address, port));
         receiveBuffer.clear();
         extractor.updateAvailableMessages();
         return extractor.nextMessage();
       } catch (EOFException exc) {
         exc.printStackTrace();
         System.err.println(buf.length + ", " + len);
       } catch (InvocationTargetException exc) {
         exc.printStackTrace();
       } catch (IllegalAccessException exc) {
         exc.printStackTrace();
       } catch (InstantiationException exc) {
         exc.printStackTrace();
       } catch (IllegalArgumentException e) {
         e.printStackTrace();
       } catch (InvalidCompressionMethodException e) {
         e.printStackTrace();
       }
     }
   } catch (ClosedChannelException exc) {
     if (isKeepAlive()) {
       throw exc;
     }
   }
   return null;
 }
Exemplo n.º 2
0
  public void resendMessage(Message message, IP remoteAddress, int remotePort) throws IOException {
    if (remoteAddress == null) remoteAddress = IP.getLocalHost();

    message.setSentStamp(getConvertedTime(remoteAddress, remotePort));
    message.setMessageServer(this);
    try {
      sendBuffer.clear();
      sendBuffer.put(extractor.convertMessage(message));
      sendBuffer.flip();
      channel.send(sendBuffer, new InetSocketAddress(remoteAddress.toString(), remotePort));
      message.setRemoteAddress(remoteAddress);
      message.setRemotePort(remotePort);
      messageSent(message);
    } catch (IllegalAccessException exc) {
      throw new RuntimeException(exc);
    } catch (IllegalArgumentException exc) {
      throw new RuntimeException(exc);
    } catch (InvocationTargetException exc) {
      throw new RuntimeException(exc);
    }
  }