/** * Inserts message into queue * * @param message - message to insert * @throws RemoteException */ @Override public synchronized void putMessage(Message message) throws RemoteException { Message messageTmp = new Message(); messageTmp.setClock(message.getClock()); messageTmp.setColor(message.getColor()); messageTmp.setPayload(message.getPayload()); messageTmp.setType(message.getType()); this.messageQueue.add(message); }
/** * Token processing & sending * * @param mess - message */ private void processToken(Message mess) throws RemoteException, NotBoundException { Message outMess = new Message(); outMess.setType(MessageType.TOKEN); this.log.logNode("Received message -> " + mess.toString(), clock, this.nodeInfo.getID()); if (mess.getColor() == Color.BLACK) { // black token if (this.nodeInfo.getID() == 0) { // primary process this.log.logNode("BLACK node has been received.", clock, 0); this.startTokenSent = false; } else { outMess.setColor(Color.BLACK); } } else { // white token if (this.nodeInfo.getID() == 0) { // primary process --> received white node;all stops working this.log.logNode("Work has been terminated :).", clock, 0); sendTerminatedMessage(); this.init(); } else { // i am not the primary process if (this.myState == NodeState.PASSIVE) { // I don't have any work outMess.setColor(myColor); } else { // I have work outMess.setColor(Color.BLACK); } } } // get node count and send token if node count is bigger than one if (this.directory.getNodeCount() == 1) return; // send data if (this.nodeInfo.getID() != 0) { int neighbourID = getNeighbourID(); this.sendMessageToProcess(outMess, neighbourID); } // reset my color this.myColor = Color.WHITE; }