COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
cosmos.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 cosmos.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[] 
)
75 {
76  int nbytes;
78  vector<string> nl;
79  data_list_nodes(nl);
80  Agent *agent;
81 
82  // dont' print debug messages
83  //agent->debug_level = 0;
84  agent = new Agent("","");
85 
86  // check command line arguments
87  switch (argc)
88  {
89  case 1:
90  {
91  printf("Usage: cosmos [ list | dump [soh, beat, ###] | node_name agent_name \"request [ arguments ]\" ]\n");
92  printf("Usage (future): cosmos [-node -makenode -updatetle ...]\n");
93  // printf("\n List of available nodes:\n\n");
94  // print_node_list(nl);
95  // printf("\n");
96  exit(1);
97  }
98  break;
99  case 2:
100  case 3:
101  case 4:
102  case 5:
103  // agent dump request
104  if (!strcmp(argv[1],"dump"))
105  {
106  double lmjd = 0., dmjd;
107  string channel;
108  Agent::AgentMessage cnum;
110  int i;
111  locstruc loc;
112 
113 // JIMNOTE: this block will never be entered
114 
115  switch(argc)
116  {
117  case 5:
118  agent_name = argv[4];
119  case 4:
120  node_name = argv[3];
121  case 3:
122  channel = argv[2];
123  if (channel == "soh")
124  {
125  cnum = Agent::AgentMessage::SOH;
126  }
127  else
128  {
129  if (channel == "beat")
130  {
131  cnum = Agent::AgentMessage::BEAT;
132  }
133  else
134  {
135  cnum = (Agent::AgentMessage)atoi(channel.c_str());
136  }
137  }
138  break;
139  case 2:
140  channel.clear();
141  cnum = Agent::AgentMessage::ALL;
142  break;
143  }
144 
145  while (1)
146  {
147  int32_t iretn;
148  if ((iretn=agent->readring(message, cnum, 1., Agent::Where::TAIL)) > 0)
149  {
151 
152  // Skip if either not Agent::AgentMessage::ALL, or not desired AGENT_MESSAGE
153  if (!channel.empty() && cnum != pretn)
154  {
155  continue;
156  }
157 
158  if (!node_name.empty() && node_name != message.meta.beat.node)
159  {
160  continue;
161  }
162 
163  if (!agent_name.empty() && agent_name != message.meta.beat.proc)
164  {
165  continue;
166  }
167 
168  switch (pretn)
169  {
170  case Agent::AgentMessage::SOH:
171  printf("[SOH]");
172  break;
173  case Agent::AgentMessage::BEAT:
174  printf("[BEAT]");
175  break;
176  default:
177  printf("[%d]",pretn);
178  break;
179  }
180 
181  printf("%.15g:[%s:%s][%s:%u](%" PRIu64 ":%" PRIu64 ":%" PRIu64 ")\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());
182  printf("%s\n",message.jdata.c_str());
183  if (pretn < Agent::AgentMessage::BINARY)
184  {
185  if (!channel.empty())
186  {
187  printf("%s\n",message.adata.c_str());
188  }
189  }
190 
191  if ((channel=="info") && pretn == Agent::AgentMessage::TRACK)
192  {
193  if (agent->cinfo->node.loc.utc > 0.)
194  {
195  if (lmjd > 0.)
196  dmjd = 86400.*(agent->cinfo->node.loc.utc-lmjd);
197  else
198  dmjd = 0.;
199  loc.pos.icrf.s = agent->cinfo->node.loc.pos.icrf.s;
200  loc.pos.utc = agent->cinfo->node.loc.utc;
201  pos_eci(&loc);
202  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);
203  lmjd = agent->cinfo->node.loc.utc;
204  }
205  }
206 
207  if ((channel=="imu") && pretn == Agent::AgentMessage::IMU)
208  {
209  for (i=0; i<agent->cinfo->devspec.imu_cnt; i++)
210  {
211  if (agent->cinfo->agent[0].beat.utc > 0.)
212  {
213  if (lmjd > 0.)
214  dmjd = 86400.*(agent->cinfo->agent[0].beat.utc-lmjd);
215  else
216  dmjd = 0.;
217  printf("%.15g %.4g\n",loc.utc,dmjd);
218  lmjd = agent->cinfo->agent[0].beat.utc;
219  }
220  }
221  }
222  }
223  fflush(stdout);
224  } //end infinite while loop
225  break;
226  }
227  else if (!strcmp(argv[1],"list"))
228  {
229  size_t agent_count = 0;
230  ElapsedTime et;
231  do
232  {
233  if (agent->agent_list.size() > agent_count)
234  {
235  for (size_t i=agent_count; i<agent->agent_list.size(); ++i)
236  {
237  beatstruc cbeat = agent->agent_list[i];
238  agent->send_request(cbeat,(char *)"getvalue {\"agent_pid\"}", output, REQUEST_WAIT_TIME);
239  printf("[%lu] %.15g %s %s %s %hu %u\n",i,cbeat.utc,cbeat.node,cbeat.proc,cbeat.addr,cbeat.port,cbeat.bsz);
240  printf("\t%s\n",output.c_str());
241  fflush(stdout);
242  }
243  agent_count = agent->agent_list.size();
244  }
245  COSMOS_SLEEP(.1);
246  } while (et.split() < SERVER_WAIT_TIME);
247  exit(0);
248  break;
249  }
250 
251  default:
252  if (!strcmp(argv[1],"dump"))
253  {
254  double lmjd = 0., dmjd;
255  string channel;
256  Agent::AgentMessage cnum;
258  string header;
259  int i;
260  locstruc loc;
261 
262  if(argc == 3)
263  {
264  channel = argv[2];
265  if (channel == "soh")
266  {
267  cnum = Agent::AgentMessage::SOH;
268  }
269  else
270  {
271  if (channel == "beat")
272  {
273  cnum = Agent::AgentMessage::BEAT;
274  }
275  else
276  {
277  cnum = (Agent::AgentMessage)atoi(channel.c_str());
278  }
279  }
280  }
281  else
282  {
283  channel.clear();
284  cnum = Agent::AgentMessage::ALL;
285  }
286 
287  while (1)
288  {
289  int32_t iretn;
290  if ((iretn=agent->readring(message, Agent::AgentMessage::ALL, 1., Agent::Where::TAIL)) > 0)
291  {
293  // Skip if either not Agent::AgentMessage::ALL, or not desired AGENT_MESSAGE
294  if (!channel.empty() && cnum != pretn)
295  {
296  continue;
297  }
298 
299  header.resize(message.meta.jlength);
300  if (pretn < Agent::AgentMessage::BINARY)
301  {
302  memcpy(&header[0], message.adata.data(), message.meta.jlength);
305  json_parse(message.adata.c_str(), agent->cinfo);
306  }
307  else
308  {
309  memcpy(&header[0], message.bdata.data(), message.meta.jlength);
310  }
311 
312  switch (pretn)
313  {
314  case Agent::AgentMessage::SOH:
315  printf("[SOH]");
316  break;
317  case Agent::AgentMessage::BEAT:
318  printf("[BEAT]");
319  break;
320  default:
321  printf("[%d]",pretn);
322  break;
323  }
324 
325  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);
326 
327  if (pretn < Agent::AgentMessage::BINARY && !channel.empty())
328  {
329  printf("%s\n",message.adata.c_str());
330  }
331 
332  if ((channel=="info") && pretn == Agent::AgentMessage::TRACK)
333  {
334  if (agent->cinfo->node.loc.utc > 0.)
335  {
336  if (lmjd > 0.)
337  dmjd = 86400.*(agent->cinfo->node.loc.utc-lmjd);
338  else
339  dmjd = 0.;
340  loc.pos.icrf.s = agent->cinfo->node.loc.pos.icrf.s;
341  loc.pos.utc = agent->cinfo->node.loc.utc;
342  pos_eci(&loc);
343  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);
344  lmjd = agent->cinfo->node.loc.utc;
345  }
346  }
347 
348  if ((channel=="imu") && pretn == Agent::AgentMessage::IMU)
349  {
350  for (i=0; i<agent->cinfo->devspec.imu_cnt; i++)
351  {
352  if (agent->cinfo->agent[0].beat.utc > 0.)
353  {
354  if (lmjd > 0.)
355  dmjd = 86400.*(agent->cinfo->agent[0].beat.utc-lmjd);
356  else
357  dmjd = 0.;
358  printf("%.15g %.4g\n",loc.utc,dmjd);
359  lmjd = agent->cinfo->agent[0].beat.utc;
360  }
361  }
362  }
363  }
364  fflush(stdout);
365  } //end infinite while loop
366  }
367  else
368  {
369  nl.clear();
370 
371  if ((nbytes = agent->get_agent(argv[1], argv[2], SERVER_WAIT_TIME, cbeat)) > 0)
372  {
373  if(argc == 3)
374  {
375  printf("List of available requests:\n");
376  nbytes = agent->send_request(cbeat,(char*)"help", ref(output), REQUEST_WAIT_TIME);
377  printf("%s [%d]\n", output.c_str(), nbytes);
378  }
379  else
380  {
381  string request;
382  request = argv[3];
383  for (size_t i=0; i<(size_t)argc-4; ++i)
384  {
385  request += " ";
386  request += argv[i+4];
387  }
388  nbytes = agent->send_request(cbeat,request.c_str(), output, REQUEST_WAIT_TIME);
389  printf("%s [%d]\n", output.c_str(), nbytes);
390  }
391  }
392  else
393  {
394  if (!nbytes)
395  fprintf(stderr,"node-agent pair [%s:%s] not found\n",argv[1],argv[2]);
396  else
397  printf("Error: %d\n", nbytes);
398  }
399  }
400  }
401 }
uint16_t imu_cnt
Definition: jsondef.h:3871
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
uint32_t bsz
Transfer buffer size.
Definition: jsondef.h:938
const int SERVER_WAIT_TIME
Definition: cosmos.cpp:54
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
devicestruc
Definition: jsondef.h:150
ElapsedTime et
Definition: agent_cpu_device_test.cpp:51
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
char proc[40+1]
Heartbeat Agent Name.
Definition: jsondef.h:930
string output
Definition: cosmos.cpp:69
char node[40+1]
Definition: jsondef.h:928
string agent_name
Definition: cosmos.cpp:71
string node_name
Definition: cosmos.cpp:70
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
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
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
Definition: elapsedtime.h:62
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
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
const int REQUEST_WAIT_TIME
Definition: cosmos.cpp:53
double split()
ElapsedTime::split, gets the current elapsed time since the start()
Definition: elapsedtime.cpp:234
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

Variable Documentation

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