collectagent.cpp 3.65 KB
Newer Older
1
2
3
4
5
//============================================================================
// Name        : CollectAgent.cpp
// Author      : Axel Auweter
// Version     :
// Copyright   : Leibniz Supercomputing Centre
6
// Description : Main code of the CollectAgent
7
8
//============================================================================

9
#include <cstdlib>
10
#include <signal.h>
11

12
#include <boost/date_time/posix_time/posix_time.hpp>
13

14
#include "simplemqttserver.h"
15
#include "sensordatastore.h"
16

17
using namespace std;
18
19

int keepRunning;
20
21
uint64_t msgCtr;
uint64_t pmsgCtr;
22
SensorDataStore *mySensorDataStore;
23

24
25
void sigHandler(int sig)
{
26
27
28
  keepRunning = 0;
}

29
30
31
32
33
void mqttCallback(SimpleMQTTMessage *msg)
{
  /*
   * Increment the msgCtr/vmsgCtr for statistics.
   */
34
  msgCtr++;
35
36
37
38
39
40
  if (msg->isPublish())
    pmsgCtr++;

  /*
   * Decode the message and put into the database.
   */
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  if (msg->isPublish()) {
      /*
       * Calculate Time Stamp.
       */
      boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
      boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time();
      boost::posix_time::time_duration diff = now - epoch;

      uint64_t ts = diff.total_nanoseconds();

      /*
       * Check if we can decode the message topic
       * into a valid SensorId. If successful, store
       * the record in the database.
       */
      SensorId sid;
      if (mySensorDataStore->topicToSid(&sid,msg->getTopic())) {
58
#if 0
59
60
61
62
63
64
65
66
67
68
69
70
71
          cout << "Topic decode successful:"
               << "\nRaw: " << sid.raw[0] << " " << sid.raw[1]
               << "\ndatacenter_id: " << hex << (uint32_t)sid.dl.datacenter_id
               << "\ncluster_id: " << hex << (uint32_t)sid.dl.cluster_id
               << "\nrack_id_msb: " << hex << (uint32_t)sid.dl.rack_id_msb
               << "\nrack_id_lsb: " << hex << (uint32_t)sid.dl.rack_id_lsb
               << "\nchassis_id: " << hex << (uint32_t)sid.dl.chassis_id
               << "\nbic_id: " << hex << (uint32_t)sid.dl.bic_id
               << "\nbnc_id: " << hex << (uint32_t)sid.dl.bnc_id
               << "\nknc_id: " << hex << (uint32_t)sid.dl.knc_id
               << "\ndevice_id: " << hex << sid.dsid.device_id
               << "\nsensor_id: " << hex << sid.dsid.sensor_id
               << "\n";
72
73
#endif

74
          mySensorDataStore->insert(&sid, ts, *((uint64_t*)msg->getPayload()));
75
      }
76
  }
77
  delete msg;
78
79
}

80
int main(void) {
81
82

  try{
83
      /*
84
       * Catch SIGINT signals to allow for proper server shutdowns.
85
       */
86
      signal(SIGINT, sigHandler);
87
88

      /*
89
       * Allocate and initialize sensor data store.
90
       */
91
      mySensorDataStore = new SensorDataStore();
92
93
94
95

      /*
       * Start the MQTT Message Server.
       */
96
97
98
99
100
101
      SimpleMQTTServer ms;
      ms.setMessageCallback(mqttCallback);
      ms.start();

      cout << "Server running...\n";

102
103
104
105
106
107
108
      /*
       * Run (hopefully) forever...
       */
      keepRunning = 1;
      timeval start, end;
      double elapsed;

109
110
      while(keepRunning) {
          gettimeofday(&start, NULL);
111
          sleep(60);
112
113
114
115
          /* not really thread safe but will do the job */
          gettimeofday(&end, NULL);
          elapsed = (end.tv_sec - start.tv_sec) * 1000.0;
          elapsed += (end.tv_usec - start.tv_usec) / 1000.0;
116
117
          float publish = msgCtr?(pmsgCtr*100.0)/msgCtr:0;
          cout << "Message rate: " << (msgCtr/elapsed)*1000.0 << " messages/second (" << publish << "% PUBLISH)\n";
118
          msgCtr = 0;
119
          pmsgCtr = 0;
120
121
      }

122
      cout << "Stopping...\n";
123
124

      ms.stop();
125
      delete mySensorDataStore;
126
  }
127
128
  catch (const exception& e) {
      cout << "Exception: " << e.what() << "\n";
129
      exit(EXIT_FAILURE);
130
131
  }

132
  return EXIT_SUCCESS;
133
}