Exemplo n.º 1
0
  public static void main(String[] args) {
    // sample node reading
    NodeReading nodeReading1 = new NodeReading();
    nodeReading1.setTestbedId("1");
    nodeReading1.setNodeId("urn:ctinetwork:carrot_delete_me");
    nodeReading1.setCapabilityName("urn:ctinetwork:node:capability:lockScreen");
    nodeReading1.setTimestamp(Long.toString(new Date().getTime()));
    nodeReading1.setReading("1.0");

    NodeReading nodeReading2 = new NodeReading();
    nodeReading2.setTestbedId("1");
    nodeReading2.setNodeId("urn:ctinetwork:carrot_delete_moi");
    nodeReading2.setCapabilityName("urn:ctinetwork:node:capability:lockScreen");
    nodeReading2.setTimestamp(Long.toString(new Date().getTime()));
    nodeReading2.setReading("1.0");

    /** WebSocket Call */
    final String webSocketUrl = "ws://localhost:8080/uberdust/insertreading.ws";

    // insert node reading using WebSockets

    try {
      InsertReadingWebSocketClient.getInstance().connect(webSocketUrl);

      int counter = 0;
      while (true) {
        System.out.println(counter);
        if (counter % 2 == 0) {
          nodeReading1.setTestbedId(String.valueOf(counter % 4));
          InsertReadingWebSocketClient.getInstance().sendNodeReading(nodeReading1);
        } else {
          nodeReading2.setTestbedId(String.valueOf(counter % 4));
          InsertReadingWebSocketClient.getInstance().sendNodeReading(nodeReading2);
        }
        Thread.sleep(5);
        counter++;
      }
    } catch (IOException e) {
      e.printStackTrace(); // To change body of catch statement use File | Settings | File
      // Templates.
    } catch (InterruptedException e) {
      e.printStackTrace(); // To change body of catch statement use File | Settings | File
      // Templates.
    }
  }
  @Override
  public final void messageReceived(
      final ChannelHandlerContext ctx, final MessageEvent messageEvent)
      throws InvalidProtocolBufferException {
    final Messages.Msg message = (Messages.Msg) messageEvent.getMessage();
    if (WSNApp.MSG_TYPE_LISTENER_MESSAGE.equals(message.getMsgType())) {
      final WSNAppMessages.Message wsnAppMessage =
          WSNAppMessages.Message.parseFrom(message.getPayload());
      parse(wsnAppMessage.toString());
      messageCounter++;
      if (messageCounter == REPORT_LIMIT) {
        final long milliseconds = System.currentTimeMillis() - lastTime;
        final double stat = messageCounter / (milliseconds / (double) REPORT_LIMIT);
        LOGGER.info("MessageRate : " + stat + " messages/sec");
        final ThreadPoolExecutor pool = (ThreadPoolExecutor) executorService;
        LOGGER.info("PoolSize : " + pool.getPoolSize() + " Active :" + pool.getActiveCount());
        LOGGER.info("Peak : " + pool.getLargestPoolSize());
        final NodeReading nodeReading = new NodeReading();
        nodeReading.setTestbedId("3");
        nodeReading.setNodeId("urn:ctinetwork:uberdust");
        nodeReading.setCapabilityName("urn:ctinetwork:testbedlistener:poolSize");
        nodeReading.setReading(String.valueOf(pool.getPoolSize()));
        nodeReading.setTimestamp(String.valueOf((new Date()).getTime()));
        new WsCommiter(nodeReading);
        nodeReading.setCapabilityName("urn:ctinetwork:testbedlistener:activeThreads");
        nodeReading.setReading(String.valueOf(pool.getActiveCount()));
        new WsCommiter(nodeReading);
        nodeReading.setCapabilityName("urn:ctinetwork:testbedlistener:messageRate");
        nodeReading.setReading(String.valueOf(stat));
        new WsCommiter(nodeReading);

        lastTime = System.currentTimeMillis();
        messageCounter = 0;
      }

    } else {
      LOGGER.error("got a message of type " + message.getMsgType());
    }
  }