示例#1
0
  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);
      }
    }
  }
示例#2
0
  @Override
  protected int doReadMessages(List<Object> buf) throws Exception {
    SctpChannel ch = javaChannel();

    RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();
    ByteBuf buffer = allocHandle.allocate(config().getAllocator());
    boolean free = true;
    try {
      ByteBuffer data = buffer.internalNioBuffer(buffer.writerIndex(), buffer.writableBytes());
      int pos = data.position();

      MessageInfo messageInfo = ch.receive(data, null, notificationHandler);
      if (messageInfo == null) {
        return 0;
      }
      buf.add(
          new SctpMessage(
              messageInfo, buffer.writerIndex(buffer.writerIndex() + data.position() - pos)));
      free = false;
      return 1;
    } catch (Throwable cause) {
      PlatformDependent.throwException(cause);
      return -1;
    } finally {
      int bytesRead = buffer.readableBytes();
      allocHandle.record(bytesRead);
      if (free) {
        buffer.release();
      }
    }
  }
示例#3
0
 @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();
 }
示例#4
0
  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);
  }
示例#5
0
 private void registerAcceptedChannel(SctpChannel acceptedSocket, Thread currentThread) {
   try {
     ChannelPipeline pipeline = channel.getConfig().getPipelineFactory().getPipeline();
     SctpWorker worker = nextWorker();
     worker.register(
         new SctpAcceptedChannel(
             channel.getFactory(),
             pipeline,
             channel,
             SctpServerPipelineSink.this,
             acceptedSocket,
             worker,
             currentThread),
         null);
   } catch (Exception e) {
     if (logger.isWarnEnabled()) {
       logger.warn("Failed to initialize an accepted socket.", e);
     }
     try {
       acceptedSocket.close();
     } catch (IOException e2) {
       if (logger.isWarnEnabled()) {
         logger.warn("Failed to close a partially accepted socket.", e2);
       }
     }
   }
 }
示例#6
0
 private static SctpChannel newSctpChannel() {
   try {
     return SctpChannel.open();
   } catch (IOException e) {
     throw new ChannelException("Failed to open a sctp channel.", e);
   }
 }
示例#7
0
  /**
   * Create a new instance
   *
   * @param parent the {@link Channel} which is the parent of this {@link NioSctpChannel} or {@code
   *     null}.
   * @param sctpChannel the underlying {@link SctpChannel}
   */
  public NioSctpChannel(Channel parent, SctpChannel sctpChannel) {
    super(parent, sctpChannel, SelectionKey.OP_READ);
    try {
      sctpChannel.configureBlocking(false);
      config = new NioSctpChannelConfig(this, sctpChannel);
      notificationHandler = new SctpNotificationHandler(this);
    } catch (IOException e) {
      try {
        sctpChannel.close();
      } catch (IOException e2) {
        if (logger.isWarnEnabled()) {
          logger.warn("Failed to close a partially initialized sctp channel.", e2);
        }
      }

      throw new ChannelException("Failed to enter non-blocking mode.", e);
    }
  }
示例#8
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);
    }
  }
示例#9
0
  // 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);
        }
      }
    }
  }
示例#10
0
 @Override
 public boolean isActive() {
   SctpChannel ch = javaChannel();
   return ch.isOpen() && association() != null;
 }