COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
agent_orig.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_orig.cpp:

Functions

bool is_node (std::vector< string > nl, string node_name)
 
void print_node_list (std::vector< string > &nlp)
 
int main (int argc, char *argv[])
 

Variables

string output
 
Agentagent
 
const int REQUEST_WAIT_TIME = 2
 
const int SERVER_WAIT_TIME = 4
 

Function Documentation

bool is_node ( std::vector< string >  nl,
string  node_name 
)
65 {
66 
67  for (string node: nl)
68  {
69  if(node == node_name)
70  {
71  return true;
72  }
73  }
74  return false;
75 }
string node_name
Definition: agent_001.cpp:46
static string node
Definition: agent_monitor.cpp:126
void print_node_list ( std::vector< string > &  nlp)
78  {
79 
80  if(nlp.empty())
81  {
82  return;
83  }
84 
85  for(string n: nlp)
86  {
87  printf(" %s\n", n.c_str());
88  }
89  return;
90 }
int main ( int  argc,
char *  argv[] 
)
93 {
94  int nbytes;
96  std::vector<string> nl;
97  data_list_nodes(nl);
98 
99  agent = new Agent();
100 
101  // check command line arguments
102  switch (argc)
103  {
104  case 1:
105  {
106  printf("Usage: agent [ list | dump [soh, beat, ###] | node_name agent_name \"request [ arguments ]\" ]\n");
107  // printf("\n List of available nodes:\n\n");
108  // print_node_list(nl);
109  // printf("\n");
110  exit(1);
111  }
112  break;
113  case 2:
114  // agent dump request
115  if (!strcmp(argv[1],"dump"))
116  {
117  double lmjd = 0., dmjd;
118  string channel;
119  Agent::AgentMessage cnum;
120  Agent::messstruc mess;
121  int i;
122  locstruc loc;
123 
124  if(argc == 3)
125  {
126  channel = argv[2];
127  if (channel == "soh")
128  {
129  cnum = Agent::AgentMessage::SOH;
130  }
131  else
132  {
133  if (channel == "beat")
134  {
135  cnum = Agent::AgentMessage::BEAT;
136  }
137  else
138  {
139  cnum = (Agent::AgentMessage)atoi(channel.c_str());
140  }
141  }
142  }
143  else
144  {
145  channel.clear();
146  cnum = Agent::AgentMessage::ALL;
147  }
148 
149  while (1)
150  {
151  int32_t iretn;
152  if ((iretn=agent->readring(mess, Agent::AgentMessage::ALL, 1)) > 0)
153  {
155  string utc = json_extract_namedmember(mess.jdata.c_str(), "agent_utc");
156  string node = json_convert_string(json_extract_namedmember(mess.jdata.c_str(), "agent_node"));
157  string proc = json_extract_namedmember(mess.jdata.c_str(), "agent_proc");
158  string addr = json_convert_string(json_extract_namedmember(mess.jdata.c_str(), "agent_addr"));
159  string port = json_extract_namedmember(mess.jdata.c_str(), "agent_port");
160  if (!channel.empty() && cnum != pretn)
161  {
162  continue;
163  }
164 
165  if (pretn < Agent::AgentMessage::BINARY)
166  {
169  json_parse(mess.adata.c_str(), agent->cinfo);
170  }
171 
172  switch (pretn)
173  {
174  case Agent::AgentMessage::SOH:
175  printf("[SOH]");
176  break;
177  case Agent::AgentMessage::BEAT:
178  printf("[BEAT]");
179  break;
180  default:
181  printf("[%d]",pretn);
182  break;
183  }
184  printf("%s:[%s:%s][%s:%s](%" PRIu64 ":%" PRIu64 ")\n",utc.c_str(), node.c_str(), proc.c_str(), addr.c_str(), port.c_str(), mess.jdata.size(), mess.adata.size());
185  if (pretn < Agent::AgentMessage::BINARY && !channel.empty())
186  {
187  printf("%s\n",mess.adata.c_str());
188  }
189  if ((channel=="info") && pretn == Agent::AgentMessage::TRACK)
190  {
191  if (agent->cinfo->node.loc.utc > 0.)
192  {
193  if (lmjd > 0.)
194  dmjd = 86400.*(agent->cinfo->node.loc.utc-lmjd);
195  else
196  dmjd = 0.;
197  loc.pos.icrf.s = agent->cinfo->node.loc.pos.icrf.s;
198  loc.pos.utc = agent->cinfo->node.loc.utc;
199  pos_eci(&loc);
200  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);
201  lmjd = agent->cinfo->node.loc.utc;
202  }
203  }
204  if ((channel=="imu") && pretn == Agent::AgentMessage::IMU)
205  {
206  for (i=0; i<agent->cinfo->devspec.imu_cnt; i++)
207  {
208  if (agent->cinfo->agent[0].beat.utc > 0.)
209  {
210  if (lmjd > 0.)
211  dmjd = 86400.*(agent->cinfo->agent[0].beat.utc-lmjd);
212  else
213  dmjd = 0.;
214  printf("%.15g %.4g\n",loc.utc,dmjd);
215  lmjd = agent->cinfo->agent[0].beat.utc;
216  }
217  }
218  }
219  }
220  fflush(stdout);
221  } //end infinite while loop
222  }
223  else if (!strcmp(argv[1],"list"))
224  {
225  std::vector<beatstruc> cbeat;
227 
228  if (cbeat.size() > 0)
229  {
230  std::cout<<"Number of Agents found: "<<cbeat.size()<<std::endl;
231  for (unsigned int i=0; i<cbeat.size(); i++)
232  {
233  agent->send_request(cbeat[i],(char *)"getvalue {\"agent_pid\"}",output, REQUEST_WAIT_TIME);
234  printf("[%d] %.15g %s %s %s %hu %u\n",i,cbeat[i].utc,cbeat[i].node,cbeat[i].proc,cbeat[i].addr,cbeat[i].port,cbeat[i].bsz);
235  printf("\t%s\n",output.c_str());
236  }
237  }
238  exit(0);
239  }
240  break;
241  default:
242 
243  // if (argc == 2)
244  // {
245  // if (is_node(nl,argv[1]))
246  // {
247  // std::vector<beatstruc> cbeat;
248  // cbeat = agent->find_agents(SERVER_WAIT_TIME);
249 
250  // printf("\n List of available agents:\n\n");
251 
252  // if (!cbeat.empty()) {
253  // std::cout<<"Number of Agents found: "<<cbeat.size()<<std::endl;
254  // for (unsigned int i=0; i<cbeat.size(); i++)
255  // if(!strcmp(argv[1],cbeat[i].node))
256  // printf(" %s\n", cbeat[i].proc);
257 
258  // }
259  // printf("\n");
260  // }
261  // else
262  // {
263  // printf("\n Node <%s> not found.\n",argv[1]);
264  // printf("\n List of available nodes:\n\n");
265  // print_node_list(nl);
266  // printf("\n");
267  // }
268  // exit(1);
269  // }
270 
271 
272  // if (argc != 4)
273  // {
274  // printf("Usage: agent [ list | dump [soh, beat, ###] | node_name agent_name \"request [ arguments ]\" ]\n");
275  // user_is_clueless = true;
276  // //exit (1);
277  // }
278 
279  if (!strcmp(argv[1],"dump"))
280  {
281  double lmjd = 0., dmjd;
282  string channel;
283  Agent::AgentMessage cnum;
284  Agent::messstruc mess;
285  int i;
286  locstruc loc;
287 
288  if(argc == 3)
289  {
290  channel = argv[2];
291  if (channel == "soh")
292  {
293  cnum = Agent::AgentMessage::SOH;
294  }
295  else
296  {
297  if (channel == "beat")
298  {
299  cnum = Agent::AgentMessage::BEAT;
300  }
301  else
302  {
303  cnum = (Agent::AgentMessage)atoi(channel.c_str());
304  }
305  }
306  }
307  else
308  {
309  channel.clear();
310  cnum = Agent::AgentMessage::ALL;
311  }
312 
313  while (1)
314  {
315  int32_t iretn;
316  if ((iretn=agent->readring(mess, Agent::AgentMessage::ALL, 1)) > 0)
317  {
319  string utc = json_extract_namedmember(mess.jdata.c_str(), "agent_utc");
320  string node = json_convert_string(json_extract_namedmember(mess.jdata.c_str(), "agent_node"));
321  string proc = json_extract_namedmember(mess.jdata.c_str(), "agent_proc");
322  string addr = json_convert_string(json_extract_namedmember(mess.jdata.c_str(), "agent_addr"));
323  string port = json_extract_namedmember(mess.jdata.c_str(), "agent_port");
324  if (!channel.empty() && cnum != pretn)
325  {
326  continue;
327  }
328 
329  if (pretn < Agent::AgentMessage::BINARY)
330  {
333  json_parse(mess.adata.c_str(), agent->cinfo);
334  }
335 
336  switch (pretn)
337  {
338  case Agent::AgentMessage::SOH:
339  printf("[SOH]");
340  break;
341  case Agent::AgentMessage::BEAT:
342  printf("[BEAT]");
343  break;
344  default:
345  printf("[%d]",pretn);
346  break;
347  }
348  printf("%s:[%s:%s][%s:%s](%" PRIu64 ":%" PRIu64 ")\n",utc.c_str(), node.c_str(), proc.c_str(), addr.c_str(), port.c_str(), mess.jdata.size(), mess.adata.size());
349  if (pretn < Agent::AgentMessage::BINARY && !channel.empty())
350  {
351  printf("%s\n",mess.adata.c_str());
352  }
353  if ((channel=="info") && pretn == Agent::AgentMessage::TRACK)
354  {
355  if (agent->cinfo->node.loc.utc > 0.)
356  {
357  if (lmjd > 0.)
358  dmjd = 86400.*(agent->cinfo->node.loc.utc-lmjd);
359  else
360  dmjd = 0.;
361  loc.pos.icrf.s = agent->cinfo->node.loc.pos.icrf.s;
362  loc.pos.utc = agent->cinfo->node.loc.utc;
363  pos_eci(&loc);
364  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);
365  lmjd = agent->cinfo->node.loc.utc;
366  }
367  }
368  if ((channel=="imu") && pretn == Agent::AgentMessage::IMU)
369  {
370  for (i=0; i<agent->cinfo->devspec.imu_cnt; i++)
371  {
372  if (agent->cinfo->agent[0].beat.utc > 0.)
373  {
374  if (lmjd > 0.)
375  dmjd = 86400.*(agent->cinfo->agent[0].beat.utc-lmjd);
376  else
377  dmjd = 0.;
378  printf("%.15g %.4g\n",loc.utc,dmjd);
379  lmjd = agent->cinfo->agent[0].beat.utc;
380  }
381  }
382  }
383  }
384  fflush(stdout);
385  } //end infinite while loop
386  }
387  else
388  {
389  nl.clear();
390 
391  if ((nbytes = agent->get_agent(argv[1], argv[2], SERVER_WAIT_TIME, cbeat)) > 0)
392  {
393  if(argc == 3)
394  {
395  printf("List of available requests:\n");
396  nbytes = agent->send_request(cbeat, "help", output, REQUEST_WAIT_TIME);
397  printf("%s [%d]\n",output.c_str(), nbytes);
398  }
399  else
400  {
401  string request;
402  request = argv[3];
403  for (size_t i=0; i<(size_t)argc-4; ++i)
404  {
405  request += " ";
406  request += argv[i+4];
407  }
408  nbytes = agent->send_request(cbeat ,request , output, REQUEST_WAIT_TIME);
409  printf("%s [%d]\n",output.c_str(), nbytes);
410  }
411  }
412  else
413  {
414  if (!nbytes)
415  fprintf(stderr,"node-agent pair [%s:%s] not found\n",argv[1],argv[2]);
416  else
417  printf("Error: %d\n",nbytes);
418  }
419  }
420  }
421 }
static string port
Definition: add_radio.cpp:16
uint16_t imu_cnt
Definition: jsondef.h:3871
AgentMessage
Type of Agent Message. Types > 127 are binary.
Definition: agentclass.h:216
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
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
devicestruc
Definition: jsondef.h:150
string json_extract_namedmember(string json, string token)
Extract JSON value matching name.
Definition: jsonlib.cpp:4417
string adata
Definition: agentclass.h:276
vector< string > data_list_nodes()
Get list of Nodes, directly.
Definition: datalib.cpp:583
nodestruc node
Structure for summary information in node.
Definition: jsondef.h:4220
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
double utc
Definition: convertdef.h:735
char name[40+1]
Node Name.
Definition: jsondef.h:3556
Definition: agentclass.h:139
const int REQUEST_WAIT_TIME
Definition: agent_orig.cpp:61
locstruc loc
Location structure.
Definition: jsondef.h:3596
const int SERVER_WAIT_TIME
Definition: agent_orig.cpp:62
#define DEGOF(rad)
Degrees of a Radian value.
Definition: math/constants.h:33
string output
Definition: agent_orig.cpp:58
int32_t pos_eci(locstruc *loc)
Set ECI position.
Definition: convertlib.cpp:258
double h
Height in meters.
Definition: vector.h:229
Storage for messages.
Definition: agentclass.h:272
nodestruc
Definition: jsondef.h:146
double lon
Longitude in radians.
Definition: vector.h:227
float powuse
Definition: jsondef.h:3433
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
string jdata
Definition: agentclass.h:277
vector< agentstruc > agent
Single entry vector for agent information.
Definition: jsondef.h:4247
string json_convert_string(string object)
Convert JSON to string.
Definition: jsonlib.cpp:4557
static beatstruc cbeat
Definition: agent_file.cpp:92
cosmosstruc * cinfo
Definition: agentclass.h:346
geoidpos geod
Definition: convertdef.h:741
Agent * agent
Definition: agent_orig.cpp:59
devspecstruc devspec
Structure for devices (components) special data in node, by type.
Definition: jsondef.h:4241
static string node
Definition: agent_monitor.cpp:126
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
vector< beatstruc > find_agents(double waitsec=0.)
Find single server.
Definition: agentclass.cpp:605
float powgen
Definition: jsondef.h:3432

Variable Documentation

string output
Agent* agent
const int REQUEST_WAIT_TIME = 2
const int SERVER_WAIT_TIME = 4