COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
agent_002.cpp File Reference
#include "support/configCosmos.h"
#include "agent/agentclass.h"
#include "support/stringlib.h"
#include <iostream>
#include <string>
Include dependency graph for agent_002.cpp:

Functions

int32_t hello_agent_002_request_function (string &request, string &response, Agent *a)
 The function to handle agent_001's request. More...
 
int main (int argc, char **argv)
 Program to demonstrate inter-communication between agents. More...
 

Variables

static uint64_t request_counter = 0
 number of requests that have been run since restarting agent More...
 
string node_name = "sat_001"
 ensure only one agent class instance per process More...
 
string agent_name = "agent_002"
 
string node_agent_name = "["+node_name+":"+agent_name+"]"
 
static Agentagent
 

Function Documentation

int32_t hello_agent_002_request_function ( string &  request,
string &  response,
Agent a 
)

The function to handle agent_001's request.

Parameters
Theresponse to send back to agent_001.
Returns
int
178 {
179  // Send response back to agent_001
180  response = "hello " + std::to_string(request_counter);
181 
182  // agent is set to this pointer?
183  //cout << "[" << node_name << ":" << agent_name <<"] received a request from ["<<a->nodeName<<":"<<a->agentName<<"]! Its response is: " << response << endl;
184  return 0;
185 }
string to_string(char *value)
Definition: stringlib.cpp:220
static uint64_t request_counter
number of requests that have been run since restarting agent
Definition: agent_002.cpp:44
int main ( int  argc,
char **  argv 
)

Program to demonstrate inter-communication between agents.

54 {
55  //int jah = 420;
56  //int jah2 = 240;
57  // construct agent
58  cout << node_agent_name <<" starting..."<<endl;
59  agent = new Agent(node_name, agent_name, 1.);
60  if(agent->last_error() < 0) {
61  cout<<"error: unable to start "<<node_agent_name<<" ("<<agent->last_error()<<") "<<cosmos_error_string(agent->last_error())<<endl;
62  exit(1);
63  } else {
64  cout << ""<<node_agent_name<<" started."<<endl;
65  }
66 
67  // add custom request functions for this agent
68  agent->add_request("any_body_out_there", hello_agent_002_request_function, "a request to respond with 'hello'");
69 
70 
71  // Start executing the agent
72  while(agent->running()) {
73  cout<<node_agent_name<<" running..."<<endl;
74 
75  // Sleep for 4 sec
76  COSMOS_SLEEP(4.);
77  // Initiate request to agent_001
78  // name of agent target for request
79  string agent_target = "agent_001";
80 
81 /* old tests
82  beatstruc agent_target_heartbeat = agent->find_agent(node_name, agent_target, 2.);
83  string target_request_name = "identify_yourself";
84  string response;
85  agent->send_request(agent_target_heartbeat, target_request_name, response, 2.);
86  cout<<node_agent_name<<" transmit <"<<target_request_name<<"> request to ["<<node_name<<":"<<agent_target<<"]..."<<endl;
87 
88  if (response.size() > 1) {
89  // The case if agent_001 is on and successfully receives the request
90  cout << node_agent_name<<" received <"<<target_request_name<<"> response from ["<<agent_target_heartbeat.node<<":"<<agent_target_heartbeat.proc<<"]:"<<endl;
91  cout<<" RX: \""<< response <<"\" ("<<response.size()<<" bytes)"<< endl;
92  }
93 
94  COSMOS_SLEEP(2.);
95  string response2;
96  string request2 = "setvalue {\"node_loc_orbit\":"+std::to_string((jah++))+"}";
97  // orphan request for testing
98  cout<<"attempting setting orbit value..."<<endl;
99  agent->send_request(agent->find_agent(node_name, agent_target, 2.), request2, response2, 2.);
100 
101  COSMOS_SLEEP(2.);
102  // orphan request for testing
103  string response1;
104  string request1 = "setvalue {\"node_loc_utc\":"+std::to_string((jah2--))+"}";
105  cout<<"attempting setting UTC value..."<<endl;
106  agent->send_request(agent->find_agent(node_name, agent_target, 2.), request1, response1, 2.);
107 */
108 
109  // old way
110  string response3;
111  cout<<"attempting 1.0 way of getting UTC value..."<<endl;
112  string request3 = "getvalue {\"node_loc_utc\"}";
113  cout<<" request = <"<<request3<<">"<<endl;
114  agent->send_request(agent->find_agent(node_name, agent_target, 2.), request3, response3, 2.);
115  cout<<" response = <"<<response3<<">"<<endl;
116 
117  // new way short name
118  string response4;
119  cout<<"attempting 2.0 way of getting UTC value with short name..."<<endl;
120  string request4 = "get_value \"Short UTC\"";
121  cout<<" request = <"<<request4<<">"<<endl;
122  agent->send_request(agent->find_agent(node_name, agent_target, 2.), request4, response4, 2.);
123  cout<<" response = <"<<response4<<">"<<endl;
124 
125  // new way long name
126  string response7;
127  cout<<"attempting 2.0 way of getting UTC value with long name..."<<endl;
128  string request7 = "get_value \"Longest Ever UTC\"";
129  cout<<" request = <"<<request7<<">"<<endl;
130  agent->send_request(agent->find_agent(node_name, agent_target, 2.), request7, response7, 2.);
131  cout<<" response = <"<<response7<<">"<<endl;
132 
133  string req = "get_value \"Longest Ever UTC\" \"Short UTC\"";
134  cout<<" request = <"<<req<<">"<<endl;
135  string res;
136  agent->send_request(agent->find_agent(node_name, agent_target, 2.), req, res, 2.);
137  cout<<" response = <"<<res<<">"<<endl;
138 
139  //req.clear();
140  //req = "get_value \"cosmosdata\"";
141  //cout<<" request = <"<<req<<">"<<endl;
142  //res.clear();
143  //agent->send_request(agent->find_agent(node_name, agent_target, 2.), req, res, 2.);
144  //cout<<" response = <"<<res<<">"<<endl;
145 
146  req.clear();
147  req = "set_value {\"Longest Ever UTC\": 215},{\"user[0].tool\": \"Super Tool!\"}";
148  cout<<" request = <"<<req<<">"<<endl;
149  res.clear();
150  agent->send_request(agent->find_agent(node_name, agent_target, 2.), req, res, 2.);
151  cout<<" response = <"<<res<<">"<<endl;
152 
153  req.clear();
154  req = "get_value \"user\"";
155  cout<<" request = <"<<req<<">"<<endl;
156  res.clear();
157  agent->send_request(agent->find_agent(node_name, agent_target, 2.), req, res, 2.);
158  cout<<" response = <"<<res<<">"<<endl;
159 
160  // try out agent_calc
161  //string request5 = "add 3 4";
162  //string response5;
163  //cout<<"attempting calc..."<<endl;
164  //agent->send_request(agent->find_agent("otb", "calc", 2.), request5, response5, 2.);
165  //cout<<" response = <"<<response5<<">"<<endl;
166 
167  }
168  return 0;
169 }
int32_t send_request(beatstruc cbeat, string request, string &output, float waitsec=5.)
Send a request over AGENT.
Definition: agentclass.cpp:424
int32_t hello_agent_002_request_function(string &request, string &response, Agent *cdata)
The function to handle agent_001&#39;s request.
Definition: agent_002.cpp:177
string cosmos_error_string(int32_t cosmos_errno)
Definition: cosmos-errno.cpp:45
uint16_t running()
Check if we&#39;re supposed to be running.
Definition: agentclass.cpp:391
string node_agent_name
Definition: agent_002.cpp:49
string node_name
ensure only one agent class instance per process
Definition: agent_002.cpp:47
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
static Agent * agent
Definition: agent_002.cpp:50
int32_t last_error()
Definition: agentclass.cpp:414
beatstruc find_agent(string node, string agent, double waitsec=0.)
Find agent.
Definition: agentclass.cpp:559
string agent_name
Definition: agent_002.cpp:48

Variable Documentation

uint64_t request_counter = 0
static

number of requests that have been run since restarting agent

string node_name = "sat_001"

ensure only one agent class instance per process

string agent_name = "agent_002"
string node_agent_name = "["+node_name+":"+agent_name+"]"
Agent* agent
static