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); } } }
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 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; }
@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(); }
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); } } } }