Add capability to list current variable server connections in JSON re… (#732)

* Add capability to list current variable server connections in JSON ref #678

* Add client tag to the connection info.

* Name consistency tweak in generation of JSON variable-server connection list. ref #732

* Add client IP address and port. ref #732

* Output should be going to the stringstream, not std::cout. Ref #732
This commit is contained in:
jmpenn 2019-02-18 17:11:41 -06:00 committed by GitHub
parent a2cee328d2
commit b9278c4a72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 93 additions and 0 deletions

View File

@ -10,6 +10,7 @@
#include <queue> #include <queue>
#include <vector> #include <vector>
#include <string> #include <string>
#include <iostream>
#include <pthread.h> #include <pthread.h>
#include "trick/tc.h" #include "trick/tc.h"
#include "trick/reference.h" #include "trick/reference.h"
@ -39,6 +40,11 @@ namespace Trick {
*/ */
~VariableServer() ; ~VariableServer() ;
/**
@brief Write a JSON encoded list of Variable Server Connections to the given stream.
*/
friend std::ostream& operator<< (std::ostream& s, Trick::VariableServer& vs);
/** /**
@brief Set up the listen port during default_data so it is available at the start of initialization. @brief Set up the listen port during default_data so it is available at the start of initialization.
*/ */

View File

@ -6,6 +6,7 @@
#ifndef VARIABLESERVERREFERENCE_HH #ifndef VARIABLESERVERREFERENCE_HH
#define VARIABLESERVERREFERENCE_HH #define VARIABLESERVERREFERENCE_HH
#include <iostream>
#include "trick/reference.h" #include "trick/reference.h"
union cv_converter ; union cv_converter ;
@ -24,6 +25,8 @@ namespace Trick {
VariableReference(REF2 * in_ref) ; VariableReference(REF2 * in_ref) ;
~VariableReference() ; ~VariableReference() ;
friend std::ostream& operator<< (std::ostream& s, const Trick::VariableReference& vref);
/** Pointer to trick variable reference structure.\n */ /** Pointer to trick variable reference structure.\n */
REF2 * ref ; REF2 * ref ;
cv_converter * conversion_factor ; // ** udunits conversion factor cv_converter * conversion_factor ; // ** udunits conversion factor

View File

@ -8,6 +8,7 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <iostream>
#include <pthread.h> #include <pthread.h>
#include "trick/tc.h" #include "trick/tc.h"
#include "trick/ThreadBase.hh" #include "trick/ThreadBase.hh"
@ -27,6 +28,8 @@ namespace Trick {
public: public:
enum ConnectionType { TCP, UDP, MCAST } ; enum ConnectionType { TCP, UDP, MCAST } ;
friend std::ostream& operator<< (std::ostream& s, Trick::VariableServerThread& vst);
/** /**
@brief Constructor. @brief Constructor.
@param listen_dev - the TCDevice set up in listen() @param listen_dev - the TCDevice set up in listen()

View File

@ -6,6 +6,7 @@
extern "C" { extern "C" {
#endif #endif
void var_server_list_connections(void);
const char * var_server_get_hostname(void) ; const char * var_server_get_hostname(void) ;
unsigned short var_server_get_port(void) ; unsigned short var_server_get_port(void) ;

View File

@ -63,6 +63,16 @@ Trick::VariableReference::VariableReference(REF2 * in_ref ) {
} }
std::ostream& Trick::operator<< (std::ostream& s, const Trick::VariableReference& vref) {
if (vref.ref->reference != NULL) {
s << " \"" << vref.ref->reference << "\"";
} else {
s<< " null";
}
return s;
}
Trick::VariableReference::~VariableReference() { Trick::VariableReference::~VariableReference() {
free(ref) ; free(ref) ;
free(buffer_in) ; free(buffer_in) ;

View File

@ -1,5 +1,6 @@
#include <netdb.h> #include <netdb.h>
#include <iostream>
#include "trick/VariableServer.hh" #include "trick/VariableServer.hh"
#include "trick/tc_proto.h" #include "trick/tc_proto.h"
@ -17,6 +18,26 @@ Trick::VariableServer::VariableServer() :
Trick::VariableServer::~VariableServer() { Trick::VariableServer::~VariableServer() {
} }
std::ostream& Trick::operator<< (std::ostream& s, Trick::VariableServer& vs) {
std::map < pthread_t , VariableServerThread * >::iterator it ;
std::cout << "{\"variable-server-connections\":[" << std::endl;
int count = 0;
int n_connections = (int)vs.var_server_threads.size();
for ( it = vs.var_server_threads.begin() ; it != vs.var_server_threads.end() ; it++ ) {
s << "{" << std::endl;
s << *(*it).second;
s << "}";
if ((n_connections-count)>1) {
std::cout << "," ;
}
s << std::endl;
count ++;
}
s << "]}" << std::endl;
return s;
}
bool Trick::VariableServer::get_enabled() { bool Trick::VariableServer::get_enabled() {
return enabled ; return enabled ;
} }

View File

@ -1,4 +1,5 @@
#include <iostream>
#include <stdlib.h> #include <stdlib.h>
#include "trick/VariableServerThread.hh" #include "trick/VariableServerThread.hh"
#include "trick/exec_proto.h" #include "trick/exec_proto.h"
@ -63,6 +64,50 @@ Trick::VariableServerThread::~VariableServerThread() {
free( stripped_msg ) ; free( stripped_msg ) ;
} }
std::ostream& Trick::operator<< (std::ostream& s, Trick::VariableServerThread& vst) {
std::vector <Trick::VariableReference *>::iterator it;
struct sockaddr_in otherside;
socklen_t len = (socklen_t)sizeof(otherside);
s << " \"connection\":{" << std::endl;
s << " \"client-tag\":\"" << vst.connection.client_tag << "\"," << std::endl;
int err = getpeername(vst.connection.socket, (struct sockaddr*)&otherside, &len);
if (err == 0) {
s << " \"client-IP-address\":\"" << inet_ntoa(otherside.sin_addr) << "\"," << std::endl;
s << " \"client-port\":\"" << ntohs(otherside.sin_port) << "\"," << std::endl;
} else {
s << " \"client-IP-address\":\"unknown\"," << std::endl;
s << " \"client-port\":\"unknown\"," << std::endl;
}
if (vst.binary_data) {
s << " \"format\":\"BINARY\",";
} else {
s << " \"format\":\"ASCII\",";
}
s << std::endl;
s << " \"update-rate\":" << vst.update_rate << "," << std::endl;
s << " \"variables\":[" << std::endl;
int n_vars = (int)vst.vars.size();
for (int i=0 ; i<n_vars ; i++) {
s << *(vst.vars[i]);
if ((n_vars-i)>1) {
s << "," ;
}
s << std::endl;
}
s << " ]" << std::endl;
s << " }" << std::endl;
return s;
}
void Trick::VariableServerThread::set_vs_ptr(Trick::VariableServer * in_vs) { void Trick::VariableServerThread::set_vs_ptr(Trick::VariableServer * in_vs) {
vs = in_vs ; vs = in_vs ;
} }

View File

@ -387,6 +387,10 @@ Trick::VariableServer * var_server_get_var_server() {
return the_vs ; return the_vs ;
} }
extern "C" void var_server_list_connections(void) {
std::cout << *the_vs << std::endl;
}
/** /**
* @relates Trick::VariableServer * @relates Trick::VariableServer
* @copydoc Trick::VariableServer::get_hostname * @copydoc Trick::VariableServer::get_hostname