COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
agent-2-0.cpp File Reference
#include "support/configCosmos.h"
#include <stdlib.h>
#include "agent/agentclass.h"
#include "support/jsonlib.h"
#include "physics/physicslib.h"
#include "support/datalib.h"
#include "sys/stat.h"
#include "limits.h"
#include <iostream>
Include dependency graph for agent-2-0.cpp:

Functions

int main (int argc, char *argv[])
 

Variables

const int REQUEST_WAIT_TIME = 2
 
const int SERVER_WAIT_TIME = 6
 
string output
 
string node_name = ""
 
string agent_name = ""
 

Function Documentation

int main ( int  argc,
char *  argv[] 
)
62 {
63  int nbytes;
65  Agent *agent;
66 
67  // dont' print debug messages
68  //agent->debug_level = 0;
69 
70  agent = new Agent();
71  if (agent->cinfo == nullptr)
72  {
73  fprintf(agent->get_debug_fd(), "%16.10f %s Failed to start Agent %s on Node %s Dated %s : %s\n",currentmjd(), mjd2iso8601(currentmjd()).c_str(), agent->getAgent().c_str(), agent->getNode().c_str(), utc2iso8601(data_ctime(argv[0])).c_str(), cosmos_error_string(NODE_ERROR_NODE).c_str());
74  exit(NODE_ERROR_NODE);
75  }
76 
77 
78  // check command line arguments
79  switch (argc)
80  {
81  case 1:
82  {
83  printf("Usage: agent [ list | dump [soh, beat, ###] | node_name agent_name \"request [ arguments ]\" ]\n");
84  exit(1);
85  }
86  break;
87  case 2:
88  case 3:
89  case 4:
90  case 5:
91  // agent dump request
92  if (!strcmp(argv[1],"dump"))
93  {
94  double lmjd = 0., dmjd;
95  string channel;
96  Agent::AgentMessage message_type;
98  int i;
99  locstruc loc;
100 
101 // JIMNOTE: this block will never be entered
102 
103  switch(argc)
104  {
105  case 5:
106  agent_name = argv[4];
107  case 4:
108  node_name = argv[3];
109  case 3:
110  channel = argv[2];
111  if (channel == "soh")
112  {
113  message_type = Agent::AgentMessage::SOH;
114  }
115  else if (channel == "beat")
116  {
117  message_type = Agent::AgentMessage::BEAT;
118  }
119  else if (channel == "request")
120  {
121  message_type = Agent::AgentMessage::REQUEST;
122  }
123  else if (channel == "response")
124  {
125  message_type = Agent::AgentMessage::RESPONSE;
126  }
127  else
128  {
129  message_type = (Agent::AgentMessage)atoi(channel.c_str());
130  }
131  break;
132  case 2:
133  channel.clear();
134  message_type = Agent::AgentMessage::ALL;
135  break;
136  }
137 
138  while (1)
139  {
140  int32_t iretn;
141  if ((iretn=agent->readring(message, message_type, 1., Agent::Where::TAIL)) > 0)
142  {
143  Agent::AgentMessage message_type_read = (Agent::AgentMessage)iretn;
144 
145  // Skip if either not Agent::AgentMessage::ALL, or not desired AGENT_MESSAGE
146  if (!channel.empty() && message_type != message_type_read)
147  {
148  continue;
149  }
150 
151  if (!node_name.empty() && node_name != message.meta.beat.node)
152  {
153  continue;
154  }
155 
156  if (!agent_name.empty() && agent_name != message.meta.beat.proc)
157  {
158  continue;
159  }
160 
161  switch (message_type_read)
162  {
163  case Agent::AgentMessage::SOH:
164  printf("[SOH]");
165  break;
166  case Agent::AgentMessage::BEAT:
167  printf("[BEAT]");
168  break;
169  case Agent::AgentMessage::REQUEST:
170  printf("[REQUEST]");
171  break;
172  case Agent::AgentMessage::RESPONSE:
173  printf("[RESPONSE]");
174  break;
175  default:
176  printf("[%d]",message_type_read);
177  break;
178  }
179 
180  printf("%.15g:[%s:%s][%s:%u](%lu:%lu:%zu)\n",message.meta.beat.utc, message.meta.beat.node, message.meta.beat.proc, message.meta.beat.addr, message.meta.beat.port, message.jdata.size(), message.adata.size(), message.bdata.size());
181  printf("%s\n",message.jdata.c_str());
182  if (message_type_read < Agent::AgentMessage::BINARY)
183  {
184  if (!channel.empty())
185  {
186  printf("%s\n",message.adata.c_str());
187  }
188  }
189 
190  if ((channel=="info") && message_type_read == Agent::AgentMessage::TRACK)
191  {
192  if (agent->cinfo->node.loc.utc > 0.)
193  {
194  if (lmjd > 0.)
195  dmjd = 86400.*(agent->cinfo->node.loc.utc-lmjd);
196  else
197  dmjd = 0.;
198  loc.pos.icrf.s = agent->cinfo->node.loc.pos.icrf.s;
199  loc.pos.utc = agent->cinfo->node.loc.utc;
200  pos_eci(&loc);
201  printf("%16.15g %6.4g %s %8.3f %8.3f %8.3f %5.1f %5.1f %5.1f\n",agent->cinfo->node.loc.utc,dmjd,agent->cinfo->node.name,DEGOF(loc.pos.geod.s.lon),DEGOF(loc.pos.geod.s.lat),loc.pos.geod.s.h,agent->cinfo->node.phys.powgen,agent->cinfo->node.phys.powuse,agent->cinfo->node.phys.battlev);
202  lmjd = agent->cinfo->node.loc.utc;
203  }
204  }
205 
206  if ((channel=="imu") && message_type_read == Agent::AgentMessage::IMU) {
207  for (i=0; i<agent->cinfo->devspec.imu_cnt; i++) {
208  if (agent->cinfo->agent[0].beat.utc > 0.) {
209  if (lmjd > 0.)
210  dmjd = 86400.*(agent->cinfo->agent[0].beat.utc-lmjd);
211  else
212  dmjd = 0.;
213  printf("%.15g %.4g\n",loc.utc,dmjd);
214  lmjd = agent->cinfo->agent[0].beat.utc;
215  }
216  }
217  }
218  }
219  fflush(stdout);
220  } //end infinite while loop
221  break;
222  }
223  else if (!strcmp(argv[1],"list"))
224  {
225  size_t agent_count = 0;
226  ElapsedTime et;
227  agent->post(Agent::AgentMessage::REQUEST, "heartbeat");
228  COSMOS_SLEEP(.5);
229  do {
230  if (agent->agent_list.size() > agent_count) {
231  for (size_t i=agent_count; i<agent->agent_list.size(); ++i) {
232  beatstruc cbeat = agent->agent_list[i];
233  // NS1
234  //agent->send_request(cbeat,(char *)"getvalue {\"agent_pid\"}", output, REQUEST_WAIT_TIME);
235  // NS2 -- works!
236  agent->send_request(cbeat, "get_value {\"agent[0].pid\"}", output, REQUEST_WAIT_TIME);
237  printf("[%lu] %.15g %s %s %s %hu %u\n",i,cbeat.utc,cbeat.node,cbeat.proc,cbeat.addr,cbeat.port,cbeat.bsz);
238  printf("\t%s\n",output.c_str());
239  fflush(stdout);
240  }
241  agent_count = agent->agent_list.size();
242  }
243  COSMOS_SLEEP(.1);
244  } while (et.split() < SERVER_WAIT_TIME);
245  exit(0);
246  break;
247  } else if (!strcmp(argv[1],"list_json")) {
248  size_t agent_count = 0;
249  ElapsedTime et;
250  agent->post(Agent::AgentMessage::REQUEST, "heartbeat");
251  COSMOS_SLEEP(.1);
252  printf("{\"agent_list\":[");
253  do {
254  if (agent->agent_list.size() > agent_count) {
255  for (size_t i=agent_count; i<agent->agent_list.size(); ++i) {
256  beatstruc cbeat = agent->agent_list[i];
257  // NS1
258  //agent->send_request(cbeat,(char *)"getvalue {\"agent_pid\"}", output, REQUEST_WAIT_TIME);
259  // NS2 -- works!
260  agent->send_request(cbeat, "get_value {\"agent[0].pid\"}", output, REQUEST_WAIT_TIME);
261 
262  if(i>0) printf(",");
263  printf("{\"agent_proc\": \"%s\", ", cbeat.proc);
264  printf("\"agent_utc\": %.15g, ", cbeat.utc);
265  printf("\"agent_node\": \"%s\", ", cbeat.node);
266  printf("\"agent_addr\": \"%s\", ", cbeat.addr);
267  printf("\"agent_port\": %hu, ", cbeat.port);
268  printf("\"agent_bsz\": %u, ", cbeat.bsz);
269  // HANDLE RESPONSE OUTPUT FORMAT
270  size_t status_pos;
271  if((status_pos= output.find("[OK]") )!= string::npos){
272  if(output.at(0) == '{'){
273  if(status_pos - 1 >= 0 && output.at(status_pos - 1) == '}'){
274  printf("\"output\": %s,", output.substr(0, status_pos).c_str());
275  } else {
276  printf("\"output\": %s,", output.c_str());
277  }
278  } else {
279  printf("\"output\": \"%s\",", output.substr(status_pos ).c_str());
280  }
281  printf("\"status\": \"OK\"}");
282  } else if((status_pos = output.find("[NOK]") )!= string::npos){
283  printf("\"status\": \"NOK\"}");
284  } else {
285  printf("\"output\": %s }", output.c_str());
286  }
287  fflush(stdout);
288  }
289  fflush(stdout);
290  agent_count = agent->agent_list.size();
291  }
292  COSMOS_SLEEP(.1);
293  } while (et.split() < SERVER_WAIT_TIME);
294  printf("]}\n");
295  exit(0);
296  break;
297  }
298 
299  default:
300  if (!strcmp(argv[1],"dump")) {
301  double lmjd = 0., dmjd;
302  string channel;
303  Agent::AgentMessage message_type;
305  string header;
306  int i;
307  locstruc loc;
308 
309  if(argc == 3) {
310  channel = argv[2];
311  if (channel == "soh") {
312  message_type = Agent::AgentMessage::SOH;
313  } else {
314  if (channel == "beat") {
315  message_type = Agent::AgentMessage::BEAT;
316  } else {
317  message_type = (Agent::AgentMessage)atoi(channel.c_str());
318  }
319  }
320  } else {
321  channel.clear();
322  message_type = Agent::AgentMessage::ALL;
323  }
324 
325  while (1)
326  {
327  int32_t iretn;
328  if ((iretn=agent->readring(message, Agent::AgentMessage::ALL, 1., Agent::Where::TAIL)) > 0) {
329  Agent::AgentMessage message_type_read = (Agent::AgentMessage)iretn;
330  // Skip if either not Agent::AgentMessage::ALL, or not desired AGENT_MESSAGE
331  if (!channel.empty() && message_type != message_type_read) { continue; }
332 
333  header.resize(message.meta.jlength);
334  if (message_type_read < Agent::AgentMessage::BINARY)
335  {
336  // NS1
337  memcpy(&header[0], message.adata.data(), message.meta.jlength);
340  json_parse(message.adata.c_str(), agent->cinfo);
341  // NS2
342  } else {
343  memcpy(&header[0], message.bdata.data(), message.meta.jlength);
344  }
345 
346  switch (message_type_read) {
347  case Agent::AgentMessage::SOH:
348  printf("[SOH]");
349  break;
350  case Agent::AgentMessage::BEAT:
351  printf("[BEAT]");
352  break;
353  case Agent::AgentMessage::REQUEST:
354  printf("[REQUEST]");
355  break;
356  case Agent::AgentMessage::RESPONSE:
357  printf("[RESPONSE]");
358  break;
359  default:
360  printf("[%d]",message_type_read);
361  break;
362  }
363 
364  printf("[%d] %.15g %s %s %s %hu %u\n",i,message.meta.beat.utc,message.meta.beat.node,message.meta.beat.proc,message.meta.beat.addr,message.meta.beat.port,message.meta.beat.bsz);
365 
366  if (message_type_read < Agent::AgentMessage::BINARY && !channel.empty()) {
367  printf("%s\n",message.adata.c_str());
368  }
369 
370  if ((channel=="info") && message_type_read == Agent::AgentMessage::TRACK) {
371  if (agent->cinfo->node.loc.utc > 0.) {
372  if (lmjd > 0.)
373  dmjd = 86400.*(agent->cinfo->node.loc.utc-lmjd);
374  else
375  dmjd = 0.;
376  loc.pos.icrf.s = agent->cinfo->node.loc.pos.icrf.s;
377  loc.pos.utc = agent->cinfo->node.loc.utc;
378  pos_eci(&loc);
379  printf("%16.15g %6.4g %s %8.3f %8.3f %8.3f %5.1f %5.1f %5.1f\n",agent->cinfo->node.loc.utc,dmjd,agent->cinfo->node.name,DEGOF(loc.pos.geod.s.lon),DEGOF(loc.pos.geod.s.lat),loc.pos.geod.s.h,agent->cinfo->node.phys.powgen,agent->cinfo->node.phys.powuse,agent->cinfo->node.phys.battlev);
380  lmjd = agent->cinfo->node.loc.utc;
381  }
382  }
383 
384  if ((channel=="imu") && message_type_read == Agent::AgentMessage::IMU) {
385  for (i=0; i<agent->cinfo->devspec.imu_cnt; i++) {
386  if (agent->cinfo->agent[0].beat.utc > 0.) {
387  if (lmjd > 0.)
388  dmjd = 86400.*(agent->cinfo->agent[0].beat.utc-lmjd);
389  else
390  dmjd = 0.;
391  printf("%.15g %.4g\n",loc.utc,dmjd);
392  lmjd = agent->cinfo->agent[0].beat.utc;
393  }
394  }
395  }
396  }
397  fflush(stdout);
398  } //end infinite while loop
399  } else {
400 
401 // cbeat = agent->find_agent(argv[1], argv[2], SERVER_WAIT_TIME);
402 // if (cbeat.exists)
403  if ((nbytes = agent->get_agent(argv[1], argv[2], SERVER_WAIT_TIME, cbeat)) > 0) {
404  if(argc == 3)
405  {
406  nbytes = agent->send_request(cbeat, "help", std::ref(output), REQUEST_WAIT_TIME);
407  printf("%s [%d]\n", output.c_str(), nbytes);
408  }
409  else
410  {
411  string request;
412  request = argv[3];
413  for (size_t i=0; i<(size_t)argc-4; ++i)
414  {
415  request += " ";
416  request += argv[i+4];
417  }
418  nbytes = agent->send_request(cbeat,request.c_str(), output, REQUEST_WAIT_TIME);
419  // printf("%s [%d]\n", output.c_str(), nbytes);
420  // printf("{\"request_output\": %s, \"bytes\": %d }\n", output.c_str(), nbytes);
421  // HANDLE RESPONSE OUTPUT FORMAT
422  printf("{");
423  size_t status_pos;
424  if((status_pos= output.find("[OK]") )!= string::npos){
425  if(output.at(0) == '{'){
426  if(status_pos - 1 >= 0 && output.at(status_pos - 1) == '}'){
427  printf("\"output\": %s,", output.substr(0, status_pos).c_str());
428  } else {
429  printf("\"output\": %s,", output.c_str());
430  }
431  } else {
432  printf("\"output\": \"%s\",", output.substr(0,status_pos ).c_str());
433  }
434  printf("\"status\": \"OK\"}\n");
435  } else if((status_pos = output.find("[NOK]") )!= string::npos){
436  printf("\"status\": \"NOK\"}\n");
437  } else {
438  printf("\"output\": %s }\n", output.c_str());
439  }
440  }
441  } else {
442  if (!nbytes){
443  fprintf(stderr,"node-agent pair [%s:%s] not found\n",argv[1],argv[2]);
444  printf("{\"error\": \"node-agent pair [%s:%s] not found\" }\n",argv[1],argv[2]);
445  }
446  else
447  printf("Error: %d\n", nbytes);
448  }
449  }
450  }
451 }
uint16_t imu_cnt
Definition: jsondef.h:3871
FILE * get_debug_fd(double mjd=0.)
Definition: agentclass.cpp:2645
AgentMessage
Type of Agent Message. Types > 127 are binary.
Definition: agentclass.h:216
beatstruc beat
Definition: agentclass.h:268
vector< uint8_t > bdata
Definition: agentclass.h:275
Definition: jsondef.h:923
int i
Definition: rw_test.cpp:37
int32_t json_parse(string jstring, cosmosstruc *cinfo)
Parse JSON using Name Space.
Definition: jsonlib.cpp:4799
int32_t send_request(beatstruc cbeat, string request, string &output, float waitsec=5.)
Send a request over AGENT.
Definition: agentclass.cpp:424
string getNode()
Listen for heartbeat.
Definition: agentclass.cpp:2607
uint32_t bsz
Transfer buffer size.
Definition: jsondef.h:938
char addr[18]
Protocol Address.
Definition: jsondef.h:934
float battlev
Definition: jsondef.h:3431
int iretn
Definition: rw_test.cpp:37
int32_t json_clear_cosmosstruc(int32_t type, cosmosstruc *cinfo)
Clear global data structure.
Definition: jsonlib.cpp:6268
double utc
Master time for location, in Modified Julian Day.
Definition: convertdef.h:879
string output
Definition: agent-2-0.cpp:56
devicestruc
Definition: jsondef.h:150
ElapsedTime et
Definition: agent_cpu_device_test.cpp:51
const int SERVER_WAIT_TIME
Definition: agent-2-0.cpp:54
string adata
Definition: agentclass.h:276
nodestruc node
Structure for summary information in node.
Definition: jsondef.h:4220
string cosmos_error_string(int32_t cosmos_errno)
Definition: cosmos-errno.cpp:45
int32_t get_agent(string node, string agent, double waitsec, beatstruc &cbeat)
Get specific server.
Definition: agentclass.cpp:513
rvector s
Location.
Definition: convertdef.h:163
int32_t post(messstruc mess)
Post a Cosmos::Agent::messstruc.
Definition: agentclass.cpp:2074
string node_name
Definition: agent-2-0.cpp:57
char proc[40+1]
Heartbeat Agent Name.
Definition: jsondef.h:930
char node[40+1]
Definition: jsondef.h:928
string getAgent()
Definition: agentclass.cpp:2609
static Agent * agent
ensure the Agent constructor creates only one instance per process
Definition: agent_001.cpp:45
double utc
Definition: convertdef.h:735
uint8_t message[300]
Definition: kpc9612p_send.cpp:36
char name[40+1]
Node Name.
Definition: jsondef.h:3556
const int REQUEST_WAIT_TIME
Definition: agent-2-0.cpp:53
uint16_t port
AGENT port.
Definition: jsondef.h:936
Definition: agentclass.h:139
uint16_t jlength
Definition: agentclass.h:267
locstruc loc
Location structure.
Definition: jsondef.h:3596
#define DEGOF(rad)
Degrees of a Radian value.
Definition: math/constants.h:33
int32_t pos_eci(locstruc *loc)
Set ECI position.
Definition: convertlib.cpp:258
double h
Height in meters.
Definition: vector.h:229
string agent_name
Definition: agent-2-0.cpp:58
Storage for messages.
Definition: agentclass.h:272
nodestruc
Definition: jsondef.h:146
double lon
Longitude in radians.
Definition: vector.h:227
double data_ctime(string path)
Definition: datalib.cpp:1910
float powuse
Definition: jsondef.h:3433
Definition: elapsedtime.h:62
double currentmjd(double offset)
Current UTC in Modified Julian Days.
Definition: timelib.cpp:65
posstruc pos
posstruc for this time.
Definition: convertdef.h:881
gvector s
Position vector.
Definition: convertdef.h:263
double lat
Latitude in radians.
Definition: vector.h:225
pollstruc meta
Definition: agentclass.h:274
string jdata
Definition: agentclass.h:277
string utc2iso8601(double utc)
ISO 8601 version of time.
Definition: timelib.cpp:1286
vector< agentstruc > agent
Single entry vector for agent information.
Definition: jsondef.h:4247
double utc
Definition: jsondef.h:926
static beatstruc cbeat
Definition: agent_file.cpp:92
cosmosstruc * cinfo
Definition: agentclass.h:346
vector< beatstruc > agent_list
List of active agents.
Definition: agentclass.h:349
geoidpos geod
Definition: convertdef.h:741
devspecstruc devspec
Structure for devices (components) special data in node, by type.
Definition: jsondef.h:4241
double split()
ElapsedTime::split, gets the current elapsed time since the start()
Definition: elapsedtime.cpp:234
string mjd2iso8601(double mjd)
Definition: timelib.cpp:1316
cartpos icrf
Definition: convertdef.h:736
Inertial Measurement Unit.
Definition: jsondef.h:496
Definition: convertdef.h:876
physicsstruc phys
Definition: jsondef.h:3597
int32_t readring(messstruc &message, AgentMessage type=Agent::AgentMessage::ALL, float waitsec=1., Where where=Where::TAIL, string proc="", string node="")
Check Ring for message.
Definition: agentclass.cpp:2395
float powgen
Definition: jsondef.h:3432
#define NODE_ERROR_NODE
Definition: cosmos-errno.h:143

Variable Documentation

const int REQUEST_WAIT_TIME = 2
const int SERVER_WAIT_TIME = 6
string output
string node_name = ""
string agent_name = ""