private void init() throws IOException {
    messageBuffer = new ArrayList();
    receiveBuffer = ByteBuffer.allocate(CAPACITY);
    sendBuffer = ByteBuffer.allocate(CAPACITY);
    buf = new byte[CAPACITY];

    channel = DatagramChannel.open();
    channel.configureBlocking(false);

    InetAddress host = null;
    if (getAddress() != null) {
      host = InetAddress.getByAddress(getAddress().getBytes());
    }
    channel.socket().bind(new InetSocketAddress(host, getPort()));
    channel.socket().setReceiveBufferSize(CAPACITY);

    certifier = new MessageCertifier(this);
    extractor = new MessageExtractor(this);
  }
Example #2
0
  /**
   * Each of the Enums can override thier deault behavior of "___seek___-past"
   *
   * @param src the ByteBuffer of the input file
   * @param register array holding values pointing to Stack offsets
   * @param stack A stack of 32-bit pointers only to src positions
   */
  private void subIndex(ByteBuffer src, int[] register, IntBuffer stack) {
    System.err.println(name() + ":subIndex src:stack" + src.position() + ':' + stack.position());
    int begin = src.position();
    int stackPtr = stack.position();
    stack.put(begin);
    if (___isRecord___ && ___subrecord___ != null) {/*                 try {
                final model.UUID table = model.UUID.valueOf(___subrecord___.getSimpleName());
                if (table != null) {
                    //stow the original location
                    int mark = stack.position();
                    stack.position((register[TopLevelRecord.TableRecord.ordinal()] + table.___seek___) / 4);
                    ___subrecord___.getMethod("index", ByteBuffer.class, int[].class, IntBuffer.class).invoke(null);
                    //resume the lower stack activities
                    stack.position(mark);
                }
            } catch (Exception e) {
                throw new Error(e.getMessage());
            }
*/ }
  }
 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;
 }
  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);
    }
  }