Ejemplo n.º 1
0
  /**
   * 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);
  }
Ejemplo n.º 2
0
  /** Queue process --> until it is not empty */
  private void processQueue() throws RemoteException, NotBoundException, InterruptedException {
    if (this.messageQueue.isEmpty()) {
      Thread.sleep(1000);
      return;
    }

    while (!this.messageQueue.isEmpty()) {
      Message mess = this.messageQueue.poll();
      MessageType type = mess.getType();
      // synchronize my logical time
      this.setNewClock(mess.getClock());

      switch (type) {
        case START:
          this.log.logNode("Starting work ...", -1, this.nodeInfo.getID());
          this.workAmount = new Random().nextInt(SLEEP_TIME_MODULE);
          this.log.logNode(
              "Generated work amount is " + this.workAmount + ".", clock, this.nodeInfo.getID());
          this.workStopAccepted = true;
          break;

        case GET_WORK: // some work with random time
          this.log.logNode(
              "Work has been received: " + mess.toString() + ".", clock, this.nodeInfo.getID());
          this.workAmount += Integer.parseInt(mess.getPayload());
          this.log.logNode(
              "New work amount is " + this.workAmount + ".", clock, this.nodeInfo.getID());
          break;

        case TOKEN:
          this.log.logNode(
              "Token has been received " + mess.toString(), clock, this.nodeInfo.getID());
          processToken(mess);
          break;

        case STOP:
          this.directory.logout(this.nodeInfo);
          this.log.logNode("Exiting", clock, this.nodeInfo.getID());
          System.exit(0);
          break;
        case WORK_TERMINATED:
          this.log.logNode("Work has been terminated :).", clock, this.nodeInfo.getID());
          this.init();
          break;
        default:
          // do nothing by default
          break;
      }
    }
  }