COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
agent_example.cpp File Reference
#include "support/configCosmos.h"
#include <stdio.h>
#include "agent/agentclass.h"
#include "device/cpu/devicecpu.h"
Include dependency graph for agent_example.cpp:

Macros

#define MAXBUFFERSIZE   2560
 
#define REQUEST_RUN_PROGRAM   0
 

Functions

int32_t request_run_program (string &request, string &response, Agent *)
 
int main (int argc, char *argv[])
 

Variables

char agentname [COSMOS_MAX_NAME+1] = "example"
 
char node [50] = "null"
 
int waitsec = 5
 
Agentagent
 
int32_t diskfree
 
int32_t stateflag
 
int32_t myport
 
DeviceCpu cpu
 

Macro Definition Documentation

#define MAXBUFFERSIZE   2560
#define REQUEST_RUN_PROGRAM   0

Function Documentation

int32_t request_run_program ( string &  request,
string &  response,
Agent  
)
111 {
112  int i;
113  int32_t iretn = 0;
114  FILE *pd;
115  bool flag;
116 
117  // Run Program
118  flag = false;
119  for (i=0; i<AGENTMAXBUFFER-1; i++)
120  {
121  if (flag)
122  {
123  if (request[i] != ' ')
124  break;
125  }
126  else
127  {
128  if (request[i] == ' ')
129  flag = true;
130  }
131  }
132 
133  if (i == AGENTMAXBUFFER-1)
134  {
135  response = ("unmatched");
136  }
137  else
138  {
139  // open process and read response
140 #ifdef COSMOS_WIN_BUILD_MSVC
141  if ((pd=_popen(&request[i], "r")) != NULL)
142 #else
143  if ((pd=popen(&request[i],"r")) != NULL)
144 #endif
145  {
146  response.clear();
147  iretn = fread(&response[0],1,AGENTMAXBUFFER-1,pd);
148  response[iretn] = 0;
149  iretn = 1;
150 #ifdef COSMOS_WIN_BUILD_MSVC
151  _pclose(pd);
152 #else
153  pclose(pd); // close process
154 #endif
155  }
156  else
157  {
158  response.clear();
159  iretn = 0;
160  }
161  }
162 
163  return iretn;
164 }
double pd[9][150]
Definition: nrlmsise-00_data.cpp:88
int i
Definition: rw_test.cpp:37
int iretn
Definition: rw_test.cpp:37
#define AGENTMAXBUFFER
Maximum AGENT transfer buffer size.
Definition: jsondef.h:438
int main ( int  argc,
char *  argv[] 
)
61 {
62  int32_t iretn;
63 
64  // Check for other instance of this agent
65  if (argc == 2)
66  strcpy(node,argv[1]);
67 
68  // Initialization stuff
69 
70 
71  // Initialize the Agent
72  // near future: support cubesat space protocol
73  // port number = 0 in this case, automatic assignment of port
74  if (!(agent = new Agent(node, agentname, 1., MAXBUFFERSIZE, (bool)true)))
76 
77  // Add additional requests
78  if ((iretn=agent->add_request("runprogram",request_run_program)))
79  exit (iretn);
80 
81  // Start main thread
82 #if !defined(COSMOS_WIN_OS)
83 // FILE *fp;
84 // struct statfs fsbuf;
85 #endif
86  double nmjd;
87  int32_t sleept;
88 
89  // Start performing the body of the agent
90  nmjd = currentmjd(0.);
91  while(agent->running())
92  {
93  // Set beginning of next cycle;
94  nmjd += agent->cinfo->agent[0].aprd/86400.;
95  // Gather system information
96  if (agent->cinfo->devspec.cpu_cnt)
97  {
98  agent->cinfo->device[agent->cinfo->devspec.cpu[0]].cpu.load = cpu.getLoad();
100  }
101 
102  sleept = (int32_t)((nmjd - currentmjd(0.))*86400000000.);
103  if (sleept < 0) sleept = 0;
104  COSMOS_USLEEP(sleept);
105  }
106  return 0;
107 }
#define MAXBUFFERSIZE
Definition: agent_example.cpp:49
int iretn
Definition: rw_test.cpp:37
char node[50]
Definition: agent_example.cpp:43
char agentname[COSMOS_MAX_NAME+1]
Definition: agent_example.cpp:42
double getVirtualMemoryTotal()
Definition: devicecpu.cpp:168
vector< devicestruc > device
Vector of all general (common) information for devices (components) in node.
Definition: jsondef.h:4238
uint16_t running()
Check if we&#39;re supposed to be running.
Definition: agentclass.cpp:391
int32_t add_request(string token, external_request_function function, string synopsis="", string description="")
Add internal request to Agent request list with description and synopsis.
Definition: agentclass.cpp:312
Definition: agentclass.h:139
double getLoad()
Definition: devicecpu.cpp:38
uint16_t cpu_cnt
Definition: jsondef.h:3867
double currentmjd(double offset)
Current UTC in Modified Julian Days.
Definition: timelib.cpp:65
vector< uint16_t > cpu
Definition: jsondef.h:3900
vector< agentstruc > agent
Single entry vector for agent information.
Definition: jsondef.h:4247
DeviceCpu cpu
Definition: agent_example.cpp:58
cosmosstruc * cinfo
Definition: agentclass.h:346
Agent * agent
Definition: agent_example.cpp:47
int32_t request_run_program(string &request, string &response, Agent *)
Definition: agent_example.cpp:110
#define AGENT_ERROR_JSON_CREATE
Definition: cosmos-errno.h:105
devspecstruc devspec
Structure for devices (components) special data in node, by type.
Definition: jsondef.h:4241

Variable Documentation

char agentname[COSMOS_MAX_NAME+1] = "example"
char node[50] = "null"
int waitsec = 5
Agent* agent
int32_t diskfree
int32_t stateflag
int32_t myport
DeviceCpu cpu