Exemple #1
0
  @Override
  protected boolean doWriteMessage(Object msg, ChannelOutboundBuffer in) throws Exception {
    SctpMessage packet = (SctpMessage) msg;
    ByteBuf data = packet.content();
    int dataLen = data.readableBytes();
    if (dataLen == 0) {
      return true;
    }

    ByteBufAllocator alloc = alloc();
    boolean needsCopy = data.nioBufferCount() != 1;
    if (!needsCopy) {
      if (!data.isDirect() && alloc.isDirectBufferPooled()) {
        needsCopy = true;
      }
    }
    ByteBuffer nioData;
    if (!needsCopy) {
      nioData = data.nioBuffer();
    } else {
      data = alloc.directBuffer(dataLen).writeBytes(data);
      nioData = data.nioBuffer();
    }
    final MessageInfo mi =
        MessageInfo.createOutgoing(association(), null, packet.streamIdentifier());
    mi.payloadProtocolID(packet.protocolIdentifier());
    mi.streamNumber(packet.streamIdentifier());
    mi.unordered(packet.isUnordered());

    final int writtenBytes = javaChannel().send(nioData, mi);
    return writtenBytes > 0;
  }
  public void run() {
    while (true) {
      try {
        byteBuffer.clear();
        final com.sun.nio.sctp.MessageInfo messageInfo =
            channel.receive(
                byteBuffer,
                null,
                new NotificationHandler<Void>() {
                  @Override
                  public HandlerResult handleNotification(
                      Notification notification, Void attachment) {
                    System.out.println("handleNotification notification = " + notification);
                    return HandlerResult.CONTINUE;
                  }
                });
        System.out.println("after receive, messageInfo = " + messageInfo);
        if ((messageInfo != null) && (messageInfo.isComplete())) {
          System.out.println("received complete message " + byteBuffer);

          byteBuffer.flip();
          channel.send(
              byteBuffer,
              com.sun.nio.sctp.MessageInfo.createOutgoing(messageInfo.association(), null, 0));
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
 @Override
 public long transferTo(SctpChannel ch) throws IOException {
   final MessageInfo messageInfo = MessageInfo.createOutgoing(ch.association(), null, streamNo);
   messageInfo.payloadProtocolID(protocolId);
   messageInfo.streamNumber(streamNo);
   ch.send(buffer, messageInfo);
   return writtenBytes();
 }
Exemple #4
0
 /**
  * Essential data that is being carried within SCTP Data Chunk
  *
  * @param msgInfo the {@link MessageInfo}
  * @param payloadBuffer channel buffer
  */
 public SctpMessage(MessageInfo msgInfo, ByteBuf payloadBuffer) {
   super(payloadBuffer);
   if (msgInfo == null) {
     throw new NullPointerException("msgInfo");
   }
   this.msgInfo = msgInfo;
   streamIdentifier = msgInfo.streamNumber();
   protocolIdentifier = msgInfo.payloadProtocolID();
   unordered = msgInfo.isUnordered();
 }
  public void respondRequest() {
    // Follow algorithm procedure to send token if necessary
    if ((havePrivilege) && (!isRequesting) && (RN.get(lastRequest) == LN.get(lastRequest) + 1)) {
      havePrivilege = false;

      String encodedPrivilegeMsg = encodePrivelege(requestQueue, LN);
      sendPrivelege(encodedPrivilegeMsg, lastRequest);
      SocketAddress mSocketAddress =
          new InetSocketAddress(
              mConfigReader.getNodeConfig(lastRequest)[1],
              Integer.parseInt(mConfigReader.getNodeConfig(lastRequest)[2]));
      MessageInfo mMessageInfo = MessageInfo.createOutgoing(null, 0);

      try {

        SctpChannel mSctpChannel = SctpChannel.open();
        mSctpChannel.connect(mSocketAddress);
        ByteBuffer mByteBuffer = ByteBuffer.allocate(MESSAGE_SIZE);
        mByteBuffer.put(encodedPrivilegeMsg.getBytes());
        mByteBuffer.flip();
        mSctpChannel.send(mByteBuffer, mMessageInfo);
        System.out.println("Privilege Message sending to " + lastRequest);
      } catch (Exception e) {
        System.out.println("Exception: " + e);
      }
    }
  }
Exemple #6
0
 /** Return {@code true} if this message is complete. */
 public boolean isComplete() {
   if (msgInfo != null) {
     return msgInfo.isComplete();
   } else {
     // all outbound sctp messages are complete
     return true;
   }
 }
  public static void main(String[] args) {
    Application app = new Application();

    mSelfNodeID = Integer.parseInt(args[0]);
    mConfigFile = args[1];
    mConfigReader = new ConfigReader(mConfigFile);
    app.initializeArrays();

    /* create server to receive messages*/
    mServer =
        new SctpServer(
            app,
            mConfigReader.getNodeConfig(mSelfNodeID)[0],
            mConfigReader.getNodeConfig(mSelfNodeID)[1],
            mConfigReader.getNodeConfig(mSelfNodeID)[2],
            mConfigReader.getNodeCount() - 1);
    mServerThread = new Thread(mServer);
    mServerThread.start();

    // Timer to indicate when to request CS entry
    TimerThread timer = new TimerThread(app);
    new Thread(timer).start();

    // Create a communication channel to every other node
    for (int i = 0; i < mConfigReader.getNodeCount(); i++) {
      if (i != mSelfNodeID) {
        SocketAddress mSocketAddress =
            new InetSocketAddress(
                mConfigReader.getNodeConfig(i)[1],
                Integer.parseInt(mConfigReader.getNodeConfig(i)[2]));
        MessageInfo mMessageInfo = MessageInfo.createOutgoing(null, 0);

        try {

          SctpChannel mSctpChannel = SctpChannel.open();
          mSctpChannel.connect(mSocketAddress);
          ByteBuffer mByteBuffer = ByteBuffer.allocate(MESSAGE_SIZE);
          mByteBuffer.put("test".getBytes());
          mByteBuffer.flip();
          mSctpChannel.send(mByteBuffer, mMessageInfo);
        } catch (Exception e) {
          System.out.println("Exception: " + e);
        }
      }
    }

    app.applicationModule();

    app.testCorrectness();

    System.exit(0);
  }
  // Send token to designated node
  public void sendPrivelege(String message, int destID) {
    isRequesting = false;
    SocketAddress mSocketAddress =
        new InetSocketAddress(
            mConfigReader.getNodeConfig(destID)[1],
            Integer.parseInt(mConfigReader.getNodeConfig(destID)[2]));
    MessageInfo mMessageInfo = MessageInfo.createOutgoing(null, 0);

    try {

      SctpChannel mSctpChannel = SctpChannel.open();
      mSctpChannel.connect(mSocketAddress);
      ByteBuffer mByteBuffer = ByteBuffer.allocate(MESSAGE_SIZE);
      mByteBuffer.put(message.getBytes());
      mByteBuffer.flip();
      mSctpChannel.send(mByteBuffer, mMessageInfo);
      System.out.println("Sending token to " + destID);
    } catch (Exception e) {
      System.out.println("Exception: " + e);
    }
  }
  // Send request message to all other nodes in system
  public void broadcastRequest(String message) {
    for (int i = 0; i < mConfigReader.getNodeCount(); i++) {
      if (i != mSelfNodeID) {
        SocketAddress mSocketAddress =
            new InetSocketAddress(
                mConfigReader.getNodeConfig(i)[1],
                Integer.parseInt(mConfigReader.getNodeConfig(i)[2]));
        MessageInfo mMessageInfo = MessageInfo.createOutgoing(null, 0);

        try {

          SctpChannel mSctpChannel = SctpChannel.open();
          mSctpChannel.connect(mSocketAddress);
          ByteBuffer mByteBuffer = ByteBuffer.allocate(MESSAGE_SIZE);
          mByteBuffer.put(message.getBytes());
          mByteBuffer.flip();
          mSctpChannel.send(mByteBuffer, mMessageInfo);
          System.out.println("Sending request " + message + "to " + i);
        } catch (Exception e) {
          System.out.println("Exception: " + e);
        }
      }
    }
  }
Exemple #10
0
  private boolean read(SelectionKey k) {
    final SctpChannelImpl channel = (SctpChannelImpl) k.attachment();

    final ReceiveBufferSizePredictor predictor =
        channel.getConfig().getReceiveBufferSizePredictor();
    final int predictedRecvBufSize = predictor.nextReceiveBufferSize();

    boolean messageReceived = false;
    boolean failure = true;
    MessageInfo messageInfo = null;

    ByteBuffer bb = recvBufferPool.acquire(predictedRecvBufSize);
    try {
      messageInfo = channel.channel.receive(bb, null, notificationHandler);
      if (messageInfo != null) {
        messageReceived = true;
        if (!messageInfo.isUnordered()) {
          failure = false;
        } else {
          if (logger.isErrorEnabled()) {
            logger.error("Received unordered SCTP Packet");
          }
          failure = true;
        }
      } else {
        messageReceived = false;
        failure = false;
      }
    } catch (ClosedChannelException e) {
      // Can happen, and does not need a user attention.
    } catch (Throwable t) {
      fireExceptionCaught(channel, t);
    }

    if (messageReceived) {
      bb.flip();

      final ChannelBufferFactory bufferFactory = channel.getConfig().getBufferFactory();
      final int receivedBytes = bb.remaining();
      final ChannelBuffer buffer = bufferFactory.getBuffer(receivedBytes);
      buffer.setBytes(0, bb);
      buffer.writerIndex(receivedBytes);

      recvBufferPool.release(bb);

      // Update the predictor.
      predictor.previousReceiveBufferSize(receivedBytes);

      // Fire the event.
      fireMessageReceived(channel, new SctpFrame(messageInfo, buffer), messageInfo.address());
    } else {
      recvBufferPool.release(bb);
    }

    if (channel.channel.isBlocking() && !messageReceived || failure) {
      k.cancel(); // Some JDK implementations run into an infinite loop without this.
      close(channel, succeededFuture(channel));
      return false;
    }

    return true;
  }