2015-02-26 15:02:31 +00:00
|
|
|
|
2019-02-18 23:11:41 +00:00
|
|
|
#include <iostream>
|
2015-02-26 15:02:31 +00:00
|
|
|
#include <stdlib.h>
|
2015-06-01 15:28:29 +00:00
|
|
|
#include "trick/VariableServerThread.hh"
|
|
|
|
#include "trick/exec_proto.h"
|
|
|
|
#include "trick/TrickConstant.hh"
|
2015-02-26 15:02:31 +00:00
|
|
|
|
|
|
|
Trick::VariableServer * Trick::VariableServerThread::vs = NULL ;
|
|
|
|
|
2022-08-23 18:47:56 +00:00
|
|
|
Trick::VariableServerThread::VariableServerThread(ClientListener * in_listen_dev) :
|
|
|
|
Trick::SysThread("VarServer") , debug(0),
|
|
|
|
listener(in_listen_dev), session(NULL) {
|
2015-02-26 15:02:31 +00:00
|
|
|
|
2022-08-23 18:47:56 +00:00
|
|
|
connection_status = CONNECTION_PENDING ;
|
2015-02-26 15:02:31 +00:00
|
|
|
|
2022-08-23 18:47:56 +00:00
|
|
|
connection.initialize();
|
2015-02-26 15:02:31 +00:00
|
|
|
|
2022-08-23 18:47:56 +00:00
|
|
|
pthread_mutex_init(&connection_status_mutex, NULL);
|
|
|
|
pthread_cond_init(&connection_status_cv, NULL);
|
|
|
|
|
|
|
|
pthread_mutex_init(&restart_pause, NULL);
|
2015-02-26 15:02:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-08-23 18:47:56 +00:00
|
|
|
Trick::VariableServerThread::~VariableServerThread() {}
|
2019-02-18 23:11:41 +00:00
|
|
|
|
|
|
|
std::ostream& Trick::operator<< (std::ostream& s, Trick::VariableServerThread& vst) {
|
2019-05-13 21:05:01 +00:00
|
|
|
// Write a JSON representation of a Trick::VariableServerThread to an ostream.
|
2019-02-18 23:11:41 +00:00
|
|
|
struct sockaddr_in otherside;
|
|
|
|
socklen_t len = (socklen_t)sizeof(otherside);
|
|
|
|
|
2019-05-13 21:05:01 +00:00
|
|
|
s << " \"connection\":{\n";
|
2022-08-23 18:47:56 +00:00
|
|
|
s << " \"client_tag\":\"" << vst.connection.get_client_tag() << "\",\n";
|
2019-02-18 23:11:41 +00:00
|
|
|
|
2022-08-23 18:47:56 +00:00
|
|
|
int err = getpeername(vst.connection.get_socket(), (struct sockaddr*)&otherside, &len);
|
2019-02-18 23:11:41 +00:00
|
|
|
|
|
|
|
if (err == 0) {
|
2019-05-13 21:05:01 +00:00
|
|
|
s << " \"client_IP_address\":\"" << inet_ntoa(otherside.sin_addr) << "\",\n";
|
|
|
|
s << " \"client_port\":\"" << ntohs(otherside.sin_port) << "\",\n";
|
2019-02-18 23:11:41 +00:00
|
|
|
} else {
|
2019-05-13 21:05:01 +00:00
|
|
|
s << " \"client_IP_address\":\"unknown\",\n";
|
|
|
|
s << " \"client_port\":\"unknown\",";
|
2019-02-18 23:11:41 +00:00
|
|
|
}
|
|
|
|
|
2022-08-23 18:47:56 +00:00
|
|
|
s << *(vst.session);
|
2019-02-18 23:11:41 +00:00
|
|
|
|
|
|
|
s << " }" << std::endl;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2015-02-26 15:02:31 +00:00
|
|
|
void Trick::VariableServerThread::set_vs_ptr(Trick::VariableServer * in_vs) {
|
|
|
|
vs = in_vs ;
|
|
|
|
}
|
|
|
|
|
2022-08-23 18:47:56 +00:00
|
|
|
Trick::VariableServer * Trick::VariableServerThread::get_vs() {
|
|
|
|
return vs ;
|
2015-02-26 15:02:31 +00:00
|
|
|
}
|
|
|
|
|
2022-08-23 18:47:56 +00:00
|
|
|
void Trick::VariableServerThread::set_client_tag(std::string tag) {
|
|
|
|
connection.set_client_tag(tag);
|
2015-02-26 15:02:31 +00:00
|
|
|
}
|
|
|
|
|
2022-08-23 18:47:56 +00:00
|
|
|
Trick::ConnectionStatus Trick::VariableServerThread::wait_for_accept() {
|
2015-02-26 15:02:31 +00:00
|
|
|
|
2022-08-23 18:47:56 +00:00
|
|
|
pthread_mutex_lock(&connection_status_mutex);
|
|
|
|
while ( connection_status == CONNECTION_PENDING ) {
|
|
|
|
pthread_cond_wait(&connection_status_cv, &connection_status_mutex);
|
2015-02-26 15:02:31 +00:00
|
|
|
}
|
2022-08-23 18:47:56 +00:00
|
|
|
pthread_mutex_unlock(&connection_status_mutex);
|
|
|
|
|
|
|
|
return connection_status;
|
2015-02-26 15:02:31 +00:00
|
|
|
}
|
|
|
|
|
2022-08-23 18:47:56 +00:00
|
|
|
// This should maybe go somewhere completely different
|
|
|
|
// Leaving it in thread for now
|
|
|
|
// Gets called from the main thread as a job
|
|
|
|
void Trick::VariableServerThread::preload_checkpoint() {
|
|
|
|
|
|
|
|
// Stop variable server processing at the top of the processing loop.
|
|
|
|
pthread_mutex_lock(&restart_pause);
|
|
|
|
|
|
|
|
// Let the thread complete any data copying it has to do
|
|
|
|
// and then suspend data copying until the checkpoint is reloaded.
|
|
|
|
pthread_mutex_lock(&(session->copy_mutex));
|
|
|
|
|
|
|
|
// Save the pause state of this thread.
|
|
|
|
saved_pause_cmd = session->get_pause();
|
|
|
|
|
|
|
|
// Disallow data writing.
|
|
|
|
session->set_pause(true);
|
|
|
|
|
|
|
|
// Temporarily "disconnect" the variable references from Trick Managed Memory
|
|
|
|
// by tagging each as a "bad reference".
|
|
|
|
session->disconnect_references();
|
|
|
|
|
|
|
|
|
|
|
|
// Allow data copying to continue.
|
|
|
|
pthread_mutex_unlock(&(session->copy_mutex));
|
|
|
|
|
2015-02-26 15:02:31 +00:00
|
|
|
}
|
|
|
|
|
2022-08-23 18:47:56 +00:00
|
|
|
// Gets called from the main thread as a job
|
|
|
|
void Trick::VariableServerThread::restart() {
|
|
|
|
// Set the pause state of this thread back to its "pre-checkpoint reload" state.
|
|
|
|
session->set_pause(saved_pause_cmd) ;
|
|
|
|
|
|
|
|
// Restart the variable server processing.
|
|
|
|
pthread_mutex_unlock(&restart_pause);
|
2015-02-26 15:02:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|