Ejemplo n.º 1
0
  /**
   * send the message according to the rule
   *
   * @param message the message to send
   * @throws IOException
   */
  void send(TimeStampedMessage message) throws IOException {

    /* if is the first time to send the message set up the socket for later use */
    if (isFirstSetup) {
      setupCommunication(message);
      isFirstSetup = false;
    }

    /* check if the configuration file is edited */
    File configFile = new File(configFileName);
    long modifiedTime = configFile.lastModified();

    if (modifiedTime != fileEditTime) {
      parseYamlFile(configFileName);
      fileEditTime = modifiedTime;
    }

    /* increase the sequence number */
    message.set_seqNum(seqNum++);
    int ruleState = parseRules(message, SEND_OPTION);

    /* update clock service */
    // sedLock.lock();
    // clkService.updateTimestamp();
    TimeStampedMessage timeStampMsg = new TimeStampedMessage(message, null);
    setOriginTSMsg(message.getTimeStamp(), timeStampMsg.getTimeStamp());
    // sedLock.unlock();

    /* deal with the message according to the rule */
    switch (ruleState) {
      case DROP:
        {
          System.out.print("DROP MESSAGE");
          printALine();
          System.out.println(timeStampMsg);
          printALine();
          return;
        }
      case DELAY:
        {
          System.out.print("DELAY MESSAGE");
          printALine();
          System.out.println(timeStampMsg);
          printALine();
          senderMsgQueue.add(new TimeStampedMessage(timeStampMsg));
          return;
        }
      case DUPLICATE:
        {
          System.out.println("Duplicate Message Sent...");

          TimeStampedMessage dupMsg = new TimeStampedMessage(timeStampMsg);

          /* set the duplicate field to true */
          dupMsg.set_duplicate(true);
          sendMsg(dupMsg);
        }
      default:
        {
          sendMsg(timeStampMsg);

          /* clear the delay queue */
          sedLock.lock();
          clearSenderDelayQueue();
          sedLock.unlock();
        }
    }
  }