예제 #1
0
  public static void main(String[] args) throws Exception {

    // this should be on stable storage as per article
    long lastKnownConfigurationId = 0L;

    // group member '1'
    Processor proc = new Processor(1);

    // totem-specific configuration parameters
    String props = "port=9100&ip=225.0.0.1&nic=192.168.254.0/255.255.255.0";

    Connection conn = new evs4j.impl.SRPConnection(lastKnownConfigurationId, proc, props);

    // the default listener just discards messages
    conn.setListener(new Example());
    conn.open();

    // send a message
    boolean agreed = false;
    boolean safe = true;
    Message message = conn.createMessage(agreed);
    byte[] data = "Hello World!".getBytes();
    System.arraycopy(data, 0, message.getData(), message.getOffset(), data.length);
    message.setLength(data.length);
    conn.send(message);
  }
 /**
  * Verifies that the fields in a {@link Message} are non-null.
  * <p>
  * See https://code.google.com/p/gcm/issues/detail?id=5
  *
  * @param msg the invite to verify
  * @return {@code true} if all fields are verified, {@code false} if otherwise
  */
 private static boolean verifyFields(Message msg) {
   boolean verified = true;
   for (Entry<String, String> entry : msg.getData().entrySet()) {
     verified = entry.getValue() != null;
   }
   return verified;
 }
예제 #3
0
 public Message(Message msg) {
   this.src = msg.getSrc();
   this.dest = msg.getDest();
   this.kind = msg.getKind();
   this.data = msg.getData();
   this.seqNum = msg.getSeqNum();
   this.dupe = msg.isDupe();
 }
  /** Test of addRawData method, of class SimpleLineInterpreter. */
  @Test
  public void testAddRawData() throws Exception {
    System.out.println("addRawData");
    String testLine = "This is a test line\n";
    byte[] buf = testLine.getBytes("UTF-8");
    SimpleLineInterpreter instance = new SimpleLineInterpreter();
    instance.registerObserver(this);
    instance.addRawData(buf);

    assertEquals(testLine, lastMessageReceived.getData());
  }
  public void run() {
    while (true) {
      try {
        TimeUnit.SECONDS.sleep(1);
      } catch (Exception e) {
        e.printStackTrace();
      }
      //            System.out.println("Here is Exit at " + exit.getInfo());
      //            System.out.println("Now has occupied places number : " + exit.getOccupiedNum());
      //            System.out.println("Now has exit car number : " + exit.getExitNum());
      if (exit.hasMessage()) {
        exit.tick();
        Message message = exit.getAndRemoveEarliestMessage();
        switch (message.getType()) {
          case REQUEST:
            {
            }
            break;

          case REPLY:
            {
            }
            break;

          case INFORM_ENTER:
            {
              exit.handleInformEnterFrom(message.getSource());
            }
            break;

          case INFORM_EXIT:
            {
              exit.handleInformExitFrom(message.getSource());
            }
            break;

          case UPDATE:
            {
              exit.handleUpdateFrom(message.getSource());
            }
            break;

          case UPDATE_REPLY:
            {
              exit.handleUpdateReply(message.getData());
            }
            break;
        }
      }
    }
  }
  /** Test of addRawData method, of class SimpleLineInterpreter. */
  @Test
  public void testAddRawDataMultipleLines() throws Exception {
    System.out.println("testAddRawDataMultipleLines");
    String testLine = "This is a test line\n";
    String testLine2 = "This is a second test line\n";
    String data = testLine + testLine2;
    byte[] buf = data.getBytes("UTF-8");
    SimpleLineInterpreter instance = new SimpleLineInterpreter();
    instance.registerObserver(this);
    instance.addRawData(buf);

    assertEquals(testLine2, lastMessageReceived.getData());
    assertEquals(2, numberOfMessagesReceived);
  }
  /** Test of addRawData method, of class SimpleLineInterpreter. */
  @Test
  public void testAddRawDataLineSpansBuffers() throws Exception {
    System.out.println("testAddRawDataLineSpansBuffers");
    String startOfData = "This is a test line with out terminator. ";
    String endOfData = "This is a second line with a \n";
    String data = startOfData + endOfData;

    SimpleLineInterpreter instance = new SimpleLineInterpreter();
    instance.registerObserver(this);
    instance.addRawData(startOfData.getBytes("UTF-8"));
    instance.addRawData(endOfData.getBytes("UTF-8"));

    assertEquals(data, lastMessageReceived.getData());
    assertEquals(1, numberOfMessagesReceived);
  }
    @Override
    public void onMessage(Message message) {

      String value = new String(message.getData());

      logger.trace("NATS Subscriber ({}):  Received message: {}", id, value);

      if (checkPayload) {
        org.junit.Assert.assertTrue(REDIS_PAYLOAD.equals(value));
      }

      if (tallyMessage() == testCount) {
        logger.debug("NATS Subscriber ({}) Received {} messages.  Completed.", id, testCount);
        setComplete();
      }
    }
예제 #9
0
 public void onMessage(Message message) {
   String text = new String(message.getData(), message.getOffset(), message.getLength());
   System.err.println("received message from " + message.getSender() + " : " + text);
 }
예제 #10
0
  /**
   * Reads messages from the network. A message is either a command for the server or for an
   * internal Service.
   *
   * @param sock the Socket
   */
  private void getMessage(Socket sock) {
    int i;
    boolean alreadyConnected;
    boolean isAuthentication;

    Message message;
    byte[] signature;

    String cmd;
    String cmdData;
    StringTokenizer stk;

    ObjectConnection oc = null;

    try {
      /* streams initialization */
      oc = new ObjectConnection(sock);
      message = (Message) oc.read();
      signature = (byte[]) oc.read();
    } catch (Exception ex) {
      ex.printStackTrace();
      Logging.getLogger().warning("#Err > Unable to read message.");
      return;
    }

    // check wether a user is known or not
    alreadyConnected = isAlreadyKnown(message.getSender());

    // check if command is authentication
    isAuthentication =
        message.getApplication().equals("Server")
            && ((String) message.getData()).startsWith("AUTH");

    // signature check
    if (alreadyConnected && !isAuthentication) {
      boolean sigok = false;
      try {
        ConnectInfo ci = message.getSender();
        if (ci.verifier == null) ci = this.getCompleteConnectInfo(ci);
        sigok = ci.verifier.verify(message, signature);
      } catch (Exception e) {
        e.printStackTrace();
      }

      if (!sigok) {
        try {
          oc.write("FAILED bad signature");
        } catch (Exception e) {
        }
        Logging.getLogger().warning("#Err > bad signature: " + message.getSender());
        return;
      }
    }

    if (message.getApplication().equals("Server")) {
      cmd = null;

      try {
        stk = new StringTokenizer((String) message.getData());
        cmd = stk.nextToken();
        cmdData = stk.nextToken("\0").substring(1);
      } catch (Exception ex) {
        if (cmd == null) cmd = "";

        cmdData = "";
      }

      /* if the user asks for authentication, we try to do it and exits this method */
      if (cmd.equals("AUTH")) {
        try {
          oc.write("OK");
        } catch (Exception e) {
          e.printStackTrace();
        }

        authentification(oc, message, cmdData);
      } else if (!alreadyConnected) {
        Logging.getLogger().info("Access denied to " + message.getSender());
        try {
          oc.write("FAILED No Connection");
        } catch (Exception e) {
        }
      } else {
        internalCommand(oc, message, cmd, cmdData);
      }
    } else if (!alreadyConnected) {
      Logging.getLogger().info("Access denied to " + message.getSender());
      try {
        oc.write("FAILED No Connection");
      } catch (Exception e) {
      }
    } else {
      Service s;

      /* seek the destination service */
      boolean serviceFound = false;
      for (i = 0; i < this.services.size(); i++) {
        s = (Service) services.get(i);

        if (s.getName().equalsIgnoreCase(message.getApplication())) {
          serviceFound = true;
          UserConcept user = null;
          ServiceConcept service = null;
          try {
            user = store.getUserStore().getUser(message.getSender().getName());
            service = store.getServiceStore().getService(message.getReceiver().getName());
          } catch (Exception e) {
          }

          /* tests serviceManager for permissions */
          boolean isAutorizedService = false;
          try {
            isAutorizedService = store.getServiceStore().isAuthorizedService(user, service);
          } catch (Exception e) {
          }

          if (!isAutorizedService) {
            Logging.getLogger()
                .info(
                    "#Err > "
                        + message.getSender()
                        + " : Service denied to "
                        + message.getReceiver().getName());
            try {
              oc.write("FAILED You don't have acces to this service");
            } catch (Exception e) {
            }
          } else {
            try {
              oc.write("OK");
            } catch (Exception e) {
            }
            serviceFound = true;
            s.process(oc, message);
          }
          break;
        }
      }

      if (!serviceFound) {
        try {
          oc.write("FAILED unknown");
        } catch (Exception e) {
        }
        Logging.getLogger()
            .warning("#Err > Service " + message.getReceiver().getName() + " unknown");
      }
    }

    oc.close();
  }