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); } } }
// Perform required algorithm procedures after exiting CS private void ExitCS() { LN.set(mSelfNodeID, RN.get(mSelfNodeID)); for (int i = 0; i < mConfigReader.getNodeCount(); i++) { if (i != mSelfNodeID) { if (!elementInQueue(requestQueue, i) && (RN.get(i) == LN.get(i) + 1)) { requestQueue.add(i); } } } if (!requestQueue.isEmpty()) { havePrivilege = false; int headElement = (Integer) requestQueue.remove(); String msg = encodePrivelege(requestQueue, LN); sendPrivelege(msg, headElement); } isRequesting = false; finishedCS = true; }