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

Macros

#define MAXBUFFERSIZE   256
 
#define REQUEST_RUN_PROGRAM   0
 

Functions

int agent_data ()
 
int32_t request_login (string &request, string &response, Agent *)
 
int32_t request_getnodelist (string &request, string &response, Agent *)
 
int32_t request_log (string &request, string &response, Agent *)
 
int main (int, char **)
 

Variables

std::vector< agentstrucagents
 
agentstruc tempagent
 
std::vector< cosmosstrucnodes
 
char tempname [100]
 
char agentname [COSMOS_MAX_NAME+1] = "data"
 
std::string dataDir
 
int waitsec = 5
 
Agentagent
 
int usercount =0
 
char username [1000][50]
 
char password [1000][50]
 
int32_t diskfree
 
int32_t stateflag
 
int32_t myport
 

Macro Definition Documentation

#define MAXBUFFERSIZE   256
#define REQUEST_RUN_PROGRAM   0

Function Documentation

int agent_data ( )
189 {
190 
191 // Start performing the body of the agent
192 while(agent->running())
193  {
194  // Gather system information
195 
196 
197  COSMOS_USLEEP(10); // no support in win
198  }
199 return 0;
200 }
uint16_t running()
Check if we&#39;re supposed to be running.
Definition: agentclass.cpp:391
Agent * agent
Definition: agent_data-2-0.cpp:66
int32_t request_login ( string &  request,
string &  response,
Agent  
)
204 {
205 char user[COSMOS_MAX_NAME+1];
206 char pass[COSMOS_MAX_NAME+1];
207 int i;
208 bool login;
209 
210 login=false;
211 user[0] = 0; // These need to be reset, otherwise, logging in with no name lets these retain the previous users name/password.
212 pass[0] = 0;
213 
214 sscanf(request.c_str(),"%*s %s %s",user,pass);
215 
216 for(i=0;i<usercount;i++)
217  { //match given data against what is stored in the array
218  if((strcmp(username[i],user)==0) && strcmp(password[i],pass)==0){
219  login=true;
220  break;
221  }
222  }
223 
224 if (login==true)
225  response = ("yes");
226 else
227  response = ("no");
228 return 0;
229 }
int i
Definition: rw_test.cpp:37
char username[1000][50]
Definition: agent_data-2-0.cpp:73
#define COSMOS_MAX_NAME
Largest JSON name.
Definition: cosmos-defs.h:55
int usercount
Definition: agent_data-2-0.cpp:72
char password[1000][50]
Definition: agent_data-2-0.cpp:74
int32_t request_getnodelist ( string &  request,
string &  response,
Agent  
)
233 {
234 uint32_t i;
235 
236 response.clear();
237 for (i=0; i<nodes.size(); i++)
238  {
239  response += ' ' + nodes[i].node.name + ' ' + to_signed(nodes[i].node.type);
240  }
241 return 0;
242 }
int i
Definition: rw_test.cpp:37
string to_signed(int64_t value, uint16_t digits, bool zerofill)
Definition: stringlib.cpp:245
std::vector< cosmosstruc > nodes
Definition: agent_data-2-0.cpp:57
static string node
Definition: agent_monitor.cpp:126
int32_t request_log ( string &  request,
string &  response,
Agent  
)
246 {
247 FILE *log;
248 uint16_t i;
249 double utc;
250 
251 for (i=0; i<request.length(); ++i)
252 {
253  if (request[i] == '{')
254  break;
255 }
256 
257 json_parse(&request[i], agent->cinfo); //cinfo->stat.agent.user
258  // .event
259  // .utc ->date
260  // .node
261 utc = (int)(agent->cinfo->event[0].utc);
262 log = data_open(data_type_path(agent->cinfo->event[0].node, (char *)"data", (char *)"outgoing", utc, (char*)"request_log"), (char*)"a");
263 fprintf(log,"%s\n",&request[i]);
264 fclose(log);
265 return 0;
266 }
int i
Definition: rw_test.cpp:37
int32_t json_parse(string jstring, cosmosstruc *cinfo)
Parse JSON using Name Space.
Definition: jsonlib.cpp:4799
FILE * data_open(string path, const char *mode)
Open file from path.
Definition: datalib.cpp:1019
string data_type_path(string node, string location, string agent, double mjd, string type)
Create data file path.
Definition: datalib.cpp:910
Agent * agent
Definition: agent_data-2-0.cpp:66
cosmosstruc * cinfo
Definition: agentclass.h:346
vector< eventstruc > event
Single entry vector for event information.
Definition: jsondef.h:4250
int main ( int  ,
char **   
)
82 {
83 int32_t iretn;
84 uint32_t i;
85 char input[100];
86 FILE *fd;
87 
88 // Setting the directory where the data is
89 if (set_cosmosnodes(dataDir) < 0)
90 {
91  printf("Couldn't find Nodes directory\n");
92  exit (1);
93 }
94 
95 // check if we are already running the agent
96 if ((iretn=agent->check_agent((char *)"hmoc", agentname, waitsec)) > 0)
97  exit (iretn);
98 
99 // Load user list
100 usercount = 0;
101 sprintf(input,"%s/login.dat",dataDir.c_str());
102 
103 if ((fd = fopen(input,"r")) != NULL)
104  {
105  while(!feof(fd))
106  {//read strings into array
107  if (fgets(input,100,fd) != NULL)
108  {
109  sscanf(input, "%s %s",username[usercount],password[usercount]);
110  usercount++;
111  }
112  }
113  fclose(fd);
114  }
115 
116 // Load Node list
118 
119 for (i=0; i<nodes.size(); ++i)
120  {
121 #if defined(COSMOS_WIN_OS)
122  char command_line[100];
123  sprintf(command_line, "agent_node %s", (const char*)nodes[i].node.name);
124 
125  STARTUPINFOA si;
126  PROCESS_INFORMATION pi;
127 
128  ZeroMemory( &si, sizeof(si) );
129  si.cb = sizeof(si);
130  ZeroMemory( &pi, sizeof(pi) );
131 
132 #if defined(COSMOS_WIN_BUILD_MSVC)
133  if (CreateProcess(NULL, (LPWSTR) command_line, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
134 #else
135  if (CreateProcessA(NULL, (LPSTR) command_line, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
136 #endif
137  {
138  strcpy(tempagent.beat.node, nodes[i].node.name);
139  tempagent.pid = pi.dwProcessId;
140  agents.push_back(tempagent);
141  CloseHandle( pi.hProcess );
142  CloseHandle( pi.hThread );
143  }
144 #else
145  int32_t pid =fork();
146  switch(pid)
147  {
148  case -1:
149  strcpy(tempagent.beat.node, nodes[i].node.name);
150  tempagent.pid=pid;
151  agents.push_back(tempagent);
152  break;
153  case 0:
154  int devn;
155  devn = open("/dev/null",O_RDWR);
156  dup2(STDIN_FILENO, devn);
157  dup2(STDOUT_FILENO, devn);
158  dup2(STDERR_FILENO, devn);
159  close(devn);
160  execl("agent_node", (const char*)nodes[i].node.name,(char*)nullptr);
161 
162  fflush(stdout);
163  exit(0);
164  break;
165  default:
166  break;
167  }
168 #endif
169  }
170 // Initialize the Agent
171 // near future: support cubesat space protocol
172 // port number = 0 in this case, automatic assignment of port
173 if (!(agent = new Agent("hmoc", agentname, 1., MAXBUFFERSIZE)))
174  exit (iretn);
175 
176 // Add additional requests
177 if ((iretn=agent->add_request("login",request_login)))
178  exit (iretn);
179 if ((iretn=agent->add_request("getnodelist",request_getnodelist)))
180  exit (iretn);
181 if ((iretn=agent->add_request("log",request_log)))
182  exit (iretn);
183 
184 // Start our own thread
185 iretn = agent_data();
186 }
int32_t request_getnodelist(string &request, string &response, Agent *)
Definition: agent_data-2-0.cpp:232
int fd
Definition: arduino_lib.cpp:61
int i
Definition: rw_test.cpp:37
char username[1000][50]
Definition: agent_data-2-0.cpp:73
int waitsec
Definition: agent_data-2-0.cpp:61
int agent_data()
Definition: agent_data-2-0.cpp:188
int32_t set_cosmosnodes(string name, bool create_flag)
Set Nodes Directory.
Definition: datalib.cpp:1445
int iretn
Definition: rw_test.cpp:37
int usercount
Definition: agent_data-2-0.cpp:72
int32_t data_get_nodes(vector< cosmosstruc > &node)
Get vector of Node structures.
Definition: datalib.cpp:636
char agentname[COSMOS_MAX_NAME+1]
Definition: agent_data-2-0.cpp:59
int32_t request_login(string &request, string &response, Agent *)
Definition: agent_data-2-0.cpp:203
#define FALSE
Definition: jpleph.cpp:69
char node[40+1]
Definition: jsondef.h:928
int32_t pid
Agent process ID.
Definition: jsondef.h:1021
int32_t check_agent(string node, string agent, double waitsec)
Check agent.
Definition: agentclass.cpp:546
std::vector< cosmosstruc > nodes
Definition: agent_data-2-0.cpp:57
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
beatstruc beat
Heartbeat.
Definition: jsondef.h:1029
std::string dataDir
Definition: agent_data-2-0.cpp:60
Definition: agentclass.h:139
std::vector< agentstruc > agents
Definition: agent_data-2-0.cpp:52
Agent * agent
Definition: agent_data-2-0.cpp:66
int32_t request_log(string &request, string &response, Agent *)
Definition: agent_data-2-0.cpp:245
char password[1000][50]
Definition: agent_data-2-0.cpp:74
agentstruc tempagent
Definition: agent_data-2-0.cpp:53
#define MAXBUFFERSIZE
Definition: agent_data-2-0.cpp:68
static string node
Definition: agent_monitor.cpp:126

Variable Documentation

std::vector<agentstruc> agents
agentstruc tempagent
std::vector<cosmosstruc> nodes
char tempname[100]
char agentname[COSMOS_MAX_NAME+1] = "data"
std::string dataDir
int waitsec = 5
Agent* agent
int usercount =0
char username[1000][50]
char password[1000][50]
int32_t diskfree
int32_t stateflag
int32_t myport