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); } } }
private static SctpChannel newSctpChannel() { try { return SctpChannel.open(); } catch (IOException e) { throw new ChannelException("Failed to open a sctp channel.", e); } }
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); } } } }