diff --git a/.github/workflows/code_coverage.yml b/.github/workflows/code_coverage.yml index 27f90b37..5cf64923 100644 --- a/.github/workflows/code_coverage.yml +++ b/.github/workflows/code_coverage.yml @@ -29,7 +29,7 @@ jobs: - name: Install GTest run: | dnf config-manager --enable ol8_codeready_builder - dnf install -y gtest-devel + dnf install -y gtest-devel gmock-devel - name: Checkout repository uses: actions/checkout@master diff --git a/.github/workflows/test_linux.yml b/.github/workflows/test_linux.yml index 42637d40..f5e499d1 100644 --- a/.github/workflows/test_linux.yml +++ b/.github/workflows/test_linux.yml @@ -37,6 +37,7 @@ jobs: maven cmake zip + gdb install_gtest: echo gtest already installed conf_pkg: echo package manager already configured install_cmd: install -y @@ -64,7 +65,7 @@ jobs: python3-pip python3-venv install_gtest: | - apt-get install -y libgtest-dev + apt-get install -y libgtest-dev libgmock-dev cd /usr/src/gtest cmake . make @@ -100,7 +101,9 @@ jobs: libX11-devel libXt-devel swig3 - gtest-devel + install_gtest: | + yum install -y http://repo.okay.com.mx/centos/7/x86_64/release/okay-release-1-6.el7.noarch.rpm + yum install -y gtest gtest-devel gmock gmock-devel #-------- RHEL 8-based Only Dependencies ---------------- - cfg: { arch: rhel, arch_ver: 8 } pkg_mgr: dnf @@ -113,12 +116,12 @@ jobs: dnf install -y 'dnf-command(config-manager)' install_gtest: | dnf config-manager --enable powertools - dnf install -y gtest-devel + dnf install -y gtest-devel gmock-devel #-------- OS and Version Specific Dependencies ---------------- - cfg: { os: oraclelinux } install_gtest: | dnf config-manager --enable ol8_codeready_builder - dnf install -y gtest-devel + dnf install -y gtest-devel gmock-devel #-------- Job definition ---------------- runs-on: ubuntu-latest container: docker://${{matrix.cfg.os}}:${{matrix.cfg.tag}} diff --git a/CMakeLists.txt b/CMakeLists.txt index 99e56a2a..f40375c0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -175,7 +175,7 @@ set( IO_SRC ${CMAKE_BINARY_DIR}/temp_src/io_src/io_JITEvent.cpp ${CMAKE_BINARY_DIR}/temp_src/io_src/io_JITInputFile.cpp ${CMAKE_BINARY_DIR}/temp_src/io_src/io_JSONVariableServer.cpp - ${CMAKE_BINARY_DIR}/temp_src/io_src/io_JSONVariableServerThread.cpp + ${CMAKE_BINARY_DIR}/temp_src/io_src/io_JSONVariableServerSessionThread.cpp ${CMAKE_BINARY_DIR}/temp_src/io_src/io_JobData.cpp ${CMAKE_BINARY_DIR}/temp_src/io_src/io_MM4_Integrator.cpp ${CMAKE_BINARY_DIR}/temp_src/io_src/io_MSConnect.cpp @@ -231,7 +231,7 @@ set( IO_SRC ${CMAKE_BINARY_DIR}/temp_src/io_src/io_VariableServer.cpp ${CMAKE_BINARY_DIR}/temp_src/io_src/io_VariableServerListenThread.cpp ${CMAKE_BINARY_DIR}/temp_src/io_src/io_VariableServerReference.cpp - ${CMAKE_BINARY_DIR}/temp_src/io_src/io_VariableServerThread.cpp + ${CMAKE_BINARY_DIR}/temp_src/io_src/io_VariableServerSessionThread.cpp ${CMAKE_BINARY_DIR}/temp_src/io_src/io_Zeroconf.cpp ${CMAKE_BINARY_DIR}/temp_src/io_src/io_attributes.cpp ${CMAKE_BINARY_DIR}/temp_src/io_src/io_dllist.cpp diff --git a/docs/developer_docs/Variable-Server.md b/docs/developer_docs/Variable-Server.md new file mode 100644 index 00000000..f2d3e36e --- /dev/null +++ b/docs/developer_docs/Variable-Server.md @@ -0,0 +1,446 @@ +# Variable Server Refactor + +The goals of this refactor were as follows: + +1. Test the Trick Variable Server + - At the start, the variable server was completely uncovered by CI. This can be seen in the earliest builds uploaded to Coveralls (https://coveralls.io/builds/53258598) + - Identified a need for both unit and sim tests + - Significant refactoring was needed to be able to do any unit tests +2. Create a generic representation for a session that can be used in the webserver as well + - Currently, the webserver implementation is completely separate from the variable server, even through they do they same thing. There is a lot of repeated functionality between the two. + - The current implementation of the variable server made it impossible to reuse any of its code. +3. Clarify and document code. + + +## Class Structure + +### VariableServer.hh +The overarching variable server object. It is instantiated in VariableServerSimObject and is intended to be a singleton. + +Responsibilities: +- Initialize a listen thread +- Keep track of all sessions +- Interface with the Trick scheduler through the Trick jobs, and delegate to sessions +- Provide the API for all variable server commands to go through, and delegate to correct session + + +### VariableServerListenThread.hh +Opens a TCP port and listens for new client connections until shut down. Only 1 exists by default as a member of VariableServer, +but additional listen threads can be created through a call to trick.var_server_create_tcp_socket() + +Responsibilities: +- Open a TCP port for listening +- Continuously listen on the port +- Broadcast the port on the multicast channel +- When a client connection comes in, set up the connection, start a thread, and wait for the thread to start the connection + - Delete the thread if the connection fails + +### VariableServerSessionThread.hh +Runs asynchronous parts for the single VariableServerSession. + +Responsibilities: + +- Accept the connection and pass it into the VariableServerSession +- Delete the session and close the connection on shutdown +- Run the parts of VariableServerSession that run asynchronously until user commands shutdown or sim shutdown, including: + - Tell VSSession to read from connection and execute command + - Tell VSSession to copy sim data (if in correct mode) + - Tell VSSession to write data (if in correct mode) + + +### VariableServerSession.hh +Track information and execute commands for a single client session. + +Responsibilities: +- Keep track of the list of variables +- Track desired copy/write mode and timing +- Overrideable behaviors: + - Write out to connection when commanded + - Read in and execute commands from connection when commanded + +### VariableReference.hh +Represent a single variable in a session + +Responsibilities: +- Copy sim data into internal buffer when commanded +- Write sim data into stream when commanded +- Provide functions to write ASCII and binary data +- Track what units the user has requested +- Survive a checkpoint restart + +### TCPClientListener.hh +Represents a listening TCP server + +Responsibilities: +- Encapsulate network code +- Provide an interface to start a server, listen for new connections, and create new connections +- Create a TCPConnection when a client connects + +### ClientConnection.hh + +Responsibilities +Abstract class representing a connection to a client. Inherited classes to handle TCP, UDP, and Multicast connections +- Encapsulate network code +- Provide a generic interface for initializing, starting, shutting down, reading, and writing to network regardless of connection type + +## Threading changes + +### VariableServerListenThread is required to wait for VariableServerSessionThread to notify it when the connection has been accepted. +This was present in old design, but not implemented safely. + +#### Old design: + +``` +VariableServerSessionThread + + wait_for_accept() + - bool connection_accepted = false +``` + +`wait_for_accept` just busy waits on `connection_accepted` with no synch constructs (John Borland would be horrified) +VariableServerListenThread calls `VsThread.wait_for_accept()` + +No error handling if connection failed, which can result in hanging listen thread and extra VSThreads bc of other problems in listen thread + +#### New Design + +``` +VariableServerSessionThread + + enum ConnectionStatus { CONNECTION_PENDING, CONNECTION_SUCCESS, CONNECTION_FAIL }; + + wait_for_accept() + + - ConnectionStatus _connection_status = CONNECTION_PENDING + - pthread_mutex_t _connection_status_mutex + - pthread_cond_t _connection_status_cv + + +VariableServerSessionThread::thread_body + Try to accept connection + if connection fails: + with _connection_status_mutex: + set _connection_status to CONNECTION_FAIL + signal on _connection_status_cv + exit thread + + Do some other setup + with _connection_status_mutex: + set _connection_status to CONNECTION_SUCCESS + signal on _connection_status_cv + + Run loop + + +VariableServerSessionThread::wait_for_accept + with _connection_status_mutex: + while _connection_status == CONNECTION_PENDING: + wait on _connection_status_cv + + return _connection_status + + +VariableServerListenThread::thread_body + When we're trying to set up a connection: + Create new VSThread, set up the connection, start the thread + status = VSThread->wait_for_accept() + if status is CONNECTION_FAIL: + make sure the thread has exited and delete it + +``` + +### VariableServerSessionThread and VariableServerListenThread no longer use pthread_cancel +New termination design: +``` +Additions to Threadbase + + test_shutdown(/* optional exit handler args */) + + thread_shutdown(/* optional exit handler args */) + + - bool cancellable = true + - pthread_mutex_t shutdown_mutex + - bool should_shutdown = false + +ThreadBase::test_shutdown: + with shutdown_mutex + if should_shutdown: + call thread_shutdown + +ThreadBase::thread_shutdown + Call exit handler if one passed + Call pthread_exit(0) + +Addition to function of Threadbase::cancel_thread(): + with shutdown_mutex: Set should_shutdown to true + if cancellable == true, call pthread_cancel +``` + +Threads can set cancellable to false in their constructor, which VariableServerSessionThread and VariableServerListenThread do +Instead, they call test_shutdown at points that would be appropriate to shutdown in their loops +Careful design consideration and testing was taken so that if the threads never start, or various error cases are hit, this still goes ok + + +### VariableServerSessionThread and VariableServerListenThread pause on checkpoint reload + +#### Old design: + +``` +VariableServerSessionThread and VariableServerListenThread + - pthread_mutex_t restart_pause +``` + +This mutex is acquired by the top of the loops while they do their main processing, and released at the bottom of the loop. + +The main thread acquires the mutex in preload_checkpoint jobs and releases in restart jobs. + +This created a few bugs: +- a deadlock in VariableServerSessionThread due to a lock inversion with the VariableServer map mutex +- For some reason, holding the lock while blocked on the select syscall by the VariableServerListenThread could cause some really weird bugs, so the lock was acquired after the select call and only if it returned true. Because of this, select could be run while checkpoint restart was happening, which sometimes resulted in false positives, which created extra VariableServerSessionThreads, and the connection status was never checked so they would never shut down and cause all kinds of problems. + +#### New design + +``` +Addition to SysThread + + void force_thread_to_pause() + + void unpause_thread() + + void test_pause() + + - pthread_mutex_t _restart_pause_mutex + - bool _thread_should_pause + - pthread_cond_t _thread_wakeup_cv + - bool _thread_has_paused + - pthread_cond_t _thread_has_paused_cv +``` + +This design still ensures that thread can be paused, and that the main thread will wait until the threads are paused. It uses condvars to solve the deadlock problem and doesn't cause bugs observed in the old design. + +The main thread can control the SysThread by calling `thread->force_thead_to_pause()`, which will block until the SysThread has paused, and `thread->unpause_thread()`. The SysThread loops must call `test_pause()` at points that would be appropriate to pause. + +SysThread uses a monitor pattern internally to implement this behavior. Pseudocode for the pause managment: + +``` +// Called from main thread +SysThread::force_thread_to_pause() + with _restart_pause_mutex: + _thread_should_pause = true + while !_thread_has_paused: + wait on _thread_has_paused_cv + +// Called from main thread +SysThread::unpause_thread(): + with _restart_pause_mutex: + _thread_should_pause = false + signal on _thread_wakeup_cv + +// Called from SysThread +SysThread::test_pause(): + with _restart_pause_mutex: + if _thread_should_pause: + _thread_has_paused = true + signal on _thread_has_paused_cv + + while _thread_should_pause + wait on _thread_wakeup_cv + + _thread_has_paused = false +``` + +## VariableReference + +The VariableReference class existed in the old version only to hold buffers for copied values, it had no methods. + +In the refactored version, the VariableReference class encapsulates all interaction with the memory manager and sim variables, including copying and formatting the values. VariableReference keeps track of the units that have been requested for this variable in this session. VariableReference provides an interface to write out the value of a variable in either Ascii or binary format. + +It still uses REF2 internally, but the encapsulation will make the (hopefully) impending transition to something else easier to manage. + +The VariableReference class is covered by unit tests. + +``` +VariableReference Interface + + VariableReference(std::string var_name) + + + std::string getName() + + TRICK_TYPE getType() + + + std::string getBaseUnits() + + int setRequestedUnits(std::string new_units) + + + int stageValue () + + int prepareForWrite () + + bool isStaged () + + bool isWriteReady () + + + int writeValueAscii( std::ostream& out ) + + int writeValueBinary( std::ostream& out , bool byteswap = false) + + ((Other methods to write out information about the binary or ascii formats)) +``` + +## VariableServerSession + +The VariableServerSession class is a new class created in this refactor. A lot of the behavior of the old VariableServerSessionThread class is now in this class. + +A VariableServerSession represents a single client session. It tracks the list of variables that are being sent, the copy and write mode, output format mode, cycle, pause state, exit command. + +The VariableServerSession has methods that should be called from Trick job types to implement correct moding - + +``` +// Called from different types of Trick jobs. +// Determines whether this session should be copying and/or writing, and performs those actions if so ++ int copy_and_write_freeze(long long curr_frame); ++ int copy_and_write_freeze_scheduled(long long curr_tics); ++ int copy_and_write_scheduled(long long curr_tics); ++ int copy_and_write_top(long long curr_frame); + +// Called from VariableServerSessionThread ++ virtual int copy_and_write_async(); +``` + +The VariableServerSession has a method that should be called when it is time to handle a message and act on it. This is called from the VariableServerSessionThread loop. +``` ++ virtual int handle_message(); +``` + +The VariableServerSession has the entirety of the documented VariableServer API as methods as well. These are called from the single VariableServer object after looking up the session. + + +The VariableServerSession class is covered by unit tests. + + +## VariableServerSessionThread + +The VariableServerSessionThread class runs and manages the lifetime and synchronization of a VariableServerSessionThread, sets up and closes the connection for the VariableServerSession, and calls any VariableServerSession methods that are designed to be asynchronous with the main thread. This includes `handle_message` and `copy_and_write_async`. It also creates the cycle of the session. + +``` +VariableServerSessionThread_loop.cpp + - void thread_body() + +VariableServerSessionThread.cpp + + void preload_checkpoint() + + void restart() + + + ConnectionStatus wait_for_accept() +``` + +`thread_body()` runs the main loop of the VST. + +`preload_checkpoint()` and `restart()` are called from their respective Trick jobs in the main thread. + + +## VariableServer + +The VariableServer class itself has been the least changed by this refactor. + +Its main job is to delegate between the Trick jobs and the various sessions and threads. The VariableServer is a singleton that is declared as part of a SimObject in `default_trick_sys.sm` +``` +VariableServerSimObject { + VariableServer vs; + + {TRK} ("default_data") vs.default_data() ; + {TRK} P0 ("initialization") trick_ret = vs.init() ; + {TRK} ("preload_checkpoint") vs.suspendPreCheckpointReload(); + {TRK} ("restart") vs.restart(); + {TRK} ("restart") vs.resumePostCheckpointReload(); + {TRK} ("top_of_frame") vs.copy_and_write_top() ; + {TRK} ("automatic_last") vs.copy_and_write_scheduled() ; + + {TRK} ("freeze_init") vs.freeze_init() ; + {TRK} ("freeze_automatic") vs.copy_and_write_freeze_scheduled() ; + {TRK} ("freeze") vs.copy_and_write_freeze() ; + + {TRK} ("shutdown") vs.shutdown() ; +} +``` + +In each job: +- default_data: Initialize the listening socket with any port +- initialization: If the user has requested a different port, set that up. Start the listening thead. +- preload_checkpoint: call methods of all VariableServerListenThreads and VariableServerSessionThreads to pause in preparation for checkpoint +- restart: recreate listening threads if necessary, and call restart methods of all VariableServerListenThreads and VariableServerSessionThreads +- top_of_frame, automatic_last, freeze, freeze_automatic: Loop through all VariableServerSessions and call the appropriate copy_data_ variant +- shutdown: cancel all VariableServerListenThreads and VariableServerSessionThreads + +The VariableServer implements part of the dataflow for commands that come in through the input processor. + +The VariableServer's external API is the entry point of the variable server commands that are passed though the input processor. These commands go to a C-style API, which query the static global singleton VariableServer object. Each of these commands will look up the correct session by pthread_id and call the session's command. + +``` +Client sends `"trick.var_add("my_variable")\n"` +`VariableServerSessionThread::thread_body` calls `VariableServerSession::handle_mesage` +`VariableServerSession::handle_mesage` reads from the client connection and calls `ip_parse("trick.var_add("my_variable")\n")` +Python interpreter runs (on the variable server thread) back through the SWIG layer to invoke global method `var_add("my_variable")` in var_server_ext.cpp +`var_add()` calls the static global variable server object to look up the current session, mapped to the current pthread_id +`var_add()` calls `session->var_add("my_variable")` +session will add my_variable to its list of variables +Return from `ip_parse`, continue `thread_body` processing +``` + +Code snippets (some minor modifications for clarity): + +``` +// var_server_ext.cpp +int var_add(std::string in_name) { + Trick::VariableServerSession * session = get_session(); + + if (session != NULL ) { + session->var_add(in_name) ; + } + return(0) ; +} + +Trick::VariableServerSession * get_session() { + return the_vs->get_session(pthread_self()) ; +} + +// VariableServer.cpp +Trick::VariableServerSession * Trick::VariableServer::get_session(pthread_t thread_id) { + Trick::VariableServerSession * ret = NULL ; + pthread_mutex_lock(&map_mutex) ; + auto it = var_server_sessions.find(thread_id) ; + if ( it != var_server_sessions.end() ) { + ret = (*it).second ; + } + pthread_mutex_unlock(&map_mutex) ; + return ret ; +} + +// VariableServerSession_commands.cpp +int Trick::VariableServerSession::var_add(std::string in_name) { + + VariableReference new_var = new VariableReference(in_name); + _session_variables.push_back(new_var) ; + + return(0) ; +} +``` + +![var_add Sequence Diagram](images/var_add_sequence.png) + +Both var_server_ext.cpp and VariableServerSession_commands.cpp have a method for each command in the user facing variable server API. + +### Special case listen threads and sessions + +The user can request addition TCP, UDP, or multicast servers from the input file. This is handled in the VariableServer class. + +``` +VariableServer { + + int create_tcp_socket(const char * address, unsigned short in_port ) + + int create_udp_socket(const char * address, unsigned short in_port ) + + int create_multicast_socket(const char * mcast_address, const char * address, unsigned short in_port ) +} +``` + +Calling `create_tcp_socket` will create an additional listen thread, which will operate in the same way as the default listen thread. The VariableServer will create a TCPClientListener and start the listen thread, just like it does for the default listen thread. + +`create_udp_socket` will open a UDP socket. Since it does not require a listener, the VariableServer creates a VariableServerSessionThread and a UDPConnection directly, and starts the thread. The output for this session is sent to whatever host a message is received from. + +`create_multicast_socket` will create a UDP socket and add it to a multicast group. It will also create and start a VariableServerSessionThread and MulticastGroup object. The output for this session is broadcast on the multicast address given. + +## ClientConnection and TCPClientListener + +The purpose of these classes are to abstract away all of the network code and the differences between TCP, UDP, and multicast connections for the purpose of the VariableServerSession. + +The VariableServerListenThread uses a TCPClientListener to listen for connections on an open socket, and when one comes in, the TCPClientListener will create a TCPConnection to service the connection. + +In the UDP and Multicast case, the connection is created directly in the VariableServer class and passed into the VariableServerSessionThread. + + +## Class Diagram + +![Variable Server UML](images/vs_uml.png) + diff --git a/docs/developer_docs/images/var_add_sequence.png b/docs/developer_docs/images/var_add_sequence.png new file mode 100644 index 00000000..8ec90ae2 Binary files /dev/null and b/docs/developer_docs/images/var_add_sequence.png differ diff --git a/docs/developer_docs/images/vs_uml.png b/docs/developer_docs/images/vs_uml.png new file mode 100644 index 00000000..f44a799f Binary files /dev/null and b/docs/developer_docs/images/vs_uml.png differ diff --git a/docs/documentation/simulation_capabilities/Threads.md b/docs/documentation/simulation_capabilities/Threads.md index c032840b..7fb3daf5 100644 --- a/docs/documentation/simulation_capabilities/Threads.md +++ b/docs/documentation/simulation_capabilities/Threads.md @@ -12,7 +12,7 @@ A trick sim is a multi-threaded process, and all of the threads that are created | MessageTCDeviceListenThread | `trick_message.mdevice.get_listen_thread()` | | MessageThreadedCout | `trick_message.mtcout` | | DRDWriterThread | `trick_data_record.drd.drd_writer_thread` | -| VariableServerThread | `trick_vs.vs.get_vst(pthread_t thread_id)` | +| VariableServerSessionThread | `trick_vs.vs.get_vst(pthread_t thread_id)` | ## ThreadBase Methods diff --git a/include/trick/ClientConnection.hh b/include/trick/ClientConnection.hh index 064a1e4a..2eb5dcf7 100644 --- a/include/trick/ClientConnection.hh +++ b/include/trick/ClientConnection.hh @@ -9,11 +9,13 @@ LIBRARY DEPENDENCIES: #define CLIENT_CONNECTION_HH #include -// #include namespace Trick { class ClientConnection { public: + + virtual ~ClientConnection() { } + static const unsigned int MAX_CMD_LEN = 200000 ; enum ConnectionType { TCP, UDP, MCAST, WS } ; @@ -24,17 +26,20 @@ namespace Trick { virtual int write (const std::string& message) = 0; virtual int write (char * message, int size) = 0; - virtual std::string read (int max_len = MAX_CMD_LEN) = 0; + virtual int read (std::string& message, int max_len = MAX_CMD_LEN) = 0; virtual int setBlockMode (bool blocking) = 0; virtual int disconnect () = 0; virtual bool isInitialized() = 0; - virtual std::string getClientTag (); - virtual int setClientTag (std::string tag); + virtual std::string getClientTag () = 0; + virtual int setClientTag (std::string tag) = 0; - virtual int restart() {}; + virtual int restart() = 0; + + virtual std::string getClientHostname() = 0; + virtual int getClientPort() = 0; protected: ConnectionType _connection_type; diff --git a/include/trick/ClientListener.hh b/include/trick/ClientListener.hh deleted file mode 100644 index ebba212f..00000000 --- a/include/trick/ClientListener.hh +++ /dev/null @@ -1,63 +0,0 @@ -#ifndef CLIENT_LISTENER_HH -#define CLIENT_LISTENER_HH - -/* - PURPOSE: ( Encapsulate a TCP server. ) -*/ - -#include -#include "trick/SystemInterface.hh" -#include "trick/TCPConnection.hh" - -#define LISTENER_ERROR -1 - - -namespace Trick { - - class TCPConnection; - - class ClientListener { - public: - - ClientListener (); - ClientListener (SystemInterface * system_interface); - - ~ClientListener (); - - int initialize(std::string hostname, int port); - int initialize(); - - int setBlockMode(bool blocking); - - bool checkForNewConnections(); - - TCPConnection * setUpNewConnection (); - - std::string getHostname (); - - int getPort(); - - int disconnect(); - - int checkSocket(); - - bool validateSourceAddress(std::string source_address); - - bool isInitialized(); - - int restart(); - - private: - - int _listen_socket; - std::string _hostname; - int _port; - std::string _client_tag; - - bool _initialized; - - SystemInterface * _system_interface; /* ** */ - }; -} - -#endif \ No newline at end of file diff --git a/include/trick/MulticastGroup.hh b/include/trick/MulticastGroup.hh new file mode 100644 index 00000000..c6095b3e --- /dev/null +++ b/include/trick/MulticastGroup.hh @@ -0,0 +1,55 @@ +/* + PURPOSE: ( Encapsulate multicast functionality. ) +*/ +#ifndef MULTICAST_GROUP_HH +#define MULTICAST_GROUP_HH + +#include +#include +#include + +#include +#include + +namespace Trick { + class MulticastGroup : public UDPConnection { + public: + MulticastGroup(); + MulticastGroup (SystemInterface * system_interface); + + virtual ~MulticastGroup(); + + // Multicast specific functions + int initialize_with_receiving(std::string local_addr, std::string mcast_addr, int port); + virtual int initialize(); + + virtual int broadcast (std::string message); + virtual int addAddress (std::string addr, int port); + + // ClientConnection interface + + virtual int write (const std::string& message) override; + virtual int write (char * message, int size) override; + + virtual int read (std::string& message, int max_len = MAX_CMD_LEN) override; + + virtual int disconnect () override; + + virtual bool isInitialized() override; + + virtual int restart() override; + + virtual std::string getClientHostname() override; + virtual int getClientPort() override; + + private: + std::vector _addresses; /**< trick_io(**) Addresses to multicast to. */ + bool _initialized; /**< trick_io(**) Whether this object is ready */ + + struct sockaddr_in _self_info ; /**< trick_io(**) Save self information so we don't process our own messages */ + + SystemInterface * _system_interface; + }; +} + +#endif \ No newline at end of file diff --git a/include/trick/MulticastManager.hh b/include/trick/MulticastManager.hh deleted file mode 100644 index a69da6b2..00000000 --- a/include/trick/MulticastManager.hh +++ /dev/null @@ -1,28 +0,0 @@ -/* - PURPOSE: ( Encapsulate multicast functionality. ) -*/ - -#include -#include -#include - -namespace Trick { - class MulticastManager { - public: - MulticastManager(); - ~MulticastManager(); - - int broadcast (std::string message); - int addAddress (std::string addr, int port); - int restart (); - int initialize(); - int is_initialized(); - - - private: - std::vector addresses; /**< trick_io(**) Addresses to multicast to. */ - int mcast_socket; /**< trick_io(**) Socket opened in initialization. */ - int initialized; /**< trick_io(**) Whether manager is ready */ - - }; -} \ No newline at end of file diff --git a/include/trick/SysThread.hh b/include/trick/SysThread.hh index d5f639dd..e5aab8e4 100644 --- a/include/trick/SysThread.hh +++ b/include/trick/SysThread.hh @@ -38,7 +38,29 @@ namespace Trick { static int ensureAllShutdown(); + protected: + // Called from the main thread + void force_thread_to_pause(); + // Called from the main thread + void unpause_thread(); + + // Called from the sys_thread at a point that would be appropriate to pause + void test_pause(); + private: + /** Synchronization to safely pause and restart processing during a checkpoint reload */ + pthread_mutex_t _restart_pause_mutex ; /**< trick_io(**) */ + + // For the main thread to tell the sys_thread to pause + bool _thread_should_pause; /**< trick_io(**) */ + // For the main thread to tell the sys_thread to wake up + pthread_cond_t _thread_wakeup_cv; /**< trick_io(**) */ + + // For the main thread to wait for the sys_thread to pause + pthread_cond_t _thread_has_paused_cv; /**< trick_io(**) */ + bool _thread_has_paused; /**< trick_io(**) */ + + // Had to use Construct On First Use here to avoid the static initialziation fiasco static pthread_mutex_t& list_mutex(); static pthread_cond_t& list_empty_cv(); diff --git a/include/trick/SystemInterface.hh b/include/trick/SystemInterface.hh index b6daa767..c13dbb92 100644 --- a/include/trick/SystemInterface.hh +++ b/include/trick/SystemInterface.hh @@ -11,6 +11,7 @@ #include #include #include +#include class SystemInterface { public: @@ -19,7 +20,7 @@ class SystemInterface { virtual int setsockopt (int socket, int level, int option_name, const void * option_value, socklen_t option_len) { return ::setsockopt ( socket, level, option_name, option_value, option_len); } - virtual int bind (int socket, struct sockaddr * address, socklen_t address_len) { return ::bind ( socket, address, address_len); } + virtual int bind (int socket, const struct sockaddr * address, socklen_t address_len) { return ::bind ( socket, address, address_len); } virtual int getsockname (int socket, struct sockaddr * address, socklen_t * address_len) { return ::getsockname ( socket, address, address_len); } @@ -45,6 +46,8 @@ class SystemInterface { virtual ssize_t recvfrom (int socket, void * buffer, size_t length, int flags, struct sockaddr * address, socklen_t * address_len) { return ::recvfrom ( socket, buffer, length, flags, address, address_len); } + virtual in_addr_t inet_addr (const char * cp) { return ::inet_addr ( cp); } + }; #endif diff --git a/include/trick/TCPClientListener.hh b/include/trick/TCPClientListener.hh new file mode 100644 index 00000000..dd5435d3 --- /dev/null +++ b/include/trick/TCPClientListener.hh @@ -0,0 +1,62 @@ +#ifndef CLIENT_LISTENER_HH +#define CLIENT_LISTENER_HH + +/* + PURPOSE: ( Encapsulate a TCP server. ) +*/ + +#include +#include "trick/SystemInterface.hh" +#include "trick/TCPConnection.hh" + +#define LISTENER_ERROR -1 + + +namespace Trick { + + class TCPConnection; + + class TCPClientListener { + public: + + TCPClientListener (); + TCPClientListener (SystemInterface * system_interface); + + virtual ~TCPClientListener (); + + virtual int initialize(std::string hostname, int port); + virtual int initialize(); + + virtual int setBlockMode(bool blocking); + + virtual bool checkForNewConnections(); + + virtual TCPConnection * setUpNewConnection (); + + virtual int disconnect(); + + virtual int checkSocket(); + + virtual bool validateSourceAddress(std::string source_address); + + virtual bool isInitialized(); + + virtual int restart(); + + virtual std::string getHostname (); + virtual int getPort(); + + protected: + + int _listen_socket; + std::string _hostname; + int _port; + std::string _client_tag; + + bool _initialized; + + SystemInterface * _system_interface; /* ** */ + }; +} + +#endif \ No newline at end of file diff --git a/include/trick/TCPConnection.hh b/include/trick/TCPConnection.hh index 9285610d..6b34dedd 100644 --- a/include/trick/TCPConnection.hh +++ b/include/trick/TCPConnection.hh @@ -13,23 +13,30 @@ namespace Trick { class TCPConnection : public ClientConnection { public: + TCPConnection (); TCPConnection (int listen_socket); TCPConnection (SystemInterface * system_interface); TCPConnection (int listen_socket, SystemInterface * system_interface); - int start() override; + virtual int start() override; - int write (const std::string& message) override; - int write (char * message, int size) override; + virtual int write (const std::string& message) override; + virtual int write (char * message, int size) override; - std::string read (int max_len = MAX_CMD_LEN) override; + virtual int read (std::string& message, int max_len = MAX_CMD_LEN) override; - int disconnect () override; - bool isInitialized() override; + virtual int disconnect () override; + virtual bool isInitialized() override; - int setBlockMode(bool blocking) override; + virtual int setBlockMode(bool blocking) override; - int restart() override; + virtual int restart() override; + + virtual std::string getClientTag () override; + virtual int setClientTag (std::string tag) override; + + virtual std::string getClientHostname() override; + virtual int getClientPort() override; private: int _socket; diff --git a/include/trick/UDPConnection.hh b/include/trick/UDPConnection.hh index abfc7e6a..c64539d3 100644 --- a/include/trick/UDPConnection.hh +++ b/include/trick/UDPConnection.hh @@ -22,7 +22,7 @@ namespace Trick { int write (const std::string& message) override; int write (char * message, int size) override; - std::string read (int max_len = MAX_CMD_LEN) override; + int read (std::string& message, int max_len = MAX_CMD_LEN) override; int disconnect () override; bool isInitialized() override; @@ -30,15 +30,19 @@ namespace Trick { int setBlockMode(bool block_mode) override; int restart() override; + virtual std::string getClientTag () override; + virtual int setClientTag (std::string tag) override; + // Non-override functions int initialize(const std::string& hostname, int port); - int getPort(); std::string getHostname(); - - private: + virtual std::string getClientHostname() override; + virtual int getClientPort() override; + + protected: bool _initialized; bool _started; int _socket; diff --git a/include/trick/VariableReference.hh b/include/trick/VariableReference.hh index 6e97b217..359a300a 100644 --- a/include/trick/VariableReference.hh +++ b/include/trick/VariableReference.hh @@ -27,17 +27,10 @@ namespace Trick { ~VariableReference(); - const char* getName() const; + std::string getName() const; TRICK_TYPE getType() const; - // There are 2 different "units" variables used - REF2->units and REF2->attr->units - // REF2->attr->units should not be changed, it is what the variable is represented in internally - // REF2->units is the unit that has been requested by the variable server client - // REF2->units should be null unless var_units or var_add with units is called - // We'll refer to REF2->attr->units as BaseUnits and REF2->units as RequestedUnits - // Encapsulate all of this away so that no one else has to think about ref->attr->units vs ref->units - // ^ TODO: this is not where the above documentation should be. put it somewhere else. - const char* getBaseUnits() const; + std::string getBaseUnits() const; int setRequestedUnits(std::string new_units); // These variables have 2 staging buffers that we can swap between to allow for different copy and write-out modes @@ -66,8 +59,6 @@ namespace Trick { // Helper method for byteswapping static void byteswap_var (char * out, char * in, const VariableReference& ref); - // TODO: Some system for error messaging - private: VariableReference(); void byteswap_var(char * out, char * in) const; @@ -76,22 +67,25 @@ namespace Trick { static REF2* make_error_ref(std::string in_name); static REF2* make_do_not_resolve_ref(std::string in_name); - static int bad_ref_int; - static int do_not_resolve_bad_ref_int; + static int _bad_ref_int; + static int _do_not_resolve_bad_ref_int; - REF2 *var_info; - void *address; // -- address of data copied to buffer - int size; // -- size of data copied to buffer - bool deref; // -- indicates whether variable is pointer that needs to be dereferenced - cv_converter * conversion_factor ; // ** udunits conversion factor - TRICK_TYPE trick_type ; // -- Trick type of this variable + REF2 * _var_info; + void * _address; // -- address of data copied to buffer + int _size; // -- size of data copied to buffer + bool _deref; // -- indicates whether variable is pointer that needs to be dereferenced + cv_converter * _conversion_factor ; // ** udunits conversion factor + TRICK_TYPE _trick_type ; // -- Trick type of this variable - // TODO: use atomics for these - bool staged; - bool write_ready; + bool _staged; + bool _write_ready; - void *stage_buffer; - void *write_buffer; + void *_stage_buffer; + void *_write_buffer; + + std::string _base_units; + std::string _requested_units; + std::string _name; }; std::ostream& operator<< (std::ostream& s, const Trick::VariableReference& ref); diff --git a/include/trick/VariableServer.hh b/include/trick/VariableServer.hh index eeff4b6a..87cd1349 100644 --- a/include/trick/VariableServer.hh +++ b/include/trick/VariableServer.hh @@ -16,7 +16,7 @@ #include "trick/reference.h" #include "trick/JobData.hh" #include "trick/variable_server_sync_types.h" -#include "trick/VariableServerThread.hh" +#include "trick/VariableServerSessionThread.hh" #include "trick/VariableServerListenThread.hh" #include "trick/SysThread.hh" @@ -85,27 +85,27 @@ namespace Trick { /** @brief Copies client variable values at the top of the frame. */ - int copy_data_top() ; + int copy_and_write_top() ; /** @brief The function to copy client variable values to their output buffers when in sync mode. */ - int copy_data_scheduled() ; + int copy_and_write_scheduled() ; /** @brief The function to copy client variable values to their output buffers when in sync mode. */ - int copy_data_freeze_scheduled() ; + int copy_and_write_freeze_scheduled() ; /** @brief Copies client variable values at the top of the frame. */ - int copy_data_freeze() ; + int copy_and_write_freeze() ; /** @brief Adds a vst to the map. */ - void add_vst(pthread_t thread_id, VariableServerThread * in_vst ) ; + void add_vst(pthread_t thread_id, VariableServerSessionThread * in_vst ) ; /** @brief Adds a vst to the map. @@ -114,9 +114,9 @@ namespace Trick { /** @brief Get a vst mapped by thread id - @return the VariableServerThread mapped to the thread id if found, or NULL if not found. + @return the VariableServerSessionThread mapped to the thread id if found, or NULL if not found. */ - Trick::VariableServerThread * get_vst(pthread_t thread_id) ; + Trick::VariableServerSessionThread * get_vst(pthread_t thread_id) ; /** @brief Get a session mapped by thread id @@ -266,9 +266,9 @@ namespace Trick { void set_copy_data_job( Trick::JobData * ) ; /** - @brief Called from the S_define to set the copy_data_freeze_job ptr. + @brief Called from the S_define to set the copy_and_write_freeze_job ptr. */ - void set_copy_data_freeze_job( Trick::JobData * ) ; + void set_copy_and_write_freeze_job( Trick::JobData * ) ; protected: @@ -289,16 +289,10 @@ namespace Trick { Trick::JobData * copy_data_job ; /**< trick_io(**) trick_units(--) */ /** Pointer to freeze_automatic job that copies requested variable values to their output buffers in sync mode.\n */ - Trick::JobData * copy_data_freeze_job ; /**< trick_io(**) trick_units(--) */ + Trick::JobData * copy_and_write_freeze_job ; /**< trick_io(**) trick_units(--) */ - /** Storage for saved thread pause state. The pause state of each thread is saved - to this map by suspendPreCheckpointReload(). resumePostCheckpointReload() restores - the pause state from this map. - */ - std::map thread_pause_state_store; // ** ignore this - - /** Map thread id to the VariableServerThread object.\n */ - std::map < pthread_t , VariableServerThread * > var_server_threads ; /**< trick_io(**) */ + /** Map thread id to the VariableServerSessionThread object.\n */ + std::map < pthread_t , VariableServerSessionThread * > var_server_threads ; /**< trick_io(**) */ std::map < pthread_t , VariableServerSession * > var_server_sessions ; /**< trick_io(**) */ /** Mutex to ensure only one thread manipulates the map of var_server_threads\n */ diff --git a/include/trick/VariableServerListenThread.hh b/include/trick/VariableServerListenThread.hh index bc87a34e..3f4f6d26 100644 --- a/include/trick/VariableServerListenThread.hh +++ b/include/trick/VariableServerListenThread.hh @@ -8,10 +8,9 @@ #include #include -// #include "trick/tc.h" -#include "trick/ClientListener.hh" +#include "trick/TCPClientListener.hh" #include "trick/SysThread.hh" -#include "trick/MulticastManager.hh" +#include "trick/MulticastGroup.hh" namespace Trick { @@ -19,11 +18,14 @@ namespace Trick { /** This class runs the variable server listen loop. @author Alex Lin + @author Jackie Deans (2023) */ class VariableServerListenThread : public Trick::SysThread { public: VariableServerListenThread() ; + VariableServerListenThread(TCPClientListener * listener); + virtual ~VariableServerListenThread() ; const char * get_hostname() ; @@ -52,7 +54,7 @@ namespace Trick { void pause_listening() ; void restart_listening() ; - void create_tcp_socket(const char * address, unsigned short in_port ) ; + void set_multicast_group (MulticastGroup * group); virtual void dump( std::ostream & oss = std::cout ) ; @@ -60,28 +62,25 @@ namespace Trick { void initializeMulticast(); /** Requested variable server source address\n */ - std::string requested_source_address ; /**< trick_units(--) */ + std::string _requested_source_address ; /**< trick_units(--) */ /** Requested variable server port number.\n */ - unsigned short requested_port ; /**< trick_units(--) */ + unsigned short _requested_port ; /**< trick_units(--) */ /** User requested specific port number\n */ - bool user_requested_address ; /**< trick_units(--) */ + bool _user_requested_address ; /**< trick_units(--) */ /** User defined unique tag to easily identify this variable server port.\n */ - std::string user_tag; /**< trick_units(--) */ + std::string _user_tag; /**< trick_units(--) */ /** Turn on/off broadcasting of variable server port.\n */ - bool broadcast ; /**< trick_units(--) */ + bool _broadcast ; /**< trick_units(--) */ /** The listen device */ - ClientListener listener; /**< trick_io(**) trick_units(--) */ + TCPClientListener * _listener; /**< trick_io(**) trick_units(--) */ /* Multicast broadcaster */ - MulticastManager multicast; /**< trick_io(**) trick_units(--) */ - - /** The mutex to stop accepting new connections during restart\n */ - pthread_mutex_t restart_pause ; /**< trick_io(**) */ + MulticastGroup * _multicast; /**< trick_io(**) trick_units(--) */ } ; diff --git a/include/trick/VariableServerSession.hh b/include/trick/VariableServerSession.hh index 00a05704..97f169a5 100644 --- a/include/trick/VariableServerSession.hh +++ b/include/trick/VariableServerSession.hh @@ -2,8 +2,8 @@ PURPOSE: (Represent the state of a variable server connection.) **************************************************************************/ -#ifndef WSSESSION_HH -#define WSSESSION_HH +#ifndef VSSESSION_HH +#define VSSESSION_HH #include #include @@ -18,12 +18,99 @@ PURPOSE: (Represent the state of a variable server connection.) namespace Trick { class VariableServerSession { public: - VariableServerSession(ClientConnection * connection); - ~VariableServerSession(); + VariableServerSession(); + + virtual ~VariableServerSession(); friend std::ostream& operator<< (std::ostream& s, const Trick::VariableServerSession& session); - int handleMessage(); + /** + * @brief Set the connection object + * + * @param connection - a pointer to a ClientConnection that is initialized and ready to go + */ + virtual void set_connection(ClientConnection * connection); + + /** + @brief Read a message from the connection and act on it + */ + virtual int handle_message(); + + /** + @brief Get the pause state of this thread. + */ + virtual bool get_pause() ; + + /** + @brief Set the pause state of this thread. + */ + virtual void set_pause(bool on_off) ; + + /** + @brief Get the exit command of this session. + */ + virtual bool get_exit_cmd() ; + + /** + @brief Command session to exit + */ + virtual void set_exit_cmd() ; + + /** + @brief gets the send_stdio flag. + */ + virtual bool get_send_stdio() ; + + /** + @brief sets the send_stdio flag. + */ + virtual int set_send_stdio(bool on_off) ; + + // Called from different types of Trick jobs. + // Determines whether this session should be copying at that time, and calls internal copy methods if so + int copy_and_write_freeze(long long curr_frame); + int copy_and_write_freeze_scheduled(long long curr_tics); + int copy_and_write_scheduled(long long curr_tics); + int copy_and_write_top(long long curr_frame); + + // Called from VariableServerSessionThread + virtual int copy_and_write_async(); + + /** + @brief Copy given variable values from Trick memory to each variable's output buffer. + cyclical indicated whether it is a normal cyclical copy or a send_once copy + */ + virtual int copy_sim_data(std::vector& given_vars, bool cyclical); + virtual int copy_sim_data(); + + /** + @brief Write data from the given var only to the appropriate format (var_ascii or var_binary) from variable output buffers to socket. + */ + virtual int write_data(std::vector& var, VS_MESSAGE_TYPE message_type) ; + virtual int write_data(); + + int write_stdio(int stream, std::string text); + + void disconnect_references(); + + virtual long long get_next_tics() const; + + virtual long long get_freeze_next_tics() const; + + int freeze_init(); + + virtual double get_update_rate() const; + + void pause_copy(); + void unpause_copy(); + + virtual VS_WRITE_MODE get_write_mode () const; + virtual VS_COPY_MODE get_copy_mode () const; + + + /************************************************************************************************/ + /* Variable Server Interface */ + /************************************************************************************************/ /** @brief @userdesc Command to add a variable to a list of registered variables for value retrieval. @@ -34,7 +121,7 @@ namespace Trick { @param in_name - the variable name to retrieve @return always 0 */ - int var_add( std::string in_name ) ; + virtual int var_add( std::string in_name ) ; /** @brief @userdesc Command to add a variable to a list of registered variables for value retrieval, @@ -50,7 +137,7 @@ namespace Trick { @param units_name - the desired units, specified within curly braces @return always 0 */ - int var_add( std::string in_name, std::string units_name ) ; + virtual int var_add( std::string in_name, std::string units_name ) ; /** @brief @userdesc Command to remove a variable (previously registered with var_add) @@ -60,7 +147,7 @@ namespace Trick { @param in_name - the variable name to remove @return always 0 */ - int var_remove( std::string in_name ) ; + virtual int var_remove( std::string in_name ) ; /** @brief @userdesc Command to instruct the variable server to return the value of a variable @@ -75,7 +162,7 @@ namespace Trick { @note trick.var_add("my_object.my_variable"); trick.var_units("my_object.my_variable","{m}") is the same as trick.var_add("my_object.my_variable","{m}") */ - int var_units(std::string var_name , std::string units_name) ; + virtual int var_units(std::string var_name , std::string units_name) ; /** @brief @userdesc Command to instruct the variable server to send a Boolean value indicating @@ -89,7 +176,7 @@ namespace Trick { @param in_name - the variable name to query @return always 0 */ - int var_exists( std::string in_name ) ; + virtual int var_exists( std::string in_name ) ; /** @brief @userdesc Command to immediately send the value of a comma separated list of variables @@ -99,7 +186,7 @@ namespace Trick { @param num_vars - number of vars in in_name_list @return always 0 */ - int var_send_once(std::string in_name_list, int num_vars); + virtual int var_send_once(std::string in_name_list, int num_vars); /** @brief @userdesc Command to instruct the variable server to immediately send back the values of @@ -110,7 +197,7 @@ namespace Trick { @code trick.var_send() @endcode @return always 0 */ - int var_send() ; + virtual int var_send() ; /** @brief @userdesc Command to remove all variables from the list of variables currently @@ -120,7 +207,7 @@ namespace Trick { @code trick.var_clear() @endcode @return always 0 */ - int var_clear() ; + virtual int var_clear() ; /** @brief @userdesc Turns on validating addresses before they are referenced @@ -128,7 +215,7 @@ namespace Trick { @code trick.var_validate_address() @endcode @return always 0 */ - int var_validate_address(bool on_off) ; + virtual int var_validate_address(bool on_off) ; /** @brief @userdesc Command to instruct the variable server to output debug information. @@ -137,7 +224,7 @@ namespace Trick { @return always 0 @param level - 1,2,or 3, higher number increases amount of info output */ - int var_debug(int level) ; + virtual int var_debug(int level) ; /** @brief @userdesc Command to instruct the variable server to return values in ASCII format (this is the default). @@ -145,7 +232,7 @@ namespace Trick { @code trick.var_ascii() @endcode @return always 0 */ - int var_ascii() ; + virtual int var_ascii() ; /** @brief @userdesc Command to instruct the variable server to return values in binary format. @@ -153,7 +240,7 @@ namespace Trick { @code trick.var_binary() @endcode @return always 0 */ - int var_binary() ; + virtual int var_binary() ; /** @brief @userdesc Command to instruct the variable server to return values in binary format, @@ -163,7 +250,7 @@ namespace Trick { @code trick.var_binary_nonames() @endcode @return always 0 */ - int var_binary_nonames() ; + virtual int var_binary_nonames() ; /** @brief @userdesc Command to tell the server when to copy data @@ -175,7 +262,7 @@ namespace Trick { @param mode - One of the above enumerations @return 0 if successful, -1 if error */ - int var_set_copy_mode(int on_off) ; + virtual int var_set_copy_mode(int mode) ; /** @brief @userdesc Command to tell the server when to copy data @@ -186,7 +273,7 @@ namespace Trick { @param mode - One of the above enumerations @return 0 if successful, -1 if error */ - int var_set_write_mode(int on_off) ; + virtual int var_set_write_mode(int mode) ; /** @brief @userdesc Command to put the current variable server/client connection in sync mode, @@ -205,7 +292,7 @@ namespace Trick { 2 = sync data gather, sync socket write @return always 0 */ - int var_sync(int on_off) ; + virtual int var_sync(int on_off) ; /** @brief @userdesc Set the frame multiple @@ -214,7 +301,7 @@ namespace Trick { @param mult - The requested multiple @return 0 */ - int var_set_frame_multiple(unsigned int mult) ; + virtual int var_set_frame_multiple(unsigned int mult) ; /** @brief @userdesc Set the frame offset @@ -223,7 +310,7 @@ namespace Trick { @param offset - The requested offset @return 0 */ - int var_set_frame_offset(unsigned int offset) ; + virtual int var_set_frame_offset(unsigned int offset) ; /** @brief @userdesc Set the frame multiple @@ -232,7 +319,7 @@ namespace Trick { @param mult - The requested multiple @return 0 */ - int var_set_freeze_frame_multiple(unsigned int mult) ; + virtual int var_set_freeze_frame_multiple(unsigned int mult) ; /** @brief @userdesc Set the frame offset @@ -241,7 +328,7 @@ namespace Trick { @param offset - The requested offset @return 0 */ - int var_set_freeze_frame_offset(unsigned int offset) ; + virtual int var_set_freeze_frame_offset(unsigned int offset) ; /** @brief @userdesc Command to instruct the variable server to byteswap the return values @@ -253,7 +340,7 @@ namespace Trick { @param on_off - true (or 1) to byteswap the return data, false (or 0) to return data as is @return always 0 */ - int var_byteswap(bool on_off) ; + virtual int var_byteswap(bool on_off) ; /** @brief @userdesc Command to turn on variable server logged messages to a playback file. @@ -262,7 +349,7 @@ namespace Trick { @code trick.set_log_on() @endcode @return always 0 */ - int set_log_on() ; + virtual int set_log_on() ; /** @brief @userdesc Command to turn off variable server logged messages to a playback file. @@ -270,50 +357,40 @@ namespace Trick { @code trick.set_log_off() @endcode @return always 0 */ - int set_log_off() ; + virtual int set_log_off() ; /** @brief Command to send the number of items in the var_add list. The variable server sends a message indicator of "3", followed by the total number of variables being sent. */ - int send_list_size(); + virtual int send_list_size(); /** @brief Special command to instruct the variable server to send the contents of the S_sie.resource file; used by TV. The variable server sends a message indicator of "2", followed by a tab, followed by the file contents which are then sent as sequential ASCII messages with a maximum size of 4096 bytes each. */ - int send_sie_resource(); + virtual int send_sie_resource(); /** @brief Special command to only send the class sie class information */ - int send_sie_class(); + virtual int send_sie_class(); /** @brief Special command to only send the enumeration sie class information */ - int send_sie_enum(); + virtual int send_sie_enum(); /** @brief Special command to only send the top level objects sie class information */ - int send_sie_top_level_objects(); + virtual int send_sie_top_level_objects(); /** @brief Special command to send an arbitrary file through the variable server. */ - int send_file(std::string file_name); - - /** - @brief gets the send_stdio flag. - */ - bool get_send_stdio() ; - - /** - @brief sets the send_stdio flag. - */ - int set_send_stdio(bool on_off) ; + virtual int send_file(std::string file_name); /** @brief @userdesc Command to set the frequencty at which the variable server will send values @@ -324,157 +401,109 @@ namespace Trick { @param in_cycle - the desired frequency in seconds @return always 0 */ - int var_cycle(double in_cycle) ; + virtual int var_cycle(double in_cycle) ; /** - @brief Get the pause state of this thread. + @brief @userdesc Command exit this variable server session. + @par Python Usage: + @code trick.var_exit() @endcode + @return always 0 */ - bool get_pause() ; - - /** - @brief Set the pause state of this thread. - */ - void set_pause(bool on_off) ; - - /** - @brief Write data in the appropriate format (var_ascii or var_binary) from variable output buffers to socket. - */ - int write_data(); - - /** - @brief Write data from the given var only to the appropriate format (var_ascii or var_binary) from variable output buffers to socket. - */ - int write_data(std::vector& var, VS_MESSAGE_TYPE message_type) ; - - /** - @brief Copy client variable values from Trick memory to each variable's output buffer. - */ - int copy_sim_data(); - - /** - @brief Copy given variable values from Trick memory to each variable's output buffer. - cyclical indicated whether it is a normal cyclical copy or a send_once copy - */ - int copy_sim_data(std::vector& given_vars, bool cyclical); - - int var_exit(); - - int transmit_file(std::string sie_file); - - int copy_data_freeze(); - int copy_data_freeze_scheduled(long long curr_tics); - int copy_data_scheduled(long long curr_tics); - int copy_data_top(); - - // VS_COPY_MODE get_copy_mode(); - // VS_WRITE_MODE get_write_mode(); - - void disconnect_references(); - - long long get_next_tics() const; - - long long get_freeze_next_tics() const; - - int freeze_init(); - - double get_update_rate() const; - - // These should be private probably - int write_binary_data(const std::vector& given_vars, VS_MESSAGE_TYPE message_type); - int write_ascii_data(const std::vector& given_vars, VS_MESSAGE_TYPE message_type ); - int write_stdio(int stream, std::string text); - - VS_WRITE_MODE get_write_mode () const; - VS_COPY_MODE get_copy_mode () const; - - - pthread_mutex_t copy_mutex; /**< trick_io(**) */ - - /** Toggle to indicate var_exit commanded.\n */ - bool exit_cmd ; /**< trick_io(**) */ + virtual int var_exit(); private: - // int sendErrorMessage(const char* fmt, ... ); - // int sendSieMessage(void); - // int sendUnitsMessage(const char* vname); - ClientConnection * connection; /**< trick_io(**) */ - /** The trickcomm device used for the connection to the client.\n */ + pthread_mutex_t _copy_mutex; /**< trick_io(**) */ - VariableReference * find_session_variable(std::string name) const; + ClientConnection * _connection; /**< trick_io(**) */ - double stageTime; - bool dataStaged; + // Helper method to send a file to connection + virtual int transmit_file(std::string sie_file); - std::vector session_variables; /**< trick_io(**) */ - bool cyclicSendEnabled; /**< trick_io(**) */ - long long nextTime; /**< trick_io(**) */ - long long intervalTimeTics; /**< trick_io(**) */ + // Helper methods to write out formatted data + virtual int write_binary_data(const std::vector& given_vars, VS_MESSAGE_TYPE message_type); + virtual int write_ascii_data(const std::vector& given_vars, VS_MESSAGE_TYPE message_type ); + + virtual VariableReference * find_session_variable(std::string name) const; + + std::vector _session_variables; /**< trick_io(**) */ + + // Getters and setters for internal variables + virtual long long get_cycle_tics() const; + + virtual void set_freeze_next_tics(long long tics); + virtual void set_next_tics(long long tics); + + virtual int get_frame_multiple () const; + virtual int get_frame_offset () const; + virtual int get_freeze_frame_multiple () const; + virtual int get_freeze_frame_offset () const; + virtual bool get_enabled () const; /** Value set in var_cycle command.\n */ - double update_rate ; /**< trick_io(**) */ + double _update_rate ; /**< trick_io(**) */ /** The update rate in integer tics.\n */ - long long cycle_tics ; /**< trick_io(**) */ + long long _cycle_tics ; /**< trick_io(**) */ /** The next call time in integer tics of the job to copy client data (sync mode).\n */ - long long next_tics ; /**< trick_io(**) */ + long long _next_tics ; /**< trick_io(**) */ /** The next call time in integer tics of the job to copy client data (sync mode).\n */ - long long freeze_next_tics ; /**< trick_io(**) */ + long long _freeze_next_tics ; /**< trick_io(**) */ /** The simulation time converted to seconds\n */ - double time ; /**< trick_units(s) */ + double _time ; /**< trick_units(s) */ /** Toggle to set variable server copy as top_of_frame, scheduled, async \n */ - VS_COPY_MODE copy_mode ; /**< trick_io(**) */ + VS_COPY_MODE _copy_mode ; /**< trick_io(**) */ /** Toggle to set variable server writes as when copied or async.\n */ - VS_WRITE_MODE write_mode ; /**< trick_io(**) */ + VS_WRITE_MODE _write_mode ; /**< trick_io(**) */ /** multiples of frame_count to copy data. Only used at top_of_frame\n */ - int frame_multiple ; /**< trick_io(**) */ + int _frame_multiple ; /**< trick_io(**) */ /** multiples of frame_count to copy data. Only used at top_of_frame\n */ - int frame_offset ; /**< trick_io(**) */ + int _frame_offset ; /**< trick_io(**) */ /** multiples of frame_count to copy data. Only used at top_of_frame\n */ - int freeze_frame_multiple ; /**< trick_io(**) */ + int _freeze_frame_multiple ; /**< trick_io(**) */ /** multiples of frame_count to copy data. Only used at top_of_frame\n */ - int freeze_frame_offset ; /**< trick_io(**) */ - - // The way the modes are handled is confusing. TODO: refactor >:( + int _freeze_frame_offset ; /**< trick_io(**) */ /** Toggle to tell variable server to byteswap returned values.\n */ - bool byteswap ; /**< trick_io(**) */ + bool _byteswap ; /**< trick_io(**) */ /** Toggle to tell variable server return data in binary format.\n */ - bool binary_data ; /**< trick_io(**) */ + bool _binary_data ; /**< trick_io(**) */ /** Toggle to tell variable server return data in binary format without the variable names.\n */ - bool binary_data_nonames ; /**< trick_io(**) */ + bool _binary_data_nonames ; /**< trick_io(**) */ /** Value (1,2,or 3) that causes the variable server to output increasing amounts of debug information.\n */ - int debug ; /**< trick_io(**) */ + int _debug ; /**< trick_io(**) */ /** Toggle to enable/disable this variable server thread.\n */ - bool enabled ; /**< trick_io(**) */ + bool _enabled ; /**< trick_io(**) */ /** Toggle to turn on/off variable server logged messages to a playback file.\n */ - bool log ; /**< trick_io(**) */ + bool _log ; /**< trick_io(**) */ /** Toggle to indicate var_pause commanded.\n */ - bool pause_cmd ; /**< trick_io(**) */ + bool _pause_cmd ; /**< trick_io(**) */ /** Save pause state while reloading a checkpoint.\n */ - bool saved_pause_cmd ; /**< trick_io(**) */ + bool _saved_pause_cmd ; /**< trick_io(**) */ - bool send_stdio; + bool _send_stdio; - bool validate_address; + bool _validate_address; + + /** Toggle to indicate var_exit commanded.\n */ + bool _exit_cmd ; /**< trick_io(**) */ - int packets_copied; }; } diff --git a/include/trick/VariableServerSessionThread.hh b/include/trick/VariableServerSessionThread.hh new file mode 100644 index 00000000..8037da31 --- /dev/null +++ b/include/trick/VariableServerSessionThread.hh @@ -0,0 +1,106 @@ +/* + PURPOSE: + (VariableServerSessionThread) +*/ + +#ifndef VariableServerSessionThread_HH +#define VariableServerSessionThread_HH + +#include +#include +#include +#include "trick/SysThread.hh" +#include "trick/VariableServerSession.hh" +#include "trick/variable_server_sync_types.h" +#include "trick/variable_server_message_types.h" + +#include "trick/ClientConnection.hh" +#include "trick/TCPClientListener.hh" + +namespace Trick { + + class VariableServer ; + + /** Flag to indicate the connection has been made */ + enum ConnectionStatus { CONNECTION_PENDING, CONNECTION_SUCCESS, CONNECTION_FAIL }; + + +/** + The purpose of this class is to run a VariableServerSession. + - Manages creation and cleanup of the network connection + - Manages thread startup and shutdown + - Invokes parts of the VariableServerSession that run asynchronously (reading from client, copying and writing to client if in applicable mode) + + @author Alex Lin + @author Jackie Deans (2023) + */ + class VariableServerSessionThread : public Trick::SysThread { + + public: + friend std::ostream& operator<< (std::ostream& s, Trick::VariableServerSessionThread& vst); + + /** + @brief Constructor. + */ + VariableServerSessionThread() ; + VariableServerSessionThread(VariableServerSession * session) ; + + virtual ~VariableServerSessionThread() ; + /** + @brief static routine called from S_define to set the VariableServer pointer for all threads. + @param in_vs - the master variable server object + */ + static void set_vs_ptr(Trick::VariableServer * in_vs) ; + + void set_client_tag(std::string tag); + + /** + @brief Set the connection pointer for this thread + */ + void set_connection(ClientConnection * in_connection); + + /** + @brief Block until thread has accepted connection + */ + ConnectionStatus wait_for_accept() ; + + /** + @brief The main loop of the variable server thread that reads and processes client commands. + @return always 0 + */ + virtual void * thread_body() ; + + VariableServer * get_vs() ; + + void preload_checkpoint() ; + + void restart() ; + + void cleanup(); + + protected: + /** The Master variable server object. */ + static VariableServer * _vs ; + + /** Manages the variable list */ + VariableServerSession * _session; /**< trick_io(**) */ + + /** Connection to the client */ + ClientConnection * _connection; /**< trick_io(**) */ + + /** Value (1,2,or 3) that causes the variable server to output increasing amounts of debug information.\n */ + int _debug ; /**< trick_io(**) */ + + ConnectionStatus _connection_status ; /**< trick_io(**) */ + pthread_mutex_t _connection_status_mutex; /**< trick_io(**) */ + pthread_cond_t _connection_status_cv; /**< trick_io(**) */ + + bool _saved_pause_cmd; + } ; + + std::ostream& operator<< (std::ostream& s, VariableServerSessionThread& vst); + +} + +#endif + diff --git a/include/trick/VariableServerThread.hh b/include/trick/VariableServerThread.hh deleted file mode 100644 index 0b046035..00000000 --- a/include/trick/VariableServerThread.hh +++ /dev/null @@ -1,127 +0,0 @@ -/* - PURPOSE: - (VariableServerThread) -*/ - -#ifndef VARIABLESERVERTHREAD_HH -#define VARIABLESERVERTHREAD_HH - -#include -#include -#include -#include -#include "trick/tc.h" -#include "trick/SysThread.hh" -#include "trick/VariableServerSession.hh" -#include "trick/variable_server_sync_types.h" -#include "trick/variable_server_message_types.h" - -#include "trick/ClientConnection.hh" -#include "trick/ClientListener.hh" - -namespace Trick { - - class VariableServer ; - - /** Flag to indicate the connection has been made\n */ - enum ConnectionStatus { CONNECTION_PENDING, CONNECTION_SUCCESS, CONNECTION_FAIL }; - - -/** - This class provides variable server command processing on a separate thread for each client. - @author Alex Lin - */ - class VariableServerThread : public Trick::SysThread { - - public: - friend std::ostream& operator<< (std::ostream& s, Trick::VariableServerThread& vst); - - /** - @brief Constructor. - @param listen_dev - the TCDevice set up in listen() - */ - VariableServerThread() ; - - virtual ~VariableServerThread() ; - /** - @brief static routine called from S_define to set the VariableServer pointer for all threads. - @param in_vs - the master variable server object - */ - static void set_vs_ptr(Trick::VariableServer * in_vs) ; - - void set_client_tag(std::string tag); - - /** - @brief Open a UDP socket for this thread - */ - int open_udp_socket(const std::string& hostname, int port); - - /** - @brief Open a TCP connection for this thread - */ - int open_tcp_connection(ClientListener * listener); - - /** - @brief Block until thread has accepted connection - */ - ConnectionStatus wait_for_accept() ; - - /** - @brief The main loop of the variable server thread that reads and processes client commands. - @return always 0 - */ - virtual void * thread_body() ; - - VariableServer * get_vs() ; - - void preload_checkpoint() ; - - void restart() ; - - void cleanup(); - - protected: - - /** - @brief Called by send_sie commands to transmit files through the socket. - */ - int transmit_file(std::string file_name); - - /** The Master variable server object. */ - static VariableServer * vs ; - - /** Manages the variable list */ - VariableServerSession * session; /**< trick_io(**) */ - - /** Connection to the client */ - ClientConnection * connection; /**< trick_io(**) */ - - /** Value (1,2,or 3) that causes the variable server to output increasing amounts of debug information.\n */ - int debug ; /**< trick_io(**) */ - - /** Toggle to enable/disable this variable server thread.\n */ - bool enabled ; /**< trick_io(**) */ - - ConnectionStatus connection_status ; /**< trick_io(**) */ - pthread_mutex_t connection_status_mutex; /**< trick_io(**) */ - pthread_cond_t connection_status_cv; /**< trick_io(**) */ - - /** The mutex pauses all processing during checkpoint restart */ - pthread_mutex_t restart_pause ; /**< trick_io(**) */ - - // bool pause_cmd; - bool saved_pause_cmd; - - /** The simulation time converted to seconds\n */ - double time ; /**< trick_units(s) */ - - /** Toggle to tell variable server to send data multicast or point to point.\n */ - bool multicast ; /**< trick_io(**) */ - } ; - - std::ostream& operator<< (std::ostream& s, VariableServerThread& vst); - -} - -#endif - diff --git a/include/trick/var_binary_parser.hh b/include/trick/var_binary_parser.hh index 17a6c4e8..1203c849 100644 --- a/include/trick/var_binary_parser.hh +++ b/include/trick/var_binary_parser.hh @@ -112,7 +112,6 @@ class ParsedBinaryMessage { void combine (const ParsedBinaryMessage& message); int parse (const std::vector& bytes); - int parse (char * raw_bytes); int getMessageType() const; unsigned int getMessageSize() const; diff --git a/share/trick/pymods/trick/tests/test_variable_server.py b/share/trick/pymods/trick/tests/test_variable_server.py index 9ac8dc68..ef72c47d 100644 --- a/share/trick/pymods/trick/tests/test_variable_server.py +++ b/share/trick/pymods/trick/tests/test_variable_server.py @@ -110,7 +110,7 @@ class TestVariableServer(unittest.TestCase): def test_set_period(self): self.variable_server.set_period(10) - # We would like to verify that VariableServerThread::update_rate + # We would like to verify that VariableServerSessionThread::update_rate # was modified, but variable server threads are not registered # with the memory manager, so we can't. @@ -137,38 +137,38 @@ class TestVariableServer(unittest.TestCase): def test_pause(self): self.variable_server.pause(True) self.variable_server.pause(False) - # We would like to verify that VariableServerThread::pause_cmd + # We would like to verify that VariableServerSessionThread::pause_cmd # was modified, but variable server threads are not registered # with the memory manager, so we can't. def test_set_debug(self): self.variable_server.set_debug(3) - # We would like to verify that VariableServerThread::debug + # We would like to verify that VariableServerSessionThread::debug # was modified, but variable server threads are not registered # with the memory manager, so we can't. def test_set_tag(self): self.variable_server.set_tag('test') # We would like to verify that - # VariableServerThread::connection.client_tag was modified, but + # VariableServerSessionThread::connection.client_tag was modified, but # variable server threads are not registered with the memory # manager, so we can't. def test_set_copy_mode(self): self.variable_server.set_copy_mode() - # We would like to verify that VariableServerThread::copy_mode + # We would like to verify that VariableServerSessionThread::copy_mode # was modified, but variable server threads are not registered # with the memory manager, so we can't. def test_send_on_copy(self): self.variable_server.send_on_copy() - # We would like to verify that VariableServerThread::write_mode + # We would like to verify that VariableServerSessionThread::write_mode # was modified, but variable server threads are not registered # with the memory manager, so we can't. def test_validate_addresses(self): self.variable_server.validate_addresses() - # We would like to verify that VariableServerThread::validate_addresses + # We would like to verify that VariableServerSessionThread::validate_addresses # was modified, but variable server threads are not registered # with the memory manager, so we can't. diff --git a/share/trick/sim_objects/default_trick_sys.sm b/share/trick/sim_objects/default_trick_sys.sm index 9b7a871e..4c57d5cf 100644 --- a/share/trick/sim_objects/default_trick_sys.sm +++ b/share/trick/sim_objects/default_trick_sys.sm @@ -456,22 +456,22 @@ class VariableServerSimObject : public Trick::SimObject { {TRK} ("preload_checkpoint") vs.suspendPreCheckpointReload(); {TRK} ("restart") vs.restart(); {TRK} ("restart") vs.resumePostCheckpointReload(); - {TRK} ("top_of_frame") vs.copy_data_top() ; - {TRK} ("automatic_last") vs.copy_data_scheduled() ; + {TRK} ("top_of_frame") vs.copy_and_write_top() ; + {TRK} ("automatic_last") vs.copy_and_write_scheduled() ; {TRK} ("freeze_init") vs.freeze_init() ; - {TRK} ("freeze_automatic") vs.copy_data_freeze_scheduled() ; - {TRK} ("freeze") vs.copy_data_freeze() ; + {TRK} ("freeze_automatic") vs.copy_and_write_freeze_scheduled() ; + {TRK} ("freeze") vs.copy_and_write_freeze() ; {TRK} ("shutdown") vs.shutdown() ; - // Set the static VariableServer pointer in the VariableServerThread class. - Trick::VariableServerThread::set_vs_ptr(&vs) ; + // Set the static VariableServer pointer in the VariableServerSessionThread class. + Trick::VariableServerSessionThread::set_vs_ptr(&vs) ; // get the process_event job and set it in the event processor. - vs.set_copy_data_job(get_job("vs.copy_data_scheduled")) ; - vs.set_copy_data_freeze_job(get_job("vs.copy_data_freeze_scheduled")) ; + vs.set_copy_data_job(get_job("vs.copy_and_write_scheduled")) ; + vs.set_copy_and_write_freeze_job(get_job("vs.copy_and_write_freeze_scheduled")) ; } private: diff --git a/share/trick/trickops/WorkflowCommon.py b/share/trick/trickops/WorkflowCommon.py index 6b164cc4..8b8e0e86 100644 --- a/share/trick/trickops/WorkflowCommon.py +++ b/share/trick/trickops/WorkflowCommon.py @@ -427,7 +427,7 @@ class Job(object): by sending them the SIGKILL signal. """ try: - os.killpg(os.getpgid(self._process.pid), signal.SIGKILL) + os.killpg(os.getpgid(self._process.pid), signal.SIGABRT) self._process.wait() except: pass diff --git a/test/.gitignore b/test/.gitignore index f3d79c4a..2cb1c8bc 100644 --- a/test/.gitignore +++ b/test/.gitignore @@ -27,3 +27,4 @@ build S_sie.json *.ckpnt MonteCarlo_Meta_data_output +*.xml \ No newline at end of file diff --git a/test/SIM_test_varserv/RUN_test/err1_test.py b/test/SIM_test_varserv/RUN_test/err1_test.py new file mode 100644 index 00000000..ca34b4e7 --- /dev/null +++ b/test/SIM_test_varserv/RUN_test/err1_test.py @@ -0,0 +1,24 @@ +import trick + +from trick.unit_test import * + +def main(): + + trick.var_server_set_port(40000) + trick.var_ascii() + trick.real_time_enable() + trick.exec_set_software_frame(0.01) + + trick.exec_set_terminate_time(1000.0) + trick.var_server_set_source_address('localhost') + + varServerPort = trick.var_server_get_port() + test_output = ( os.getenv("TRICK_HOME") + "/trick_test/SIM_test_varserv.xml" ) + command = 'os.system("./models/test_client/test_client_err1 ' + str(varServerPort) + ' --gtest_output=xml:' + test_output + ' &")' + + # Start the test client after everything has been initialized (hopefully) + trick.add_read(1.0, command) + +if __name__ == "__main__": + main() + diff --git a/test/SIM_test_varserv/RUN_test/err2_test.py b/test/SIM_test_varserv/RUN_test/err2_test.py new file mode 100644 index 00000000..e5ce5927 --- /dev/null +++ b/test/SIM_test_varserv/RUN_test/err2_test.py @@ -0,0 +1,24 @@ +import trick + +from trick.unit_test import * + +def main(): + + trick.var_server_set_port(40000) + trick.var_ascii() + trick.real_time_enable() + trick.exec_set_software_frame(0.01) + + trick.exec_set_terminate_time(1000.0) + trick.var_server_set_source_address('localhost') + + varServerPort = trick.var_server_get_port() + test_output = ( os.getenv("TRICK_HOME") + "/trick_test/SIM_test_varserv.xml" ) + command = 'os.system("./models/test_client/test_client_err2 ' + str(varServerPort) + ' --gtest_output=xml:' + test_output + ' &")' + + # Start the test client after everything has been initialized (hopefully) + trick.add_read(1.0, command) + +if __name__ == "__main__": + main() + diff --git a/test/SIM_test_varserv/RUN_test/manual_test.py b/test/SIM_test_varserv/RUN_test/manual_test.py index 982de3ce..ac7013fa 100644 --- a/test/SIM_test_varserv/RUN_test/manual_test.py +++ b/test/SIM_test_varserv/RUN_test/manual_test.py @@ -5,7 +5,7 @@ from trick.unit_test import * def main(): - trick.var_server_set_port(4000) + trick.var_server_set_port(40000) trick.var_ascii() trick.real_time_enable() trick.exec_set_software_frame(0.01) diff --git a/test/SIM_test_varserv/RUN_test/unit_test.py b/test/SIM_test_varserv/RUN_test/unit_test.py index afd494eb..335dba25 100644 --- a/test/SIM_test_varserv/RUN_test/unit_test.py +++ b/test/SIM_test_varserv/RUN_test/unit_test.py @@ -1,5 +1,4 @@ import trick -import socket from trick.unit_test import * @@ -11,9 +10,11 @@ def main(): trick.exec_set_software_frame(0.01) # trick.set_var_server_info_msg_on() - trick.var_server_create_tcp_socket('localhost', 49000) - trick.var_server_create_udp_socket('', 48000) - trick.var_server_create_multicast_socket('224.10.10.10','', 47000) + hostname = trick.var_server_get_hostname() + + trick.var_server_create_tcp_socket(hostname, 49000) + trick.var_server_create_udp_socket(hostname, 48000) + trick.var_server_create_multicast_socket('224.10.10.10',hostname, 47000) trick.exec_set_terminate_time(100.0) diff --git a/test/SIM_test_varserv/S_overrides.mk b/test/SIM_test_varserv/S_overrides.mk index 1157f7b9..96897111 100644 --- a/test/SIM_test_varserv/S_overrides.mk +++ b/test/SIM_test_varserv/S_overrides.mk @@ -2,7 +2,7 @@ TRICK_CFLAGS += -I./models TRICK_CXXFLAGS += -I./models -all: test_client +all: test_client test_client_err1 test_client_err2 clean: clean_test_client TEST_CLIENT_LIBS += -L${GTEST_HOME}/lib64 -L${GTEST_HOME}/lib -lgtest -lgtest_main -lpthread -L${TRICK_LIB_DIR} -ltrick_var_binary_parser @@ -10,6 +10,12 @@ TEST_CLIENT_LIBS += -L${GTEST_HOME}/lib64 -L${GTEST_HOME}/lib -lgtest -lgtest_ma test_client: models/test_client/test_client.cpp cd models/test_client; $(TRICK_CXX) test_client.cpp -o test_client $(TRICK_CXXFLAGS) $(TRICK_SYSTEM_CXXFLAGS) $(TRICK_TEST_FLAGS) -Wno-write-strings -Wno-sign-compare -I$(TRICK_HOME)/include $(TEST_CLIENT_LIBS) +test_client_err1: models/test_client/test_client_err1.cpp + cd models/test_client; $(TRICK_CXX) test_client_err1.cpp -o test_client_err1 $(TRICK_CXXFLAGS) $(TRICK_SYSTEM_CXXFLAGS) $(TRICK_TEST_FLAGS) -Wno-write-strings -Wno-sign-compare -I$(TRICK_HOME)/include $(TEST_CLIENT_LIBS) + +test_client_err2: models/test_client/test_client_err2.cpp + cd models/test_client; $(TRICK_CXX) test_client_err2.cpp -o test_client_err2 $(TRICK_CXXFLAGS) $(TRICK_SYSTEM_CXXFLAGS) $(TRICK_TEST_FLAGS) -Wno-write-strings -Wno-sign-compare -I$(TRICK_HOME)/include $(TEST_CLIENT_LIBS) + clean_test_client: - rm -f models/test_client/test_client + rm -f models/test_client/test_client models/test_client/test_client_err1 models/test_client/test_client_err2 diff --git a/test/SIM_test_varserv/file_to_send.txt b/test/SIM_test_varserv/file_to_send.txt new file mode 100644 index 00000000..b34b754c --- /dev/null +++ b/test/SIM_test_varserv/file_to_send.txt @@ -0,0 +1 @@ +hi hello world \ No newline at end of file diff --git a/test/SIM_test_varserv/models/test_client/.gitignore b/test/SIM_test_varserv/models/test_client/.gitignore index 837a5b2a..25864a77 100644 --- a/test/SIM_test_varserv/models/test_client/.gitignore +++ b/test/SIM_test_varserv/models/test_client/.gitignore @@ -1 +1,3 @@ -test_client \ No newline at end of file +test_client +test_client_err1 +test_client_err2 \ No newline at end of file diff --git a/test/SIM_test_varserv/models/test_client/test_client.cpp b/test/SIM_test_varserv/models/test_client/test_client.cpp index 6d172d39..3d30d9ba 100644 --- a/test/SIM_test_varserv/models/test_client/test_client.cpp +++ b/test/SIM_test_varserv/models/test_client/test_client.cpp @@ -1,222 +1,7 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - +#include "test_client.hh" #include "trick/var_binary_parser.hh" +#include -#define DOUBLE_TOL 1e-5 -#define SOCKET_BUF_SIZE 204800 - -// Pretend that gtest was kind enough to provide an EXPECT_FEQ operator with a tolerance -#define EXPECT_FEQ(a,b) EXPECT_LE(fabs(a - b), DOUBLE_TOL) - -class Socket { - - public: - - int max_retries = 10; - - Socket() : _initialized(false) {} - int init(std::string hostname, int port, int mode = SOCK_STREAM) { - _multicast_socket = false; - - _hostname = hostname; - _port = port; - int tries = 0; - - while ((_socket_fd = socket(AF_INET, mode, 0)) < 0 && tries < max_retries) tries++; - - if (_socket_fd < 0) { - std::cout << "Socket connection failed" << std::endl; - return -1; - } - - struct sockaddr_in serv_addr; - serv_addr.sin_family = AF_INET; - serv_addr.sin_port = htons(port); // convert to weird network byte format - - if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0) { - std::cout << "Invalid address/ Address not supported" << std::endl; - return -1; - } - - tries = 0; - int connection_status; - - while ((connection_status = connect(_socket_fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))) < 0 && tries < max_retries) tries++; - - if (connection_status < 0) { - std::cout << "Connection failed" << std::endl; - return -1; - } - - _initialized = true; - - return 0; - } - - #ifndef __APPLE__ - int init_multicast (std::string hostname, int port) { - _multicast_socket = true; - _hostname = hostname; - _port = port; - int tries = 0; - - while ((_socket_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 && tries < max_retries) tries++; - - if (_socket_fd < 0) { - std::cout << "init_multicast: Socket open failed" << std::endl; - return -1; - } - - int value = 1; - if (setsockopt(_socket_fd, SOL_SOCKET, SO_REUSEADDR, (char *) &value, (socklen_t) sizeof(value)) < 0) { - std::cout << "init_multicast: Socket option failed" << std::endl; - return -1; - } - - struct ip_mreq mreq; - // Use setsockopt() to request that the kernel join a multicast group - mreq.imr_multiaddr.s_addr = inet_addr(_hostname.c_str()); - mreq.imr_interface.s_addr = htonl(INADDR_ANY); - if (setsockopt(_socket_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &mreq, (socklen_t) sizeof(mreq)) < 0) { - std::cout << "init_multicast: setsockopt: ip_add_membership failed" << std::endl; - return -1; - } - - - struct sockaddr_in sockin ; - - // Set up destination address - sockin.sin_family = AF_INET; - sockin.sin_addr.s_addr = htonl(INADDR_ANY); - sockin.sin_port = htons(_port); - - if ( bind(_socket_fd, (struct sockaddr *) &sockin, (socklen_t) sizeof(sockin)) < 0 ) { - std::cout << "init_multicast: bind failed" << std::endl; - return -1; - } - - _initialized = true; - return 0; - } - - #endif - - int send (std::string message) { - // weird syntax I've never used before - since the send syscall that i'm trying to use is overloaded in this class, - // I have to append :: to the front of it so that the compiler knows to look in the global namespace - int success = ::send(_socket_fd, message.c_str(), message.size(), 0); - if (success < message.size()) { - std::cout << "init_multicast: Failed to send message" << std::endl; - } - return success; - } - - int operator<< (std::string message) { - return send(message); - } - - std::string receive () { - char buffer[SOCKET_BUF_SIZE]; - int numBytes = recv(_socket_fd, buffer, SOCKET_BUF_SIZE, 0); - if (numBytes < 0) { - } else if (numBytes < SOCKET_BUF_SIZE) { - buffer[numBytes] = '\0'; - } - - return std::string(buffer); - } - - void operator>> (std::string& ret) { - ret = receive(); - } - - std::vector receive_bytes() { - unsigned char buffer[SOCKET_BUF_SIZE]; - int numBytes = recv(_socket_fd, buffer, SOCKET_BUF_SIZE, 0); - if (numBytes < 0) { - std::cout << "init_multicast: Failed to read from socket" << std::endl; - } - - std::vector bytes; - for (int i = 0; i < numBytes; i++) { - bytes.push_back(buffer[i]); - } - - return bytes; - } - - bool check_for_message_availible(long timeout_sec = 2) { - fd_set read_fd_set; - struct timeval timeout = { .tv_sec = timeout_sec, .tv_usec = 0 }; - FD_ZERO(&read_fd_set); - FD_SET(_socket_fd, &read_fd_set); - - // I have one question for the designers of the interface for this syscall: why - select(_socket_fd+1, &read_fd_set, NULL, NULL, &timeout); - - return FD_ISSET(_socket_fd, &read_fd_set); - } - - void clear_buffered_data() { - // Poll the socket - if (check_for_message_availible(0)) { - // Receive into the void if there is a message - receive(); - } - // otherwise no worries - } - - void close() { - ::close(_socket_fd); - } - - private: - int _port; - std::string _hostname; - int _socket_fd; - bool _initialized; - bool _multicast_socket; - -}; - -class VariableServerTest : public ::testing::Test { - protected: - VariableServerTest() { - socket_status = socket.init("localhost", 40000); - if (socket_status == 0) { - std::stringstream request; - request << "trick.var_set_client_tag(\"VSTest"; - request << numSession++; - request << "\") \n"; - - socket << request.str(); - } - } - ~VariableServerTest() { - socket << "trick.var_exit()\n"; - socket.close(); - } - - Socket socket; - int socket_status; - - static int numSession; -}; class VariableServerUDPTest : public ::testing::Test { protected: @@ -271,7 +56,36 @@ int VariableServerTest::numSession = 0; int VariableServerUDPTest::numSession = 0; int VariableServerTestAltListener::numSession = 0; +#ifndef __APPLE__ +class VariableServerTestMulticast : public ::testing::Test { + protected: + VariableServerTestMulticast() { + socket_status = socket.init("", 47000, SOCK_DGRAM); + socket_status = multicast_listener.init_multicast("224.10.10.10", 47000); + if (socket_status == 0) { + std::stringstream request; + request << "trick.var_set_client_tag(\"multicast_VSTest"; + request << numSession++; + request << "\") \n"; + + socket << request.str(); + } + } + ~VariableServerTestMulticast() { + socket.close(); + multicast_listener.close(); + } + + Socket socket; + Socket multicast_listener; + + int socket_status; + + static int numSession; +}; +int VariableServerTestMulticast::numSession = 0; +#endif /**********************************************************/ /* Helpful constants and functions */ @@ -345,6 +159,97 @@ void load_checkpoint (Socket& socket, const std::string& checkpoint_name) { socket << "trick.var_unpause()\n"; } +/*****************************************/ +/* Multicast Test */ +/*****************************************/ + +#ifndef __APPLE__ + +TEST_F (VariableServerTestMulticast, Strings) { + if (socket_status != 0) { + FAIL(); + } + + std::string reply; + socket << "trick.var_send_once(\"vsx.vst.o\")\n"; + std::string expected("5\tYou will rejoice to hear that no disaster has accompanied the commencement of an enterprise which you have regarded with such evil forebodings. I arrived here yesterday, and my first task is to assure my dear sister of my welfare and increasing confidence in the success of my undertaking."); + + multicast_listener >> reply; + + EXPECT_EQ(strcmp_IgnoringWhiteSpace(reply, expected), 0); + + expected = std::string("5\tI am already far north of London, and as I walk in the streets of Petersburgh, I feel a cold northern breeze play upon my cheeks, which braces my nerves and fills me with delight. Do you understand this feeling?"); + socket << "trick.var_send_once(\"vsx.vst.p\")\n"; + + multicast_listener >> reply; + + EXPECT_EQ(strcmp_IgnoringWhiteSpace(reply, expected), 0); +} + + +TEST_F (VariableServerTestMulticast, AddRemove) { + if (socket_status != 0) { + FAIL(); + } + + std::string reply; + std::string expected; + + int max_tries = 3; + int tries = 0; + + socket << "trick.var_add(\"vsx.vst.c\")\n"; + multicast_listener >> reply; + expected = std::string("0 -1234"); + + tries = 0; + while (strcmp_IgnoringWhiteSpace(reply, expected) != 0 && tries++ < max_tries) { + multicast_listener >> reply; + } + + EXPECT_EQ(strcmp_IgnoringWhiteSpace(reply, expected), 0) << "Expected: " << expected << "\tAcutal: " << reply; + + multicast_listener >> reply; + tries = 0; + while (strcmp_IgnoringWhiteSpace(reply, expected) != 0 && tries++ < max_tries) { + multicast_listener >> reply; + } + + EXPECT_EQ(strcmp_IgnoringWhiteSpace(reply, expected), 0) << "Expected: " << expected << "\tAcutal: " << reply; + + socket << "trick.var_add(\"vsx.vst.m\")\n"; + multicast_listener >> reply; + expected = std::string("0 -1234 1"); + tries = 0; + while (strcmp_IgnoringWhiteSpace(reply, expected) != 0 && tries++ < max_tries) { + multicast_listener >> reply; + } + EXPECT_EQ(strcmp_IgnoringWhiteSpace(reply, expected), 0) << "Expected: " << expected << "\tAcutal: " << reply; + + socket << "trick.var_remove(\"vsx.vst.m\")\n"; + multicast_listener >> reply; + expected = std::string("0 -1234"); + tries = 0; + while (strcmp_IgnoringWhiteSpace(reply, expected) != 0 && tries++ < max_tries) { + multicast_listener >> reply; + } + + EXPECT_EQ(strcmp_IgnoringWhiteSpace(reply, expected), 0) << "Expected: " << expected << "\tAcutal: " << reply; + + socket << "trick.var_add(\"vsx.vst.n\")\n"; + multicast_listener >> reply; + expected = std::string("0 -1234 0,1,2,3,4"); + tries = 0; + while (strcmp_IgnoringWhiteSpace(reply, expected) != 0 && tries++ < max_tries) { + multicast_listener >> reply; + } + EXPECT_EQ(strcmp_IgnoringWhiteSpace(reply, expected), 0) << "Expected: " << expected << "\tAcutal: " << reply; + + socket << "trick.var_exit()\n"; +} + +#endif + /************************************/ /* UDP Tests */ /************************************/ @@ -501,7 +406,7 @@ TEST_F (VariableServerTestAltListener, RestartAndSet) { load_checkpoint(socket, "RUN_test/reload_file.ckpnt"); socket >> reply; - expected = std::string("0\t-1234\t-123456\n"); + expected = std::string("0\t-1234\t-123456 {m}\n"); EXPECT_EQ(reply, expected); } @@ -658,6 +563,18 @@ TEST_F (VariableServerTest, Units) { EXPECT_EQ(strcmp_IgnoringWhiteSpace(reply, expected), 0); } +TEST_F (VariableServerTest, dump_info) { + if (socket_status != 0) { + FAIL(); + } + socket << "trick.var_add(\"vsx.vst.c\")\n"; + + std::string command = "trick.var_server_list_connections()\n"; + socket << command; + sleep(1); +} + + TEST_F (VariableServerTest, SendOnce) { if (socket_status != 0) { FAIL(); @@ -764,6 +681,153 @@ TEST_F (VariableServerTest, ListSize) { } +TEST_F (VariableServerTest, bitfields) { + if (socket_status != 0) { + FAIL(); + } + + socket << "trick.var_send_once(\"vsx.vst.my_bitfield.var1,vsx.vst.my_bitfield.var2,vsx.vst.my_bitfield.var3,vsx.vst.my_bitfield.var4\", 4)\n"; + std::string reply_str; + socket >> reply_str; + + EXPECT_EQ(strcmp_IgnoringWhiteSpace(reply_str, "5 3 -128 0 2112"), 0); + + socket << "trick.var_binary()\ntrick.var_send_once(\"vsx.vst.my_bitfield.var1,vsx.vst.my_bitfield.var2,vsx.vst.my_bitfield.var3,vsx.vst.my_bitfield.var4\", 4)\n"; + + std::vector reply = socket.receive_bytes(); + + ParsedBinaryMessage message; + try { + message.parse(reply); + } catch (const MalformedMessageException& ex) { + FAIL() << "Parser threw an exception: " << ex.what(); + } + + for (int i = 0; i < message.getNumVars(); i++) { + EXPECT_TRUE(message.variables[i].getType() == 12 || message.variables[i].getType() == 13); + } +} + +TEST_F (VariableServerTest, transmit_file) { + if (socket_status != 0) { + FAIL(); + } + + socket << "trick.send_file(\"file_to_send.txt\")\n"; + + std::stringstream file_contents; + + while (socket.check_for_message_availible()) { + socket >> file_contents; + } + + std::ifstream actual_file("file_to_send.txt"); + std::string expected_contents; + getline(actual_file, expected_contents); + + std::string expected_header = "2\t" + std::to_string(expected_contents.size()); + + std::string test_line; + + std::getline(file_contents, test_line); + EXPECT_EQ(test_line, expected_header); + + std::getline(file_contents, test_line); + EXPECT_EQ(test_line, expected_contents); +} + + +TEST_F (VariableServerTest, SendSieResource) { + if (socket_status != 0) { + FAIL(); + } + + socket << "trick.send_sie_resource()\n"; + + std::stringstream received_file; + + while (socket.check_for_message_availible()) { + socket >> received_file; + } + + std::string first_line; + std::getline(received_file, first_line); + + // We're not gonna bother comparing the contents + // Just check to see that the message type is correct + ASSERT_EQ(first_line.at(0), '2'); +} + +TEST_F (VariableServerTest, SendSieClass) { + if (socket_status != 0) { + FAIL(); + } + + socket << "trick.send_sie_class()\n"; + + std::stringstream received_file; + std::string file_temp; + + while (socket.check_for_message_availible()) { + socket >> file_temp; + received_file << file_temp; + } + + std::string first_line; + std::getline(received_file, first_line); + + // We're not gonna bother comparing the contents + // Just check to see that the message type is correct + ASSERT_EQ(first_line.at(0), '2'); +} + +TEST_F (VariableServerTest, SendSieEnum) { + if (socket_status != 0) { + FAIL(); + } + + socket << "trick.send_sie_enum()\n"; + + std::stringstream received_file; + std::string file_temp; + + while (socket.check_for_message_availible()) { + socket >> file_temp; + received_file << file_temp; + } + + std::string first_line; + std::getline(received_file, first_line); + + // We're not gonna bother comparing the contents + // Just check to see that the message type is correct + ASSERT_EQ(first_line.at(0), '2'); +} + + +TEST_F (VariableServerTest, SendSieClassTopLevelObjects) { + if (socket_status != 0) { + FAIL(); + } + + socket << "trick.send_sie_top_level_objects()\n"; + + std::stringstream received_file; + std::string file_temp; + + while (socket.check_for_message_availible()) { + socket >> file_temp; + received_file << file_temp; + } + + std::string first_line; + std::getline(received_file, first_line); + + // We're not gonna bother comparing the contents + // Just check to see that the message type is correct + ASSERT_EQ(first_line.at(0), '2'); +} + TEST_F (VariableServerTest, RestartAndSet) { if (socket_status != 0) { FAIL(); @@ -793,7 +857,7 @@ TEST_F (VariableServerTest, RestartAndSet) { load_checkpoint(socket, "RUN_test/reload_file.ckpnt"); socket >> reply; - expected = std::string("0\t-1234\t-123456\n"); + expected = std::string("0\t-1234\t-123456 {m}\n"); EXPECT_EQ(reply, expected); } @@ -1017,7 +1081,28 @@ TEST_F (VariableServerTest, Freeze) { // Sim mode should be MODE_FREEZE == 1 ASSERT_EQ(mode, MODE_FREEZE); - // Not sure what else to test in copy mode VS_COPY_ASYNC + // Set to back to run mode + socket << "trick.exec_run()\n"; + + // The mode takes a little bit of time to change + wait_for_mode_change(MODE_RUN); + + // Sim mode should be MODE_RUN + ASSERT_EQ(mode, MODE_RUN); + + // Test VS_COPY_SCHEDULED - should see 1 message per frame + socket << "trick.var_set_copy_mode(1)\n"; + + + // Change back to freeze mode + // Set to Freeze mode + socket << "trick.exec_freeze()\n"; + + // The mode takes a little bit of time to change + wait_for_mode_change(MODE_FREEZE); + + // Sim mode should be MODE_FREEZE == 1 + ASSERT_EQ(mode, MODE_FREEZE); // Test VS_COPY_SCHEDULED - should see 1 message per frame socket << "trick.var_set_copy_mode(1)\n"; @@ -1104,24 +1189,26 @@ TEST_F (VariableServerTest, CopyAndWriteModes) { socket.clear_buffered_data(); // Copy mode 1 (VS_COPY_SCHEDULED) write mode 0 (VS_WRITE_ASYNC) - command = "trick.var_clear()\n" + test_vars_command + "trick.var_set_copy_mode(1)\ntrick.var_add(\"vsx.vst.c\")\ntrick.var_add(\"vsx.vst.d\")\ntrick.var_unpause()\n"; + command = "trick.var_clear()\n" + test_vars_command + "trick.var_sync(1)\ntrick.var_add(\"vsx.vst.c\")\ntrick.var_add(\"vsx.vst.d\")\ntrick.var_unpause()\n"; socket << command; // With copy mode VS_COPY_SCHEDULED and write mode VS_WRITE_ASYNC, the first reply will be all 0 since the main time to copy has not occurred yet. // Is this what we want? Maybe we should have more strict communication on whether the data has been staged so the first message isn't incorrect - // spin(); + + spin(socket); expected = "-1234 1234"; parse_message(socket.receive()); EXPECT_EQ(strcmp_IgnoringWhiteSpace(vars, expected), 0) << "Received: " << vars << " Expected: " << expected; + // Test that we see a difference of exactly expected_cycle (with a tolerance for floating point issues) int prev_frame = 0; double prev_time = 0; for (int i = 0; i < num_tests; i++) { prev_time = sim_time; parse_message(socket.receive()); - EXPECT_FEQ(sim_time - prev_time, expected_cycle); + EXPECT_LT(fmod(sim_time - prev_time, expected_cycle), DOUBLE_TOL); EXPECT_EQ(strcmp_IgnoringWhiteSpace(vars, expected), 0) << "Received: " << vars << " Expected: " << expected; } @@ -1146,7 +1233,7 @@ TEST_F (VariableServerTest, CopyAndWriteModes) { for (int i = 0; i < num_tests; i++) { prev_time = sim_time; parse_message(socket.receive()); - EXPECT_FEQ(sim_time - prev_time, expected_cycle); + EXPECT_LT(fmod(sim_time - prev_time, expected_cycle), DOUBLE_TOL); EXPECT_EQ(strcmp_IgnoringWhiteSpace(vars, expected), 0) << "Received: " << vars << " Expected: " << expected; } @@ -1210,6 +1297,27 @@ TEST_F (VariableServerTest, CopyAndWriteModes) { socket.clear_buffered_data(); } +TEST_F (VariableServerTest, var_set) { + if (socket_status != 0) { + FAIL(); + } + + std::string reply; + std::string expected; + + socket << "trick.var_add(\"vsx.vst.c\")\ntrick.var_add(\"vsx.vst.blocked_from_output\")\ntrick.var_add(\"vsx.vst.blocked_from_input\")\n"; + + socket >> reply; + expected = std::string("0\t-1234\t0\t500\n"); + + EXPECT_EQ(reply, expected); + + socket << "trick.var_set(\"vsx.vst.blocked_from_input\", 0)\n"; + socket << "trick.var_set(\"vsx.vst.blocked_from_output\", 0)\n"; + + +} + bool getCompleteBinaryMessage(ParsedBinaryMessage& message, Socket& socket, bool print = false) { static const int max_retries = 5; diff --git a/test/SIM_test_varserv/models/test_client/test_client.hh b/test/SIM_test_varserv/models/test_client/test_client.hh new file mode 100644 index 00000000..50e66b5b --- /dev/null +++ b/test/SIM_test_varserv/models/test_client/test_client.hh @@ -0,0 +1,238 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + + +#define DOUBLE_TOL 1e-5 +#define SOCKET_BUF_SIZE 204800 + +// Pretend that gtest was kind enough to provide an EXPECT_FEQ operator with a tolerance +#define EXPECT_FEQ(a,b) EXPECT_LE(fabs(a - b), DOUBLE_TOL) + +class Socket { + + public: + + int max_retries = 10; + + Socket() : _initialized(false) {} + int init(std::string hostname, int port, int mode = SOCK_STREAM) { + _multicast_socket = false; + + _hostname = hostname; + _port = port; + int tries = 0; + + _socket_fd = socket(AF_INET, mode, 0); + if (_socket_fd < 0) { + std::cout << "Socket connection failed" << std::endl; + return -1; + } + + int value = 1; + if (setsockopt(_socket_fd, SOL_SOCKET, SO_REUSEADDR, (char *) &value, (socklen_t) sizeof(value)) < 0) { + std::cout << "init_multicast: Socket option failed" << std::endl; + return -1; + } + + struct sockaddr_in serv_addr; + serv_addr.sin_family = AF_INET; + serv_addr.sin_port = htons(port); // convert to weird network byte format + + if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0) { + std::cout << "Invalid address/ Address not supported" << std::endl; + return -1; + } + + tries = 0; + int connection_status; + + connection_status = connect(_socket_fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)); + if (connection_status < 0) { + std::cout << "Connection failed" << std::endl; + return -1; + } + + _initialized = true; + + return 0; + } + #ifndef __APPLE__ + int init_multicast (std::string hostname, int port) { + _multicast_socket = true; + _hostname = hostname; + _port = port; + int tries = 0; + + _socket_fd = socket(AF_INET, SOCK_DGRAM, 0); + if (_socket_fd < 0) { + std::cout << "init_multicast: Socket open failed" << std::endl; + return -1; + } + + int value = 1; + if (setsockopt(_socket_fd, SOL_SOCKET, SO_REUSEADDR, (char *) &value, (socklen_t) sizeof(value)) < 0) { + std::cout << "init_multicast: Socket option failed" << std::endl; + return -1; + } + + if (setsockopt(_socket_fd, SOL_SOCKET, SO_REUSEPORT, (char *) &value, sizeof(value)) < 0) { + perror("setsockopt: reuseport"); + } + + struct ip_mreq mreq; + // Use setsockopt() to request that the kernel join a multicast group + mreq.imr_multiaddr.s_addr = inet_addr(_hostname.c_str()); + mreq.imr_interface.s_addr = htonl(INADDR_ANY); + if (setsockopt(_socket_fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &mreq, (socklen_t) sizeof(mreq)) < 0) { + std::cout << "init_multicast: setsockopt: ip_add_membership failed" << std::endl; + return -1; + } + + + struct sockaddr_in sockin ; + + // Set up local interface + // We must bind to the multicast address + sockin.sin_family = AF_INET; + sockin.sin_addr.s_addr = inet_addr(_hostname.c_str()); + sockin.sin_port = htons(_port); + + if ( bind(_socket_fd, (struct sockaddr *) &sockin, (socklen_t) sizeof(sockin)) < 0 ) { + std::cout << "init_multicast: bind failed" << std::endl; + return -1; + } + + char loopch = 1; + + if(setsockopt(_socket_fd, IPPROTO_IP, IP_MULTICAST_LOOP, (char *)&loopch, sizeof(loopch)) < 0) + { + perror("Setting IP_MULTICAST_LOOP error"); + return -1; + } + + _initialized = true; + return 0; + } + + #endif + + int send (std::string message) { + // weird syntax I've never used before - since the send syscall that i'm trying to use is overloaded in this class, + // I have to append :: to the front of it so that the compiler knows to look in the global namespace + int success = ::send(_socket_fd, message.c_str(), message.size(), 0); + if (success < message.size()) { + std::cout << "init_multicast: Failed to send message" << std::endl; + } + return success; + } + + int operator<< (std::string message) { + return send(message); + } + + std::string receive () { + char buffer[SOCKET_BUF_SIZE]; + int numBytes = recv(_socket_fd, buffer, SOCKET_BUF_SIZE, 0); + if (numBytes < 0) { + } else if (numBytes < SOCKET_BUF_SIZE) { + buffer[numBytes] = '\0'; + } + + return std::string(buffer); + } + + void operator>> (std::string& ret) { + ret = receive(); + } + + void operator>> (std::ostream& stream) { + stream << receive(); + } + + std::vector receive_bytes() { + unsigned char buffer[SOCKET_BUF_SIZE]; + int numBytes = recv(_socket_fd, buffer, SOCKET_BUF_SIZE, 0); + if (numBytes < 0) { + std::cout << "init_multicast: Failed to read from socket" << std::endl; + } + + std::vector bytes; + for (int i = 0; i < numBytes; i++) { + bytes.push_back(buffer[i]); + } + + return bytes; + } + + bool check_for_message_availible(long timeout_sec = 2) { + fd_set read_fd_set; + struct timeval timeout = { .tv_sec = timeout_sec, .tv_usec = 0 }; + FD_ZERO(&read_fd_set); + FD_SET(_socket_fd, &read_fd_set); + + // I have one question for the designers of the interface for this syscall: why + select(_socket_fd+1, &read_fd_set, NULL, NULL, &timeout); + + return FD_ISSET(_socket_fd, &read_fd_set); + } + + void clear_buffered_data() { + // Poll the socket + if (check_for_message_availible(0)) { + // Receive into the void if there is a message + receive(); + } + // otherwise no worries + } + + void close() { + ::close(_socket_fd); + } + + private: + int _port; + std::string _hostname; + int _socket_fd; + bool _initialized; + bool _multicast_socket; + +}; + +class VariableServerTest : public ::testing::Test { + protected: + VariableServerTest() { + socket_status = socket.init("localhost", 40000); + if (socket_status == 0) { + std::stringstream request; + request << "trick.var_set_client_tag(\"VSTest"; + request << numSession++; + request << "\") \n"; + + socket << request.str(); + } + } + ~VariableServerTest() { + socket << "trick.var_exit()\n"; + socket.close(); + } + + Socket socket; + int socket_status; + + static int numSession; +}; \ No newline at end of file diff --git a/test/SIM_test_varserv/models/test_client/test_client_err1.cpp b/test/SIM_test_varserv/models/test_client/test_client_err1.cpp new file mode 100644 index 00000000..8d4e5d18 --- /dev/null +++ b/test/SIM_test_varserv/models/test_client/test_client_err1.cpp @@ -0,0 +1,13 @@ +#include "test_client.hh" + +int VariableServerTest::numSession = 0; + + +TEST_F (VariableServerTest, SendExecutiveException) { + if (socket_status != 0) { + FAIL(); + } + + std::string command = "trick.exec_terminate_with_return(1, \"" + std::string(__FILE__) + "\", " + std::to_string(__LINE__) + ", \"Error termination for testing\")\n"; + socket << command; +} diff --git a/test/SIM_test_varserv/models/test_client/test_client_err2.cpp b/test/SIM_test_varserv/models/test_client/test_client_err2.cpp new file mode 100644 index 00000000..f32af43d --- /dev/null +++ b/test/SIM_test_varserv/models/test_client/test_client_err2.cpp @@ -0,0 +1,13 @@ +#include "test_client.hh" + +int VariableServerTest::numSession = 0; + + +TEST_F (VariableServerTest, SendStdException) { + if (socket_status != 0) { + FAIL(); + } + + std::string command = "vsx.vst.throw_exception()\n"; + socket << command; +} diff --git a/test/SIM_test_varserv/models/varserv/include/VS.hh b/test/SIM_test_varserv/models/varserv/include/VS.hh index 5aa7c205..270f3a6c 100644 --- a/test/SIM_test_varserv/models/varserv/include/VS.hh +++ b/test/SIM_test_varserv/models/varserv/include/VS.hh @@ -13,13 +13,21 @@ PROGRAMMERS: ( (Lindsay Landry) (L3) (9-12-2013) ) ( (Jackie Dea #ifndef VS_HH #define VS_HH +typedef struct { + unsigned char var1 :3; + short var2 :8; + int var3 :9; + unsigned int var4 :12; +} bitfield; + + class VSTest { public: char a; unsigned char b; short c; unsigned short d; - int e; /* m xy-position */ + int e; /* m xy-position */ unsigned int f; long g; unsigned long h; @@ -31,10 +39,15 @@ class VSTest { int n[5]; std::string o; char * p; - wchar_t * q; /**< trick_chkpnt_io(**) */ + wchar_t * q; /**< trick_chkpnt_io(**) */ + + bitfield my_bitfield; int large_arr[4000]; + int blocked_from_input; /** trick_io(*o) */ + int blocked_from_output; /** trick_io(*i) */ + int status; VSTest(); @@ -48,6 +61,8 @@ class VSTest { int success(); int fail(); + void throw_exception(); + const char *status_messages[3] = { "Variable Server Test Success", "Variable Server Test Failure", diff --git a/test/SIM_test_varserv/models/varserv/src/VS.cpp b/test/SIM_test_varserv/models/varserv/src/VS.cpp index d9d2460f..24a9aa1a 100644 --- a/test/SIM_test_varserv/models/varserv/src/VS.cpp +++ b/test/SIM_test_varserv/models/varserv/src/VS.cpp @@ -8,9 +8,11 @@ PROGRAMMERS: ( (Lindsay Landry) (L3) (9-12-2013) (Jackie Deans) (CACI) (11-30-2022) ) *******************************************************************************/ #include +#include +#include + #include "../include/VS.hh" #include "trick/exec_proto.h" -#include VSTest::VSTest() {} VSTest::~VSTest() {} @@ -40,6 +42,18 @@ int VSTest::default_vars() { for (int i = 0; i < 4000; i++) { large_arr[i] = i; } + + /* Mixed Types */ + // 3,-128,0,2112 + my_bitfield.var1 = (1 << (3 - 1)) - 1; + my_bitfield.var2 = -(1 << (8 - 1)); + my_bitfield.var3 = 0; + my_bitfield.var4 = (1 << (12 - 1)) + (1 << 12/2);; + + + blocked_from_input = 500; + blocked_from_output = 1000; + } int VSTest::init() { @@ -57,8 +71,18 @@ int VSTest::success() { return 0; } +void VSTest::throw_exception() { + throw std::logic_error("Pretend an error has occured for testing"); +} + int VSTest::shutdown() { std::cout << "Shutting down with status: " << status << " Message: " << status_messages[status] << std::endl; + + // Check that the blocked_from_input variable hasnt changed + if (blocked_from_input != 500 || blocked_from_output != 0) { + status = 1; + } + exec_terminate_with_return( status , __FILE__ , __LINE__ , status_messages[status] ) ; return(0); diff --git a/test_sims.yml b/test_sims.yml index 7f708f95..4582b819 100644 --- a/test_sims.yml +++ b/test_sims.yml @@ -312,11 +312,16 @@ SIM_test_varserv: path: test/SIM_test_varserv build_args: "-t" binary: "T_main_{cpu}_test.exe" - labels: - - retries_allowed runs: RUN_test/unit_test.py: + phase: 1 returns: 0 + RUN_test/err1_test.py: + phase: 2 + returns: 10 + RUN_test/err2_test.py: + phase: 3 + returns: 10 SIM_amoeba: path: trick_sims/Cannon/SIM_amoeba build_args: "-t" diff --git a/trick_source/sim_services/CMakeLists.txt b/trick_source/sim_services/CMakeLists.txt index 42a6e895..bce805b4 100644 --- a/trick_source/sim_services/CMakeLists.txt +++ b/trick_source/sim_services/CMakeLists.txt @@ -123,7 +123,7 @@ set( SS_SRC JITInputFile/JITInputFile JITInputFile/jit_input_file_c_intf JSONVariableServer/JSONVariableServer - JSONVariableServer/JSONVariableServerThread + JSONVariableServer/JSONVariableServerSessionThread MasterSlave/MSSharedMem MasterSlave/MSSocket MasterSlave/Master @@ -190,26 +190,26 @@ set( SS_SRC VariableServer/VariableReference VariableServer/VariableServer VariableServer/VariableServerListenThread - VariableServer/VariableServerThread - VariableServer/VariableServerThread_commands - VariableServer/VariableServerThread_connect - VariableServer/VariableServerThread_copy_data - VariableServer/VariableServerThread_copy_sim_data - VariableServer/VariableServerThread_create_socket - VariableServer/VariableServerThread_freeze_init - VariableServer/VariableServerThread_loop - VariableServer/VariableServerThread_restart - VariableServer/VariableServerThread_write_data - VariableServer/VariableServerThread_write_stdio - VariableServer/VariableServer_copy_data_freeze - VariableServer/VariableServer_copy_data_freeze_scheduled - VariableServer/VariableServer_copy_data_scheduled - VariableServer/VariableServer_copy_data_top + VariableServer/VariableServerSessionThread + VariableServer/VariableServerSessionThread_commands + VariableServer/VariableServerSessionThread_connect + VariableServer/VariableServerSessionThread_copy_data + VariableServer/VariableServerSessionThread_copy_sim_data + VariableServer/VariableServerSessionThread_create_socket + VariableServer/VariableServerSessionThread_freeze_init + VariableServer/VariableServerSessionThread_loop + VariableServer/VariableServerSessionThread_restart + VariableServer/VariableServerSessionThread_write_data + VariableServer/VariableServerSessionThread_write_stdio + VariableServer/VariableServer_copy_and_write_freeze + VariableServer/VariableServer_copy_and_write_freeze_scheduled + VariableServer/VariableServer_copy_and_write_scheduled + VariableServer/VariableServer_copy_and_write_top VariableServer/VariableServer_default_data VariableServer/VariableServer_freeze_init VariableServer/VariableServer_get_next_freeze_call_time VariableServer/VariableServer_get_next_sync_call_time - VariableServer/VariableServer_get_var_server_port + VariableServer/VariableServer_open_additional_servers VariableServer/VariableServer_init VariableServer/VariableServer_restart VariableServer/VariableServer_shutdown diff --git a/trick_source/sim_services/InputProcessor/Makefile_deps b/trick_source/sim_services/InputProcessor/Makefile_deps index b8703ada..0a9e1319 100644 --- a/trick_source/sim_services/InputProcessor/Makefile_deps +++ b/trick_source/sim_services/InputProcessor/Makefile_deps @@ -133,7 +133,7 @@ object_${TRICK_HOST_CPU}/MTV.o: MTV.cpp ${TRICK_HOME}/include/trick/MTV.hh \ ${TRICK_HOME}/include/trick/trick_byteswap.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerListenThread.hh \ diff --git a/trick_source/sim_services/ThreadBase/SysThread.cpp b/trick_source/sim_services/ThreadBase/SysThread.cpp index a7f613e4..70484338 100644 --- a/trick_source/sim_services/ThreadBase/SysThread.cpp +++ b/trick_source/sim_services/ThreadBase/SysThread.cpp @@ -15,7 +15,6 @@ bool Trick::SysThread::shutdown_finished = false; - // Construct On First Use to avoid the Static Initialization Fiasco pthread_mutex_t& Trick::SysThread::list_mutex() { static pthread_mutex_t list_mutex = PTHREAD_MUTEX_INITIALIZER; @@ -33,6 +32,12 @@ std::vector& Trick::SysThread::all_sys_threads() { } Trick::SysThread::SysThread(std::string in_name) : ThreadBase(in_name) { + pthread_mutex_init(&_restart_pause_mutex, NULL); + pthread_cond_init(&_thread_has_paused_cv, NULL); + pthread_cond_init(&_thread_wakeup_cv, NULL); + _thread_has_paused = true; + _thread_should_pause = false; + pthread_mutex_lock(&(list_mutex())); all_sys_threads().push_back(this); pthread_mutex_unlock(&(list_mutex())); @@ -65,4 +70,43 @@ int Trick::SysThread::ensureAllShutdown() { pthread_mutex_unlock(&(list_mutex())); return 0; +} + +// To be called from main thread +void Trick::SysThread::force_thread_to_pause() { + pthread_mutex_lock(&_restart_pause_mutex); + // Tell thread to pause, and wait for it to signal that it has + _thread_should_pause = true; + while (!_thread_has_paused) { + pthread_cond_wait(&_thread_has_paused_cv, &_restart_pause_mutex); + } + pthread_mutex_unlock(&_restart_pause_mutex); +} + +// To be called from main thread +void Trick::SysThread::unpause_thread() { + pthread_mutex_lock(&_restart_pause_mutex); + // Tell thread to wake up + _thread_should_pause = false; + pthread_cond_signal(&_thread_wakeup_cv); + pthread_mutex_unlock(&_restart_pause_mutex); +} + + +// To be called from this thread +void Trick::SysThread::test_pause() { + pthread_mutex_lock(&_restart_pause_mutex) ; + if (_thread_should_pause) { + // Tell main thread that we're pausing + _thread_has_paused = true; + pthread_cond_signal(&_thread_has_paused_cv); + + // Wait until we're told to wake up + while (_thread_should_pause) { + pthread_cond_wait(&_thread_wakeup_cv, &_restart_pause_mutex); + } + } + + _thread_has_paused = false; + pthread_mutex_unlock(&_restart_pause_mutex) ; } \ No newline at end of file diff --git a/trick_source/sim_services/ThreadBase/ThreadBase.cpp b/trick_source/sim_services/ThreadBase/ThreadBase.cpp index ffe6fd47..8a1a5ba0 100644 --- a/trick_source/sim_services/ThreadBase/ThreadBase.cpp +++ b/trick_source/sim_services/ThreadBase/ThreadBase.cpp @@ -288,7 +288,6 @@ int Trick::ThreadBase::create_thread() { pthread_attr_init(&attr); pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); pthread_create(&pthread_id, &attr, Trick::ThreadBase::thread_helper , (void *)this); - created = true; #if __linux diff --git a/trick_source/sim_services/VariableServer/Makefile_deps b/trick_source/sim_services/VariableServer/Makefile_deps index a4cd9fd0..8d83bab4 100644 --- a/trick_source/sim_services/VariableServer/Makefile_deps +++ b/trick_source/sim_services/VariableServer/Makefile_deps @@ -1,4 +1,4 @@ -object_${TRICK_HOST_CPU}/VariableServerThread_loop.o: VariableServerThread_loop.cpp \ +object_${TRICK_HOST_CPU}/VariableServerSessionThread_loop.o: VariableServerSessionThread_loop.cpp \ ${TRICK_HOME}/include/trick/VariableServer.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ @@ -10,7 +10,7 @@ object_${TRICK_HOST_CPU}/VariableServerThread_loop.o: VariableServerThread_loop. ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ @@ -25,8 +25,8 @@ object_${TRICK_HOST_CPU}/VariableServerThread_loop.o: VariableServerThread_loop. ${TRICK_HOME}/include/trick/ExecutiveException.hh \ ${TRICK_HOME}/include/trick/exec_proto.h \ ${TRICK_HOME}/include/trick/sim_mode.h -object_${TRICK_HOST_CPU}/VariableServer_copy_data_scheduled.o: \ - VariableServer_copy_data_scheduled.cpp \ +object_${TRICK_HOST_CPU}/VariableServer_copy_and_write_scheduled.o: \ + VariableServer_copy_and_write_scheduled.cpp \ ${TRICK_HOME}/include/trick/VariableServer.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ @@ -38,7 +38,7 @@ object_${TRICK_HOST_CPU}/VariableServer_copy_data_scheduled.o: \ ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ @@ -56,13 +56,13 @@ object_${TRICK_HOST_CPU}/VariableServer_shutdown.o: VariableServer_shutdown.cpp ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ ${TRICK_HOME}/include/trick/VariableServerListenThread.hh -object_${TRICK_HOST_CPU}/VariableServer_copy_data_freeze_scheduled.o: \ - VariableServer_copy_data_freeze_scheduled.cpp \ +object_${TRICK_HOST_CPU}/VariableServer_copy_and_write_freeze_scheduled.o: \ + VariableServer_copy_and_write_freeze_scheduled.cpp \ ${TRICK_HOME}/include/trick/VariableServer.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ @@ -74,7 +74,7 @@ object_${TRICK_HOST_CPU}/VariableServer_copy_data_freeze_scheduled.o: \ ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ @@ -92,7 +92,7 @@ object_${TRICK_HOST_CPU}/VariableReference.o: \ ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ @@ -116,13 +116,13 @@ object_${TRICK_HOST_CPU}/VariableServer_get_next_freeze_call_time.o: \ ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ ${TRICK_HOME}/include/trick/VariableServerListenThread.hh \ ${TRICK_HOME}/include/trick/TrickConstant.hh -object_${TRICK_HOST_CPU}/VariableServerThread_write_stdio.o: VariableServerThread_write_stdio.cpp \ +object_${TRICK_HOST_CPU}/VariableServerSessionThread_write_stdio.o: VariableServerSessionThread_write_stdio.cpp \ ${TRICK_HOME}/include/trick/VariableServer.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ @@ -134,7 +134,7 @@ object_${TRICK_HOST_CPU}/VariableServerThread_write_stdio.o: VariableServerThrea ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ @@ -154,7 +154,7 @@ object_${TRICK_HOST_CPU}/VariableServer_get_next_sync_call_time.o: \ ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ @@ -172,7 +172,7 @@ object_${TRICK_HOST_CPU}/VariableServer_restart.o: VariableServer_restart.cpp \ ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ @@ -192,7 +192,7 @@ object_${TRICK_HOST_CPU}/var_server_ext.o: var_server_ext.cpp \ ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ @@ -204,9 +204,9 @@ object_${TRICK_HOST_CPU}/var_server_ext.o: var_server_ext.cpp \ ${TRICK_HOME}/include/trick/memorymanager_c_intf.h \ ${TRICK_HOME}/include/trick/var.h \ ${TRICK_HOME}/include/trick/io_alloc.h -object_${TRICK_HOST_CPU}/VariableServerThread_create_socket.o: \ - VariableServerThread_create_socket.cpp \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ +object_${TRICK_HOST_CPU}/VariableServerSessionThread_create_socket.o: \ + VariableServerSessionThread_create_socket.cpp \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ @@ -226,7 +226,7 @@ object_${TRICK_HOST_CPU}/VariableServerListenThread.o: VariableServerListenThrea ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ ${TRICK_HOME}/include/trick/reference.h \ @@ -241,7 +241,7 @@ object_${TRICK_HOST_CPU}/VariableServerListenThread.o: VariableServerListenThrea ${TRICK_HOME}/include/trick/command_line_protos.h \ ${TRICK_HOME}/include/trick/message_proto.h \ ${TRICK_HOME}/include/trick/message_type.h -object_${TRICK_HOST_CPU}/VariableServerThread_write_data.o: VariableServerThread_write_data.cpp \ +object_${TRICK_HOST_CPU}/VariableServerSessionThread_write_data.o: VariableServerSessionThread_write_data.cpp \ ${TRICK_HOME}/include/trick/VariableServer.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ @@ -253,7 +253,7 @@ object_${TRICK_HOST_CPU}/VariableServerThread_write_data.o: VariableServerThread ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ @@ -276,7 +276,7 @@ object_${TRICK_HOST_CPU}/VariableServer_init.o: VariableServer_init.cpp \ ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ @@ -291,7 +291,7 @@ object_${TRICK_HOST_CPU}/VariableServer_init.o: VariableServer_init.cpp \ ${TRICK_HOME}/include/trick/Threads.hh \ ${TRICK_HOME}/include/trick/ThreadTrigger.hh \ ${TRICK_HOME}/include/trick/sim_mode.h -object_${TRICK_HOST_CPU}/VariableServer_copy_data_freeze.o: VariableServer_copy_data_freeze.cpp \ +object_${TRICK_HOST_CPU}/VariableServer_copy_and_write_freeze.o: VariableServer_copy_and_write_freeze.cpp \ ${TRICK_HOME}/include/trick/VariableServer.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ @@ -303,7 +303,7 @@ object_${TRICK_HOST_CPU}/VariableServer_copy_data_freeze.o: VariableServer_copy_ ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ @@ -320,7 +320,7 @@ object_${TRICK_HOST_CPU}/VariableServer_default_data.o: VariableServer_default_d ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ @@ -337,7 +337,7 @@ object_${TRICK_HOST_CPU}/VariableServer_freeze_init.o: VariableServer_freeze_ini ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ @@ -356,14 +356,14 @@ object_${TRICK_HOST_CPU}/VariableServer.o: VariableServer.cpp \ ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ ${TRICK_HOME}/include/trick/VariableServerListenThread.hh \ ${TRICK_HOME}/include/trick/tc_proto.h -object_${TRICK_HOST_CPU}/VariableServerThread_restart.o: VariableServerThread_restart.cpp \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ +object_${TRICK_HOST_CPU}/VariableServerSessionThread_restart.o: VariableServerSessionThread_restart.cpp \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ @@ -375,7 +375,7 @@ object_${TRICK_HOST_CPU}/VariableServerThread_restart.o: VariableServerThread_re ${TRICK_HOME}/include/trick/value.h \ ${TRICK_HOME}/include/trick/dllist.h \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h -object_${TRICK_HOST_CPU}/VariableServer_copy_data_top.o: VariableServer_copy_data_top.cpp \ +object_${TRICK_HOST_CPU}/VariableServer_copy_and_write_top.o: VariableServer_copy_and_write_top.cpp \ ${TRICK_HOME}/include/trick/VariableServer.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ @@ -387,7 +387,7 @@ object_${TRICK_HOST_CPU}/VariableServer_copy_data_top.o: VariableServer_copy_dat ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ @@ -404,13 +404,13 @@ object_${TRICK_HOST_CPU}/exit_var_thread.o: exit_var_thread.cpp \ ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ ${TRICK_HOME}/include/trick/VariableServerListenThread.hh \ ${TRICK_HOME}/include/trick/tc_proto.h -object_${TRICK_HOST_CPU}/VariableServerThread_copy_data.o: VariableServerThread_copy_data.cpp \ +object_${TRICK_HOST_CPU}/VariableServerSessionThread_copy_data.o: VariableServerSessionThread_copy_data.cpp \ ${TRICK_HOME}/include/trick/VariableServer.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ @@ -422,7 +422,7 @@ object_${TRICK_HOST_CPU}/VariableServerThread_copy_data.o: VariableServerThread_ ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ @@ -432,7 +432,7 @@ object_${TRICK_HOST_CPU}/VariableServerThread_copy_data.o: VariableServerThread_ ${TRICK_HOME}/include/trick/realtimesync_proto.h \ ${TRICK_HOME}/include/trick/Clock.hh \ ${TRICK_HOME}/include/trick/Timer.hh -object_${TRICK_HOST_CPU}/VariableServerThread_connect.o: VariableServerThread_connect.cpp \ +object_${TRICK_HOST_CPU}/VariableServerSessionThread_connect.o: VariableServerSessionThread_connect.cpp \ ${TRICK_HOME}/include/trick/VariableServer.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ @@ -444,14 +444,14 @@ object_${TRICK_HOST_CPU}/VariableServerThread_connect.o: VariableServerThread_co ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ ${TRICK_HOME}/include/trick/VariableServerListenThread.hh \ ${TRICK_HOME}/include/trick/release.h -object_${TRICK_HOST_CPU}/VariableServerThread_copy_sim_data.o: \ - VariableServerThread_copy_sim_data.cpp \ +object_${TRICK_HOST_CPU}/VariableServerSessionThread_copy_sim_data.o: \ + VariableServerSessionThread_copy_sim_data.cpp \ ${TRICK_HOME}/include/trick/VariableServer.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ @@ -463,7 +463,7 @@ object_${TRICK_HOST_CPU}/VariableServerThread_copy_sim_data.o: \ ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ @@ -487,8 +487,8 @@ object_${TRICK_HOST_CPU}/VariableServerSession_freeze_init.o: VariableServerSess ${TRICK_HOME}/include/trick/dllist.h \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ ${TRICK_HOME}/include/trick/TrickConstant.hh -object_${TRICK_HOST_CPU}/VariableServer_get_var_server_port.o: \ - VariableServer_get_var_server_port.cpp \ +object_${TRICK_HOST_CPU}/VariableServer_open_additional_servers.o: \ + VariableServer_open_additional_servers.cpp \ ${TRICK_HOME}/include/trick/VariableServer.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ @@ -500,13 +500,13 @@ object_${TRICK_HOST_CPU}/VariableServer_get_var_server_port.o: \ ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ ${TRICK_HOME}/include/trick/VariableServerListenThread.hh -object_${TRICK_HOST_CPU}/VariableServerThread.o: VariableServerThread.cpp \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ +object_${TRICK_HOST_CPU}/VariableServerSessionThread.o: VariableServerSessionThread.cpp \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ @@ -521,7 +521,7 @@ object_${TRICK_HOST_CPU}/VariableServerThread.o: VariableServerThread.cpp \ ${TRICK_HOME}/include/trick/exec_proto.h \ ${TRICK_HOME}/include/trick/sim_mode.h \ ${TRICK_HOME}/include/trick/TrickConstant.hh -object_${TRICK_HOST_CPU}/VariableServerThread_loop.o: VariableServerThread_loop.cpp \ +object_${TRICK_HOST_CPU}/VariableServerSessionThread_loop.o: VariableServerSessionThread_loop.cpp \ ${TRICK_HOME}/include/trick/VariableServer.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ @@ -533,7 +533,7 @@ object_${TRICK_HOST_CPU}/VariableServerThread_loop.o: VariableServerThread_loop. ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ @@ -548,8 +548,8 @@ object_${TRICK_HOST_CPU}/VariableServerThread_loop.o: VariableServerThread_loop. ${TRICK_HOME}/include/trick/ExecutiveException.hh \ ${TRICK_HOME}/include/trick/exec_proto.h \ ${TRICK_HOME}/include/trick/sim_mode.h -object_${TRICK_HOST_CPU}/VariableServer_copy_data_scheduled.o: \ - VariableServer_copy_data_scheduled.cpp \ +object_${TRICK_HOST_CPU}/VariableServer_copy_and_write_scheduled.o: \ + VariableServer_copy_and_write_scheduled.cpp \ ${TRICK_HOME}/include/trick/VariableServer.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ @@ -561,7 +561,7 @@ object_${TRICK_HOST_CPU}/VariableServer_copy_data_scheduled.o: \ ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ @@ -579,13 +579,13 @@ object_${TRICK_HOST_CPU}/VariableServer_shutdown.o: VariableServer_shutdown.cpp ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ ${TRICK_HOME}/include/trick/VariableServerListenThread.hh -object_${TRICK_HOST_CPU}/VariableServer_copy_data_freeze_scheduled.o: \ - VariableServer_copy_data_freeze_scheduled.cpp \ +object_${TRICK_HOST_CPU}/VariableServer_copy_and_write_freeze_scheduled.o: \ + VariableServer_copy_and_write_freeze_scheduled.cpp \ ${TRICK_HOME}/include/trick/VariableServer.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ @@ -597,7 +597,7 @@ object_${TRICK_HOST_CPU}/VariableServer_copy_data_freeze_scheduled.o: \ ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ @@ -617,7 +617,7 @@ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ @@ -628,7 +628,7 @@ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ ${TRICK_HOME}/include/trick/wcs_ext.h \ ${TRICK_HOME}/include/trick/message_proto.h \ ${TRICK_HOME}/include/trick/message_type.h -object_${TRICK_HOST_CPU}/VariableServerThread_write_stdio.o: VariableServerThread_write_stdio.cpp \ +object_${TRICK_HOST_CPU}/VariableServerSessionThread_write_stdio.o: VariableServerSessionThread_write_stdio.cpp \ ${TRICK_HOME}/include/trick/VariableServer.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ @@ -640,14 +640,14 @@ object_${TRICK_HOST_CPU}/VariableServerThread_write_stdio.o: VariableServerThrea ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ ${TRICK_HOME}/include/trick/VariableServerListenThread.hh \ ${TRICK_HOME}/include/trick/variable_server_message_types.h \ ${TRICK_HOME}/include/trick/tc_proto.h -object_${TRICK_HOST_CPU}/VariableServerThread_commands.o: VariableServerThread_commands.cpp \ +object_${TRICK_HOST_CPU}/VariableServerSessionThread_commands.o: VariableServerSessionThread_commands.cpp \ ${TRICK_HOME}/include/trick/VariableServer.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ @@ -659,7 +659,7 @@ object_${TRICK_HOST_CPU}/VariableServerThread_commands.o: VariableServerThread_c ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ @@ -691,7 +691,7 @@ object_${TRICK_HOST_CPU}/VariableServer_get_next_sync_call_time.o: \ ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ @@ -709,7 +709,7 @@ object_${TRICK_HOST_CPU}/VariableServer_restart.o: VariableServer_restart.cpp \ ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ @@ -729,7 +729,7 @@ object_${TRICK_HOST_CPU}/var_server_ext.o: var_server_ext.cpp \ ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ @@ -741,9 +741,9 @@ object_${TRICK_HOST_CPU}/var_server_ext.o: var_server_ext.cpp \ ${TRICK_HOME}/include/trick/memorymanager_c_intf.h \ ${TRICK_HOME}/include/trick/var.h \ ${TRICK_HOME}/include/trick/io_alloc.h -object_${TRICK_HOST_CPU}/VariableServerThread_create_socket.o: \ - VariableServerThread_create_socket.cpp \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ +object_${TRICK_HOST_CPU}/VariableServerSessionThread_create_socket.o: \ + VariableServerSessionThread_create_socket.cpp \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ @@ -762,10 +762,10 @@ object_${TRICK_HOST_CPU}/VariableServerListenThread.o: VariableServerListenThrea ${TRICK_HOME}/include/trick/VariableServerListenThread.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ - ${TRICK_HOME}/include/trick/ClientListener.hh \ - ${TRICK_HOME}/include/trick/MulticastManager.hh \ + ${TRICK_HOME}/include/trick/TCPClientListener.hh \ + ${TRICK_HOME}/include/trick/MulticastGroup.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ ${TRICK_HOME}/include/trick/reference.h \ @@ -780,7 +780,7 @@ object_${TRICK_HOST_CPU}/VariableServerListenThread.o: VariableServerListenThrea ${TRICK_HOME}/include/trick/command_line_protos.h \ ${TRICK_HOME}/include/trick/message_proto.h \ ${TRICK_HOME}/include/trick/message_type.h -object_${TRICK_HOST_CPU}/VariableServerThread_write_data.o: VariableServerThread_write_data.cpp \ +object_${TRICK_HOST_CPU}/VariableServerSessionThread_write_data.o: VariableServerSessionThread_write_data.cpp \ ${TRICK_HOME}/include/trick/VariableServer.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ @@ -792,7 +792,7 @@ object_${TRICK_HOST_CPU}/VariableServerThread_write_data.o: VariableServerThread ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ @@ -815,7 +815,7 @@ object_${TRICK_HOST_CPU}/VariableServer_init.o: VariableServer_init.cpp \ ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ @@ -830,7 +830,7 @@ object_${TRICK_HOST_CPU}/VariableServer_init.o: VariableServer_init.cpp \ ${TRICK_HOME}/include/trick/Threads.hh \ ${TRICK_HOME}/include/trick/ThreadTrigger.hh \ ${TRICK_HOME}/include/trick/sim_mode.h -object_${TRICK_HOST_CPU}/VariableServer_copy_data_freeze.o: VariableServer_copy_data_freeze.cpp \ +object_${TRICK_HOST_CPU}/VariableServer_copy_and_write_freeze.o: VariableServer_copy_and_write_freeze.cpp \ ${TRICK_HOME}/include/trick/VariableServer.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ @@ -842,7 +842,7 @@ object_${TRICK_HOST_CPU}/VariableServer_copy_data_freeze.o: VariableServer_copy_ ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ @@ -859,7 +859,7 @@ object_${TRICK_HOST_CPU}/VariableServer_default_data.o: VariableServer_default_d ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ @@ -876,7 +876,7 @@ object_${TRICK_HOST_CPU}/VariableServer_freeze_init.o: VariableServer_freeze_ini ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ @@ -895,14 +895,14 @@ object_${TRICK_HOST_CPU}/VariableServer.o: VariableServer.cpp \ ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ ${TRICK_HOME}/include/trick/VariableServerListenThread.hh \ ${TRICK_HOME}/include/trick/tc_proto.h -object_${TRICK_HOST_CPU}/VariableServerThread_restart.o: VariableServerThread_restart.cpp \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ +object_${TRICK_HOST_CPU}/VariableServerSessionThread_restart.o: VariableServerSessionThread_restart.cpp \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ @@ -914,7 +914,7 @@ object_${TRICK_HOST_CPU}/VariableServerThread_restart.o: VariableServerThread_re ${TRICK_HOME}/include/trick/value.h \ ${TRICK_HOME}/include/trick/dllist.h \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h -object_${TRICK_HOST_CPU}/VariableServer_copy_data_top.o: VariableServer_copy_data_top.cpp \ +object_${TRICK_HOST_CPU}/VariableServer_copy_and_write_top.o: VariableServer_copy_and_write_top.cpp \ ${TRICK_HOME}/include/trick/VariableServer.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ @@ -926,7 +926,7 @@ object_${TRICK_HOST_CPU}/VariableServer_copy_data_top.o: VariableServer_copy_dat ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ @@ -943,13 +943,13 @@ object_${TRICK_HOST_CPU}/exit_var_thread.o: exit_var_thread.cpp \ ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ ${TRICK_HOME}/include/trick/VariableServerListenThread.hh \ ${TRICK_HOME}/include/trick/tc_proto.h -object_${TRICK_HOST_CPU}/VariableServerThread_copy_data.o: VariableServerThread_copy_data.cpp \ +object_${TRICK_HOST_CPU}/VariableServerSessionThread_copy_data.o: VariableServerSessionThread_copy_data.cpp \ ${TRICK_HOME}/include/trick/VariableServer.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ @@ -961,7 +961,7 @@ object_${TRICK_HOST_CPU}/VariableServerThread_copy_data.o: VariableServerThread_ ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ @@ -984,13 +984,13 @@ object_${TRICK_HOST_CPU}/VariableServer_get_next_freeze_call_time.o: \ ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ ${TRICK_HOME}/include/trick/VariableServerListenThread.hh \ ${TRICK_HOME}/include/trick/TrickConstant.hh -object_${TRICK_HOST_CPU}/VariableServerThread_connect.o: VariableServerThread_connect.cpp \ +object_${TRICK_HOST_CPU}/VariableServerSessionThread_connect.o: VariableServerSessionThread_connect.cpp \ ${TRICK_HOME}/include/trick/VariableServer.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ @@ -1002,14 +1002,14 @@ object_${TRICK_HOST_CPU}/VariableServerThread_connect.o: VariableServerThread_co ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ ${TRICK_HOME}/include/trick/VariableServerListenThread.hh \ ${TRICK_HOME}/include/trick/release.h -object_${TRICK_HOST_CPU}/VariableServerThread_copy_sim_data.o: \ - VariableServerThread_copy_sim_data.cpp \ +object_${TRICK_HOST_CPU}/VariableServerSessionThread_copy_sim_data.o: \ + VariableServerSessionThread_copy_sim_data.cpp \ ${TRICK_HOME}/include/trick/VariableServer.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ @@ -1021,7 +1021,7 @@ object_${TRICK_HOST_CPU}/VariableServerThread_copy_sim_data.o: \ ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ @@ -1031,8 +1031,8 @@ object_${TRICK_HOST_CPU}/VariableServerThread_copy_sim_data.o: \ ${TRICK_HOME}/include/trick/io_alloc.h \ ${TRICK_HOME}/include/trick/exec_proto.h \ ${TRICK_HOME}/include/trick/sim_mode.h -object_${TRICK_HOST_CPU}/VariableServerThread_freeze_init.o: VariableServerThread_freeze_init.cpp \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ +object_${TRICK_HOST_CPU}/VariableServerSessionThread_freeze_init.o: VariableServerSessionThread_freeze_init.cpp \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ @@ -1045,8 +1045,8 @@ object_${TRICK_HOST_CPU}/VariableServerThread_freeze_init.o: VariableServerThrea ${TRICK_HOME}/include/trick/dllist.h \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ ${TRICK_HOME}/include/trick/TrickConstant.hh -object_${TRICK_HOST_CPU}/VariableServer_get_var_server_port.o: \ - VariableServer_get_var_server_port.cpp \ +object_${TRICK_HOST_CPU}/VariableServer_open_additional_servers.o: \ + VariableServer_open_additional_servers.cpp \ ${TRICK_HOME}/include/trick/VariableServer.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ @@ -1058,13 +1058,13 @@ object_${TRICK_HOST_CPU}/VariableServer_get_var_server_port.o: \ ${TRICK_HOME}/include/trick/JobData.hh \ ${TRICK_HOME}/include/trick/InstrumentBase.hh \ ${TRICK_HOME}/include/trick/variable_server_sync_types.h \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ ${TRICK_HOME}/include/trick/VariableReference.hh \ ${TRICK_HOME}/include/trick/VariableServerSession.hh \ ${TRICK_HOME}/include/trick/VariableServerListenThread.hh -object_${TRICK_HOST_CPU}/VariableServerThread.o: VariableServerThread.cpp \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ +object_${TRICK_HOST_CPU}/VariableServerSessionThread.o: VariableServerSessionThread.cpp \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ @@ -1080,7 +1080,7 @@ object_${TRICK_HOST_CPU}/VariableServerThread.o: VariableServerThread.cpp \ ${TRICK_HOME}/include/trick/sim_mode.h \ ${TRICK_HOME}/include/trick/TrickConstant.hh object_${TRICK_HOST_CPU}/VariableServerSession.o: VariableServerSession.cpp \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ @@ -1098,8 +1098,8 @@ object_${TRICK_HOST_CPU}/VariableServerSession.o: VariableServerSession.cpp \ object_${TRICK_HOST_CPU}/TCConnection.o: TCConnection.cpp \ ${TRICK_HOME}/include/trick/TCConnection.hh \ ${TRICK_HOME}/include/trick/ClientConnection.hh \ - ${TRICK_HOME}/include/trick/ClientListener.hh \ - ${TRICK_HOME}/include/trick/VariableServerThread.hh \ + ${TRICK_HOME}/include/trick/TCPClientListener.hh \ + ${TRICK_HOME}/include/trick/VariableServerSessionThread.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ ${TRICK_HOME}/include/trick/ThreadBase.hh \ @@ -1115,15 +1115,15 @@ object_${TRICK_HOST_CPU}/VariableServerSession.o: VariableServerSession.cpp \ ${TRICK_HOME}/include/trick/sim_mode.h \ ${TRICK_HOME}/include/trick/TrickConstant.hh \ ${TRICK_HOME}/include/trick/tc_proto.h -object_${TRICK_HOST_CPU}/ClientListener.o: ClientListener.cpp \ +object_${TRICK_HOST_CPU}/TCPClientListener.o: TCPClientListener.cpp \ ${TRICK_HOME}/include/trick/TCPConnection.hh \ ${TRICK_HOME}/include/trick/ClientConnection.hh \ - ${TRICK_HOME}/include/trick/ClientListener.hh \ + ${TRICK_HOME}/include/trick/TCPClientListener.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ ${TRICK_HOME}/include/trick/tc_proto.h - object_${TRICK_HOST_CPU}/MulticastManager.o: MulticastManager.cpp \ - ${TRICK_HOME}/include/trick/MulticastManager.hh \ + object_${TRICK_HOST_CPU}/MulticastGroup.o: MulticastGroup.cpp \ + ${TRICK_HOME}/include/trick/MulticastGroup.hh \ ${TRICK_HOME}/include/trick/tc.h \ ${TRICK_HOME}/include/trick/trick_error_hndlr.h \ ${TRICK_HOME}/include/trick/tc_proto.h diff --git a/trick_source/sim_services/VariableServer/VariableReference.cpp b/trick_source/sim_services/VariableServer/VariableReference.cpp index ba9f97d6..a2e86a8d 100644 --- a/trick_source/sim_services/VariableServer/VariableReference.cpp +++ b/trick_source/sim_services/VariableServer/VariableReference.cpp @@ -15,19 +15,18 @@ #include "trick/UdUnits.hh" #include "trick/bitfield_proto.h" #include "trick/trick_byteswap.h" -// #include "trick/tc_proto.h" // Static variables to be addresses that are known to be the error ref address -int Trick::VariableReference::bad_ref_int = 0 ; -int Trick::VariableReference::do_not_resolve_bad_ref_int = 0 ; +int Trick::VariableReference::_bad_ref_int = 0 ; +int Trick::VariableReference::_do_not_resolve_bad_ref_int = 0 ; REF2* Trick::VariableReference::make_error_ref(std::string in_name) { REF2* new_ref; new_ref = (REF2*)calloc(1, sizeof(REF2)); new_ref->reference = strdup(in_name.c_str()) ; new_ref->units = NULL ; - new_ref->address = (char *)&bad_ref_int ; + new_ref->address = (char *)&_bad_ref_int ; new_ref->attr = (ATTRIBUTES*)calloc(1, sizeof(ATTRIBUTES)) ; new_ref->attr->type = TRICK_NUMBER_OF_TYPES ; new_ref->attr->units = (char *)"--" ; @@ -40,7 +39,7 @@ REF2* Trick::VariableReference::make_do_not_resolve_ref(std::string in_name) { new_ref = (REF2*)calloc(1, sizeof(REF2)); new_ref->reference = strdup(in_name.c_str()) ; new_ref->units = NULL ; - new_ref->address = (char *)&do_not_resolve_bad_ref_int ; + new_ref->address = (char *)&_do_not_resolve_bad_ref_int ; new_ref->attr = (ATTRIBUTES*)calloc(1, sizeof(ATTRIBUTES)) ; new_ref->attr->type = TRICK_NUMBER_OF_TYPES ; new_ref->attr->units = (char *)"--" ; @@ -62,146 +61,152 @@ REF2* make_time_ref(double * time) { return new_ref; } -Trick::VariableReference::VariableReference(std::string var_name, double* time) : staged(false), write_ready(false) { +Trick::VariableReference::VariableReference(std::string var_name, double* time) : _staged(false), _write_ready(false) { if (var_name != "time") { ASSERT(0); } - var_info = make_time_ref(time); + _var_info = make_time_ref(time); // Set up member variables - address = var_info->address; - size = var_info->attr->size ; - deref = false; + _address = _var_info->address; + _size = _var_info->attr->size ; + _deref = false; // Deal with weirdness around string vs wstring - trick_type = var_info->attr->type ; + _trick_type = _var_info->attr->type ; // Allocate stage and write buffers - stage_buffer = calloc(size, 1) ; - write_buffer = calloc(size, 1) ; + _stage_buffer = calloc(_size, 1) ; + _write_buffer = calloc(_size, 1) ; - conversion_factor = cv_get_trivial(); + _conversion_factor = cv_get_trivial(); + _base_units = _var_info->attr->units; + _requested_units = "s"; + _name = _var_info->reference; } -Trick::VariableReference::VariableReference(std::string var_name) : staged(false), write_ready(false) { +Trick::VariableReference::VariableReference(std::string var_name) : _staged(false), _write_ready(false) { if (var_name == "time") { ASSERT(0); } else { // get variable attributes from memory manager - var_info = ref_attributes(var_name.c_str()); + _var_info = ref_attributes(var_name.c_str()); } // Handle error cases - if ( var_info == NULL ) { + if ( _var_info == NULL ) { // TODO: ERROR LOGGER sendErrorMessage("Variable Server could not find variable %s.\n", var_name); // PRINTF IS NOT AN ERROR LOGGER @me printf("Variable Server could not find variable %s.\n", var_name.c_str()); - var_info = make_error_ref(var_name); - } else if ( var_info->attr ) { - if ( var_info->attr->type == TRICK_STRUCTURED ) { + _var_info = make_error_ref(var_name); + } else if ( _var_info->attr ) { + if ( _var_info->attr->type == TRICK_STRUCTURED ) { // sendErrorMessage("Variable Server: var_add cant add \"%s\" because its a composite variable.\n", var_name); printf("Variable Server: var_add cant add \"%s\" because its a composite variable.\n", var_name.c_str()); - free(var_info); - var_info = make_do_not_resolve_ref(var_name); + free(_var_info); + _var_info = make_do_not_resolve_ref(var_name); - } else if ( var_info->attr->type == TRICK_STL ) { + } else if ( _var_info->attr->type == TRICK_STL ) { // sendErrorMessage("Variable Server: var_add cant add \"%s\" because its an STL variable.\n", var_name); printf("Variable Server: var_add cant add \"%s\" because its an STL variable.\n", var_name.c_str()); - free(var_info); - var_info = make_do_not_resolve_ref(var_name); + free(_var_info); + _var_info = make_do_not_resolve_ref(var_name); } } else { // sendErrorMessage("Variable Server: BAD MOJO - Missing ATTRIBUTES."); printf("Variable Server: BAD MOJO - Missing ATTRIBUTES."); - free(var_info); - var_info = make_error_ref(var_name); + free(_var_info); + _var_info = make_error_ref(var_name); } // Set up member variables - var_info->units = NULL; - address = var_info->address; - size = var_info->attr->size ; - deref = false; + _var_info->units = NULL; + _address = _var_info->address; + _size = _var_info->attr->size ; + _deref = false; // Deal with weirdness around string vs wstring - trick_type = var_info->attr->type ; + _trick_type = _var_info->attr->type ; - if ( var_info->num_index == var_info->attr->num_index ) { + if ( _var_info->num_index == _var_info->attr->num_index ) { // single value - nothing else necessary - } else if ( var_info->attr->index[var_info->attr->num_index - 1].size != 0 ) { + } else if ( _var_info->attr->index[_var_info->attr->num_index - 1].size != 0 ) { // Constrained array - for ( int i = var_info->attr->num_index-1; i > var_info->num_index-1 ; i-- ) { - size *= var_info->attr->index[i].size ; + for ( int i = _var_info->attr->num_index-1; i > _var_info->num_index-1 ; i-- ) { + _size *= _var_info->attr->index[i].size ; } } else { // Unconstrained array - if ((var_info->attr->num_index - var_info->num_index) > 1 ) { + if ((_var_info->attr->num_index - _var_info->num_index) > 1 ) { // TODO: ERROR LOGGER - printf("Variable Server Error: var_add(%s) requests more than one dimension of dynamic array.\n", var_info->reference); + printf("Variable Server Error: var_add(%s) requests more than one dimension of dynamic array.\n", _var_info->reference); printf("Data is not contiguous so returned values are unpredictable.\n") ; } - if ( var_info->attr->type == TRICK_CHARACTER ) { - trick_type = TRICK_STRING ; - deref = true; - } else if ( var_info->attr->type == TRICK_WCHAR ) { - trick_type = TRICK_WSTRING ; - deref = true; + if ( _var_info->attr->type == TRICK_CHARACTER ) { + _trick_type = TRICK_STRING ; + _deref = true; + } else if ( _var_info->attr->type == TRICK_WCHAR ) { + _trick_type = TRICK_WSTRING ; + _deref = true; } else { - deref = true ; - size *= get_size((char*)address) ; + _deref = true ; + _size *= get_size((char*)_address) ; } } // handle strings: set a max buffer size, the copy size may vary so will be set in copy_sim_data - if (( trick_type == TRICK_STRING ) || ( trick_type == TRICK_WSTRING )) { - size = MAX_ARRAY_LENGTH ; + if (( _trick_type == TRICK_STRING ) || ( _trick_type == TRICK_WSTRING )) { + _size = MAX_ARRAY_LENGTH ; } // Allocate stage and write buffers - stage_buffer = calloc(size, 1) ; - write_buffer = calloc(size, 1) ; + _stage_buffer = calloc(_size, 1) ; + _write_buffer = calloc(_size, 1) ; - conversion_factor = cv_get_trivial(); + _conversion_factor = cv_get_trivial(); + _base_units = _var_info->attr->units; + _requested_units = ""; + _name = _var_info->reference; // Done! } Trick::VariableReference::~VariableReference() { - if (var_info != NULL) { - free( var_info ); - var_info = NULL; + if (_var_info != NULL) { + free( _var_info ); + _var_info = NULL; } - if (stage_buffer != NULL) { - free (stage_buffer); - stage_buffer = NULL; + if (_stage_buffer != NULL) { + free (_stage_buffer); + _stage_buffer = NULL; } - if (write_buffer != NULL) { - free (write_buffer); - write_buffer = NULL; + if (_write_buffer != NULL) { + free (_write_buffer); + _write_buffer = NULL; } - if (conversion_factor != NULL) { - cv_free(conversion_factor); + if (_conversion_factor != NULL) { + cv_free(_conversion_factor); } } -const char* Trick::VariableReference::getName() const { - return var_info->reference; +std::string Trick::VariableReference::getName() const { + return _name; } int Trick::VariableReference::getSizeBinary() const { - return size; + return _size; } TRICK_TYPE Trick::VariableReference::getType() const { - return trick_type; + return _trick_type; } -const char* Trick::VariableReference::getBaseUnits() const { - return var_info->attr->units; +std::string Trick::VariableReference::getBaseUnits() const { + return _base_units; } int Trick::VariableReference::setRequestedUnits(std::string units_name) { @@ -238,7 +243,7 @@ int Trick::VariableReference::setRequestedUnits(std::string units_name) { } // Interpret base unit - ut_unit * from = ut_parse(Trick::UdUnits::get_u_system(), getBaseUnits(), UT_ASCII) ; + ut_unit * from = ut_parse(Trick::UdUnits::get_u_system(), getBaseUnits().c_str(), UT_ASCII) ; if ( !from ) { std::cout << "Error in interpreting base units" << std::endl; publishError(getBaseUnits()); @@ -267,79 +272,75 @@ int Trick::VariableReference::setRequestedUnits(std::string units_name) { publish(MSG_ERROR, oss.str()); return -1 ; } else { - conversion_factor = new_conversion_factor; - } - - // Don't memory leak the old units! - if (var_info->units != NULL) { - free(var_info->units); + _conversion_factor = new_conversion_factor; } // Set the requested units. This will cause the unit string to be printed in write_value_ascii - var_info->units = strdup(new_units.c_str());; + _requested_units = new_units; } return 0; } int Trick::VariableReference::stageValue(bool validate_address) { - write_ready = false; + _write_ready = false; // Copy bytes from
to staging_point. // Try to recreate connection if it has been broken - if (var_info->address == &bad_ref_int) { - REF2 *new_ref = ref_attributes(var_info->reference); + if (_var_info->address == &_bad_ref_int) { + REF2 *new_ref = ref_attributes(_var_info->reference); if (new_ref != NULL) { - var_info = new_ref; - address = var_info->address; + _var_info = new_ref; + _address = _var_info->address; + // _requested_units = ""; } } // if there's a pointer somewhere in the address path, follow it in case pointer changed - if ( var_info->pointer_present == 1 ) { - address = follow_address_path(var_info) ; - if (address == NULL) { + if ( _var_info->pointer_present == 1 ) { + _address = follow_address_path(_var_info) ; + if (_address == NULL) { tagAsInvalid(); } else if ( validate_address ) { validate(); } else { - var_info->address = address ; + _var_info->address = _address ; } } // if this variable is a string we need to get the raw character string out of it. - if (( trick_type == TRICK_STRING ) && !deref) { - std::string * str_ptr = (std::string *)var_info->address ; + if (( _trick_type == TRICK_STRING ) && !_deref) { + std::string * str_ptr = (std::string *)_var_info->address ; // Get a pointer to the internal character array - address = (void *)(str_ptr->c_str()) ; + _address = (void *)(str_ptr->c_str()) ; } // if this variable itself is a pointer, dereference it - if ( deref ) { - address = *(void**)var_info->address ; + if ( _deref ) { + _address = *(void**)_var_info->address ; } // handle c++ string and char* - if ( trick_type == TRICK_STRING ) { - if (address == NULL) { - size = 0 ; + if ( _trick_type == TRICK_STRING ) { + if (_address == NULL) { + _size = 0 ; } else { - size = strlen((char*)address) + 1 ; + _size = strlen((char*)_address) + 1 ; } } // handle c++ wstring and wchar_t* - if ( trick_type == TRICK_WSTRING ) { - if (address == NULL) { - size = 0 ; + if ( _trick_type == TRICK_WSTRING ) { + if (_address == NULL) { + _size = 0 ; } else { - size = wcslen((wchar_t *)address) * sizeof(wchar_t); + _size = wcslen((wchar_t *)_address) * sizeof(wchar_t); } } - if(address != NULL) { - memcpy( stage_buffer , address , size ) ; + if(_address != NULL) { + memcpy( _stage_buffer , _address , _size ) ; } - staged = true; + _staged = true; return 0; } @@ -349,10 +350,10 @@ bool Trick::VariableReference::validate() { // check the memory manager if the address falls into // any of the memory blocks it knows of. Don't do this if we have a std::string or // wstring type, or we already are pointing to a bad ref. - if ( (trick_type != TRICK_STRING) and - (trick_type != TRICK_WSTRING) and - (var_info->address != &bad_ref_int) and - (get_alloc_info_of(address) == NULL) ) { + if ( (_trick_type != TRICK_STRING) and + (_trick_type != TRICK_WSTRING) and + (_var_info->address != &_bad_ref_int) and + (get_alloc_info_of(_address) == NULL) ) { // This variable is broken, make it into an error ref tagAsInvalid(); @@ -398,46 +399,41 @@ int Trick::VariableReference::getSizeAscii() const { int Trick::VariableReference::writeValueAscii( std::ostream& out ) const { // This is copied and modified from vs_format_ascii - // There's a lot here that doesn't make sense to me that I need to come back to - // There seems to be a huge buffer overflow issue in the original. - // Only strings are checked for length, arrays aren't - // But using a stream instead should make that better - // The way that arrays are handled seems weird. if (!isWriteReady()) { return -1; } int bytes_written = 0; - void * buf_ptr = write_buffer ; - while (bytes_written < size) { - bytes_written += var_info->attr->size ; + void * buf_ptr = _write_buffer ; + while (bytes_written < _size) { + bytes_written += _var_info->attr->size ; - switch (trick_type) { + switch (_trick_type) { case TRICK_CHARACTER: - if (var_info->attr->num_index == var_info->num_index) { + if (_var_info->attr->num_index == _var_info->num_index) { // Single char - out << (int)cv_convert_double(conversion_factor, *(char *)buf_ptr); + out << (int)cv_convert_double(_conversion_factor, *(char *)buf_ptr); } else { // All but last dim specified, leaves a char array write_escaped_string(out, (const char *) buf_ptr); - bytes_written = size ; + bytes_written = _size ; } break; case TRICK_UNSIGNED_CHARACTER: - if (var_info->attr->num_index == var_info->num_index) { + if (_var_info->attr->num_index == _var_info->num_index) { // Single char - out << (unsigned int)cv_convert_double(conversion_factor,*(unsigned char *)buf_ptr); + out << (unsigned int)cv_convert_double(_conversion_factor,*(unsigned char *)buf_ptr); } else { // All but last dim specified, leaves a char array write_escaped_string(out, (const char *) buf_ptr); - bytes_written = size ; + bytes_written = _size ; } break; case TRICK_WCHAR:{ - if (var_info->attr->num_index == var_info->num_index) { + if (_var_info->attr->num_index == _var_info->num_index) { out << *(wchar_t *) buf_ptr; } else { // convert wide char string char string @@ -446,7 +442,7 @@ int Trick::VariableReference::writeValueAscii( std::ostream& out ) const { char temp_buf[len]; wcs_to_ncs((wchar_t *) buf_ptr, temp_buf, len); out << temp_buf; - bytes_written = size ; + bytes_written = _size ; } } break; @@ -454,7 +450,7 @@ int Trick::VariableReference::writeValueAscii( std::ostream& out ) const { case TRICK_STRING: if ((char *) buf_ptr != NULL) { write_escaped_string(out, (const char *) buf_ptr); - bytes_written = size ; + bytes_written = _size ; } else { out << '\0'; } @@ -468,44 +464,44 @@ int Trick::VariableReference::writeValueAscii( std::ostream& out ) const { char temp_buf[len]; wcs_to_ncs( (wchar_t *) buf_ptr, temp_buf, len); out << temp_buf; - bytes_written = size ; + bytes_written = _size ; } else { out << '\0'; } break; case TRICK_SHORT: - out << (short)cv_convert_double(conversion_factor,*(short *)buf_ptr); + out << (short)cv_convert_double(_conversion_factor,*(short *)buf_ptr); break; case TRICK_UNSIGNED_SHORT: - out << (unsigned short)cv_convert_double(conversion_factor,*(unsigned short *)buf_ptr); + out << (unsigned short)cv_convert_double(_conversion_factor,*(unsigned short *)buf_ptr); break; case TRICK_INTEGER: case TRICK_ENUMERATED: - out << (int)cv_convert_double(conversion_factor,*(int *)buf_ptr); + out << (int)cv_convert_double(_conversion_factor,*(int *)buf_ptr); break; case TRICK_BOOLEAN: - out << (int)cv_convert_double(conversion_factor,*(bool *)buf_ptr); + out << (int)cv_convert_double(_conversion_factor,*(bool *)buf_ptr); break; case TRICK_BITFIELD: - out << (GET_BITFIELD(buf_ptr, var_info->attr->size, var_info->attr->index[0].start, var_info->attr->index[0].size)); + out << (GET_BITFIELD(buf_ptr, _var_info->attr->size, _var_info->attr->index[0].start, _var_info->attr->index[0].size)); break; case TRICK_UNSIGNED_BITFIELD: - out << (GET_UNSIGNED_BITFIELD(buf_ptr, var_info->attr->size, var_info->attr->index[0].start, var_info->attr->index[0].size)); + out << (GET_UNSIGNED_BITFIELD(buf_ptr, _var_info->attr->size, _var_info->attr->index[0].start, _var_info->attr->index[0].size)); break; case TRICK_UNSIGNED_INTEGER: - out << (unsigned int)cv_convert_double(conversion_factor,*(unsigned int *)buf_ptr); + out << (unsigned int)cv_convert_double(_conversion_factor,*(unsigned int *)buf_ptr); break; case TRICK_LONG: { long l = *(long *)buf_ptr; - if (conversion_factor != cv_get_trivial()) { - l = (long)cv_convert_double(conversion_factor, l); + if (_conversion_factor != cv_get_trivial()) { + l = (long)cv_convert_double(_conversion_factor, l); } out << l; break; @@ -513,25 +509,25 @@ int Trick::VariableReference::writeValueAscii( std::ostream& out ) const { case TRICK_UNSIGNED_LONG: { unsigned long ul = *(unsigned long *)buf_ptr; - if (conversion_factor != cv_get_trivial()) { - ul = (unsigned long)cv_convert_double(conversion_factor, ul); + if (_conversion_factor != cv_get_trivial()) { + ul = (unsigned long)cv_convert_double(_conversion_factor, ul); } out << ul; break; } case TRICK_FLOAT: - out << std::setprecision(8) << cv_convert_float(conversion_factor,*(float *)buf_ptr); + out << std::setprecision(8) << cv_convert_float(_conversion_factor,*(float *)buf_ptr); break; case TRICK_DOUBLE: - out << std::setprecision(16) << cv_convert_double(conversion_factor,*(double *)buf_ptr); + out << std::setprecision(16) << cv_convert_double(_conversion_factor,*(double *)buf_ptr); break; case TRICK_LONG_LONG: { long long ll = *(long long *)buf_ptr; - if (conversion_factor != cv_get_trivial()) { - ll = (long long)cv_convert_double(conversion_factor, ll); + if (_conversion_factor != cv_get_trivial()) { + ll = (long long)cv_convert_double(_conversion_factor, ll); } out << ll; break; @@ -539,8 +535,8 @@ int Trick::VariableReference::writeValueAscii( std::ostream& out ) const { case TRICK_UNSIGNED_LONG_LONG: { unsigned long long ull = *(unsigned long long *)buf_ptr; - if (conversion_factor != cv_get_trivial()) { - ull = (unsigned long long)cv_convert_double(conversion_factor, ull); + if (_conversion_factor != cv_get_trivial()) { + ull = (unsigned long long)cv_convert_double(_conversion_factor, ull); } out << ull; break; @@ -556,18 +552,18 @@ int Trick::VariableReference::writeValueAscii( std::ostream& out ) const { } } // end switch - if (bytes_written < size) { + if (bytes_written < _size) { // if returning an array, continue array as comma separated values out << ","; - buf_ptr = (void*) ((long)buf_ptr + var_info->attr->size) ; + buf_ptr = (void*) ((long)buf_ptr + _var_info->attr->size) ; } } //end while - if (var_info->units) { - if ( var_info->attr->mods & TRICK_MODS_UNITSDASHDASH ) { + if (_requested_units != "") { + if ( _var_info->attr->mods & TRICK_MODS_UNITSDASHDASH ) { out << " {--}"; } else { - out << " {" << var_info->units << "}"; + out << " {" << _requested_units << "}"; } } @@ -576,36 +572,36 @@ int Trick::VariableReference::writeValueAscii( std::ostream& out ) const { void Trick::VariableReference::tagAsInvalid () { std::string save_name(getName()) ; - free(var_info) ; - var_info = make_error_ref(save_name) ; - address = var_info->address ; + free(_var_info) ; + _var_info = make_error_ref(save_name) ; + _address = _var_info->address ; } int Trick::VariableReference::prepareForWrite() { - if (!staged) { + if (!_staged) { return 1; } - void * temp_p = stage_buffer; - stage_buffer = write_buffer; - write_buffer = temp_p; + void * temp_p = _stage_buffer; + _stage_buffer = _write_buffer; + _write_buffer = temp_p; - staged = false; - write_ready = true; + _staged = false; + _write_ready = true; return 0; } bool Trick::VariableReference::isStaged() const { - return staged; + return _staged; } bool Trick::VariableReference::isWriteReady() const { - return write_ready; + return _write_ready; } int Trick::VariableReference::writeTypeBinary( std::ostream& out, bool byteswap ) const { - int local_type = trick_type; + int local_type = _trick_type; if (byteswap) { local_type = trick_byteswap_int(local_type); } @@ -615,7 +611,7 @@ int Trick::VariableReference::writeTypeBinary( std::ostream& out, bool byteswap } int Trick::VariableReference::writeSizeBinary( std::ostream& out, bool byteswap ) const { - int local_size = size; + int local_size = _size; if (byteswap) { local_size = trick_byteswap_int(local_size); } @@ -625,15 +621,15 @@ int Trick::VariableReference::writeSizeBinary( std::ostream& out, bool byteswap } int Trick::VariableReference::writeNameBinary( std::ostream& out, bool byteswap ) const { - const char * name = getName(); + std::string name = getName(); - int name_size = strlen(name); + int name_size = name.size(); if (byteswap) { name_size = trick_byteswap_int(name_size); } out.write(const_cast(reinterpret_cast(&name_size)), sizeof(int)); - out.write(name, strlen(name)); + out.write(name.c_str(), name.size()); return 0; } @@ -644,11 +640,11 @@ void Trick::VariableReference::byteswap_var (char * out, char * in) const { void Trick::VariableReference::byteswap_var (char * out, char * in, const VariableReference& ref) { - ATTRIBUTES * attr = ref.var_info->attr; + ATTRIBUTES * attr = ref._var_info->attr; int array_size = 1; // Determine how many elements are in this array if it is an array - for (int j = 0; j < ref.var_info->attr->num_index; j++) { + for (int j = 0; j < ref._var_info->attr->num_index; j++) { array_size *= attr->index[j].size; } @@ -696,41 +692,42 @@ void Trick::VariableReference::byteswap_var (char * out, char * in, const Variab int Trick::VariableReference::writeValueBinary( std::ostream& out, bool byteswap ) const { - char buf[20480]; - int temp_i ; - unsigned int temp_ui ; - - // int offset = 0; - - switch ( var_info->attr->type ) { - case TRICK_BITFIELD: - temp_i = GET_BITFIELD(address , var_info->attr->size , - var_info->attr->index[0].start, var_info->attr->index[0].size) ; - memcpy(buf, &temp_i , (size_t)size) ; - break ; - case TRICK_UNSIGNED_BITFIELD: - temp_ui = GET_UNSIGNED_BITFIELD(address , var_info->attr->size , - var_info->attr->index[0].start, var_info->attr->index[0].size) ; - memcpy(buf , &temp_ui , (size_t)size) ; - break ; - case TRICK_NUMBER_OF_TYPES: - // TRICK_NUMBER_OF_TYPES is an error case - temp_i = 0 ; - memcpy(buf , &temp_i , (size_t)size) ; - break ; - default: - if (byteswap) - byteswap_var(buf, (char *) address); - else - memcpy(buf , address , (size_t)size) ; - break ; + if ( _trick_type == TRICK_BITFIELD ) { + int temp_i = GET_BITFIELD(_write_buffer , _var_info->attr->size , + _var_info->attr->index[0].start, _var_info->attr->index[0].size) ; + out.write((char *)(&temp_i), _size); + return _size; } - out.write(buf, size); + if ( _trick_type == TRICK_UNSIGNED_BITFIELD ) { + int temp_unsigned = GET_UNSIGNED_BITFIELD(_write_buffer , _var_info->attr->size , + _var_info->attr->index[0].start, _var_info->attr->index[0].size) ; + out.write((char *)(&temp_unsigned), _size); + return _size; + } + + if (_trick_type == TRICK_NUMBER_OF_TYPES) { + // TRICK_NUMBER_OF_TYPES is an error case + int temp_zero = 0 ; + out.write((char *)(&temp_zero), _size); + return _size; + } + + if (byteswap) { + char * byteswap_buf = (char *) calloc (_size, 1); + byteswap_var(byteswap_buf, (char *) _write_buffer); + out.write(byteswap_buf, _size); + free (byteswap_buf); + } + else { + out.write((char *) _write_buffer, _size); + } + + return _size; + } std::ostream& Trick::operator<< (std::ostream& s, const Trick::VariableReference& ref) { - s << " \"" << ref.getName() << "\""; return s; } \ No newline at end of file diff --git a/trick_source/sim_services/VariableServer/VariableServer.cpp b/trick_source/sim_services/VariableServer/VariableServer.cpp index 01c3db97..2800778c 100644 --- a/trick_source/sim_services/VariableServer/VariableServer.cpp +++ b/trick_source/sim_services/VariableServer/VariableServer.cpp @@ -16,11 +16,11 @@ Trick::VariableServer::VariableServer() : } Trick::VariableServer::~VariableServer() { - + the_vs = NULL; } std::ostream& Trick::operator<< (std::ostream& s, Trick::VariableServer& vs) { - std::map < pthread_t , VariableServerThread * >::iterator it ; + std::map < pthread_t , VariableServerSessionThread * >::iterator it ; s << "{\"variable_server_connections\":[\n"; int count = 0; @@ -66,31 +66,28 @@ bool Trick::VariableServer::get_log() { void Trick::VariableServer::set_var_server_log_on() { log = true; // turn log on for all current vs clients - std::map < pthread_t , VariableServerSession * >::iterator it ; - for ( it = var_server_sessions.begin() ; it != var_server_sessions.end() ; it++ ) { - (*it).second->set_log_on(); + for ( auto& session_it : var_server_sessions ) { + session_it.second->set_log_on(); } } void Trick::VariableServer::set_var_server_log_off() { log = false; // turn log off for all current vs clients - std::map < pthread_t , VariableServerSession * >::iterator it ; - for ( it = var_server_sessions.begin() ; it != var_server_sessions.end() ; it++ ) { - (*it).second->set_log_off(); + for ( auto& session_it : var_server_sessions ) { + session_it.second->set_log_off(); } } const char * Trick::VariableServer::get_hostname() { - const char * ret = (listen_thread.get_hostname()) ; - return ret; + return listen_thread.get_hostname(); } Trick::VariableServerListenThread & Trick::VariableServer::get_listen_thread() { return listen_thread ; } -void Trick::VariableServer::add_vst(pthread_t in_thread_id, VariableServerThread * in_vst) { +void Trick::VariableServer::add_vst(pthread_t in_thread_id, VariableServerSessionThread * in_vst) { pthread_mutex_lock(&map_mutex) ; var_server_threads[in_thread_id] = in_vst ; pthread_mutex_unlock(&map_mutex) ; @@ -102,9 +99,9 @@ void Trick::VariableServer::add_session(pthread_t in_thread_id, VariableServerSe pthread_mutex_unlock(&map_mutex) ; } -Trick::VariableServerThread * Trick::VariableServer::get_vst(pthread_t thread_id) { - std::map < pthread_t , Trick::VariableServerThread * >::iterator it ; - Trick::VariableServerThread * ret = NULL ; +Trick::VariableServerSessionThread * Trick::VariableServer::get_vst(pthread_t thread_id) { + std::map < pthread_t , Trick::VariableServerSessionThread * >::iterator it ; + Trick::VariableServerSessionThread * ret = NULL ; pthread_mutex_lock(&map_mutex) ; it = var_server_threads.find(thread_id) ; if ( it != var_server_threads.end() ) { @@ -115,10 +112,9 @@ Trick::VariableServerThread * Trick::VariableServer::get_vst(pthread_t thread_id } Trick::VariableServerSession * Trick::VariableServer::get_session(pthread_t thread_id) { - std::map < pthread_t , Trick::VariableServerSession * >::iterator it ; Trick::VariableServerSession * ret = NULL ; pthread_mutex_lock(&map_mutex) ; - it = var_server_sessions.find(thread_id) ; + auto it = var_server_sessions.find(thread_id) ; if ( it != var_server_sessions.end() ) { ret = (*it).second ; } @@ -142,6 +138,6 @@ void Trick::VariableServer::set_copy_data_job( Trick::JobData * in_job ) { copy_data_job = in_job ; } -void Trick::VariableServer::set_copy_data_freeze_job( Trick::JobData * in_job ) { - copy_data_freeze_job = in_job ; +void Trick::VariableServer::set_copy_and_write_freeze_job( Trick::JobData * in_job ) { + copy_and_write_freeze_job = in_job ; } diff --git a/trick_source/sim_services/VariableServer/VariableServerListenThread.cpp b/trick_source/sim_services/VariableServer/VariableServerListenThread.cpp index c5ea2163..771cfea6 100644 --- a/trick_source/sim_services/VariableServer/VariableServerListenThread.cpp +++ b/trick_source/sim_services/VariableServer/VariableServerListenThread.cpp @@ -3,8 +3,7 @@ #include #include "trick/VariableServerListenThread.hh" -#include "trick/VariableServerThread.hh" -#include "trick/tc_proto.h" +#include "trick/VariableServerSessionThread.hh" #include "trick/exec_proto.h" #include "trick/command_line_protos.h" #include "trick/message_proto.h" @@ -12,32 +11,44 @@ #define MAX_MACHINE_NAME 80 -Trick::VariableServerListenThread::VariableServerListenThread() : - Trick::SysThread("VarServListen"), - requested_port(0), - user_requested_address(false), - broadcast(true), - listener() -{ - pthread_mutex_init(&restart_pause, NULL); - char hname[MAX_MACHINE_NAME]; - gethostname(hname, MAX_MACHINE_NAME); - requested_source_address = std::string(hname); +Trick::VariableServerListenThread::VariableServerListenThread() : VariableServerListenThread (NULL) {} + +Trick::VariableServerListenThread::VariableServerListenThread(TCPClientListener * listener) : + Trick::SysThread("VarServListen"), + _requested_source_address(""), + _requested_port(0), + _user_requested_address(false), + _broadcast(true), + _listener(listener), + _multicast(new MulticastGroup()) +{ + if (_listener != NULL) { + // If we were passed a listener + // We assume it is already initialized + _requested_source_address = _listener->getHostname(); + _requested_port = _listener->getPort(); + _user_requested_address = true; + } else { + // Otherwise, make one + _listener = new TCPClientListener; + } cancellable = false; } Trick::VariableServerListenThread::~VariableServerListenThread() { - // if (multicast != NULL) { - // delete multicast; - // multicast = NULL; - // } + delete _listener; + delete _multicast; } +void Trick::VariableServerListenThread::set_multicast_group (MulticastGroup * group) { + delete _multicast; + _multicast = group; +} const char * Trick::VariableServerListenThread::get_hostname() { - std::string hostname = listener.getHostname(); + std::string hostname = _requested_source_address; char * ret = (char *) malloc(hostname.length() + 1); strncpy(ret, hostname.c_str(), hostname.length()); ret[hostname.length()] = '\0'; @@ -45,80 +56,74 @@ const char * Trick::VariableServerListenThread::get_hostname() { } unsigned short Trick::VariableServerListenThread::get_port() { - return requested_port; + return _requested_port; } void Trick::VariableServerListenThread::set_port(unsigned short in_port) { - requested_port = in_port; - user_requested_address = true ; + _requested_port = in_port; + _user_requested_address = true ; } std::string Trick::VariableServerListenThread::get_user_tag() { - return user_tag ; + return _user_tag ; } const std::string& Trick::VariableServerListenThread::get_user_tag_ref() { - return user_tag ; + return _user_tag ; } void Trick::VariableServerListenThread::set_user_tag(std::string in_tag) { - user_tag = in_tag ; + _user_tag = in_tag ; } void Trick::VariableServerListenThread::set_source_address(const char * address) { if ( address == NULL ) { - requested_source_address = std::string("") ; + _requested_source_address = std::string("") ; } else { - requested_source_address = std::string(address) ; + _requested_source_address = std::string(address) ; } - user_requested_address = true ; + _user_requested_address = true; } std::string Trick::VariableServerListenThread::get_source_address() { - return listener.getHostname() ; + return _requested_source_address ; } bool Trick::VariableServerListenThread::get_broadcast() { - return broadcast; + return _broadcast; } -// in_broadcast atomic? We'll see what tsan says. Maybe go nuts with atomics void Trick::VariableServerListenThread::set_broadcast(bool in_broadcast) { - broadcast = in_broadcast; + _broadcast = in_broadcast; } +// Called from default data int Trick::VariableServerListenThread::init_listen_device() { - int ret = listener.initialize(); - requested_port = listener.getPort(); - user_requested_address = true; + int ret = _listener->initialize(); + _requested_port = _listener->getPort(); + _requested_source_address = _listener->getHostname(); return ret; } +// Called from init jobs int Trick::VariableServerListenThread::check_and_move_listen_device() { int ret ; - /* The user has requested a different source address or port in the input file */ - listener.disconnect(); - ret = listener.initialize(requested_source_address, requested_port); - requested_port = listener.getPort(); - requested_source_address = listener.getHostname(); - if (ret != 0) { - message_publish(MSG_ERROR, "ERROR: Could not establish variable server source_address %s: port %d. Aborting.\n", - requested_source_address.c_str(), requested_port); - return -1 ; + if (_user_requested_address) { + /* The user has requested a different source address or port in the input file */ + _listener->disconnect(); + ret = _listener->initialize(_requested_source_address, _requested_port); + _requested_port = _listener->getPort(); + _requested_source_address = _listener->getHostname(); + if (ret != 0) { + message_publish(MSG_ERROR, "ERROR: Could not establish variable server source_address %s: port %d. Aborting.\n", + _requested_source_address.c_str(), _requested_port); + return -1 ; + } } return 0 ; } -void Trick::VariableServerListenThread::create_tcp_socket(const char * address, unsigned short in_port ) { - listener.initialize(address, in_port); - requested_source_address = listener.getHostname(); - requested_port = listener.getPort(); - user_requested_address = true; - - message_publish(MSG_INFO, "Created TCP variable server %s: %d\n", requested_source_address.c_str(), in_port); -} - void * Trick::VariableServerListenThread::thread_body() { // This thread listens for incoming client connections, and when one is received, creates a new thread to handle the session // Also broadcasts on multicast channel @@ -129,7 +134,7 @@ void * Trick::VariableServerListenThread::thread_body() { std::string version = std::string(exec_get_current_version()) ; version.erase(version.find_last_not_of(" \t\f\v\n\r")+1); - // get username to broadcast on multicast channel + // get username to _broadcast on multicast channel struct passwd *passp = getpwuid(getuid()) ; std::string user_name; if ( passp == NULL ) { @@ -138,9 +143,9 @@ void * Trick::VariableServerListenThread::thread_body() { user_name = strdup(passp->pw_name) ; } - listener.setBlockMode(true); + _listener->setBlockMode(true); - if ( broadcast ) { + if ( _broadcast ) { initializeMulticast(); } @@ -148,20 +153,15 @@ void * Trick::VariableServerListenThread::thread_body() { // Quit here if it's time test_shutdown(); - // Look for a new client requesting a connection - if (listener.checkForNewConnections()) { - // pause here during restart - pthread_mutex_lock(&restart_pause) ; + // Pause here if we need to + test_pause(); - // Recheck - sometimes we get false positive if something happens during restart - if (!listener.checkForNewConnections()) { - pthread_mutex_unlock(&restart_pause) ; - continue; - } + // Look for a new client requesting a connection + if (_listener->checkForNewConnections()) { // Create a new thread to service this connection - VariableServerThread * vst = new Trick::VariableServerThread() ; - vst->open_tcp_connection(&listener) ; + VariableServerSessionThread * vst = new Trick::VariableServerSessionThread() ; + vst->set_connection(_listener->setUpNewConnection()); vst->copy_cpus(get_cpus()) ; vst->create_thread() ; ConnectionStatus status = vst->wait_for_accept() ; @@ -172,23 +172,20 @@ void * Trick::VariableServerListenThread::thread_body() { vst->join_thread(); delete vst; } - - pthread_mutex_unlock(&restart_pause) ; - - } else if ( broadcast ) { + } else if ( _broadcast ) { // Otherwise, broadcast on the multicast channel if enabled char buf1[1024]; - sprintf(buf1 , "%s\t%hu\t%s\t%d\t%s\t%s\t%s\t%s\t%s\t%hu\n" , listener.getHostname(), (unsigned short)listener.getPort() , + snprintf(buf1 , sizeof(buf1), "%s\t%hu\t%s\t%d\t%s\t%s\t%s\t%s\t%s\t%hu\n" , _listener->getHostname().c_str(), (unsigned short)_listener->getPort() , user_name.c_str() , (int)getpid() , command_line_args_get_default_dir() , command_line_args_get_cmdline_name() , - command_line_args_get_input_file() , version.c_str() , user_tag.c_str(), (unsigned short)listener.getPort() ) ; + command_line_args_get_input_file() , version.c_str() , _user_tag.c_str(), (unsigned short)_listener->getPort() ) ; - std::string message = buf1; + std::string message(buf1); - if (!multicast.is_initialized()) { + if (!_multicast->isInitialized()) { // In case broadcast was turned on after this loop was entered initializeMulticast(); } - multicast.broadcast(message); + _multicast->broadcast(message); } } @@ -201,26 +198,27 @@ int Trick::VariableServerListenThread::restart() { int ret ; - listener.restart(); + _listener->restart(); - if ( user_requested_address ) { - - if (!listener.validateSourceAddress(requested_source_address)) { - requested_source_address.clear() ; + if ( _user_requested_address ) { + // If the use requested an address and/or port, make sure we reinitialize to the same one + if (!_listener->validateSourceAddress(_requested_source_address)) { + _requested_source_address.clear() ; } - printf("variable server restart user_port requested set %s:%d\n",requested_source_address.c_str(), requested_port); + printf("variable server restart user_port requested set %s:%d\n",_requested_source_address.c_str(), _requested_port); - listener.disconnect(); - ret = listener.initialize(requested_source_address, requested_port); + _listener->disconnect(); + ret = _listener->initialize(_requested_source_address, _requested_port); if (ret != TC_SUCCESS) { - message_publish(MSG_ERROR, "ERROR: Could not establish listen port %d for Variable Server. Aborting.\n", requested_port); + message_publish(MSG_ERROR, "ERROR: Could not establish listen port %d for Variable Server. Aborting.\n", _requested_port); return (-1); } } else { - listener.checkSocket(); - printf("restart variable server message port = %d\n", listener.getPort()); + // Otherwise, just ask the listener what port it's using + _listener->checkSocket(); + printf("restart variable server message port = %d\n", _listener->getPort()); } initializeMulticast(); @@ -229,27 +227,29 @@ int Trick::VariableServerListenThread::restart() { } void Trick::VariableServerListenThread::initializeMulticast() { - multicast.initialize(); - multicast.addAddress("239.3.14.15", 9265); - multicast.addAddress("224.3.14.15", 9265); + _multicast->initialize(); + _multicast->addAddress("239.3.14.15", 9265); + _multicast->addAddress("224.3.14.15", 9265); } void Trick::VariableServerListenThread::pause_listening() { - pthread_mutex_lock(&restart_pause) ; + // pthread_mutex_lock(&_restart_pause) ; + force_thread_to_pause(); } void Trick::VariableServerListenThread::restart_listening() { - listener.restart(); - pthread_mutex_unlock(&restart_pause) ; + _listener->restart(); + unpause_thread(); + // pthread_mutex_unlock(&_restart_pause) ; } void Trick::VariableServerListenThread::dump( std::ostream & oss ) { oss << "Trick::VariableServerListenThread (" << name << ")" << std::endl ; - oss << " source_address = " << listener.getHostname() << std::endl ; - oss << " port = " << listener.getPort() << std::endl ; - oss << " user_requested_address = " << user_requested_address << std::endl ; - oss << " user_tag = " << user_tag << std::endl ; - oss << " broadcast = " << broadcast << std::endl ; + oss << " source_address = " << _listener->getHostname() << std::endl ; + oss << " port = " << _listener->getPort() << std::endl ; + oss << " user_requested_address = " << _user_requested_address << std::endl ; + oss << " user_tag = " << _user_tag << std::endl ; + oss << " broadcast = " << _broadcast << std::endl ; Trick::ThreadBase::dump(oss) ; } diff --git a/trick_source/sim_services/VariableServer/VariableServerSession.cpp b/trick_source/sim_services/VariableServer/VariableServerSession.cpp index 32c03932..f40d9557 100644 --- a/trick_source/sim_services/VariableServer/VariableServerSession.cpp +++ b/trick_source/sim_services/VariableServer/VariableServerSession.cpp @@ -6,94 +6,116 @@ #include "trick/realtimesync_proto.h" -Trick::VariableServerSession::VariableServerSession(ClientConnection * conn) { - debug = 0; - enabled = true ; - log = false ; - copy_mode = VS_COPY_ASYNC ; - write_mode = VS_WRITE_ASYNC ; - frame_multiple = 1 ; - frame_offset = 0 ; - freeze_frame_multiple = 1 ; - freeze_frame_offset = 0 ; - update_rate = 0.1 ; - cycle_tics = (long long)(update_rate * exec_get_time_tic_value()) ; - if (cycle_tics == 0) { - cycle_tics = 1; +Trick::VariableServerSession::VariableServerSession() { + _debug = 0; + _enabled = true ; + _log = false ; + _copy_mode = VS_COPY_ASYNC ; + _write_mode = VS_WRITE_ASYNC ; + _frame_multiple = 1 ; + _frame_offset = 0 ; + _freeze_frame_multiple = 1 ; + _freeze_frame_offset = 0 ; + _update_rate = 0.1 ; + _cycle_tics = (long long)(_update_rate * exec_get_time_tic_value()) ; + if (_cycle_tics == 0) { + _cycle_tics = 1; } - next_tics = TRICK_MAX_LONG_LONG ; - freeze_next_tics = TRICK_MAX_LONG_LONG ; - // multicast = false; - connection = conn; - byteswap = false ; - validate_address = false ; - send_stdio = false ; + _next_tics = TRICK_MAX_LONG_LONG ; + _freeze_next_tics = TRICK_MAX_LONG_LONG ; + _byteswap = false ; + _validate_address = false ; + _send_stdio = false ; - binary_data = false; - byteswap = false; - binary_data_nonames = false; - packets_copied = 0 ; + _binary_data = false; + _byteswap = false; + _binary_data_nonames = false; - exit_cmd = false; - pause_cmd = false; + _exit_cmd = false; + _pause_cmd = false; - - // incoming_msg = (char *) calloc(MAX_CMD_LEN, 1); - // stripped_msg = (char *) calloc(MAX_CMD_LEN, 1); - - - pthread_mutex_init(©_mutex, NULL); + pthread_mutex_init(&_copy_mutex, NULL); } Trick::VariableServerSession::~VariableServerSession() { - // free (incoming_msg); - // free (stripped_msg); + for (unsigned int ii = 0 ; ii < _session_variables.size() ; ii++ ) { + delete _session_variables[ii]; + } + } + + +void Trick::VariableServerSession::set_connection(ClientConnection * conn) { + _connection = conn; } + // Command to turn on log to varserver_log file int Trick::VariableServerSession::set_log_on() { - log = true; + _log = true; return(0) ; } // Command to turn off log to varserver_log file int Trick::VariableServerSession::set_log_off() { - log = false; + _log = false; return(0) ; } +bool Trick::VariableServerSession::get_pause() { + return _pause_cmd ; +} + +void Trick::VariableServerSession::set_pause( bool on_off) { + _pause_cmd = on_off ; +} + + +bool Trick::VariableServerSession::get_exit_cmd() { + return _exit_cmd ; +} + +void Trick::VariableServerSession::pause_copy() { + pthread_mutex_lock(&_copy_mutex); +} + +void Trick::VariableServerSession::unpause_copy() { + pthread_mutex_unlock(&_copy_mutex); +} + void Trick::VariableServerSession::disconnect_references() { - for (VariableReference * variable : session_variables) { + for (VariableReference * variable : _session_variables) { variable->tagAsInvalid(); } } long long Trick::VariableServerSession::get_next_tics() const { - if ( ! enabled ) { + if ( ! _enabled ) { return TRICK_MAX_LONG_LONG ; } - return next_tics ; + return _next_tics ; } long long Trick::VariableServerSession::get_freeze_next_tics() const { - if ( ! enabled ) { + if ( ! _enabled ) { return TRICK_MAX_LONG_LONG ; } - return freeze_next_tics ; + return _freeze_next_tics ; } -int Trick::VariableServerSession::handleMessage() { +int Trick::VariableServerSession::handle_message() { - std::string received_message = connection->read(ClientConnection::MAX_CMD_LEN); - if (received_message.size() > 0) { + std::string received_message; + int nbytes = _connection->read(received_message); + if (nbytes > 0) { ip_parse(received_message.c_str()); /* returns 0 if no parsing error */ } - + + return nbytes; } Trick::VariableReference * Trick::VariableServerSession::find_session_variable(std::string name) const { - for (VariableReference * ref : session_variables) { + for (VariableReference * ref : _session_variables) { // Look for matching name if (name.compare(ref->getName()) == 0) { return ref; @@ -104,19 +126,57 @@ Trick::VariableReference * Trick::VariableServerSession::find_session_variable(s } double Trick::VariableServerSession::get_update_rate() const { - return update_rate; + return _update_rate; } VS_WRITE_MODE Trick::VariableServerSession::get_write_mode () const { - return write_mode; + return _write_mode; } VS_COPY_MODE Trick::VariableServerSession::get_copy_mode () const { - return copy_mode; + return _copy_mode; } +long long Trick::VariableServerSession::get_cycle_tics() const { + return _cycle_tics; +} + +int Trick::VariableServerSession::get_frame_multiple () const { + return _frame_multiple; +} + +int Trick::VariableServerSession::get_frame_offset () const { + return _frame_offset; +} + +int Trick::VariableServerSession::get_freeze_frame_multiple () const { + return _freeze_frame_multiple; +} + +int Trick::VariableServerSession::get_freeze_frame_offset () const { + return _freeze_frame_offset; +} + +bool Trick::VariableServerSession::get_enabled () const { + return _enabled; +} + +void Trick::VariableServerSession::set_freeze_next_tics(long long tics) { + _freeze_next_tics = tics; +} + +void Trick::VariableServerSession::set_next_tics(long long tics) { + _next_tics = tics; +} + +void Trick::VariableServerSession::set_exit_cmd() { + _exit_cmd = true; +} + + + std::ostream& Trick::operator<< (std::ostream& s, const Trick::VariableServerSession& session) { - if (session.binary_data) { + if (session._binary_data) { s << " \"format\":\"BINARY\",\n"; } else { s << " \"format\":\"ASCII\",\n"; @@ -125,9 +185,9 @@ std::ostream& Trick::operator<< (std::ostream& s, const Trick::VariableServerSes s << " \"variables\":[\n"; - int n_vars = (int)session.session_variables.size(); + int n_vars = (int)session._session_variables.size(); for (int i=0 ; i1) { s << "," ; } diff --git a/trick_source/sim_services/VariableServer/VariableServerSessionThread.cpp b/trick_source/sim_services/VariableServer/VariableServerSessionThread.cpp new file mode 100644 index 00000000..a1108d23 --- /dev/null +++ b/trick_source/sim_services/VariableServer/VariableServerSessionThread.cpp @@ -0,0 +1,134 @@ + +#include +#include +#include "trick/VariableServerSessionThread.hh" +#include "trick/exec_proto.h" +#include "trick/message_proto.h" +#include "trick/message_type.h" +#include "trick/TrickConstant.hh" +#include "trick/UDPConnection.hh" +#include "trick/TCPConnection.hh" + + +Trick::VariableServer * Trick::VariableServerSessionThread::_vs = NULL ; + +static int instance_num = 0; + +Trick::VariableServerSessionThread::VariableServerSessionThread() : VariableServerSessionThread (new VariableServerSession()) {} + +Trick::VariableServerSessionThread::VariableServerSessionThread(VariableServerSession * session) : + Trick::SysThread(std::string("VarServer" + std::to_string(instance_num++))) , _debug(0), _session(session), _connection(NULL) { + + _connection_status = CONNECTION_PENDING ; + + + pthread_mutex_init(&_connection_status_mutex, NULL); + pthread_cond_init(&_connection_status_cv, NULL); + + cancellable = false; +} + +Trick::VariableServerSessionThread::~VariableServerSessionThread() { + cleanup(); +} + +std::ostream& Trick::operator<< (std::ostream& s, Trick::VariableServerSessionThread& vst) { + // Write a JSON representation of a Trick::VariableServerSessionThread to an ostream. + s << " \"connection\":{\n"; + s << " \"client_tag\":\"" << vst._connection->getClientTag() << "\",\n"; + + s << " \"client_IP_address\":\"" << vst._connection->getClientHostname() << "\",\n"; + s << " \"client_port\":\"" << vst._connection->getClientPort() << "\",\n"; + + pthread_mutex_lock(&vst._connection_status_mutex); + if (vst._connection_status == CONNECTION_SUCCESS) { + s << *(vst._session); + } + pthread_mutex_unlock(&vst._connection_status_mutex); + + s << " }" << std::endl; + return s; +} + +void Trick::VariableServerSessionThread::set_vs_ptr(Trick::VariableServer * in_vs) { + _vs = in_vs ; +} + +Trick::VariableServer * Trick::VariableServerSessionThread::get_vs() { + return _vs ; +} + +void Trick::VariableServerSessionThread::set_client_tag(std::string tag) { + _connection->setClientTag(tag); +} + +void Trick::VariableServerSessionThread::set_connection(Trick::ClientConnection * in_connection) { + _connection = in_connection; +} + +Trick::ConnectionStatus Trick::VariableServerSessionThread::wait_for_accept() { + + pthread_mutex_lock(&_connection_status_mutex); + while ( _connection_status == CONNECTION_PENDING ) { + pthread_cond_wait(&_connection_status_cv, &_connection_status_mutex); + } + pthread_mutex_unlock(&_connection_status_mutex); + + return _connection_status; +} + +// Gets called from the main thread as a job +void Trick::VariableServerSessionThread::preload_checkpoint() { + + // Stop variable server processing at the top of the processing loop. + force_thread_to_pause(); + + + // Make sure that the _session has been initialized + pthread_mutex_lock(&_connection_status_mutex); + if (_connection_status == CONNECTION_SUCCESS) { + + // Let the thread complete any data copying it has to do + // and then suspend data copying until the checkpoint is reloaded. + _session->pause_copy(); + + // 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. + _session->unpause_copy(); + } + pthread_mutex_unlock(&_connection_status_mutex); +} + +// Gets called from the main thread as a job +void Trick::VariableServerSessionThread::restart() { + // Set the pause state of this thread back to its "pre-checkpoint reload" state. + _connection->restart(); + + pthread_mutex_lock(&_connection_status_mutex); + if (_connection_status == CONNECTION_SUCCESS) { + _session->set_pause(_saved_pause_cmd) ; + } + pthread_mutex_unlock(&_connection_status_mutex); + + // Restart the variable server processing. + unpause_thread(); +} + +void Trick::VariableServerSessionThread::cleanup() { + _connection->disconnect(); + + if (_session != NULL) { + delete _session; + _session = NULL; + } +} + diff --git a/trick_source/sim_services/VariableServer/VariableServerThread_loop.cpp b/trick_source/sim_services/VariableServer/VariableServerSessionThread_loop.cpp similarity index 50% rename from trick_source/sim_services/VariableServer/VariableServerThread_loop.cpp rename to trick_source/sim_services/VariableServer/VariableServerSessionThread_loop.cpp index 7c68d08d..b0126daf 100644 --- a/trick_source/sim_services/VariableServer/VariableServerThread_loop.cpp +++ b/trick_source/sim_services/VariableServer/VariableServerSessionThread_loop.cpp @@ -1,65 +1,58 @@ -#include -#include #include #ifdef __linux #include #endif -#include - #include "trick/VariableServer.hh" -#include "trick/variable_server_sync_types.h" -#include "trick/input_processor_proto.h" -#include "trick/tc_proto.h" #include "trick/message_proto.h" -#include "trick/message_type.h" #include "trick/realtimesync_proto.h" #include "trick/ExecutiveException.hh" #include "trick/exec_proto.h" +#include "trick/VariableServerSessionThread.hh" + void exit_var_thread(void *in_vst) ; -void * Trick::VariableServerThread::thread_body() { +void * Trick::VariableServerSessionThread::thread_body() { // Check for short running sims test_shutdown(NULL, NULL); - // We need to make the thread to VariableServerThread map before we accept the connection. + // We need to make the thread to VariableServerSessionThread map before we accept the connection. // Otherwise we have a race where this thread is unknown to the variable server and the // client gets confirmation that the connection is ready for communication. - vs->add_vst( pthread_self() , this ) ; + _vs->add_vst( pthread_self() , this ) ; // Accept client connection - int status = connection->start(); + int status = _connection->start(); if (status != 0) { - // TODO: Use a real error handler - vs->delete_vst(pthread_self()); + _vs->delete_vst(pthread_self()); // Tell main thread that we failed to initialize - pthread_mutex_lock(&connection_status_mutex); - connection_status = CONNECTION_FAIL; - pthread_cond_signal(&connection_status_cv); - pthread_mutex_unlock(&connection_status_mutex); + pthread_mutex_lock(&_connection_status_mutex); + _connection_status = CONNECTION_FAIL; + pthread_cond_signal(&_connection_status_cv); + pthread_mutex_unlock(&_connection_status_mutex); - cleanup(); - pthread_exit(NULL); + thread_shutdown(); } - // Create session - session = new VariableServerSession(connection); - vs->add_session( pthread_self(), session ); + // Give the initialized connection to the session + // Don't touch the connection anymore until we shut them both down + _session->set_connection(_connection); + _vs->add_session( pthread_self(), _session ); // Tell main that we are ready - pthread_mutex_lock(&connection_status_mutex); - connection_status = CONNECTION_SUCCESS; - pthread_cond_signal(&connection_status_cv); - pthread_mutex_unlock(&connection_status_mutex); + pthread_mutex_lock(&_connection_status_mutex); + _connection_status = CONNECTION_SUCCESS; + pthread_cond_signal(&_connection_status_cv); + pthread_mutex_unlock(&_connection_status_mutex); // if log is set on for variable server (e.g., in input file), turn log on for each client - if (vs->get_log()) { - session->set_log_on(); + if (_vs->get_log()) { + _session->set_log_on(); } try { @@ -68,47 +61,41 @@ void * Trick::VariableServerThread::thread_body() { test_shutdown(exit_var_thread, (void *) this); // Pause here if we are in a restart condition - pthread_mutex_lock(&restart_pause) ; + test_pause(); // Look for a message from the client // Parse and execute if one is availible - session->handleMessage(); + int read_status = _session->handle_message(); + if ( read_status < 0 ) { + break ; + } // Check to see if exit is necessary - if (session->exit_cmd == true) { - pthread_mutex_unlock(&restart_pause) ; + if (_session->get_exit_cmd() == true) { break; } - // Copy data out of sim if async mode - if ( session->get_copy_mode() == VS_COPY_ASYNC ) { - session->copy_sim_data() ; + // Tell session it's time to copy and write if the mode is correct + int ret =_session->copy_and_write_async(); + if (ret < 0) { + break; } - - bool should_write_async = (session->get_write_mode() == VS_WRITE_ASYNC) || - ( session->get_copy_mode() == VS_COPY_ASYNC && (session->get_write_mode() == VS_WRITE_WHEN_COPIED)) || - (! is_real_time()); - // Write data out to connection if async mode and not paused - if ( should_write_async && !session->get_pause()) { - int ret = session->write_data() ; - if ( ret < 0 ) { - pthread_mutex_unlock(&restart_pause) ; - break ; - } - } - pthread_mutex_unlock(&restart_pause) ; - - usleep((unsigned int) (session->get_update_rate() * 1000000)); + // Sleep for the appropriate cycle time + usleep((unsigned int) (_session->get_update_rate() * 1000000)); } } catch (Trick::ExecutiveException & ex ) { message_publish(MSG_ERROR, "\nVARIABLE SERVER COMMANDED exec_terminate\n ROUTINE: %s\n DIAGNOSTIC: %s\n" , ex.file.c_str(), ex.message.c_str()) ; - exit(ex.ret_code) ; + + exec_signal_terminate(); + } catch (const std::exception &ex) { message_publish(MSG_ERROR, "\nVARIABLE SERVER caught std::exception\n DIAGNOSTIC: %s\n" , ex.what()) ; - exit(-1) ; + + exec_signal_terminate(); + #ifdef __linux #ifdef __GNUC__ #if __GNUC__ >= 4 && __GNUC_MINOR__ >= 2 @@ -128,19 +115,17 @@ void * Trick::VariableServerThread::thread_body() { throw; #else message_publish(MSG_ERROR, "\nVARIABLE SERVER caught unknown exception\n" ) ; - exit(-1) ; + exec_signal_terminate(); #endif #endif #endif } - if (debug >= 3) { - message_publish(MSG_DEBUG, "%p tag=<%s> var_server receive loop exiting\n", connection, connection->getClientTag().c_str()); + if (_debug >= 3) { + message_publish(MSG_DEBUG, "%p tag=<%s> var_server receive loop exiting\n", _connection, _connection->getClientTag().c_str()); } thread_shutdown(exit_var_thread, this); - - return NULL ; - + // No return from this. } diff --git a/trick_source/sim_services/VariableServer/VariableServerSession_commands.cpp b/trick_source/sim_services/VariableServer/VariableServerSession_commands.cpp index bd399503..7d7d2e6f 100644 --- a/trick_source/sim_services/VariableServer/VariableServerSession_commands.cpp +++ b/trick_source/sim_services/VariableServer/VariableServerSession_commands.cpp @@ -7,7 +7,6 @@ #include "trick/VariableServerSession.hh" #include "trick/variable_server_message_types.h" #include "trick/memorymanager_c_intf.h" -// #include "trick/tc_proto.h" #include "trick/exec_proto.h" #include "trick/command_line_protos.h" #include "trick/message_proto.h" @@ -21,12 +20,12 @@ int Trick::VariableServerSession::var_add(std::string in_name) { VariableReference * new_var; if (in_name == "time") { - new_var = new VariableReference(in_name, &time); + new_var = new VariableReference(in_name, &_time); } else { new_var = new VariableReference(in_name); } - session_variables.push_back(new_var) ; + _session_variables.push_back(new_var) ; return(0) ; } @@ -60,7 +59,7 @@ int Trick::VariableServerSession::var_send_once(std::string in_name, int num_var for (auto& varName : var_names) { VariableReference * new_var; if (varName == "time") { - new_var = new VariableReference(varName, &time); + new_var = new VariableReference(varName, &_time); } else { new_var = new VariableReference(varName); } @@ -75,11 +74,11 @@ int Trick::VariableServerSession::var_send_once(std::string in_name, int num_var int Trick::VariableServerSession::var_remove(std::string in_name) { - for (unsigned int ii = 0 ; ii < session_variables.size() ; ii++ ) { - std::string var_name = session_variables[ii]->getName(); + for (unsigned int ii = 0 ; ii < _session_variables.size() ; ii++ ) { + std::string var_name = _session_variables[ii]->getName(); if ( ! var_name.compare(in_name) ) { - delete session_variables[ii]; - session_variables.erase(session_variables.begin() + ii) ; + delete _session_variables[ii]; + _session_variables.erase(_session_variables.begin() + ii) ; break ; } } @@ -110,29 +109,29 @@ int Trick::VariableServerSession::var_exists(std::string in_name) { error = true; } - if (binary_data) { + if (_binary_data) { /* send binary 1 or 0 */ msg_type = VS_VAR_EXISTS ; memcpy(buf1, &msg_type , sizeof(msg_type)) ; buf1[4] = (error==false); - if (debug >= 2) { - // message_publish(MSG_DEBUG, "%p tag=<%s> var_server sending 1 binary byte\n", &connection, connection.client_tag); + if (_debug >= 2) { + // message_publish(MSG_DEBUG, "%p tag=<%s> var_server sending 1 binary byte\n", &_connection, _connection.client_tag); } - connection->write(buf1, 5); + _connection->write(buf1, 5); } else { /* send ascii "1" or "0" */ sprintf(buf1, "%d\t%d\n", VS_VAR_EXISTS, (error==false)); - if (debug >= 2) { - // message_publish(MSG_DEBUG, "%p tag=<%s> var_server sending:\n%s\n", &connection, connection.client_tag, buf1) ; + if (_debug >= 2) { + // message_publish(MSG_DEBUG, "%p tag=<%s> var_server sending:\n%s\n", &_connection, _connection.client_tag, buf1) ; } std::string write_string(buf1); if (write_string.length() != strlen(buf1)) { std::cout << "PROBLEM WITH STRING LENGTH: VAR_EXISTS ASCII" << std::endl; } - connection->write(write_string); + _connection->write(write_string); } return(0) ; @@ -140,9 +139,9 @@ int Trick::VariableServerSession::var_exists(std::string in_name) { int Trick::VariableServerSession::var_clear() { - while( !session_variables.empty() ) { - delete session_variables.back(); - session_variables.pop_back(); + while( !_session_variables.empty() ) { + delete _session_variables.back(); + _session_variables.pop_back(); } return(0) ; @@ -155,67 +154,59 @@ int Trick::VariableServerSession::var_send() { } int Trick::VariableServerSession::var_cycle(double in_rate) { - update_rate = in_rate ; - cycle_tics = (long long)(update_rate * exec_get_time_tic_value()) ; + _update_rate = in_rate ; + _cycle_tics = (long long)(_update_rate * exec_get_time_tic_value()) ; return(0) ; } -bool Trick::VariableServerSession::get_pause() { - return pause_cmd ; -} - -void Trick::VariableServerSession::set_pause( bool on_off) { - pause_cmd = on_off ; -} - int Trick::VariableServerSession::var_exit() { - exit_cmd = true ; + _exit_cmd = true ; return(0) ; } int Trick::VariableServerSession::var_validate_address(bool on_off) { - validate_address = on_off ; + _validate_address = on_off ; return(0) ; } int Trick::VariableServerSession::var_debug(int level) { - debug = level ; + _debug = level ; return(0) ; } int Trick::VariableServerSession::var_ascii() { - binary_data = 0 ; + _binary_data = 0 ; return(0) ; } int Trick::VariableServerSession::var_binary() { - binary_data = 1 ; + _binary_data = 1 ; return(0) ; } int Trick::VariableServerSession::var_binary_nonames() { - binary_data = 1 ; - binary_data_nonames = 1 ; + _binary_data = 1 ; + _binary_data_nonames = 1 ; return(0) ; } int Trick::VariableServerSession::var_set_copy_mode(int mode) { if ( mode >= VS_COPY_ASYNC and mode <= VS_COPY_TOP_OF_FRAME ) { - copy_mode = (VS_COPY_MODE)mode ; - if ( copy_mode == VS_COPY_SCHEDULED ) { + _copy_mode = (VS_COPY_MODE)mode ; + if ( _copy_mode == VS_COPY_SCHEDULED ) { long long sim_time_tics ; sim_time_tics = exec_get_time_tics() ; // round the next call time to a multiple of the cycle - sim_time_tics -= sim_time_tics % cycle_tics ; - next_tics = sim_time_tics + cycle_tics ; + sim_time_tics -= sim_time_tics % _cycle_tics ; + _next_tics = sim_time_tics + _cycle_tics ; sim_time_tics = exec_get_freeze_time_tics() ; // round the next call time to a multiple of the cycle - sim_time_tics -= sim_time_tics % cycle_tics ; - freeze_next_tics = sim_time_tics + cycle_tics ; + sim_time_tics -= sim_time_tics % _cycle_tics ; + _freeze_next_tics = sim_time_tics + _cycle_tics ; } else { - next_tics = TRICK_MAX_LONG_LONG ; + _next_tics = TRICK_MAX_LONG_LONG ; } return 0 ; } @@ -224,7 +215,7 @@ int Trick::VariableServerSession::var_set_copy_mode(int mode) { int Trick::VariableServerSession::var_set_write_mode(int mode) { if ( mode >= VS_WRITE_ASYNC and mode <= VS_WRITE_WHEN_COPIED ) { - write_mode = (VS_WRITE_MODE)mode ; + _write_mode = (VS_WRITE_MODE)mode ; return 0 ; } return -1 ; @@ -252,46 +243,46 @@ int Trick::VariableServerSession::var_sync(int mode) { } int Trick::VariableServerSession::var_set_frame_multiple(unsigned int mult) { - frame_multiple = mult ; + _frame_multiple = mult ; return 0 ; } int Trick::VariableServerSession::var_set_frame_offset(unsigned int offset) { - frame_offset = offset ; + _frame_offset = offset ; return 0 ; } int Trick::VariableServerSession::var_set_freeze_frame_multiple(unsigned int mult) { - freeze_frame_multiple = mult ; + _freeze_frame_multiple = mult ; return 0 ; } int Trick::VariableServerSession::var_set_freeze_frame_offset(unsigned int offset) { - freeze_frame_offset = offset ; + _freeze_frame_offset = offset ; return 0 ; } int Trick::VariableServerSession::var_byteswap(bool on_off) { - byteswap = on_off ; + _byteswap = on_off ; return(0) ; } bool Trick::VariableServerSession::get_send_stdio() { - return send_stdio ; + return _send_stdio ; } int Trick::VariableServerSession::set_send_stdio(bool on_off) { - send_stdio = on_off ; + _send_stdio = on_off ; return(0) ; } int Trick::VariableServerSession::send_list_size() { unsigned int msg_type = VS_LIST_SIZE; - int var_count = session_variables.size(); + int var_count = _session_variables.size(); // send number of variables - if (binary_data) { + if (_binary_data) { // send in the binary message header format: // char buf1[12] ; @@ -302,20 +293,20 @@ int Trick::VariableServerSession::send_list_size() { memset(&(buf1[4]), 0, sizeof(int)); // message size = 0 memcpy(&(buf1[8]), &var_count, sizeof(var_count)); - if (debug >= 2) { - message_publish(MSG_DEBUG, "%p tag=<%s> var_server sending %d event variables\n", connection, connection->getClientTag().c_str(), var_count); + if (_debug >= 2) { + message_publish(MSG_DEBUG, "%p tag=<%s> var_server sending %d event variables\n", _connection, _connection->getClientTag().c_str(), var_count); } - connection->write(buf1, sizeof (buf1)); + _connection->write(buf1, sizeof (buf1)); } else { std::stringstream write_string; write_string << VS_LIST_SIZE << "\t" << var_count << "\n"; // ascii - if (debug >= 2) { - message_publish(MSG_DEBUG, "%p tag=<%s> var_server sending number of event variables:\n%s\n", connection, connection->getClientTag().c_str(), write_string.str().c_str()) ; + if (_debug >= 2) { + message_publish(MSG_DEBUG, "%p tag=<%s> var_server sending number of event variables:\n%s\n", _connection, _connection->getClientTag().c_str(), write_string.str().c_str()) ; } - connection->write(write_string.str()); + _connection->write(write_string.str()); } return 0 ; @@ -330,15 +321,15 @@ int Trick::VariableServerSession::transmit_file(std::string sie_file) { char buffer[packet_size+1] ; int ret ; - if (debug >= 2) { - message_publish(MSG_DEBUG,"%p tag=<%s> var_server opening %s.\n", connection, connection->getClientTag().c_str(), sie_file.c_str()) ; + if (_debug >= 2) { + message_publish(MSG_DEBUG,"%p tag=<%s> var_server opening %s.\n", _connection, _connection->getClientTag().c_str(), sie_file.c_str()) ; } if ((fp = fopen(sie_file.c_str() , "r")) == NULL ) { message_publish(MSG_ERROR,"Variable Server Error: Cannot open %s.\n", sie_file.c_str()) ; sprintf(buffer, "%d\t-1\n", VS_SIE_RESOURCE) ; std::string message(buffer); - connection->write(message); + _connection->write(message); return(-1) ; } @@ -347,11 +338,11 @@ int Trick::VariableServerSession::transmit_file(std::string sie_file) { sprintf(buffer, "%d\t%u\n\0" , VS_SIE_RESOURCE, file_size) ; std::string message(buffer); - connection->write(message); + _connection->write(message); rewind(fp) ; // Switch to blocking writes since this could be a large transfer. - if (connection->setBlockMode(true)) { + if (_connection->setBlockMode(true)) { message_publish(MSG_DEBUG,"Variable Server Error: Failed to set socket to blocking mode.\n"); } @@ -359,7 +350,7 @@ int Trick::VariableServerSession::transmit_file(std::string sie_file) { bytes_read = fread(buffer , 1 , packet_size , fp) ; message = std::string(buffer); message.resize(bytes_read); - ret = connection->write(message); + ret = _connection->write(message); if (ret != (int)bytes_read) { message_publish(MSG_ERROR,"Variable Server Error: Failed to send SIE file. Bytes read: %d Bytes sent: %d\n", bytes_read, ret) ; return(-1); @@ -368,7 +359,7 @@ int Trick::VariableServerSession::transmit_file(std::string sie_file) { } // Switch back to non-blocking writes. - if (connection->setBlockMode(false)) { + if (_connection->setBlockMode(false)) { message_publish(MSG_DEBUG,"Variable Server Error: Failed to set socket to non-blocking mode.\n"); return(-1); } diff --git a/trick_source/sim_services/VariableServer/VariableServerSession_copy_and_write_modes.cpp b/trick_source/sim_services/VariableServer/VariableServerSession_copy_and_write_modes.cpp new file mode 100644 index 00000000..15a74656 --- /dev/null +++ b/trick_source/sim_services/VariableServer/VariableServerSession_copy_and_write_modes.cpp @@ -0,0 +1,119 @@ + +#include +#include + +#include "trick/VariableServerSession.hh" +#include "trick/variable_server_sync_types.h" +#include "trick/realtimesync_proto.h" + +// These methods should be called from approprate jobs or from the VST + +int Trick::VariableServerSession::copy_and_write_freeze(long long curr_freeze_frame) { + int ret = 0 ; + + if (!get_enabled()) + return ret; + + if (get_copy_mode() == VS_COPY_TOP_OF_FRAME) { + long long temp_frame = curr_freeze_frame % get_freeze_frame_multiple() ; + if ( temp_frame == get_freeze_frame_offset() ) { + copy_sim_data() ; + if ( !get_pause() and get_write_mode() == VS_WRITE_WHEN_COPIED and is_real_time()) { + ret = write_data() ; + if ( ret < 0 ) { + set_exit_cmd(); + } + } + } + } + + return ret ; +} + +int Trick::VariableServerSession::copy_and_write_freeze_scheduled(long long curr_tics) { + int ret = 0 ; + + if (!get_enabled()) + return ret; + + if (get_copy_mode() == VS_COPY_SCHEDULED) { + if ( get_freeze_next_tics() <= curr_tics ) { + copy_sim_data() ; + if ( !get_pause() && get_write_mode() == VS_WRITE_WHEN_COPIED && is_real_time()) { + ret = write_data() ; + if ( ret < 0 ) { + set_exit_cmd(); + } + } + set_freeze_next_tics(curr_tics + get_cycle_tics()) ; + } + } + return ret ; +} + +int Trick::VariableServerSession::copy_and_write_scheduled(long long curr_tics) { + int ret = 0 ; + + if (!get_enabled()) + return ret; + + if (get_copy_mode() == VS_COPY_SCHEDULED) { + if ( get_next_tics() <= curr_tics ) { + copy_sim_data() ; + if ( !get_pause() && get_write_mode() == VS_WRITE_WHEN_COPIED && is_real_time()) { + ret = write_data() ; + if ( ret < 0 ) { + set_exit_cmd(); + } + } + set_next_tics(curr_tics + get_cycle_tics()) ; + } + } + return ret ; +} + +int Trick::VariableServerSession::copy_and_write_top(long long curr_frame) { + int ret = 0 ; + + if (!get_enabled()) + return ret; + + if (get_copy_mode() == VS_COPY_TOP_OF_FRAME) { + long long temp_frame = curr_frame % get_frame_multiple() ; + if ( temp_frame == get_frame_offset() ) { + copy_sim_data() ; + if ( !get_pause() && get_write_mode() == VS_WRITE_WHEN_COPIED && is_real_time()) { + ret = write_data() ; + if ( ret < 0 ) { + set_exit_cmd(); + } + } + } + } + return ret ; +} + +int Trick::VariableServerSession::copy_and_write_async() { + int ret = 0; + + if (!get_enabled()) + return ret; + + if (get_copy_mode() == VS_COPY_ASYNC ) { + copy_sim_data() ; + } + + // Write data out to connection if async mode or non-realtime, and not paused + bool should_write_async = (get_write_mode() == VS_WRITE_ASYNC) || + ( get_copy_mode() == VS_COPY_ASYNC && get_write_mode() == VS_WRITE_WHEN_COPIED)|| + (! is_real_time()); + + if ( !get_pause() && should_write_async) { + ret = write_data() ; + if ( ret < 0 ) { + set_exit_cmd(); + } + } + + return ret; +} diff --git a/trick_source/sim_services/VariableServer/VariableServerSession_copy_data.cpp b/trick_source/sim_services/VariableServer/VariableServerSession_copy_data.cpp deleted file mode 100644 index 2b6325fc..00000000 --- a/trick_source/sim_services/VariableServer/VariableServerSession_copy_data.cpp +++ /dev/null @@ -1,90 +0,0 @@ - -#include -#include - -#include "trick/VariableServerSession.hh" -#include "trick/variable_server_sync_types.h" -#include "trick/exec_proto.h" -#include "trick/realtimesync_proto.h" - -// These methods should be called from main thread jobs - -int Trick::VariableServerSession::copy_data_freeze() { - - int ret = 0 ; - long long curr_frame = exec_get_freeze_frame_count() ; - long long temp_frame ; - - if ( enabled and copy_mode == VS_COPY_TOP_OF_FRAME) { - temp_frame = curr_frame % freeze_frame_multiple ; - if ( temp_frame == freeze_frame_offset ) { - copy_sim_data() ; - if ( !pause_cmd and write_mode == VS_WRITE_WHEN_COPIED and is_real_time()) { - ret = write_data() ; - if ( ret < 0 ) { - exit_cmd = true ; - } - } - } - } - return ret ; -} - -int Trick::VariableServerSession::copy_data_freeze_scheduled(long long curr_tics) { - - int ret = 0 ; - - if ( enabled and copy_mode == VS_COPY_SCHEDULED) { - if ( freeze_next_tics <= curr_tics ) { - copy_sim_data() ; - if ( !pause_cmd and write_mode == VS_WRITE_WHEN_COPIED and is_real_time()) { - ret = write_data() ; - if ( ret < 0 ) { - exit_cmd = true ; - } - } - freeze_next_tics = curr_tics + cycle_tics ; - } - } - return ret ; -} - -int Trick::VariableServerSession::copy_data_scheduled(long long curr_tics) { - - int ret = 0 ; - - if ( enabled and copy_mode == VS_COPY_SCHEDULED) { - if ( next_tics <= curr_tics ) { - copy_sim_data() ; - if ( !pause_cmd and write_mode == VS_WRITE_WHEN_COPIED and is_real_time()) { - ret = write_data() ; - if ( ret < 0 ) { - exit_cmd = true ; - } - } - next_tics = curr_tics + cycle_tics ; - } - } - return ret ; -} - -int Trick::VariableServerSession::copy_data_top() { - - int ret = 0 ; - long long curr_frame = exec_get_frame_count() ; - long long temp_frame ; - - if ( enabled and copy_mode == VS_COPY_TOP_OF_FRAME) { - temp_frame = curr_frame % frame_multiple ; - if ( temp_frame == frame_offset ) { - copy_sim_data() ; - if ( !pause_cmd and write_mode == VS_WRITE_WHEN_COPIED and is_real_time()) { - ret = write_data() ; - if ( ret < 0 ) { - exit_cmd = true ; - } - } - } - } - return ret ; -} diff --git a/trick_source/sim_services/VariableServer/VariableServerSession_copy_sim_data.cpp b/trick_source/sim_services/VariableServer/VariableServerSession_copy_sim_data.cpp index 2598f39f..50febc3f 100644 --- a/trick_source/sim_services/VariableServer/VariableServerSession_copy_sim_data.cpp +++ b/trick_source/sim_services/VariableServer/VariableServerSession_copy_sim_data.cpp @@ -10,7 +10,7 @@ // These actually do the copying int Trick::VariableServerSession::copy_sim_data() { - return copy_sim_data(session_variables, true); + return copy_sim_data(_session_variables, true); } int Trick::VariableServerSession::copy_sim_data(std::vector& given_vars, bool cyclical) { @@ -19,16 +19,16 @@ int Trick::VariableServerSession::copy_sim_data(std::vector return 0; } - if ( pthread_mutex_trylock(©_mutex) == 0 ) { + if ( pthread_mutex_trylock(&_copy_mutex) == 0 ) { // Get the simulation time we start this copy - time = (double)exec_get_time_tics() / exec_get_time_tic_value() ; + _time = (double)exec_get_time_tics() / exec_get_time_tic_value() ; for (auto curr_var : given_vars ) { curr_var->stageValue(); } - pthread_mutex_unlock(©_mutex) ; + pthread_mutex_unlock(&_copy_mutex) ; } return 0; diff --git a/trick_source/sim_services/VariableServer/VariableServerSession_freeze_init.cpp b/trick_source/sim_services/VariableServer/VariableServerSession_freeze_init.cpp index 03baeba2..0f16323b 100644 --- a/trick_source/sim_services/VariableServer/VariableServerSession_freeze_init.cpp +++ b/trick_source/sim_services/VariableServer/VariableServerSession_freeze_init.cpp @@ -6,10 +6,10 @@ #include "trick/TrickConstant.hh" int Trick::VariableServerSession::freeze_init() { - if ( enabled && copy_mode == VS_COPY_SCHEDULED) { - freeze_next_tics = cycle_tics ; + if ( _enabled && _copy_mode == VS_COPY_SCHEDULED) { + _freeze_next_tics = _cycle_tics ; } else { - freeze_next_tics = TRICK_MAX_LONG_LONG ; + _freeze_next_tics = TRICK_MAX_LONG_LONG ; } return 0 ; } diff --git a/trick_source/sim_services/VariableServer/VariableServerSession_write_data.cpp b/trick_source/sim_services/VariableServer/VariableServerSession_write_data.cpp index 33cda9ab..2ae2fa03 100644 --- a/trick_source/sim_services/VariableServer/VariableServerSession_write_data.cpp +++ b/trick_source/sim_services/VariableServer/VariableServerSession_write_data.cpp @@ -15,11 +15,6 @@ PROGRAMMERS: (((Alex Lin) (NASA) (8/06) (--))) #include "trick/message_proto.h" #include "trick/message_type.h" - -extern "C" { - void *trick_bswap_buffer(void *out, void *in, ATTRIBUTES * attr, int tofrom) ; -} - #define MAX_MSG_LEN 8192 @@ -37,9 +32,9 @@ int Trick::VariableServerSession::write_binary_data(const std::vectorgetName()); + total_var_size += var->getName().size(); } total_var_size += type_size; @@ -70,7 +65,7 @@ int Trick::VariableServerSession::write_binary_data(const std::vectorwriteNameBinary(stream, byteswap); + if (!_binary_data_nonames) { + var->writeNameBinary(stream, _byteswap); } - var->writeTypeBinary(stream, byteswap); - var->writeSizeBinary(stream, byteswap); - var->writeValueBinary(stream, byteswap); + var->writeTypeBinary(stream, _byteswap); + var->writeSizeBinary(stream, _byteswap); + var->writeValueBinary(stream, _byteswap); } var_index += curr_message_num_vars; + // Send it out! char write_buf[MAX_MSG_LEN]; stream.read(write_buf, curr_message_size); - connection->write(write_buf, curr_message_size); + _connection->write(write_buf, curr_message_size); } return 0; @@ -118,6 +114,7 @@ int Trick::VariableServerSession::write_ascii_data(const std::vectorwrite(message); + int result = _connection->write(message); if (result < 0) return result; + // Clear out the message stream message_stream.str(""); message_size = 0; @@ -143,12 +141,12 @@ int Trick::VariableServerSession::write_ascii_data(const std::vectorwrite(message); + int result = _connection->write(message); return result; } int Trick::VariableServerSession::write_data() { - return write_data(session_variables, VS_VAR_LIST); + return write_data(_session_variables, VS_VAR_LIST); } int Trick::VariableServerSession::write_data(std::vector& given_vars, VS_MESSAGE_TYPE message_type) { @@ -159,12 +157,12 @@ int Trick::VariableServerSession::write_data(std::vector& g int result = 0; - if ( pthread_mutex_trylock(©_mutex) == 0 ) { + if ( pthread_mutex_trylock(&_copy_mutex) == 0 ) { // Check that all of the variables are staged for (VariableReference * variable : given_vars ) { if (!variable->isStaged()) { - pthread_mutex_unlock(©_mutex) ; - return 1; + pthread_mutex_unlock(&_copy_mutex) ; + return 0; } } @@ -173,10 +171,10 @@ int Trick::VariableServerSession::write_data(std::vector& g variable->prepareForWrite(); } - pthread_mutex_unlock(©_mutex) ; + pthread_mutex_unlock(&_copy_mutex) ; // Send out in correct format - if (binary_data) { + if (_binary_data) { result = write_binary_data(given_vars, message_type ); } else { // ascii mode diff --git a/trick_source/sim_services/VariableServer/VariableServerSession_write_stdio.cpp b/trick_source/sim_services/VariableServer/VariableServerSession_write_stdio.cpp index 7732867e..f95e77bf 100644 --- a/trick_source/sim_services/VariableServer/VariableServerSession_write_stdio.cpp +++ b/trick_source/sim_services/VariableServer/VariableServerSession_write_stdio.cpp @@ -11,7 +11,7 @@ int Trick::VariableServerSession::write_stdio(int stream, std::string text) { outstream << VS_STDIO << " " << stream << " " << (int)text.length() << "\n"; outstream << text; - connection->write(outstream.str()); + _connection->write(outstream.str()); return 0 ; } diff --git a/trick_source/sim_services/VariableServer/VariableServerThread.cpp b/trick_source/sim_services/VariableServer/VariableServerThread.cpp deleted file mode 100644 index 659df23a..00000000 --- a/trick_source/sim_services/VariableServer/VariableServerThread.cpp +++ /dev/null @@ -1,159 +0,0 @@ - -#include -#include -#include "trick/VariableServerThread.hh" -#include "trick/exec_proto.h" -#include "trick/message_proto.h" -#include "trick/message_type.h" -#include "trick/TrickConstant.hh" -#include "trick/UDPConnection.hh" -#include "trick/TCPConnection.hh" - - -Trick::VariableServer * Trick::VariableServerThread::vs = NULL ; - -static int instance_num = 0; - -Trick::VariableServerThread::VariableServerThread() : - Trick::SysThread(std::string("VarServer" + std::to_string(instance_num++))) , debug(0), session(NULL), connection(NULL) { - - connection_status = CONNECTION_PENDING ; - - - pthread_mutex_init(&connection_status_mutex, NULL); - pthread_cond_init(&connection_status_cv, NULL); - - pthread_mutex_init(&restart_pause, NULL); - - cancellable = false; -} - -Trick::VariableServerThread::~VariableServerThread() {} - -std::ostream& Trick::operator<< (std::ostream& s, Trick::VariableServerThread& vst) { - // Write a JSON representation of a Trick::VariableServerThread to an ostream. - struct sockaddr_in otherside; - socklen_t len = (socklen_t)sizeof(otherside); - - s << " \"connection\":{\n"; - s << " \"client_tag\":\"" << vst.connection->getClientTag() << "\",\n"; - - // int err = getpeername(vst.connection->get_socket(), (struct sockaddr*)&otherside, &len); - - // if (err == 0) { - // s << " \"client_IP_address\":\"" << inet_ntoa(otherside.sin_addr) << "\",\n"; - // s << " \"client_port\":\"" << ntohs(otherside.sin_port) << "\",\n"; - // } else { - // s << " \"client_IP_address\":\"unknown\",\n"; - // s << " \"client_port\":\"unknown\","; - // } - - pthread_mutex_lock(&vst.connection_status_mutex); - if (vst.connection_status == CONNECTION_SUCCESS) { - s << *(vst.session); - } - pthread_mutex_unlock(&vst.connection_status_mutex); - - s << " }" << std::endl; - return s; -} - -void Trick::VariableServerThread::set_vs_ptr(Trick::VariableServer * in_vs) { - vs = in_vs ; -} - -Trick::VariableServer * Trick::VariableServerThread::get_vs() { - return vs ; -} - -void Trick::VariableServerThread::set_client_tag(std::string tag) { - connection->setClientTag(tag); -} - -int Trick::VariableServerThread::open_udp_socket(const std::string& hostname, int port) { - UDPConnection * udp_conn = new UDPConnection(); - int status = udp_conn->initialize(hostname, port); - - connection = udp_conn; - - if (status == 0) { - message_publish(MSG_INFO, "Created UDP variable server %s: %d\n", udp_conn->getHostname().c_str(), udp_conn->getPort()); - } - - return status; -} - -int Trick::VariableServerThread::open_tcp_connection(ClientListener * listener) { - connection = listener->setUpNewConnection(); - - return 0; -} - -Trick::ConnectionStatus Trick::VariableServerThread::wait_for_accept() { - - pthread_mutex_lock(&connection_status_mutex); - while ( connection_status == CONNECTION_PENDING ) { - pthread_cond_wait(&connection_status_cv, &connection_status_mutex); - } - pthread_mutex_unlock(&connection_status_mutex); - - return connection_status; -} - -// 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); - - // Make sure that the session has been initialized - pthread_mutex_lock(&connection_status_mutex); - if (connection_status == CONNECTION_SUCCESS) { - - // 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)); - - } - pthread_mutex_unlock(&connection_status_mutex); -} - -// 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. - connection->restart(); - - pthread_mutex_lock(&connection_status_mutex); - if (connection_status == CONNECTION_SUCCESS) { - session->set_pause(saved_pause_cmd) ; - } - pthread_mutex_unlock(&connection_status_mutex); - - // Restart the variable server processing. - pthread_mutex_unlock(&restart_pause); -} - -void Trick::VariableServerThread::cleanup() { - connection->disconnect(); - - if (session != NULL) - delete session; -} - - diff --git a/trick_source/sim_services/VariableServer/VariableServer_copy_and_write_freeze.cpp b/trick_source/sim_services/VariableServer/VariableServer_copy_and_write_freeze.cpp new file mode 100644 index 00000000..81a479d1 --- /dev/null +++ b/trick_source/sim_services/VariableServer/VariableServer_copy_and_write_freeze.cpp @@ -0,0 +1,13 @@ +#include "trick/VariableServer.hh" +#include "trick/exec_proto.h" + +int Trick::VariableServer::copy_and_write_freeze() { + + pthread_mutex_lock(&map_mutex) ; + for ( auto it = var_server_sessions.begin() ; it != var_server_sessions.end() ; it++ ) { + (*it).second->copy_and_write_freeze(exec_get_freeze_frame_count()) ; + } + pthread_mutex_unlock(&map_mutex) ; + + return 0 ; +} diff --git a/trick_source/sim_services/VariableServer/VariableServer_copy_data_freeze_scheduled.cpp b/trick_source/sim_services/VariableServer/VariableServer_copy_and_write_freeze_scheduled.cpp similarity index 52% rename from trick_source/sim_services/VariableServer/VariableServer_copy_data_freeze_scheduled.cpp rename to trick_source/sim_services/VariableServer/VariableServer_copy_and_write_freeze_scheduled.cpp index cd6ecf7d..511afbd2 100644 --- a/trick_source/sim_services/VariableServer/VariableServer_copy_data_freeze_scheduled.cpp +++ b/trick_source/sim_services/VariableServer/VariableServer_copy_and_write_freeze_scheduled.cpp @@ -5,17 +5,14 @@ #include "trick/VariableServer.hh" #include "trick/TrickConstant.hh" -int Trick::VariableServer::copy_data_freeze_scheduled() { +int Trick::VariableServer::copy_and_write_freeze_scheduled() { - long long next_call_tics ; - std::map < pthread_t , VariableServerSession * >::iterator it ; - - next_call_tics = TRICK_MAX_LONG_LONG ; + long long next_call_tics = TRICK_MAX_LONG_LONG ; pthread_mutex_lock(&map_mutex) ; - for ( it = var_server_sessions.begin() ; it != var_server_sessions.end() ; it++ ) { + for ( auto it = var_server_sessions.begin() ; it != var_server_sessions.end() ; it++ ) { VariableServerSession * session = (*it).second ; - session->copy_data_freeze_scheduled(copy_data_freeze_job->next_tics) ; + session->copy_and_write_freeze_scheduled(copy_and_write_freeze_job->next_tics) ; if ( session->get_freeze_next_tics() < next_call_tics ) { next_call_tics = session->get_freeze_next_tics() ; } @@ -23,8 +20,8 @@ int Trick::VariableServer::copy_data_freeze_scheduled() { pthread_mutex_unlock(&map_mutex) ; //reschedule the current job. TODO: a call needs to be created to do this the OO way - if ( copy_data_freeze_job != NULL ) { - copy_data_freeze_job->next_tics = next_call_tics ; + if ( copy_and_write_freeze_job != NULL ) { + copy_and_write_freeze_job->next_tics = next_call_tics ; } return(0) ; diff --git a/trick_source/sim_services/VariableServer/VariableServer_copy_data_scheduled.cpp b/trick_source/sim_services/VariableServer/VariableServer_copy_and_write_scheduled.cpp similarity index 57% rename from trick_source/sim_services/VariableServer/VariableServer_copy_data_scheduled.cpp rename to trick_source/sim_services/VariableServer/VariableServer_copy_and_write_scheduled.cpp index 4ba077ea..9ef7fde2 100644 --- a/trick_source/sim_services/VariableServer/VariableServer_copy_data_scheduled.cpp +++ b/trick_source/sim_services/VariableServer/VariableServer_copy_and_write_scheduled.cpp @@ -5,17 +5,14 @@ #include "trick/VariableServer.hh" #include "trick/TrickConstant.hh" -int Trick::VariableServer::copy_data_scheduled() { +int Trick::VariableServer::copy_and_write_scheduled() { - long long next_call_tics ; - std::map < pthread_t , VariableServerSession * >::iterator it ; - - next_call_tics = TRICK_MAX_LONG_LONG ; + long long next_call_tics = TRICK_MAX_LONG_LONG; pthread_mutex_lock(&map_mutex) ; - for ( it = var_server_sessions.begin() ; it != var_server_sessions.end() ; it++ ) { - auto session = (*it).second ; - session->copy_data_scheduled(copy_data_job->next_tics) ; + for ( auto it = var_server_sessions.begin() ; it != var_server_sessions.end() ; it++ ) { + VariableServerSession * session = (*it).second ; + session->copy_and_write_scheduled(copy_data_job->next_tics) ; if ( session->get_next_tics() < next_call_tics ) { next_call_tics = session->get_next_tics() ; } diff --git a/trick_source/sim_services/VariableServer/VariableServer_copy_and_write_top.cpp b/trick_source/sim_services/VariableServer/VariableServer_copy_and_write_top.cpp new file mode 100644 index 00000000..3118098d --- /dev/null +++ b/trick_source/sim_services/VariableServer/VariableServer_copy_and_write_top.cpp @@ -0,0 +1,13 @@ +#include "trick/exec_proto.h" +#include "trick/VariableServer.hh" + +int Trick::VariableServer::copy_and_write_top() { + + pthread_mutex_lock(&map_mutex) ; + for ( auto it = var_server_sessions.begin() ; it != var_server_sessions.end() ; it++ ) { + (*it).second->copy_and_write_top(exec_get_frame_count()) ; + } + pthread_mutex_unlock(&map_mutex) ; + + return 0 ; +} diff --git a/trick_source/sim_services/VariableServer/VariableServer_copy_data_freeze.cpp b/trick_source/sim_services/VariableServer/VariableServer_copy_data_freeze.cpp deleted file mode 100644 index 62b4bcdf..00000000 --- a/trick_source/sim_services/VariableServer/VariableServer_copy_data_freeze.cpp +++ /dev/null @@ -1,18 +0,0 @@ - -#include -#include - -#include "trick/VariableServer.hh" - -int Trick::VariableServer::copy_data_freeze() { - - std::map < pthread_t , VariableServerSession * >::iterator it ; - - pthread_mutex_lock(&map_mutex) ; - for ( it = var_server_sessions.begin() ; it != var_server_sessions.end() ; it++ ) { - (*it).second->copy_data_freeze() ; - } - pthread_mutex_unlock(&map_mutex) ; - - return 0 ; -} diff --git a/trick_source/sim_services/VariableServer/VariableServer_copy_data_top.cpp b/trick_source/sim_services/VariableServer/VariableServer_copy_data_top.cpp deleted file mode 100644 index 3b4dad61..00000000 --- a/trick_source/sim_services/VariableServer/VariableServer_copy_data_top.cpp +++ /dev/null @@ -1,18 +0,0 @@ - -#include -#include - -#include "trick/VariableServer.hh" - -int Trick::VariableServer::copy_data_top() { - - std::map < pthread_t , VariableServerSession * >::iterator it ; - - pthread_mutex_lock(&map_mutex) ; - for ( it = var_server_sessions.begin() ; it != var_server_sessions.end() ; it++ ) { - (*it).second->copy_data_top() ; - } - pthread_mutex_unlock(&map_mutex) ; - - return 0 ; -} diff --git a/trick_source/sim_services/VariableServer/VariableServer_freeze_init.cpp b/trick_source/sim_services/VariableServer/VariableServer_freeze_init.cpp index 8f7bddce..c7a81c04 100644 --- a/trick_source/sim_services/VariableServer/VariableServer_freeze_init.cpp +++ b/trick_source/sim_services/VariableServer/VariableServer_freeze_init.cpp @@ -9,15 +9,11 @@ int Trick::VariableServer::freeze_init() { - long long next_call_tics ; - VariableServerSession * session ; - std::map < pthread_t , VariableServerSession * >::iterator it ; - - next_call_tics = TRICK_MAX_LONG_LONG ; + long long next_call_tics = TRICK_MAX_LONG_LONG ; pthread_mutex_lock(&map_mutex) ; - for ( it = var_server_sessions.begin() ; it != var_server_sessions.end() ; it++ ) { - session = (*it).second ; + for ( auto it = var_server_sessions.begin() ; it != var_server_sessions.end() ; it++ ) { + VariableServerSession * session = (*it).second ; session->freeze_init() ; if ( session->get_freeze_next_tics() < next_call_tics ) { next_call_tics = session->get_freeze_next_tics() ; @@ -26,8 +22,8 @@ int Trick::VariableServer::freeze_init() { pthread_mutex_unlock(&map_mutex) ; //reschedule the current job. TODO: a call needs to be created to do this the OO way - if ( copy_data_freeze_job != NULL ) { - copy_data_freeze_job->next_tics = next_call_tics ; + if ( copy_and_write_freeze_job != NULL ) { + copy_and_write_freeze_job->next_tics = next_call_tics ; } return(0) ; diff --git a/trick_source/sim_services/VariableServer/VariableServer_get_next_freeze_call_time.cpp b/trick_source/sim_services/VariableServer/VariableServer_get_next_freeze_call_time.cpp index 12c33189..f001c76f 100644 --- a/trick_source/sim_services/VariableServer/VariableServer_get_next_freeze_call_time.cpp +++ b/trick_source/sim_services/VariableServer/VariableServer_get_next_freeze_call_time.cpp @@ -6,23 +6,18 @@ int Trick::VariableServer::get_next_freeze_call_time() { - std::map < pthread_t , VariableServerSession * >::iterator it ; - VariableServerSession * session ; - - long long next_call_tics ; - - next_call_tics = TRICK_MAX_LONG_LONG ; + long long next_call_tics = TRICK_MAX_LONG_LONG; pthread_mutex_lock(&map_mutex) ; - for ( it = var_server_sessions.begin() ; it != var_server_sessions.end() ; it++ ) { - session = (*it).second ; + for ( auto it = var_server_sessions.begin() ; it != var_server_sessions.end() ; it++ ) { + VariableServerSession * session = (*it).second ; if ( session->get_freeze_next_tics() < next_call_tics ) { next_call_tics = session->get_freeze_next_tics() ; } } pthread_mutex_unlock(&map_mutex) ; - copy_data_freeze_job->next_tics = next_call_tics ; + copy_and_write_freeze_job->next_tics = next_call_tics ; return(0) ; } diff --git a/trick_source/sim_services/VariableServer/VariableServer_get_next_sync_call_time.cpp b/trick_source/sim_services/VariableServer/VariableServer_get_next_sync_call_time.cpp index e6d6d552..fd969d64 100644 --- a/trick_source/sim_services/VariableServer/VariableServer_get_next_sync_call_time.cpp +++ b/trick_source/sim_services/VariableServer/VariableServer_get_next_sync_call_time.cpp @@ -6,16 +6,11 @@ int Trick::VariableServer::get_next_sync_call_time() { - std::map < pthread_t , VariableServerSession * >::iterator it ; - VariableServerSession * session ; - - long long next_call_tics ; - - next_call_tics = TRICK_MAX_LONG_LONG ; + long long next_call_tics = TRICK_MAX_LONG_LONG ; pthread_mutex_lock(&map_mutex) ; - for ( it = var_server_sessions.begin() ; it != var_server_sessions.end() ; it++ ) { - session = (*it).second ; + for ( auto it = var_server_sessions.begin() ; it != var_server_sessions.end() ; it++ ) { + VariableServerSession * session = (*it).second ; if ( session->get_next_tics() < next_call_tics ) { next_call_tics = session->get_next_tics() ; } diff --git a/trick_source/sim_services/VariableServer/VariableServer_get_var_server_port.cpp b/trick_source/sim_services/VariableServer/VariableServer_get_var_server_port.cpp deleted file mode 100644 index 0e009dbd..00000000 --- a/trick_source/sim_services/VariableServer/VariableServer_get_var_server_port.cpp +++ /dev/null @@ -1,44 +0,0 @@ - -#include -#include "trick/VariableServer.hh" - - -int Trick::VariableServer::create_tcp_socket(const char * address, unsigned short in_port ) { - Trick::VariableServerListenThread * new_listen_thread = new Trick::VariableServerListenThread ; - new_listen_thread->create_tcp_socket(address, in_port) ; - new_listen_thread->copy_cpus(listen_thread.get_cpus()) ; - new_listen_thread->create_thread() ; - additional_listen_threads[new_listen_thread->get_pthread_id()] = new_listen_thread ; - - return 0 ; -} - -int Trick::VariableServer::create_udp_socket(const char * address, unsigned short in_port ) { - // UDP sockets are created without a listen thread - int ret ; - Trick::VariableServerThread * vst ; - vst = new Trick::VariableServerThread() ; - ret = vst->open_udp_socket(address, in_port) ; - if ( ret == 0 ) { - vst->copy_cpus(listen_thread.get_cpus()) ; - vst->create_thread() ; - } - //vst->var_debug(3) ; - - return 0 ; -} - -int Trick::VariableServer::create_multicast_socket(const char * mcast_address, const char * address, unsigned short in_port ) { - // int ret ; - // Trick::VariableServerThread * vst ; - // vst = new Trick::VariableServerThread(NULL) ; - // ret = vst->create_mcast_socket(mcast_address, address, in_port) ; - // if ( ret == 0 ) { - // vst->copy_cpus(listen_thread.get_cpus()) ; - // vst->create_thread() ; - // } - //vst->var_debug(3) ; - - return 0 ; -} - diff --git a/trick_source/sim_services/VariableServer/VariableServer_open_additional_servers.cpp b/trick_source/sim_services/VariableServer/VariableServer_open_additional_servers.cpp new file mode 100644 index 00000000..27445a51 --- /dev/null +++ b/trick_source/sim_services/VariableServer/VariableServer_open_additional_servers.cpp @@ -0,0 +1,88 @@ + +#include +#include "trick/VariableServer.hh" +#include "trick/message_proto.h" +#include "trick/message_type.h" + +#include "trick/TCPConnection.hh" +#include "trick/UDPConnection.hh" +#include "trick/TCPClientListener.hh" + +int Trick::VariableServer::create_tcp_socket(const char * address, unsigned short in_port ) { + // Open a VariableServerListenThread to manage this server + + TCPClientListener * listener = new TCPClientListener(); + int status = listener->initialize(address, in_port); + + if (status != 0) { + delete listener; + return 0; + } + + std::string set_address = listener->getHostname(); + int set_port = listener->getPort(); + + Trick::VariableServerListenThread * new_listen_thread = new Trick::VariableServerListenThread(listener) ; + + new_listen_thread->copy_cpus(listen_thread.get_cpus()) ; + new_listen_thread->create_thread() ; + additional_listen_threads[new_listen_thread->get_pthread_id()] = new_listen_thread ; + + message_publish(MSG_INFO, "Created TCP variable server %s: %d\n", set_address.c_str(), set_port); + + return 0 ; +} + +int Trick::VariableServer::create_udp_socket(const char * address, unsigned short in_port ) { + // UDP sockets are created without a listen thread, and represent only 1 session + // Create a VariableServerSessionThread to manage this session + + UDPConnection * udp_conn = new UDPConnection(); + int status = udp_conn->initialize(address, in_port); + if ( status != 0 ) { + delete udp_conn; + return 0; + } + + std::string set_address = udp_conn->getHostname(); + int set_port = udp_conn->getPort(); + + Trick::VariableServerSessionThread * vst = new Trick::VariableServerSessionThread() ; + + vst->set_connection(udp_conn); + vst->copy_cpus(listen_thread.get_cpus()) ; + vst->create_thread() ; + + message_publish(MSG_INFO, "Created UDP variable server %s: %d\n", set_address.c_str(), set_port); + + return 0 ; +} + +int Trick::VariableServer::create_multicast_socket(const char * mcast_address, const char * address, unsigned short in_port ) { + + // Multicast sockets are created without a listen thread, and represent only 1 session + // Create a VariableServerSessionThread to manage this session + + MulticastGroup * multicast = new MulticastGroup(); + message_publish(MSG_INFO, "Created UDP variable server %s: %d\n", address, in_port); + + int status = multicast->initialize_with_receiving(address, mcast_address, in_port); + if ( status != 0 ) { + delete multicast; + return 0; + } + + std::string set_address = multicast->getHostname(); + int set_port = multicast->getPort(); + + Trick::VariableServerSessionThread * vst = new Trick::VariableServerSessionThread() ; + + vst->set_connection(multicast); + vst->copy_cpus(listen_thread.get_cpus()) ; + vst->create_thread() ; + + message_publish(MSG_INFO, "Multicast variable server output %s:%d\n", mcast_address, set_port) ; + + return 0 ; +} + diff --git a/trick_source/sim_services/VariableServer/VariableServer_restart.cpp b/trick_source/sim_services/VariableServer/VariableServer_restart.cpp index dc312a3a..46b62ac0 100644 --- a/trick_source/sim_services/VariableServer/VariableServer_restart.cpp +++ b/trick_source/sim_services/VariableServer/VariableServer_restart.cpp @@ -9,9 +9,12 @@ int Trick::VariableServer::restart() { if ( listen_thread.get_pthread_id() == 0 ) { listen_thread.create_thread() ; } - std::map < pthread_t , VariableServerListenThread * >::iterator it ; - for( it = additional_listen_threads.begin() ; it != additional_listen_threads.end() ; it++ ) { - (*it).second->restart() ; + + for (const auto& listen_it : additional_listen_threads) { + listen_it.second->restart(); + if ( listen_it.second->get_pthread_id() == 0 ) { + listen_it.second->create_thread() ; + } } return 0 ; } @@ -23,17 +26,17 @@ int Trick::VariableServer::restart() { // Suspend variable server processing prior to reloading a checkpoint. int Trick::VariableServer::suspendPreCheckpointReload() { - std::map::iterator pos ; + // Pause listening on all listening threads listen_thread.pause_listening() ; for (const auto& listen_it : additional_listen_threads) { listen_it.second->pause_listening(); } + // Suspend session threads pthread_mutex_lock(&map_mutex) ; - for ( pos = var_server_threads.begin() ; pos != var_server_threads.end() ; pos++ ) { - VariableServerThread* vst = (*pos).second ; - vst->preload_checkpoint() ; + for (const auto& vst_it : var_server_threads ) { + vst_it.second->preload_checkpoint() ; } pthread_mutex_unlock(&map_mutex) ; @@ -42,16 +45,16 @@ int Trick::VariableServer::suspendPreCheckpointReload() { // Resume variable server processing after reloading a MemoryManager (ASCII) checkpoint. int Trick::VariableServer::resumePostCheckpointReload() { - std::map::iterator pos ; + std::map::iterator pos ; + // Resume all session threads pthread_mutex_lock(&map_mutex) ; - // For each Variable Server Thread ... - for ( pos = var_server_threads.begin() ; pos != var_server_threads.end() ; pos++ ) { - VariableServerThread* vst = (*pos).second ; - vst->restart() ; + for (const auto& vst_it : var_server_threads ) { + vst_it.second->restart() ; } pthread_mutex_unlock(&map_mutex) ; + // Restart listening on all listening threads listen_thread.restart_listening() ; for (const auto& listen_it : additional_listen_threads) { listen_it.second->restart_listening(); diff --git a/trick_source/sim_services/VariableServer/VariableServer_shutdown.cpp b/trick_source/sim_services/VariableServer/VariableServer_shutdown.cpp index 84dd71bc..499da7c9 100644 --- a/trick_source/sim_services/VariableServer/VariableServer_shutdown.cpp +++ b/trick_source/sim_services/VariableServer/VariableServer_shutdown.cpp @@ -2,13 +2,16 @@ #include "trick/VariableServer.hh" int Trick::VariableServer::shutdown() { + + // Shutdown all listen threads listen_thread.cancel_thread() ; - for (const auto& listen_it : additional_listen_threads) { + for (auto& listen_it : additional_listen_threads) { listen_it.second->cancel_thread(); } + // Shutdown all session threads pthread_mutex_lock(&map_mutex) ; - for (const auto& it : var_server_threads) { + for (auto& it : var_server_threads) { it.second->cancel_thread() ; } pthread_mutex_unlock(&map_mutex) ; diff --git a/trick_source/sim_services/VariableServer/exit_var_thread.cpp b/trick_source/sim_services/VariableServer/exit_var_thread.cpp index f8a02d85..80210980 100644 --- a/trick_source/sim_services/VariableServer/exit_var_thread.cpp +++ b/trick_source/sim_services/VariableServer/exit_var_thread.cpp @@ -1,13 +1,11 @@ #include "trick/VariableServer.hh" +// This should only be called from the VST itself void exit_var_thread(void *in_vst) { - Trick::VariableServerThread * vst = (Trick::VariableServerThread *) in_vst ; - Trick::VariableServer * vs = vst->get_vs() ; + Trick::VariableServerSessionThread * vst = (Trick::VariableServerSessionThread *) in_vst ; - if (vst->get_pthread_id() != pthread_self()) { - std::cerr << "exit_var_thread must be called from the variable server thread" << std::endl; - } + Trick::VariableServer * vs = vst->get_vs() ; vs->delete_session(vst->get_pthread_id()); diff --git a/trick_source/sim_services/VariableServer/test/Makefile b/trick_source/sim_services/VariableServer/test/Makefile index 43d77b14..7d7fdbff 100644 --- a/trick_source/sim_services/VariableServer/test/Makefile +++ b/trick_source/sim_services/VariableServer/test/Makefile @@ -8,12 +8,12 @@ include $(dir $(lastword $(MAKEFILE_LIST)))../../../../share/trick/makefiles/Makefile.common # Replace -isystem with -I so ICG doesn't skip Trick headers -TRICK_SYSTEM_CXXFLAGS := $(subst -isystem,-I,$(TRICK_SYSTEM_CXXFLAGS)) +TRICK_SYSTEM_CXXFLAGS := $(subst -isystem,-I,$(TRICK_SYSTEM_CXXFLAGS)) -Wno-unused-command-line-argument # Flags passed to the preprocessor. TRICK_CXXFLAGS += -I$(GTEST_HOME)/include -I$(TRICK_HOME)/include -g -Wall -Wextra -Wno-sign-compare -std=c++11 ${TRICK_SYSTEM_CXXFLAGS} ${TRICK_TEST_FLAGS} TRICK_LIBS = -L${TRICK_LIB_DIR} -ltrick_mm -ltrick_units -ltrick_comm -ltrick_pyip -ltrick -ltrick_mm -ltrick_units -ltrick_comm -ltrick_pyip -ltrick -ltrick_var_binary_parser -ltrick_connection_handlers -ltrick_comm -TRICK_EXEC_LINK_LIBS += -L${GTEST_HOME}/lib64 -L${GTEST_HOME}/lib -lgtest -lgtest_main -lpthread +TRICK_EXEC_LINK_LIBS += -L${GTEST_HOME}/lib64 -L${GTEST_HOME}/lib -lgtest -lgtest_main -lgmock -lpthread # All tests produced by this Makefile. Remember to add new tests you # created to the list. @@ -22,14 +22,13 @@ VARIABLE_REFERENCE_TESTS = VariableReference_test \ VariableReference_writeValueAscii_test \ VariableReference_writeValueBinary_test -VARIABLE_SESSION_TESTS = VariableServerSession_test +VARIABLE_SESSION_TESTS = VariableServerSession_test -TESTS = $(VARIABLE_REFERENCE_TESTS) $(VARIABLE_SESSION_TESTS) +TESTS = $(VARIABLE_REFERENCE_TESTS) $(VARIABLE_SESSION_TESTS) VariableServerSessionThread_test VariableServerListenThread_test VariableServer_test TEST_OBJS = $(addprefix $(OBJ_DIR)/, $(addsuffix .o, $(TESTS))) -TO_ICG = VariableReference_test \ - TestConnection +TO_ICG = VariableReference_test ICG_OBJS = $(addprefix $(OBJ_DIR)/io_, $(addsuffix .o, $(TO_ICG))) @@ -58,10 +57,20 @@ $(ICG_OBJS): $(OBJ_DIR)/io_%.o : %.hh $(OBJ_DIR) $(TRICK_CXX) $(TRICK_CXXFLAGS) -c io_src/io_$*.cpp -o $@ -L${TRICK_HOME}/lib_${TRICK_HOST_CPU} $(TRICK_LIBS) $(TRICK_EXEC_LINK_LIBS) $(VARIABLE_REFERENCE_TESTS): %: $(OBJ_DIR)/%.o $(OBJ_DIR)/io_VariableReference_test.o - $(TRICK_CXX) $(TRICK_SYSTEM_LDFLAGS) -o $@ $^ -L${TRICK_HOME}/lib_${TRICK_HOST_CPU} $(TRICK_LIBS) $(TRICK_EXEC_LINK_LIBS) + $(TRICK_CXX) $(TRICK_SYSTEM_LDFLAGS) $(TRICK_CXXFLAGS) -o $@ $^ -L${TRICK_HOME}/lib_${TRICK_HOST_CPU} $(TRICK_LIBS) $(TRICK_EXEC_LINK_LIBS) + +$(VARIABLE_SESSION_TESTS): %: $(OBJ_DIR)/%.o + $(TRICK_CXX) $(TRICK_SYSTEM_LDFLAGS) $(TRICK_CXXFLAGS) $^ -L${TRICK_HOME}/lib_${TRICK_HOST_CPU} $(TRICK_LIBS) $(TRICK_EXEC_LINK_LIBS) -o $@ + +VariableServerSessionThread_test: %: $(OBJ_DIR)/%.o + $(TRICK_CXX) $(TRICK_SYSTEM_LDFLAGS) $(TRICK_CXXFLAGS) -o $@ $^ -L${TRICK_HOME}/lib_${TRICK_HOST_CPU} $(TRICK_LIBS) $(TRICK_EXEC_LINK_LIBS) + +VariableServerListenThread_test: %: $(OBJ_DIR)/%.o + $(TRICK_CXX) $(TRICK_SYSTEM_LDFLAGS) $(TRICK_CXXFLAGS) -o $@ $^ -L${TRICK_HOME}/lib_${TRICK_HOST_CPU} $(TRICK_LIBS) $(TRICK_EXEC_LINK_LIBS) + +VariableServer_test: %: $(OBJ_DIR)/%.o + $(TRICK_CXX) $(TRICK_SYSTEM_LDFLAGS) $(TRICK_CXXFLAGS) -o $@ $^ -L${TRICK_HOME}/lib_${TRICK_HOST_CPU} $(TRICK_LIBS) $(TRICK_EXEC_LINK_LIBS) -$(VARIABLE_SESSION_TESTS): %: $(OBJ_DIR)/%.o $(OBJ_DIR)/io_TestConnection.o - $(TRICK_CXX) $(TRICK_SYSTEM_LDFLAGS) -o $@ $^ -L${TRICK_HOME}/lib_${TRICK_HOST_CPU} $(TRICK_LIBS) $(TRICK_EXEC_LINK_LIBS) code-coverage: test # Give rid of any old code-coverage HTML we may have. diff --git a/trick_source/sim_services/VariableServer/test/MockClientConnection.hh b/trick_source/sim_services/VariableServer/test/MockClientConnection.hh new file mode 100644 index 00000000..83b8d0c0 --- /dev/null +++ b/trick_source/sim_services/VariableServer/test/MockClientConnection.hh @@ -0,0 +1,24 @@ +#ifndef MOCK_CLIENT_CONNECTION_HH +#define MOCK_CLIENT_CONNECTION_HH + +#include "trick/ClientConnection.hh" +#include + +class MockClientConnection : public Trick::ClientConnection { + public: + MOCK_METHOD0(start, int()); + MOCK_METHOD1(write, int(const std::string& message)); + MOCK_METHOD2(write, int(char * message, int size)); + MOCK_METHOD2(read, int(std::string& message, int max_len)); + MOCK_METHOD0(disconnect, int()); + MOCK_METHOD1(setBlockMode, int(bool blocking)); + MOCK_METHOD0(isInitialized, bool()); + MOCK_METHOD0(restart, int()); + MOCK_METHOD0(getClientTag, std::string()); + MOCK_METHOD1(setClientTag, int(std::string tag)); + MOCK_METHOD0(getClientHostname, std::string()); + MOCK_METHOD0(getClientPort, int()); + +}; + +#endif \ No newline at end of file diff --git a/trick_source/sim_services/VariableServer/test/MockMulticastGroup.hh b/trick_source/sim_services/VariableServer/test/MockMulticastGroup.hh new file mode 100644 index 00000000..275eefa3 --- /dev/null +++ b/trick_source/sim_services/VariableServer/test/MockMulticastGroup.hh @@ -0,0 +1,18 @@ +#ifndef MOCK_MULTICAST_GROUP_HH +#define MOCK_MULTICAST_GROUP_HH + +#include "trick/MulticastGroup.hh" + +#include + +class MockMulticastGroup : public Trick::MulticastGroup { + public: + MOCK_METHOD1(broadcast, int(std::string)); + MOCK_METHOD2(addAddress, int(std::string, int)); + MOCK_METHOD0(restart, int()); + MOCK_METHOD0(isInitialized, bool()); + MOCK_METHOD0(initialize, int()); + MOCK_METHOD0(disconnect, int()); +}; + +#endif \ No newline at end of file diff --git a/trick_source/sim_services/VariableServer/test/MockTCPClientListener.hh b/trick_source/sim_services/VariableServer/test/MockTCPClientListener.hh new file mode 100644 index 00000000..cf457d06 --- /dev/null +++ b/trick_source/sim_services/VariableServer/test/MockTCPClientListener.hh @@ -0,0 +1,24 @@ +#ifndef MOCK_CLIENT_LISTENER_HH +#define MOCK_CLIENT_LISTENER_HH + +#include "trick/TCPClientListener.hh" + +#include + +class MockTCPClientListener : public Trick::TCPClientListener { + public: + MOCK_METHOD2(initialize, int(std::string, int)); + MOCK_METHOD0(initialize, int()); + MOCK_METHOD1(setBlockMode, int(bool)); + MOCK_METHOD0(checkForNewConnections, bool()); + MOCK_METHOD0(setUpNewConnection, Trick::TCPConnection*()); + MOCK_METHOD0(disconnect, int()); + MOCK_METHOD0(checkSocket, int()); + MOCK_METHOD1(validateSourceAddress, bool(std::string)); + MOCK_METHOD0(isInitialized, bool()); + MOCK_METHOD0(restart, int()); + MOCK_METHOD0(getHostname, std::string()); + MOCK_METHOD0(getPort, int()); +}; + +#endif \ No newline at end of file diff --git a/trick_source/sim_services/VariableServer/test/MockTCPConnection.hh b/trick_source/sim_services/VariableServer/test/MockTCPConnection.hh new file mode 100644 index 00000000..37a28456 --- /dev/null +++ b/trick_source/sim_services/VariableServer/test/MockTCPConnection.hh @@ -0,0 +1,21 @@ +#ifndef MOCK_TCP_CONNECTION_HH +#define MOCK_TCP_CONNECTION_HH + +#include "trick/TCPConnection.hh" +#include + +class MockTCPConnection : public Trick::TCPConnection { + public: + MOCK_METHOD0(start, int()); + MOCK_METHOD1(write, int(const std::string& message)); + MOCK_METHOD2(write, int(char * message, int size)); + MOCK_METHOD2(read, int(std::string& message, int max_len)); + MOCK_METHOD0(disconnect, int()); + MOCK_METHOD1(setBlockMode, int(bool blocking)); + MOCK_METHOD0(isInitialized, bool()); + MOCK_METHOD0(restart, int()); + MOCK_METHOD0(getClientTag, std::string()); + MOCK_METHOD1(setClientTag, int(std::string tag)); +}; + +#endif \ No newline at end of file diff --git a/trick_source/sim_services/VariableServer/test/MockVariableServerSession.hh b/trick_source/sim_services/VariableServer/test/MockVariableServerSession.hh new file mode 100644 index 00000000..8339a81d --- /dev/null +++ b/trick_source/sim_services/VariableServer/test/MockVariableServerSession.hh @@ -0,0 +1,33 @@ +#ifndef MOCK_VARIABLE_SERVER_SESSION_HH +#define MOCK_VARIABLE_SERVER_SESSION_HH + +#include "trick/VariableServerSession.hh" +#include + +class MockVariableServerSession : public Trick::VariableServerSession { + public: + MOCK_METHOD0(handle_message, int()); + MOCK_METHOD0(write_data, int()); + MOCK_METHOD0(get_exit_cmd, bool()); + MOCK_METHOD0(get_pause, bool()); + MOCK_CONST_METHOD0(get_enabled, bool()); + MOCK_CONST_METHOD0(get_write_mode, VS_WRITE_MODE()); + MOCK_CONST_METHOD0(get_copy_mode, VS_COPY_MODE()); + MOCK_METHOD0(set_log_on, int()); + MOCK_METHOD0(set_log_off, int()); + MOCK_METHOD0(copy_sim_data, int()); + MOCK_CONST_METHOD0(get_update_rate, double()); + MOCK_CONST_METHOD0(get_frame_multiple, int()); + MOCK_CONST_METHOD0(get_frame_offset, int()); + MOCK_CONST_METHOD0(get_freeze_frame_multiple, int()); + MOCK_CONST_METHOD0(get_freeze_frame_offset, int()); + MOCK_CONST_METHOD0(get_next_tics, long long ()); + MOCK_CONST_METHOD0(get_freeze_next_tics, long long()); + MOCK_METHOD0(set_exit_cmd, void()); + + MOCK_METHOD0(copy_and_write_async, int()); + + int copy_and_write_async_concrete() { return Trick::VariableServerSession::copy_and_write_async(); } +}; + +#endif \ No newline at end of file diff --git a/trick_source/sim_services/VariableServer/test/TestConnection.hh b/trick_source/sim_services/VariableServer/test/TestConnection.hh deleted file mode 100644 index 89eea807..00000000 --- a/trick_source/sim_services/VariableServer/test/TestConnection.hh +++ /dev/null @@ -1,80 +0,0 @@ -#ifndef TEST_CONNECTION_HH -#define TEST_CONNECTION_HH - -#include "trick/ClientConnection.hh" -#include -#include -#include -#include - -class TestConnection : public Trick::ClientConnection { - public: - ~TestConnection () { - for (char * message : binary_messages_written) { - free (message); - } - } - - int initialize() { - valid = true; - } - - int write (const std::string& message) { - if (!valid) - return -1; - - ascii_messages_written.emplace_back(message); - return 0; - } - - int write (char * message, int size) { - if (!valid) - return -1; - - char * msg_copy = (char *) malloc(size+1); - memcpy(msg_copy, message, size+1); - binary_messages_written.push_back(msg_copy); - - return 0; - } - - std::string read (int max_len = MAX_CMD_LEN) { - if (queued_messages.empty()) { - return ""; - } - - std::string ret = queued_messages.front(); - queued_messages.pop(); - return ret; - } - - int disconnect () { - valid = false; - return 0; - } - - std::string get_client_tag () { - return client_tag; - } - - int set_client_tag(std::string tag) { - client_tag = tag; - } - - int set_block_mode (int mode) { - return 0; - } - - - std::queue queued_messages; - std::vector ascii_messages_written; - std::vector binary_messages_written; - - - std::string client_tag; - bool valid = true; - -}; - - -#endif \ No newline at end of file diff --git a/trick_source/sim_services/VariableServer/test/VariableReference_test.cc b/trick_source/sim_services/VariableServer/test/VariableReference_test.cc index 6ff68a5f..9c616441 100644 --- a/trick_source/sim_services/VariableServer/test/VariableReference_test.cc +++ b/trick_source/sim_services/VariableServer/test/VariableReference_test.cc @@ -9,7 +9,7 @@ TEST_F(VariableReference_test, getName) { // ACT // ASSERT - EXPECT_EQ(strcmp(ref.getName(), "test_int"), 0); + EXPECT_EQ(ref.getName(), std::string("test_int")); } TEST_F(VariableReference_test, validateAddress) { @@ -162,7 +162,7 @@ TEST_F(VariableReference_test, printWithoutUnits) { std::stringstream ss; // ACT - EXPECT_STREQ(ref.getBaseUnits(), "m"); + EXPECT_EQ(ref.getBaseUnits(), std::string("m")); // ASSERT // Doesn't actually print with units unless set diff --git a/trick_source/sim_services/VariableServer/test/VariableReference_writeValueBinary_test.cc b/trick_source/sim_services/VariableServer/test/VariableReference_writeValueBinary_test.cc index 7f4ba5a9..7cdc41e9 100644 --- a/trick_source/sim_services/VariableServer/test/VariableReference_writeValueBinary_test.cc +++ b/trick_source/sim_services/VariableServer/test/VariableReference_writeValueBinary_test.cc @@ -121,7 +121,7 @@ TEST_F(VariableReference_test, writeValueBinary_string) { unsigned char expected_bytes[6] = {0x61, 0x62, 0x63, 0x64, 0x65, 0x66}; // ASSERT - for (int i = 0; i < test_a.length(); i++) { + for (unsigned int i = 0; i < test_a.length(); i++) { EXPECT_EQ(static_cast(actual_bytes[i]), expected_bytes[i]); } } @@ -140,12 +140,12 @@ TEST_F(VariableReference_test, writeNameBinary) { ref.writeNameBinary(ss); // ASSERT - char * actual_bytes = (char *) malloc (sizeof(int) + strlen(ref.getName())); + char * actual_bytes = (char *) malloc (sizeof(int) + ref.getName().size()); ss.read(actual_bytes, sizeof(int) + 6); unsigned char expected_bytes[sizeof(int) + 6] = {0x06, 0x00, 0x00, 0x00, 0x74, 0x65, 0x73, 0x74, 0x5f, 0x61}; // ASSERT - for (int i = 0; i < sizeof(int) + strlen(ref.getName()); i++) { + for (int i = 0; i < sizeof(int) + ref.getName().size(); i++) { EXPECT_EQ(static_cast(actual_bytes[i]), expected_bytes[i]); } } diff --git a/trick_source/sim_services/VariableServer/test/VariableServerListenThread_test.cc b/trick_source/sim_services/VariableServer/test/VariableServerListenThread_test.cc new file mode 100644 index 00000000..dbfd409b --- /dev/null +++ b/trick_source/sim_services/VariableServer/test/VariableServerListenThread_test.cc @@ -0,0 +1,392 @@ +/******************************TRICK HEADER************************************* +PURPOSE: ( Tests for the VariableServerListenThread class ) +*******************************************************************************/ + +#include +#include + +#include "trick/Executive.hh" +#include "trick/CommandLineArguments.hh" +#include "trick/VariableServer.hh" + +#include "trick/VariableServerListenThread.hh" + +#include "MockTCPClientListener.hh" +#include "MockTCPConnection.hh" +#include "MockMulticastGroup.hh" + +using ::testing::Return; +using ::testing::_; +using ::testing::AtLeast; + + +/* + Test Fixture. + */ +class VariableServerListenThread_test : public ::testing::Test { + protected: + // Static global dependencies that I would like to eventually mock out + Trick::Executive * executive; + Trick::CommandLineArguments * cmd_args; + Trick::VariableServer * varserver; + + // Listener + MockTCPClientListener * listener; + MockMulticastGroup * mcast; + + VariableServerListenThread_test() { + // Set up dependencies that haven't been broken + executive = new Trick::Executive; + cmd_args = new Trick::CommandLineArguments; + varserver = new Trick::VariableServer; + Trick::VariableServerSessionThread::set_vs_ptr(varserver); + + // Set up mocks + listener = new MockTCPClientListener; + mcast = new MockMulticastGroup; + } + + ~VariableServerListenThread_test() { + delete executive; + delete cmd_args; + delete varserver; + } + + void SetUp() {} + void TearDown() {} +}; + +void setup_normal_listener_expectations (MockTCPClientListener * listener) { + // Starting the connection succeeds + EXPECT_CALL(*listener, getHostname()) + .WillRepeatedly(Return("MyHostname")); + EXPECT_CALL(*listener, getPort()) + .WillRepeatedly(Return(1234)); +} + +void setup_normal_mcast_expectations (MockMulticastGroup * mcast) { + EXPECT_CALL(*mcast, initialize()) + .WillOnce(Return(0)); + + EXPECT_CALL(*mcast, isInitialized()) + .WillRepeatedly(Return(1)); + + EXPECT_CALL(*mcast, addAddress(_, _)) + .Times(2); + + EXPECT_CALL(*mcast, broadcast(_)) + .Times(AtLeast(1)); +} + +TEST_F(VariableServerListenThread_test, init_listen_device) { + // ARRANGE + setup_normal_listener_expectations(listener); + EXPECT_CALL(*listener, initialize()) + .WillOnce(Return(0)); + + Trick::VariableServerListenThread listen_thread (listener); + + // ACT + listen_thread.init_listen_device(); + + // ASSERT +} + +TEST_F(VariableServerListenThread_test, init_listen_device_fails) { + // ARRANGE + setup_normal_listener_expectations(listener); + EXPECT_CALL(*listener, initialize()) + .WillOnce(Return(-1)); + + Trick::VariableServerListenThread listen_thread (listener); + + // ACT + int status = listen_thread.init_listen_device(); + + // ASSERT + EXPECT_EQ(status, -1); +} + + + +TEST_F(VariableServerListenThread_test, get_hostname) { + // ARRANGE + setup_normal_listener_expectations(listener); + + Trick::VariableServerListenThread listen_thread (listener); + + // ACT + const char * hostname = listen_thread.get_hostname(); + + // ASSERT + EXPECT_STREQ(hostname, "MyHostname"); +} + +TEST_F(VariableServerListenThread_test, get_port) { + // ARRANGE + setup_normal_listener_expectations(listener); + + Trick::VariableServerListenThread listen_thread (listener); + + // ACT + int port = listen_thread.get_port(); + + // ASSERT + EXPECT_EQ(port, 1234); +} + +TEST_F(VariableServerListenThread_test, get_port_returns_requested) { + // ARRANGE + setup_normal_listener_expectations(listener); + + Trick::VariableServerListenThread listen_thread (listener); + + // ACT + listen_thread.set_port(4321); + int port = listen_thread.get_port(); + + // ASSERT + EXPECT_EQ(port, 4321); +} + +TEST_F(VariableServerListenThread_test, check_and_move_listen_device_init_fails) { + // ARRANGE + setup_normal_listener_expectations(listener); + Trick::VariableServerListenThread listen_thread (listener); + listen_thread.set_port(4321); + + EXPECT_CALL(*listener, disconnect()); + EXPECT_CALL(*listener, initialize(_, _)) + .WillOnce(Return(1)); + + // ACT + int status = listen_thread.check_and_move_listen_device(); + + // ASSERT + EXPECT_EQ(status, -1); +} + +TEST_F(VariableServerListenThread_test, check_and_move_listen_device) { + // ARRANGE + setup_normal_listener_expectations(listener); + Trick::VariableServerListenThread listen_thread (listener); + listen_thread.set_port(4321); + + EXPECT_CALL(*listener, disconnect()); + EXPECT_CALL(*listener, initialize(_, _)) + .WillOnce(Return(0)); + + // ACT + int status = listen_thread.check_and_move_listen_device(); + + // ASSERT + EXPECT_EQ(status, 0); +} + +TEST_F(VariableServerListenThread_test, run_thread) { + // ARRANGE + setup_normal_listener_expectations(listener); + setup_normal_mcast_expectations(mcast); + + EXPECT_CALL(*listener, setBlockMode(true)) + .Times(1); + + EXPECT_CALL(*listener, checkForNewConnections()) + .WillRepeatedly(Return(false)); + + Trick::VariableServerListenThread listen_thread (listener); + listen_thread.set_multicast_group(mcast); + + // ACT + listen_thread.create_thread(); + + sleep(3); + + listen_thread.cancel_thread(); + listen_thread.join_thread(); + + // ASSERT +} + + +TEST_F(VariableServerListenThread_test, run_thread_no_broadcast) { + // ARRANGE + setup_normal_listener_expectations(listener); + + // Expect no calls to mcast + EXPECT_CALL (*mcast, initialize()) + .Times(0); + + EXPECT_CALL (*mcast, broadcast(_)) + .Times(0); + + EXPECT_CALL(*listener, setBlockMode(true)) + .Times(1); + + EXPECT_CALL(*listener, checkForNewConnections()) + .WillRepeatedly(Return(false)); + + Trick::VariableServerListenThread listen_thread (listener); + listen_thread.set_broadcast(false); + listen_thread.set_multicast_group(mcast); + + // ACT + listen_thread.create_thread(); + + sleep(3); + + listen_thread.cancel_thread(); + listen_thread.join_thread(); + + // ASSERT +} + + + +TEST_F(VariableServerListenThread_test, run_thread_turn_on_broadcast) { + // ARRANGE + setup_normal_listener_expectations(listener); + setup_normal_mcast_expectations(mcast); + + EXPECT_CALL(*listener, setBlockMode(true)) + .Times(1); + + EXPECT_CALL(*listener, checkForNewConnections()) + .WillRepeatedly(Return(false)); + + EXPECT_CALL(*mcast, isInitialized()) + .WillOnce(Return(0)) + .WillRepeatedly(Return(1)); + + Trick::VariableServerListenThread listen_thread (listener); + listen_thread.set_broadcast(false); + listen_thread.set_multicast_group(mcast); + + // ACT + listen_thread.create_thread(); + + EXPECT_EQ(listen_thread.get_broadcast(), false); + + sleep(3); + listen_thread.set_broadcast(true); + sleep(3); + EXPECT_EQ(listen_thread.get_broadcast(), true); + + + listen_thread.cancel_thread(); + listen_thread.join_thread(); + + // ASSERT +} + +TEST_F(VariableServerListenThread_test, accept_connection) { + // ARRANGE + setup_normal_listener_expectations(listener); + + // Expect no calls to mcast + EXPECT_CALL (*mcast, initialize()) + .Times(0); + + EXPECT_CALL (*mcast, broadcast(_)) + .Times(0); + + EXPECT_CALL(*listener, setBlockMode(true)) + .Times(1); + + EXPECT_CALL(*listener, checkForNewConnections()) + .WillOnce(Return(true)) + .WillRepeatedly(Return(false)); + + MockTCPConnection connection; + EXPECT_CALL(connection, start()) + .WillOnce(Return(0)); + + EXPECT_CALL(connection, read(_, _)) + .WillOnce(Return(-1)); + + EXPECT_CALL(connection, disconnect()); + + EXPECT_CALL(*listener, setUpNewConnection()) + .WillOnce(Return(&connection)); + + Trick::VariableServerListenThread listen_thread (listener); + listen_thread.set_broadcast(false); + listen_thread.set_multicast_group(mcast); + + // ACT + listen_thread.create_thread(); + + listen_thread.dump(std::cout); + + sleep(3); + + listen_thread.cancel_thread(); + listen_thread.join_thread(); + + // ASSERT +} + + +TEST_F(VariableServerListenThread_test, connection_fails) { + // ARRANGE + setup_normal_listener_expectations(listener); + + // Expect no calls to mcast + EXPECT_CALL (*mcast, initialize()) + .Times(0); + + EXPECT_CALL (*mcast, broadcast(_)) + .Times(0); + + EXPECT_CALL(*listener, setBlockMode(true)) + .Times(1); + + EXPECT_CALL(*listener, checkForNewConnections()) + .WillOnce(Return(true)) + .WillRepeatedly(Return(false)); + + MockTCPConnection connection; + EXPECT_CALL(connection, start()) + .WillOnce(Return(-1)); + + EXPECT_CALL(connection, disconnect()); + + EXPECT_CALL(*listener, setUpNewConnection()) + .WillOnce(Return(&connection)); + + Trick::VariableServerListenThread listen_thread (listener); + listen_thread.set_broadcast(false); + listen_thread.set_multicast_group(mcast); + + // ACT + listen_thread.create_thread(); + + sleep(3); + + listen_thread.cancel_thread(); + listen_thread.join_thread(); + + // ASSERT +} + + +TEST_F(VariableServerListenThread_test, restart_fails) { + // ARRANGE + + setup_normal_listener_expectations(listener); + Trick::VariableServerListenThread listen_thread (listener); + + EXPECT_CALL(*listener, restart()); + EXPECT_CALL(*listener, validateSourceAddress(_)) + .WillOnce(Return(false)); + + EXPECT_CALL(*listener, initialize(_, _)) + .WillOnce(Return(-1)); + + EXPECT_CALL(*listener, disconnect()); + + + // ACT + // ASSERT + EXPECT_EQ(listen_thread.restart(), -1); +} \ No newline at end of file diff --git a/trick_source/sim_services/VariableServer/test/VariableServerSessionThread_test.cc b/trick_source/sim_services/VariableServer/test/VariableServerSessionThread_test.cc new file mode 100644 index 00000000..5b9cb609 --- /dev/null +++ b/trick_source/sim_services/VariableServer/test/VariableServerSessionThread_test.cc @@ -0,0 +1,357 @@ +/******************************TRICK HEADER************************************* +PURPOSE: ( Tests for the VariableServerSessionThread class ) +*******************************************************************************/ + +#include +#include +#include + +#include "trick/VariableServer.hh" +#include "trick/RealtimeSync.hh" +#include "trick/GetTimeOfDayClock.hh" +#include "trick/ITimer.hh" +#include "trick/ExecutiveException.hh" + +#include "trick/VariableServerSessionThread.hh" + +#include "MockVariableServerSession.hh" +#include "MockClientConnection.hh" + +using ::testing::Return; +using ::testing::_; +using ::testing::AtLeast; +using ::testing::DoAll; +using ::testing::Throw; +using ::testing::Const; +using ::testing::NiceMock; + + +// Set up default mock behavior + +void setup_default_connection_mocks (MockClientConnection * connection) { + // Starting the connection succeeds + ON_CALL(*connection, start()) + .WillByDefault(Return(0)); + + // We should always get a disconnect call + ON_CALL(*connection, disconnect()) + .WillByDefault(Return(0)); +} + +void setup_default_session_mocks (MockVariableServerSession * session, bool command_exit = true) { + ON_CALL(*session, handle_message()) + .WillByDefault(Return(0)); + + ON_CALL(*session, copy_and_write_async()) + .WillByDefault(Return(0)); + + ON_CALL(*session, get_pause()) + .WillByDefault(Return(false)); + + ON_CALL(Const(*session), get_update_rate()) + .WillByDefault(Return(0.001)); + + ON_CALL(*session, get_exit_cmd()) + .WillByDefault(Return(false)); +} + +/* + Test Fixture. + */ +class VariableServerSessionThread_test : public ::testing::Test { + protected: + // Static global dependencies that I would like to eventually mock out + Trick::VariableServer * varserver; + Trick::RealtimeSync * realtime_sync; + Trick::GetTimeOfDayClock clock; + Trick::ITimer timer; + + MockClientConnection connection; + NiceMock * session; + + VariableServerSessionThread_test() { + // Set up dependencies that haven't been broken + varserver = new Trick::VariableServer; + Trick::VariableServerSessionThread::set_vs_ptr(varserver); + realtime_sync = new Trick::RealtimeSync(&clock, &timer); + + // Set up mocks + session = new NiceMock; + setup_default_connection_mocks(&connection); + setup_default_session_mocks(session); + } + + ~VariableServerSessionThread_test() { + delete varserver; + delete realtime_sync; + } + + void SetUp() {} + void TearDown() {} +}; + + +// Helper functions for common cases of Mock expectations + +void setup_normal_connection_expectations (MockClientConnection * connection) { + // Starting the connection succeeds + EXPECT_CALL(*connection, start()) + .Times(1) + .WillOnce(Return(0)); + + // We should always get a disconnect call + EXPECT_CALL(*connection, disconnect()) + .Times(1); +} + +void set_session_exit_after_some_loops(MockVariableServerSession * session) { + EXPECT_CALL(*session, get_exit_cmd()) + .WillOnce(Return(false)) + .WillOnce(Return(false)) + .WillOnce(Return(false)) + .WillOnce(Return(false)) + .WillOnce(Return(false)) + .WillOnce(Return(true)); +} + +TEST_F(VariableServerSessionThread_test, connection_failure) { + // ARRANGE + + // Starting the connection fails + EXPECT_CALL(connection, start()) + .Times(1) + .WillOnce(Return(1)); + + // Set up VariableServerSessionThread + Trick::VariableServerSessionThread * vst = new Trick::VariableServerSessionThread(session) ; + vst->set_connection(&connection); + + // ACT + vst->create_thread(); + pthread_t id = vst->get_pthread_id(); + Trick::ConnectionStatus status = vst->wait_for_accept(); + vst->join_thread(); + + // ASSERT + EXPECT_EQ(status, Trick::ConnectionStatus::CONNECTION_FAIL); + + // There should be nothing in the VariableServer's thread list + EXPECT_EQ(varserver->get_vst(id), (Trick::VariableServerSessionThread *) NULL); + EXPECT_EQ(varserver->get_session(id), (Trick::VariableServerSession *) NULL); + + delete session; +} + + +TEST_F(VariableServerSessionThread_test, exit_if_handle_message_fails) { + // ARRANGE + setup_normal_connection_expectations(&connection); + + // Handle a message, but it fails + EXPECT_CALL(*session, handle_message()) + .Times(1) + .WillOnce(Return(-1)); + + + // Set up VariableServerSessionThread + Trick::VariableServerSessionThread * vst = new Trick::VariableServerSessionThread(session) ; + vst->set_connection(&connection); + + // ACT + vst->create_thread(); + pthread_t id = vst->get_pthread_id(); + Trick::ConnectionStatus status = vst->wait_for_accept(); + ASSERT_EQ(status, Trick::ConnectionStatus::CONNECTION_SUCCESS); + + vst->join_thread(); + + // ASSERT + + // There should be nothing in the VariableServer's thread list + EXPECT_EQ(varserver->get_vst(id), (Trick::VariableServerSessionThread *) NULL); + EXPECT_EQ(varserver->get_session(id), (Trick::VariableServerSession *) NULL); +} + + +TEST_F(VariableServerSessionThread_test, exit_if_write_fails) { + // ARRANGE + setup_normal_connection_expectations(&connection); + + // Write out data + EXPECT_CALL(*session, copy_and_write_async()) + .WillOnce(Return(-1)); + + + // Set up VariableServerSessionThread + Trick::VariableServerSessionThread * vst = new Trick::VariableServerSessionThread(session) ; + vst->set_connection(&connection); + + // ACT + vst->create_thread(); + pthread_t id = vst->get_pthread_id(); + Trick::ConnectionStatus status = vst->wait_for_accept(); + ASSERT_EQ(status, Trick::ConnectionStatus::CONNECTION_SUCCESS); + + vst->join_thread(); + + // ASSERT + + // There should be nothing in the VariableServer's thread list + EXPECT_EQ(varserver->get_vst(id), (Trick::VariableServerSessionThread *) NULL); + EXPECT_EQ(varserver->get_session(id), (Trick::VariableServerSession *) NULL); +} + +TEST_F(VariableServerSessionThread_test, exit_commanded) { + // ARRANGE + setup_normal_connection_expectations(&connection); + set_session_exit_after_some_loops(session); + + // Set up VariableServerSessionThread + Trick::VariableServerSessionThread * vst = new Trick::VariableServerSessionThread(session) ; + vst->set_connection(&connection); + + // ACT + vst->create_thread(); + pthread_t id = vst->get_pthread_id(); + Trick::ConnectionStatus status = vst->wait_for_accept(); + ASSERT_EQ(status, Trick::ConnectionStatus::CONNECTION_SUCCESS); + + // Confirm that the session has been created + Trick::VariableServerSession * vs_session = varserver->get_session(id); + ASSERT_TRUE(vs_session == session); + + // Runs for a few loops, then exits + + // Thread should shut down + vst->join_thread(); + + // ASSERT + // There should be nothing in the VariableServer's thread list + EXPECT_EQ(varserver->get_vst(id), (Trick::VariableServerSessionThread *) NULL); + EXPECT_EQ(varserver->get_session(id), (Trick::VariableServerSession *) NULL); +} + +TEST_F(VariableServerSessionThread_test, thread_cancelled) { + // ARRANGE + setup_normal_connection_expectations(&connection); + + // Set up VariableServerSessionThread + Trick::VariableServerSessionThread * vst = new Trick::VariableServerSessionThread(session) ; + vst->set_connection(&connection); + vst->create_thread(); + pthread_t id = vst->get_pthread_id(); + Trick::ConnectionStatus status = vst->wait_for_accept(); + ASSERT_EQ(status, Trick::ConnectionStatus::CONNECTION_SUCCESS); + + // Confirm that the session has been created + Trick::VariableServerSession * vs_session = varserver->get_session(id); + ASSERT_TRUE(vs_session == session); + + // ACT + vst->cancel_thread(); + + // Thread should shut down + vst->join_thread(); + + // ASSERT + // There should be nothing in the VariableServer's thread list + EXPECT_EQ(varserver->get_vst(id), (Trick::VariableServerSessionThread *) NULL); + EXPECT_EQ(varserver->get_session(id), (Trick::VariableServerSession *) NULL); +} + + +TEST_F(VariableServerSessionThread_test, turn_session_log_on) { + // ARRANGE + setup_normal_connection_expectations(&connection); + set_session_exit_after_some_loops(session); + + varserver->set_var_server_log_on(); + + // We expect a the session's log to be turned on + EXPECT_CALL(*session, set_log_on()) + .Times(1); + + // Set up VariableServerSessionThread + Trick::VariableServerSessionThread * vst = new Trick::VariableServerSessionThread(session) ; + vst->set_connection(&connection); + + // ACT + vst->create_thread(); + pthread_t id = vst->get_pthread_id(); + Trick::ConnectionStatus status = vst->wait_for_accept(); + ASSERT_EQ(status, Trick::ConnectionStatus::CONNECTION_SUCCESS); + + // Confirm that the session has been created + Trick::VariableServerSession * vs_session = varserver->get_session(id); + ASSERT_TRUE(vs_session == session); + + // Thread should shut down + vst->join_thread(); + + // ASSERT + // There should be nothing in the VariableServer's thread list + EXPECT_EQ(varserver->get_vst(id), (Trick::VariableServerSessionThread *) NULL); + EXPECT_EQ(varserver->get_session(id), (Trick::VariableServerSession *) NULL); +} + +TEST_F(VariableServerSessionThread_test, throw_trick_executive_exception) { + // ARRANGE + setup_normal_connection_expectations(&connection); + + EXPECT_CALL(*session, get_exit_cmd()) + .WillRepeatedly(Return(false)); + + // Set up VariableServerSessionThread + Trick::VariableServerSessionThread * vst = new Trick::VariableServerSessionThread(session) ; + vst->set_connection(&connection); + + EXPECT_CALL(*session, handle_message()) + .WillOnce(Throw(Trick::ExecutiveException(-1, __FILE__, __LINE__, "Trick::ExecutiveException Error message for testing"))); + + // ACT + vst->create_thread(); + pthread_t id = vst->get_pthread_id(); + Trick::ConnectionStatus status = vst->wait_for_accept(); + ASSERT_EQ(status, Trick::ConnectionStatus::CONNECTION_SUCCESS); + + // Confirm that the session has been created + Trick::VariableServerSession * vs_session = varserver->get_session(id); + ASSERT_TRUE(vs_session == session); + + // Thread should shut down + vst->join_thread(); + + // ASSERT + // There should be nothing in the VariableServer's thread list + EXPECT_EQ(varserver->get_vst(id), (Trick::VariableServerSessionThread *) NULL); + EXPECT_EQ(varserver->get_session(id), (Trick::VariableServerSession *) NULL); +} + +TEST_F(VariableServerSessionThread_test, throw_exception) { + // ARRANGE + setup_normal_connection_expectations(&connection); + + EXPECT_CALL(*session, get_exit_cmd()) + .WillRepeatedly(Return(false)); + + // Set up VariableServerSessionThread + Trick::VariableServerSessionThread * vst = new Trick::VariableServerSessionThread(session) ; + vst->set_connection(&connection); + + EXPECT_CALL(*session, handle_message()) + .WillOnce(Throw(std::logic_error("Error message for testing"))); + + // ACT + vst->create_thread(); + pthread_t id = vst->get_pthread_id(); + Trick::ConnectionStatus status = vst->wait_for_accept(); + ASSERT_EQ(status, Trick::ConnectionStatus::CONNECTION_SUCCESS); + + // Thread should shut down + vst->join_thread(); + + // ASSERT + // There should be nothing in the VariableServer's thread list + EXPECT_EQ(varserver->get_vst(id), (Trick::VariableServerSessionThread *) NULL); + EXPECT_EQ(varserver->get_session(id), (Trick::VariableServerSession *) NULL); +} \ No newline at end of file diff --git a/trick_source/sim_services/VariableServer/test/VariableServerSession_test.cc b/trick_source/sim_services/VariableServer/test/VariableServerSession_test.cc index 66069a16..3a702c99 100644 --- a/trick_source/sim_services/VariableServer/test/VariableServerSession_test.cc +++ b/trick_source/sim_services/VariableServer/test/VariableServerSession_test.cc @@ -3,6 +3,8 @@ PURPOSE: ( Tests for the VariableServerSession class ) *******************************************************************************/ #include +#include + #include #include #include @@ -11,10 +13,25 @@ PURPOSE: ( Tests for the VariableServerSession class ) #include "trick/MemoryManager.hh" #include "trick/UdUnits.hh" -#include "TestConnection.hh" +#include "trick/RealtimeSync.hh" +#include "trick/GetTimeOfDayClock.hh" +#include "trick/ITimer.hh" + #include "trick/VariableServerSession.hh" #include "trick/var_binary_parser.hh" +#include "MockClientConnection.hh" +#include "MockVariableServerSession.hh" + + + +using ::testing::AtLeast; +using ::testing::_; +using ::testing::Truly; +using ::testing::Args; +using ::testing::Return; +using ::testing::Invoke; + /* Test Fixture. @@ -23,16 +40,26 @@ class VariableServerSession_test : public ::testing::Test { protected: Trick::MemoryManager *memmgr; Trick::UdUnits * udunits; + Trick::RealtimeSync * realtime_sync; - TestConnection connection; + Trick::GetTimeOfDayClock clock; + Trick::ITimer timer; + + + MockClientConnection connection; VariableServerSession_test() { memmgr = new Trick::MemoryManager; udunits = new Trick::UdUnits; + realtime_sync = new Trick::RealtimeSync(&clock, &timer); udunits->read_default_xml(); } - ~VariableServerSession_test() { delete memmgr; } + ~VariableServerSession_test() { + delete memmgr; + delete realtime_sync; + } + void SetUp() {} void TearDown() {} }; @@ -46,7 +73,7 @@ TEST_F(VariableServerSession_test, toString) { (void) memmgr->declare_extern_var(&b, "double b"); (void) memmgr->declare_extern_var(&c, "std::string c"); - Trick::VariableServerSession session(&connection); + Trick::VariableServerSession session; session.var_add("a"); session.var_add("b"); @@ -68,8 +95,7 @@ TEST_F(VariableServerSession_test, toString) { TEST_F(VariableServerSession_test, var_sync) { // ARRANGE - Trick::VariableServerSession session(&connection); - + Trick::VariableServerSession session; // ACT session.var_sync(0); @@ -94,7 +120,8 @@ TEST_F(VariableServerSession_test, var_sync) { TEST_F(VariableServerSession_test, large_message_ascii) { // ARRANGE - Trick::VariableServerSession session(&connection); + Trick::VariableServerSession session; + session.set_connection(&connection); const static int big_arr_size = 4000; // Make an array too big to fit in a single message @@ -104,49 +131,75 @@ TEST_F(VariableServerSession_test, large_message_ascii) { big_arr[i] = i; } + // Set it up with the memory manager (void) memmgr->declare_extern_var(&big_arr, "int big_arr[4000]"); + // Create references for all of them + std::vector vars; for (int i = 0; i < big_arr_size; i++) { std::string var_name = "big_arr[" + std::to_string(i) + "]"; - session.var_add(var_name.c_str()); + Trick::VariableReference * var = new Trick::VariableReference(var_name); + var->stageValue(); + vars.push_back(var); } - // ACT - session.copy_sim_data(); - session.write_data(); - - // ASSERT - ASSERT_TRUE(connection.ascii_messages_written.size() > 1); - int counter = 0; - - int message_index = 0; - for (int i = 0; i < connection.ascii_messages_written.size(); i++) { - std::string message = connection.ascii_messages_written[i]; - std::stringstream ss(message); + // Make a matcher for the Mock + // This will check that the messages that are passed into connection.write() are correct + int val_counter = 0; + int message_counter = 0; + int final_val = 0; + auto constructedCorrectly = [&] (std::string s) -> bool { + std::stringstream ss(s); std::string token; - // First val in first message should be the message type, 0 - if (i == 0) { + if (message_counter == 0) { + // First val in first message should be the message type, 0 std::getline(ss, token, '\t'); int message_type = stoi(token); - EXPECT_EQ(message_type, 0); + if (message_type != 0) return false; + } else { + // First val in all other message should be (last val in previous message)+1 + std::getline(ss, token, '\t'); + int first_num = stoi(token); + if (first_num != final_val+1) return false; + val_counter++; } + int num = 0; + + // The rest of the message should just be sequential while (std::getline(ss, token, '\t')) { if (token == "\n") { break; } - - int num = stoi(token); - EXPECT_EQ(counter, num); - counter++; + + num = stoi(token); + if (val_counter != num) return false; + val_counter++; } - } - EXPECT_EQ(counter, big_arr_size); + // Save the final value so that we can verify the next message starts correctly + final_val = num; + message_counter++; + + return true; + }; + + // Set up the mock connection - it should make 3 calls to write + // The constructedCorrectly matcher will ensure that the values passed in are what we want them to be + EXPECT_CALL(connection, write(Truly(constructedCorrectly))) + .Times(3); + + // ACT + session.write_data(vars, (VS_MESSAGE_TYPE) 0); + + // ASSERT + // Make sure that the entire message was written out + ASSERT_EQ(val_counter, big_arr_size); } -TEST_F(VariableServerSession_test, large_message_binary) { +TEST_F(VariableServerSession_test, DISABLED_large_message_binary) { // ARRANGE - Trick::VariableServerSession session(&connection); + Trick::VariableServerSession session; + session.set_connection(&connection); session.var_binary(); const static int big_arr_size = 4000; @@ -157,26 +210,57 @@ TEST_F(VariableServerSession_test, large_message_binary) { big_arr[i] = i; } + // Set it up with the memory manager (void) memmgr->declare_extern_var(&big_arr, "int big_arr[4000]"); + // Create references for all of them + std::vector vars; for (int i = 0; i < big_arr_size; i++) { std::string var_name = "big_arr[" + std::to_string(i) + "]"; - session.var_add(var_name.c_str()); + Trick::VariableReference * var = new Trick::VariableReference(var_name); + var->stageValue(); + vars.push_back(var); } + // Create a matcher that parses and collects the arguments into a ParsedBinaryMessage + ParsedBinaryMessage full_message; + auto binaryConstructedCorrectly = [&] (std::tuple msg_tuple) -> bool { + char * message; + int size; + std::tie(message, size) = msg_tuple; + ParsedBinaryMessage partial_message; + + // Put the message into a vector the parser + std::vector bytes; + for (int i = 0; i < size; i++) { + bytes.push_back(message[i]); + } + + // Parse and add it to the full message + try { + partial_message.parse(bytes); + full_message.combine(partial_message); + } catch (const MalformedMessageException& ex) { + std::cout << "Parser failed with message: " << ex.what(); + return false; + } + + return true; + }; + + // Set up the mock connection - it should make a bunch of calls to write + // The constructedCorrectly matcher will ensure that the values passed in are what we want them to be + EXPECT_CALL(connection, write(_, _)).With(Args<0,1>(Truly(binaryConstructedCorrectly))).Times(AtLeast(3)); + // ACT - session.copy_sim_data(); - session.write_data(); + session.write_data(vars, (VS_MESSAGE_TYPE) 0); // ASSERT - ParsedBinaryMessage full_message; - for (int i = 0; i < connection.binary_messages_written.size(); i++) { - ParsedBinaryMessage partial_message; - partial_message.parse(connection.binary_messages_written[i]); - full_message.combine(partial_message); - } - + + // Make sure we got all of the values ASSERT_EQ(full_message.getNumVars(), big_arr_size); + + // Make sure all of the values are correct and have names for (int i = 0; i < big_arr_size; i++) { try { std::string var_name = "big_arr[" + std::to_string(i) + "]"; @@ -188,3 +272,985 @@ TEST_F(VariableServerSession_test, large_message_binary) { } } } + + +void setup_partial_session_mock(MockVariableServerSession& session) { + EXPECT_CALL(session, copy_and_write_async()) + .WillOnce(Invoke(&session, &MockVariableServerSession::copy_and_write_async_concrete)); +} + +void set_session_modes(MockVariableServerSession& session, VS_COPY_MODE copy_mode, VS_WRITE_MODE write_mode, bool pause) { + EXPECT_CALL(session, get_copy_mode()) + .WillRepeatedly(Return(copy_mode)); + EXPECT_CALL(session, get_write_mode()) + .WillRepeatedly(Return(write_mode)); + EXPECT_CALL(session, get_pause()) + .WillOnce(Return(pause)); +} + + +TEST_F(VariableServerSession_test, copy_async_disabled) { + // ARRANGE + MockVariableServerSession session; + setup_partial_session_mock(session); + + // set disabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(false)); + + // Nothing else should be called + EXPECT_CALL(session, copy_sim_data()) + .Times(0); + + EXPECT_CALL(session, write_data()) + .Times(0); + + session.copy_and_write_async(); +} + + +TEST_F(VariableServerSession_test, copy_async_copy_and_write) { + // ARRANGE + MockVariableServerSession session; + setup_partial_session_mock(session); + + // set enabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(true)); + + // Copy async, write when copied, not paused + set_session_modes(session, VS_COPY_ASYNC, VS_WRITE_ASYNC, false); + + // Copy and write should be called + EXPECT_CALL(session, copy_sim_data()) + .Times(1); + + EXPECT_CALL(session, write_data()) + .Times(1); + + // ACT + int result = session.copy_and_write_async(); + + // ASSERT + ASSERT_GE(result, 0); +} + +TEST_F(VariableServerSession_test, copy_async_copy_write_when_copied) { + // ARRANGE + MockVariableServerSession session; + setup_partial_session_mock(session); + + // set enabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(true)); + + // Copy async, write when copied, not paused + set_session_modes(session, VS_COPY_ASYNC, VS_WRITE_WHEN_COPIED, false); + + // Copy and write should be called + EXPECT_CALL(session, copy_sim_data()) + .Times(1); + + EXPECT_CALL(session, write_data()) + .Times(1); + + // ACT + int result = session.copy_and_write_async(); + + // ASSERT + ASSERT_GE(result, 0); +} + +TEST_F(VariableServerSession_test, copy_async_no_copy_or_write) { + // ARRANGE + MockVariableServerSession session; + setup_partial_session_mock(session); + + // set enabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(true)); + + // Copy async, write when copied, not paused + set_session_modes(session, VS_COPY_SCHEDULED, VS_WRITE_WHEN_COPIED, false); + realtime_sync->active = true; + + + // Copy and write should be called + EXPECT_CALL(session, copy_sim_data()) + .Times(0); + + EXPECT_CALL(session, write_data()) + .Times(0); + + // ACT + int result = session.copy_and_write_async(); + + // ASSERT + ASSERT_GE(result, 0); +} + +TEST_F(VariableServerSession_test, copy_async_non_realtime) { + // ARRANGE + MockVariableServerSession session; + setup_partial_session_mock(session); + + // set enabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(true)); + + // Copy async, write async, paused + set_session_modes(session, VS_COPY_SCHEDULED, VS_WRITE_WHEN_COPIED, false); + realtime_sync->active = false; + + // Copy and write should be called + EXPECT_CALL(session, copy_sim_data()) + .Times(0); + + EXPECT_CALL(session, write_data()) + .Times(1); + + // ACT + int result = session.copy_and_write_async(); + + // ASSERT + ASSERT_GE(result, 0); +} + +TEST_F(VariableServerSession_test, copy_async_paused) { + // ARRANGE + MockVariableServerSession session; + setup_partial_session_mock(session); + + // set enabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(true)); + + // Copy async, write async, paused + set_session_modes(session, VS_COPY_ASYNC, VS_WRITE_ASYNC, true); + + // Copy and write should be called + EXPECT_CALL(session, copy_sim_data()) + .Times(1); + + EXPECT_CALL(session, write_data()) + .Times(0); + + // ACT + int result = session.copy_and_write_async(); + + // ASSERT + ASSERT_GE(result, 0); +} + + +TEST_F(VariableServerSession_test, copy_async_write_fails) { + // ARRANGE + MockVariableServerSession session; + setup_partial_session_mock(session); + + // set enabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(true)); + + // Copy async, write async, paused + set_session_modes(session, VS_COPY_ASYNC, VS_WRITE_ASYNC, false); + + // Copy and write should be called + EXPECT_CALL(session, copy_sim_data()) + .Times(1); + + EXPECT_CALL(session, write_data()) + .WillOnce(Return(-1)); + + // Exit should be called + EXPECT_CALL(session, set_exit_cmd()) + .Times(1); + + // ACT + int result = session.copy_and_write_async(); + + // ASSERT + ASSERT_EQ(result, -1); +} + + +TEST_F(VariableServerSession_test, copy_and_write_top_disabled) { + // ARRANGE + MockVariableServerSession session; + + // set disabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(false)); + + // Nothing else should be called + EXPECT_CALL(session, copy_sim_data()) + .Times(0); + + EXPECT_CALL(session, write_data()) + .Times(0); + + session.copy_and_write_top(100); +} + +TEST_F(VariableServerSession_test, copy_and_write_top_copy_top_write_async) { + // ARRANGE + MockVariableServerSession session; + + // set enabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(true)); + + set_session_modes(session, VS_COPY_TOP_OF_FRAME, VS_WRITE_ASYNC, false); + realtime_sync->active = true; + + EXPECT_CALL(session, get_frame_multiple()) + .WillRepeatedly(Return(10)); + EXPECT_CALL(session, get_frame_offset()) + .WillRepeatedly(Return(0)); + + // Should only copy + EXPECT_CALL(session, copy_sim_data()) + .Times(1); + + EXPECT_CALL(session, write_data()) + .Times(0); + + // ACT + int result = session.copy_and_write_top(100); + + // ASSERT + EXPECT_EQ(result, 0); +} + +TEST_F(VariableServerSession_test, copy_and_write_top_copy_top_write_when_copied) { + // ARRANGE + MockVariableServerSession session; + + // set enabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(true)); + + set_session_modes(session, VS_COPY_TOP_OF_FRAME, VS_WRITE_WHEN_COPIED, false); + realtime_sync->active = true; + + EXPECT_CALL(session, get_frame_multiple()) + .WillRepeatedly(Return(10)); + EXPECT_CALL(session, get_frame_offset()) + .WillRepeatedly(Return(0)); + + // Should only copy + EXPECT_CALL(session, copy_sim_data()) + .Times(1); + + EXPECT_CALL(session, write_data()) + .Times(1); + + // ACT + int result = session.copy_and_write_top(100); + + // ASSERT + EXPECT_EQ(result, 0); +} + +TEST_F(VariableServerSession_test, copy_and_write_top_copy_top_dont_write_if_non_realtime) { + // ARRANGE + MockVariableServerSession session; + + // set enabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(true)); + + set_session_modes(session, VS_COPY_TOP_OF_FRAME, VS_WRITE_WHEN_COPIED, false); + realtime_sync->active = false; + + EXPECT_CALL(session, get_frame_multiple()) + .WillRepeatedly(Return(10)); + EXPECT_CALL(session, get_frame_offset()) + .WillRepeatedly(Return(0)); + + // Should only copy + EXPECT_CALL(session, copy_sim_data()) + .Times(1); + + EXPECT_CALL(session, write_data()) + .Times(0); + + // ACT + int result = session.copy_and_write_top(100); + + // ASSERT + EXPECT_EQ(result, 0); +} + +TEST_F(VariableServerSession_test, copy_and_write_top_copy_top_write_paused) { + // ARRANGE + MockVariableServerSession session; + + // set enabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(true)); + + set_session_modes(session, VS_COPY_TOP_OF_FRAME, VS_WRITE_WHEN_COPIED, true); + realtime_sync->active = true; + + EXPECT_CALL(session, get_frame_multiple()) + .WillRepeatedly(Return(10)); + EXPECT_CALL(session, get_frame_offset()) + .WillRepeatedly(Return(0)); + + // Should only copy + EXPECT_CALL(session, copy_sim_data()) + .Times(1); + + EXPECT_CALL(session, write_data()) + .Times(0); + + // ACT + int result = session.copy_and_write_top(100); + + // ASSERT + EXPECT_EQ(result, 0); +} + + +TEST_F(VariableServerSession_test, copy_and_write_top_wrong_offset) { + // ARRANGE + MockVariableServerSession session; + + // set enabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(true)); + + EXPECT_CALL(session, get_copy_mode()) + .WillRepeatedly(Return(VS_COPY_TOP_OF_FRAME)); + EXPECT_CALL(session, get_write_mode()) + .WillRepeatedly(Return(VS_WRITE_WHEN_COPIED)); + + realtime_sync->active = true; + + EXPECT_CALL(session, get_frame_multiple()) + .WillRepeatedly(Return(10)); + EXPECT_CALL(session, get_frame_offset()) + .WillRepeatedly(Return(0)); + + // Should only copy + EXPECT_CALL(session, copy_sim_data()) + .Times(0); + + EXPECT_CALL(session, write_data()) + .Times(0); + + // ACT + int result = session.copy_and_write_top(101); + + // ASSERT + EXPECT_EQ(result, 0); +} + + + +TEST_F(VariableServerSession_test, copy_and_write_freeze_disabled) { + // ARRANGE + MockVariableServerSession session; + + // set disabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(false)); + + // Nothing else should be called + EXPECT_CALL(session, copy_sim_data()) + .Times(0); + + EXPECT_CALL(session, write_data()) + .Times(0); + + session.copy_and_write_freeze(100); +} + +TEST_F(VariableServerSession_test, copy_and_write_freeze_copy_top_write_async) { + // ARRANGE + MockVariableServerSession session; + + // set enabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(true)); + + set_session_modes(session, VS_COPY_TOP_OF_FRAME, VS_WRITE_ASYNC, false); + realtime_sync->active = true; + + EXPECT_CALL(session, get_freeze_frame_multiple()) + .WillRepeatedly(Return(10)); + EXPECT_CALL(session, get_freeze_frame_offset()) + .WillRepeatedly(Return(0)); + + // Should only copy + EXPECT_CALL(session, copy_sim_data()) + .Times(1); + + EXPECT_CALL(session, write_data()) + .Times(0); + + // ACT + int result = session.copy_and_write_freeze(100); + + // ASSERT + EXPECT_EQ(result, 0); +} + +TEST_F(VariableServerSession_test, copy_and_write_freeze_copy_top_write_when_copied) { + // ARRANGE + MockVariableServerSession session; + + // set enabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(true)); + + set_session_modes(session, VS_COPY_TOP_OF_FRAME, VS_WRITE_WHEN_COPIED, false); + realtime_sync->active = true; + + EXPECT_CALL(session, get_freeze_frame_multiple()) + .WillRepeatedly(Return(10)); + EXPECT_CALL(session, get_freeze_frame_offset()) + .WillRepeatedly(Return(0)); + + // Should only copy + EXPECT_CALL(session, copy_sim_data()) + .Times(1); + + EXPECT_CALL(session, write_data()) + .Times(1); + + // ACT + int result = session.copy_and_write_freeze(100); + + // ASSERT + EXPECT_EQ(result, 0); +} + +TEST_F(VariableServerSession_test, copy_and_write_freeze_copy_top_dont_write_if_non_realtime) { + // ARRANGE + MockVariableServerSession session; + + // set enabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(true)); + + set_session_modes(session, VS_COPY_TOP_OF_FRAME, VS_WRITE_WHEN_COPIED, false); + realtime_sync->active = false; + + EXPECT_CALL(session, get_freeze_frame_multiple()) + .WillRepeatedly(Return(10)); + EXPECT_CALL(session, get_freeze_frame_offset()) + .WillRepeatedly(Return(0)); + + // Should only copy + EXPECT_CALL(session, copy_sim_data()) + .Times(1); + + EXPECT_CALL(session, write_data()) + .Times(0); + + // ACT + int result = session.copy_and_write_freeze(100); + + // ASSERT + EXPECT_EQ(result, 0); +} + +TEST_F(VariableServerSession_test, copy_and_write_freeze_copy_top_write_paused) { + // ARRANGE + MockVariableServerSession session; + + // set enabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(true)); + + set_session_modes(session, VS_COPY_TOP_OF_FRAME, VS_WRITE_WHEN_COPIED, true); + realtime_sync->active = true; + + EXPECT_CALL(session, get_freeze_frame_multiple()) + .WillRepeatedly(Return(10)); + EXPECT_CALL(session, get_freeze_frame_offset()) + .WillRepeatedly(Return(0)); + + // Should only copy + EXPECT_CALL(session, copy_sim_data()) + .Times(1); + + EXPECT_CALL(session, write_data()) + .Times(0); + + // ACT + int result = session.copy_and_write_freeze(100); + + // ASSERT + EXPECT_EQ(result, 0); +} + + +TEST_F(VariableServerSession_test, copy_and_write_freeze_wrong_offset) { + // ARRANGE + MockVariableServerSession session; + + // set enabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(true)); + + EXPECT_CALL(session, get_copy_mode()) + .WillRepeatedly(Return(VS_COPY_TOP_OF_FRAME)); + EXPECT_CALL(session, get_write_mode()) + .WillRepeatedly(Return(VS_WRITE_WHEN_COPIED)); + + realtime_sync->active = true; + + EXPECT_CALL(session, get_freeze_frame_multiple()) + .WillRepeatedly(Return(10)); + EXPECT_CALL(session, get_freeze_frame_offset()) + .WillRepeatedly(Return(0)); + + // Should only copy + EXPECT_CALL(session, copy_sim_data()) + .Times(0); + + EXPECT_CALL(session, write_data()) + .Times(0); + + // ACT + int result = session.copy_and_write_freeze(101); + + // ASSERT + EXPECT_EQ(result, 0); +} + +TEST_F(VariableServerSession_test, copy_and_write_scheduled_disabled) { + // ARRANGE + MockVariableServerSession session; + + // set enabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(false)); + + // Shouldn't do anything + EXPECT_CALL(session, copy_sim_data()) + .Times(0); + + EXPECT_CALL(session, write_data()) + .Times(0); + + // ACT + int result = session.copy_and_write_scheduled(100); + + // ASSERT + EXPECT_EQ(result, 0); +} + +TEST_F(VariableServerSession_test, copy_and_write_scheduled_wrong_mode) { + // ARRANGE + MockVariableServerSession session; + + // set enabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(false)); + EXPECT_CALL(session, get_copy_mode()) + .WillRepeatedly(Return(VS_COPY_TOP_OF_FRAME)); + + // Shouldn't do anything + EXPECT_CALL(session, copy_sim_data()) + .Times(0); + + EXPECT_CALL(session, write_data()) + .Times(0); + + // ACT + int result = session.copy_and_write_scheduled(100); + + // ASSERT + EXPECT_EQ(result, 0); +} + +TEST_F(VariableServerSession_test, copy_and_write_scheduled_wrong_tics) { + // ARRANGE + MockVariableServerSession session; + + // set enabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(false)); + EXPECT_CALL(session, get_copy_mode()) + .WillRepeatedly(Return(VS_COPY_TOP_OF_FRAME)); + EXPECT_CALL(session, get_next_tics()) + .WillRepeatedly(Return(10)); + + // Shouldn't do anything + EXPECT_CALL(session, copy_sim_data()) + .Times(0); + + EXPECT_CALL(session, write_data()) + .Times(0); + + // ACT + int result = session.copy_and_write_scheduled(100); + + // ASSERT + EXPECT_EQ(result, 0); +} + + +TEST_F(VariableServerSession_test, copy_and_write_scheduled_copy_and_write) { + // ARRANGE + MockVariableServerSession session; + + // set enabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(true)); + + set_session_modes(session, VS_COPY_SCHEDULED, VS_WRITE_WHEN_COPIED, false); + realtime_sync->active = true; + + EXPECT_CALL(session, get_next_tics()) + .WillRepeatedly(Return(100)); + + // Shouldn't do anything + EXPECT_CALL(session, copy_sim_data()) + .Times(1); + + EXPECT_CALL(session, write_data()) + .Times(1); + + // ACT + int result = session.copy_and_write_scheduled(100); + + // ASSERT + EXPECT_EQ(result, 0); +} + +TEST_F(VariableServerSession_test, copy_and_write_scheduled_copy_scheduled_write_async) { + // ARRANGE + MockVariableServerSession session; + + // set enabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(true)); + + set_session_modes(session, VS_COPY_SCHEDULED, VS_WRITE_ASYNC, false); + realtime_sync->active = true; + + EXPECT_CALL(session, get_next_tics()) + .WillRepeatedly(Return(100)); + + // Shouldn't do anything + EXPECT_CALL(session, copy_sim_data()) + .Times(1); + + EXPECT_CALL(session, write_data()) + .Times(0); + + // ACT + int result = session.copy_and_write_scheduled(100); + + // ASSERT + EXPECT_EQ(result, 0); +} + +TEST_F(VariableServerSession_test, copy_and_write_scheduled_paused) { + // ARRANGE + MockVariableServerSession session; + + // set enabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(true)); + + set_session_modes(session, VS_COPY_SCHEDULED, VS_WRITE_ASYNC, true); + realtime_sync->active = true; + + EXPECT_CALL(session, get_next_tics()) + .WillRepeatedly(Return(100)); + + // Shouldn't do anything + EXPECT_CALL(session, copy_sim_data()) + .Times(1); + + EXPECT_CALL(session, write_data()) + .Times(0); + + // ACT + int result = session.copy_and_write_scheduled(100); + + // ASSERT + EXPECT_EQ(result, 0); +} + +TEST_F(VariableServerSession_test, copy_and_write_scheduled_non_realtime) { + // ARRANGE + MockVariableServerSession session; + + // set enabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(true)); + + set_session_modes(session, VS_COPY_SCHEDULED, VS_WRITE_ASYNC, false); + realtime_sync->active = false; + + EXPECT_CALL(session, get_next_tics()) + .WillRepeatedly(Return(100)); + + // Shouldn't do anything + EXPECT_CALL(session, copy_sim_data()) + .Times(1); + + EXPECT_CALL(session, write_data()) + .Times(0); + + // ACT + int result = session.copy_and_write_scheduled(100); + + // ASSERT + EXPECT_EQ(result, 0); +} + + +TEST_F(VariableServerSession_test, copy_and_write_scheduled_write_fails) { + // ARRANGE + MockVariableServerSession session; + + // set enabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(true)); + + set_session_modes(session, VS_COPY_SCHEDULED, VS_WRITE_WHEN_COPIED, false); + realtime_sync->active = true; + + EXPECT_CALL(session, get_next_tics()) + .WillRepeatedly(Return(100)); + + EXPECT_CALL(session, copy_sim_data()) + .Times(1); + + EXPECT_CALL(session, write_data()) + .WillOnce(Return(-1)); + + EXPECT_CALL(session, set_exit_cmd()) + .Times(1); + + // ACT + int result = session.copy_and_write_scheduled(100); + + // ASSERT + EXPECT_EQ(result, -1); +} + + +TEST_F(VariableServerSession_test, copy_and_write_freeze_scheduled_disabled) { + // ARRANGE + MockVariableServerSession session; + + // set enabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(false)); + + // Shouldn't do anything + EXPECT_CALL(session, copy_sim_data()) + .Times(0); + + EXPECT_CALL(session, write_data()) + .Times(0); + + // ACT + int result = session.copy_and_write_freeze_scheduled(100); + + // ASSERT + EXPECT_EQ(result, 0); +} + +TEST_F(VariableServerSession_test, copy_and_write_freeze_scheduled_wrong_mode) { + // ARRANGE + MockVariableServerSession session; + + // set enabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(false)); + EXPECT_CALL(session, get_copy_mode()) + .WillRepeatedly(Return(VS_COPY_TOP_OF_FRAME)); + + // Shouldn't do anything + EXPECT_CALL(session, copy_sim_data()) + .Times(0); + + EXPECT_CALL(session, write_data()) + .Times(0); + + // ACT + int result = session.copy_and_write_freeze_scheduled(100); + + // ASSERT + EXPECT_EQ(result, 0); +} + +TEST_F(VariableServerSession_test, copy_and_write_freeze_scheduled_wrong_tics) { + // ARRANGE + MockVariableServerSession session; + + // set enabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(false)); + EXPECT_CALL(session, get_copy_mode()) + .WillRepeatedly(Return(VS_COPY_TOP_OF_FRAME)); + EXPECT_CALL(session, get_freeze_next_tics()) + .WillRepeatedly(Return(10)); + + // Shouldn't do anything + EXPECT_CALL(session, copy_sim_data()) + .Times(0); + + EXPECT_CALL(session, write_data()) + .Times(0); + + // ACT + int result = session.copy_and_write_freeze_scheduled(100); + + // ASSERT + EXPECT_EQ(result, 0); +} + + +TEST_F(VariableServerSession_test, copy_and_write_freeze_scheduled_copy_and_write) { + // ARRANGE + MockVariableServerSession session; + + // set enabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(true)); + + set_session_modes(session, VS_COPY_SCHEDULED, VS_WRITE_WHEN_COPIED, false); + realtime_sync->active = true; + + EXPECT_CALL(session, get_freeze_next_tics()) + .WillRepeatedly(Return(100)); + + // Shouldn't do anything + EXPECT_CALL(session, copy_sim_data()) + .Times(1); + + EXPECT_CALL(session, write_data()) + .Times(1); + + // ACT + int result = session.copy_and_write_freeze_scheduled(100); + + // ASSERT + EXPECT_EQ(result, 0); +} + +TEST_F(VariableServerSession_test, copy_and_write_freeze_scheduled_copy_scheduled_write_async) { + // ARRANGE + MockVariableServerSession session; + + // set enabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(true)); + + set_session_modes(session, VS_COPY_SCHEDULED, VS_WRITE_ASYNC, false); + realtime_sync->active = true; + + EXPECT_CALL(session, get_freeze_next_tics()) + .WillRepeatedly(Return(100)); + + // Shouldn't do anything + EXPECT_CALL(session, copy_sim_data()) + .Times(1); + + EXPECT_CALL(session, write_data()) + .Times(0); + + // ACT + int result = session.copy_and_write_freeze_scheduled(100); + + // ASSERT + EXPECT_EQ(result, 0); +} + +TEST_F(VariableServerSession_test, copy_and_write_freeze_scheduled_paused) { + // ARRANGE + MockVariableServerSession session; + + // set enabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(true)); + + set_session_modes(session, VS_COPY_SCHEDULED, VS_WRITE_ASYNC, true); + realtime_sync->active = true; + + EXPECT_CALL(session, get_freeze_next_tics()) + .WillRepeatedly(Return(100)); + + // Shouldn't do anything + EXPECT_CALL(session, copy_sim_data()) + .Times(1); + + EXPECT_CALL(session, write_data()) + .Times(0); + + // ACT + int result = session.copy_and_write_freeze_scheduled(100); + + // ASSERT + EXPECT_EQ(result, 0); +} + +TEST_F(VariableServerSession_test, copy_and_write_freeze_scheduled_non_realtime) { + // ARRANGE + MockVariableServerSession session; + + // set enabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(true)); + + set_session_modes(session, VS_COPY_SCHEDULED, VS_WRITE_ASYNC, false); + realtime_sync->active = false; + + EXPECT_CALL(session, get_freeze_next_tics()) + .WillRepeatedly(Return(100)); + + // Shouldn't do anything + EXPECT_CALL(session, copy_sim_data()) + .Times(1); + + EXPECT_CALL(session, write_data()) + .Times(0); + + // ACT + int result = session.copy_and_write_freeze_scheduled(100); + + // ASSERT + EXPECT_EQ(result, 0); +} + + +TEST_F(VariableServerSession_test, copy_and_write_freeze_scheduled_write_fails) { + // ARRANGE + MockVariableServerSession session; + + // set enabled + EXPECT_CALL(session, get_enabled()) + .WillOnce(Return(true)); + + set_session_modes(session, VS_COPY_SCHEDULED, VS_WRITE_WHEN_COPIED, false); + realtime_sync->active = true; + + EXPECT_CALL(session, get_freeze_next_tics()) + .WillRepeatedly(Return(100)); + + EXPECT_CALL(session, copy_sim_data()) + .Times(1); + + EXPECT_CALL(session, write_data()) + .WillOnce(Return(-1)); + + EXPECT_CALL(session, set_exit_cmd()) + .Times(1); + + // ACT + int result = session.copy_and_write_freeze_scheduled(100); + + // ASSERT + EXPECT_EQ(result, -1); +} \ No newline at end of file diff --git a/trick_source/sim_services/VariableServer/test/VariableServer_test.cc b/trick_source/sim_services/VariableServer/test/VariableServer_test.cc new file mode 100644 index 00000000..b3029c2c --- /dev/null +++ b/trick_source/sim_services/VariableServer/test/VariableServer_test.cc @@ -0,0 +1,112 @@ +/******************************TRICK HEADER************************************* +PURPOSE: ( Tests for the VariableServer class ) +*******************************************************************************/ + +#include +#include + +#include "MockVariableServerSession.hh" + +#include "trick/VariableServer.hh" + + +/* + Test Fixture. + */ +class VariableServer_test : public ::testing::Test { + protected: + Trick::VariableServer vs; + + VariableServer_test() { + + } + + ~VariableServer_test() { + } + + void SetUp() {} + void TearDown() {} +}; + +TEST_F(VariableServer_test, set_log_on) { + // ARRANGE + MockVariableServerSession session; + EXPECT_CALL(session, set_log_on()) + .Times(1); + + pthread_t id = (pthread_t) 5; + vs.add_session(id, &session); + + // ACT + vs.set_var_server_log_on(); + + // ASSERT + EXPECT_EQ(vs.get_log(), true); +} + +TEST_F(VariableServer_test, set_log_off) { + // ARRANGE + MockVariableServerSession session; + EXPECT_CALL(session, set_log_off()) + .Times(1); + + pthread_t id = (pthread_t) 5; + vs.add_session(id, &session); + + // ACT + vs.set_var_server_log_off(); + + // ASSERT + EXPECT_EQ(vs.get_log(), false); +} + +TEST_F(VariableServer_test, enabled_by_default) { + // ARRANGE + // ACT + // ASSERT + EXPECT_EQ(vs.get_enabled(), true); +} + +TEST_F(VariableServer_test, set_enabled) { + // ARRANGE + // ACT + vs.set_enabled(false); + + // ASSERT + EXPECT_EQ(vs.get_enabled(), false); +} + +TEST_F(VariableServer_test, info_msg_off_by_default) { + // ARRANGE + // ACT + // ASSERT + EXPECT_EQ(vs.get_info_msg(), false); +} + +TEST_F(VariableServer_test, set_info_msg) { + // ARRANGE + // ACT + vs.set_var_server_info_msg_on(); + + // ASSERT + EXPECT_EQ(vs.get_info_msg(), true); +} + +TEST_F(VariableServer_test, set_info_msg_off) { + // ARRANGE + // ACT + vs.set_var_server_info_msg_off(); + + // ASSERT + EXPECT_EQ(vs.get_info_msg(), false); +} + +TEST_F(VariableServer_test, info_dump) { + + MockVariableServerSession session; + pthread_t id = (pthread_t) 5; + vs.add_session(id, &session); + + std::cout << vs << std::endl; + +} \ No newline at end of file diff --git a/trick_source/sim_services/VariableServer/var_server_ext.cpp b/trick_source/sim_services/VariableServer/var_server_ext.cpp index a5d5d010..dd8fccba 100644 --- a/trick_source/sim_services/VariableServer/var_server_ext.cpp +++ b/trick_source/sim_services/VariableServer/var_server_ext.cpp @@ -11,9 +11,7 @@ extern Trick::VariableServer * the_vs ; -int command_debug = 0; - -Trick::VariableServerThread * get_vst() { +Trick::VariableServerSessionThread * get_vst() { return the_vs->get_vst(pthread_self()) ; } @@ -22,10 +20,6 @@ Trick::VariableServerSession * get_session() { } int var_add(std::string in_name) { - if (command_debug) { - std::cout << "var_add: " << in_name << std::endl; - } - Trick::VariableServerSession * session = get_session(); if (session != NULL ) { @@ -35,9 +29,6 @@ int var_add(std::string in_name) { } int var_add(std::string in_name, std::string in_units) { - if (command_debug) { - std::cout << "var_add: " << in_name << std::endl; - } Trick::VariableServerSession * session = get_session(); if (session != NULL ) { @@ -47,9 +38,6 @@ int var_add(std::string in_name, std::string in_units) { } int var_remove(std::string in_name) { - if (command_debug) { - std::cout << "var_remove: " << in_name << std::endl; - } Trick::VariableServerSession * session = get_session(); if (session != NULL ) { @@ -59,9 +47,6 @@ int var_remove(std::string in_name) { } int var_units(std::string var_name , std::string units_name) { - if (command_debug) { - std::cout << "var_units: " << var_name << std::endl; - } Trick::VariableServerSession * session = get_session(); if (session != NULL ) { @@ -71,9 +56,6 @@ int var_units(std::string var_name , std::string units_name) { } int var_exists(std::string in_name) { - if (command_debug) { - std::cout << "var_exists: " << in_name << std::endl; - } Trick::VariableServerSession * session = get_session(); if (session != NULL ) { @@ -83,9 +65,6 @@ int var_exists(std::string in_name) { } int var_send_once(std::string in_name) { - if (command_debug) { - std::cout << "var_send_once: " << in_name << std::endl; - } Trick::VariableServerSession * session = get_session(); if (session != NULL ) { @@ -95,9 +74,6 @@ int var_send_once(std::string in_name) { } int var_send_once(std::string in_name, int num) { - if (command_debug) { - std::cout << "var_send_once: " << in_name << std::endl; - } Trick::VariableServerSession * session = get_session(); if (session != NULL ) { @@ -107,10 +83,6 @@ int var_send_once(std::string in_name, int num) { } int var_send() { - if (command_debug) { - std::cout << "var_send: " << std::endl; - } - Trick::VariableServerSession * session = get_session(); if (session != NULL ) { @@ -120,9 +92,6 @@ int var_send() { } int var_clear() { - if (command_debug) { - std::cout << "var_clear: " << std::endl; - } Trick::VariableServerSession * session = get_session(); if (session != NULL ) { @@ -134,10 +103,6 @@ int var_clear() { } int var_cycle(double in_rate) { - if (command_debug) { - std::cout << "var_cycle: " << in_rate << std::endl; - } - Trick::VariableServerSession * session = get_session(); if (session != NULL ) { @@ -147,10 +112,6 @@ int var_cycle(double in_rate) { } int var_pause() { - if (command_debug) { - std::cout << "var_pause" << std::endl; - } - Trick::VariableServerSession * session = get_session(); if (session != NULL ) { @@ -161,9 +122,6 @@ int var_pause() { } int var_unpause() { - if (command_debug) { - std::cout << "var_unpause" << std::endl; - } Trick::VariableServerSession * session = get_session(); if (session != NULL ) { @@ -174,9 +132,6 @@ int var_unpause() { } int var_exit() { - if (command_debug) { - std::cout << "var_exit" << std::endl; - } Trick::VariableServerSession * session = get_session(); if (session != NULL ) { @@ -186,9 +141,6 @@ int var_exit() { } int var_validate_address(int on_off) { - if (command_debug) { - std::cout << "var_validate_address" << std::endl; - } Trick::VariableServerSession * session = get_session(); if (session != NULL ) { @@ -351,10 +303,7 @@ int var_write_stdio(int stream , std::string text ) { } int var_set_client_tag( std::string text ) { - if (command_debug) { - std::cout << "var_set_client_tag: " << text << std::endl; - } - Trick::VariableServerThread * vst = get_vst(); + Trick::VariableServerSessionThread * vst = get_vst(); if (vst != NULL) { vst->set_client_tag(text); @@ -372,9 +321,6 @@ int var_set_client_tag( std::string text ) { } int var_send_list_size() { - if (command_debug) { - std::cout << "var_send_list_size" << std::endl; - } Trick::VariableServerSession * session = get_session(); if (session != NULL ) { @@ -457,7 +403,6 @@ extern "C" void var_server_list_connections(void) { */ extern "C" const char * var_server_get_hostname(void) { const char * ret = (the_vs->get_hostname()) ; - printf("varserverext: %s", ret); return ret; } diff --git a/trick_source/trick_utils/connection_handlers/ClientConnection.cpp b/trick_source/trick_utils/connection_handlers/ClientConnection.cpp index f6945bc9..3df752b4 100644 --- a/trick_source/trick_utils/connection_handlers/ClientConnection.cpp +++ b/trick_source/trick_utils/connection_handlers/ClientConnection.cpp @@ -1,10 +1 @@ -#include "trick/ClientConnection.hh" - -std::string Trick::ClientConnection::getClientTag () { - return _client_tag; -} - -int Trick::ClientConnection::setClientTag (std::string tag) { - _client_tag = tag; - return 0; -} \ No newline at end of file +#include "trick/ClientConnection.hh" \ No newline at end of file diff --git a/trick_source/trick_utils/connection_handlers/MulticastGroup.cpp b/trick_source/trick_utils/connection_handlers/MulticastGroup.cpp new file mode 100644 index 00000000..35c9bc6d --- /dev/null +++ b/trick_source/trick_utils/connection_handlers/MulticastGroup.cpp @@ -0,0 +1,228 @@ +#include "trick/MulticastGroup.hh" +#include +#include +#include + + +Trick::MulticastGroup::MulticastGroup() : MulticastGroup(new SystemInterface) {} + +Trick::MulticastGroup::MulticastGroup (SystemInterface * system_interface) : _initialized(false), _system_interface(system_interface) {} + +Trick::MulticastGroup::~MulticastGroup() {} + +int Trick::MulticastGroup::restart () { + // Keep address list the same, but we may need to get a new socket + _system_interface = new SystemInterface(); + // return initialize(); + // return 0; +} + + +int Trick::MulticastGroup::broadcast (std::string message) { + if (!isInitialized()) { + return -1; + } + + const char * message_send = message.c_str(); + for (struct sockaddr_in& address : _addresses) { + int status = _system_interface->sendto(_socket , message_send , strlen(message_send) , 0 , (struct sockaddr *)&address , (socklen_t)sizeof(address)) ; + if (status == -1) { + char * readable_addr = inet_ntoa( address.sin_addr ); + std::string err_msg = "MulticastGroup: Failed to broadcast to address " + std::string(readable_addr); + perror(err_msg.c_str()); + } + } + return 0; +} + +int Trick::MulticastGroup::addAddress (std::string addr, int port) { + auto in_addr = _system_interface->inet_addr(addr.c_str()); + if (in_addr == -1) { + std::string error_msg = "MulticastGroup: Cannot add address " + addr + " to multicast group"; + perror(error_msg.c_str()); + return -1; + } + + struct sockaddr_in mcast_addr; + memset(&mcast_addr, 0, sizeof(mcast_addr)); + mcast_addr.sin_family = AF_INET; + mcast_addr.sin_addr.s_addr = in_addr; + mcast_addr.sin_port = htons((uint16_t) port);; + _addresses.emplace_back(mcast_addr); + return 0; +} + +bool Trick::MulticastGroup::isInitialized () { + return _initialized; +} + +int Trick::MulticastGroup::initialize() { + _socket = _system_interface->socket(AF_INET, SOCK_DGRAM, 0); + if (_socket < 0) { + perror("MulticastGroup: socket"); + return -1; + } + + int option_value = 1; + if (_system_interface->setsockopt(_socket, SOL_SOCKET, SO_REUSEADDR, (char *) &option_value, (socklen_t) sizeof(option_value)) < 0) { + perror("MulticastGroup: setsockopt: reuseaddr"); + return -1; + } +#ifdef SO_REUSEPORT + if (_system_interface->setsockopt(_socket, SOL_SOCKET, SO_REUSEPORT, (char *) &option_value, sizeof(option_value)) < 0) { + perror("MulticastGroup: setsockopt: reuseport"); + return -1; + } +#endif + _initialized = true; + return 0; +} + +int Trick::MulticastGroup::initialize_with_receiving(std::string addr, std::string mcast_addr, int port) { + UDPConnection::initialize(addr, port); + + struct ip_mreq mreq; + mreq.imr_multiaddr.s_addr = inet_addr(mcast_addr.c_str()); + mreq.imr_interface.s_addr = htonl(INADDR_ANY); + int ret = setsockopt(_socket, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) ; + if ( ret < 0 ) { + perror("ip_add_membership") ; + return ret ; + } + + // Save our info so that we know to ignore messages from ourself + memset(&_remote_serv_addr, 0, sizeof(struct sockaddr_in)); + _remote_serv_addr.sin_family = AF_INET; + _remote_serv_addr.sin_addr.s_addr = inet_addr(mcast_addr.c_str()); + _remote_serv_addr.sin_port = htons(port); + + int s_in_size = sizeof(_self_info) ; + getsockname( _socket , (struct sockaddr *)&_self_info, (socklen_t *)&s_in_size) ; + if ( _self_info.sin_addr.s_addr == 0 ) { + char hname[80]; + struct hostent *ip_host = NULL; + gethostname(hname, (size_t) 80); + ip_host = gethostbyname(hname); + if (ip_host != NULL) { + memcpy(&(_self_info.sin_addr.s_addr), ip_host->h_addr, (size_t) ip_host->h_length); + } else { + _self_info.sin_addr.s_addr = inet_addr(addr.c_str()); + _self_info.sin_port = htons(port); + } + } + + setBlockMode(false); + + + return 0; +} + +int Trick::MulticastGroup::disconnect() { + if (_initialized) { + _system_interface->close(_socket); + } + return 0; +} + + +int Trick::MulticastGroup::write (char * message, int size) { + if (!_started) + return -1; + + socklen_t sock_size = sizeof(_remote_serv_addr); + + return _system_interface->sendto(_socket, message, size, 0, (struct sockaddr *) &_remote_serv_addr, sock_size ); +} + +int Trick::MulticastGroup::write (const std::string& message) { + if (!_started) + return -1; + + char send_buf[message.length()+1]; + strcpy (send_buf, message.c_str()); + socklen_t sock_size = sizeof(_remote_serv_addr); + + return _system_interface->sendto(_socket, send_buf, message.length(), 0, (struct sockaddr *) &_remote_serv_addr, sock_size ); +} + + +int Trick::MulticastGroup::read (std::string& message, int max_len) { + if (!_started) + return 0; + + struct sockaddr_in s_in ; + socklen_t sock_size = sizeof(s_in) ; + + char incoming_msg[max_len]; + int nbytes = _system_interface->recvfrom( _socket, incoming_msg, MAX_CMD_LEN, MSG_PEEK,(struct sockaddr *)&s_in, &sock_size ) ; + + if (nbytes == 0 ) { + return 0; + } + + if (nbytes == -1) { + if (errno == EAGAIN || errno == EWOULDBLOCK) { + // There's nothing ready in the socket, just return an empty string + message = ""; + return 0; + } + + // Otherwise, some other system error has occurred. Return an error. + return -1; + } + + // Make sure that this message isn't from ourself + if ( s_in.sin_addr.s_addr == _self_info.sin_addr.s_addr && s_in.sin_port == _self_info.sin_port) { + // Clear out the socket + _system_interface->recvfrom( _socket, incoming_msg, MAX_CMD_LEN, 0 , 0, 0) ; + return 0; + } + + /* find the last newline that is present on the socket */ + incoming_msg[nbytes] = '\0' ; + char *last_newline = rindex( incoming_msg , '\n') ; + + /* if there is a newline then there is a complete command on the socket */ + if ( last_newline != NULL ) { + socklen_t sock_size = sizeof(_remote_serv_addr); + /* only remove up to (and including) the last newline on the socket */ + int size = last_newline - incoming_msg + 1; + nbytes = _system_interface->recvfrom( _socket, incoming_msg, size, 0 , 0, 0) ; + } else { + nbytes = 0 ; + } + + std::stringstream msg_stream; + + if ( nbytes > 0 ) { + // Strip out \r characers + + int msg_len = nbytes ; + incoming_msg[msg_len] = '\0' ; + + for( int ii = 0 , jj = 0 ; ii <= msg_len ; ii++ ) { + if ( incoming_msg[ii] != '\r' ) { + msg_stream << incoming_msg[ii] ; + } + } + } + + message = msg_stream.str(); + return message.size(); +} + +std::string Trick::MulticastGroup::getClientHostname() { + if (!_started) { + return ""; + } + + return inet_ntoa(_remote_serv_addr.sin_addr); +} + +int Trick::MulticastGroup::getClientPort() { + if (!_started) { + return 0; + } + + return ntohs(_remote_serv_addr.sin_port); +} \ No newline at end of file diff --git a/trick_source/trick_utils/connection_handlers/MulticastManager.cpp b/trick_source/trick_utils/connection_handlers/MulticastManager.cpp deleted file mode 100644 index f5b82a31..00000000 --- a/trick_source/trick_utils/connection_handlers/MulticastManager.cpp +++ /dev/null @@ -1,65 +0,0 @@ -#include "trick/MulticastManager.hh" -#include -#include - -Trick::MulticastManager::MulticastManager() { -} - -Trick::MulticastManager::~MulticastManager() { - -} - -int Trick::MulticastManager::restart () { - // Keep address list the same, but we may need to get a new socket - return initialize(); -} - - -int Trick::MulticastManager::broadcast (std::string message) { - if (!is_initialized()) { - initialize(); - } - const char * message_send = message.c_str(); - for (struct sockaddr_in& address : addresses) { - sendto(mcast_socket , message_send , strlen(message_send) , 0 , (struct sockaddr *)&address , (socklen_t)sizeof(address)) ; - } - return 0; -} - -int Trick::MulticastManager::addAddress (std::string addr, int port) { - - struct sockaddr_in mcast_addr; - memset(&mcast_addr, 0, sizeof(mcast_addr)); - mcast_addr.sin_family = AF_INET; - mcast_addr.sin_addr.s_addr = inet_addr(addr.c_str()); - mcast_addr.sin_port = htons((uint16_t) port); - addresses.emplace_back(mcast_addr); - return 0; -} - -int Trick::MulticastManager::is_initialized () { - return initialized; -} - -int Trick::MulticastManager::initialize() { - - mcast_socket = socket(AF_INET, SOCK_DGRAM, 0); - if (mcast_socket < 0) { - perror("vs_mcast_init socket"); - return -1; - } - - int option_value = 1; - if (setsockopt(mcast_socket, SOL_SOCKET, SO_REUSEADDR, (char *) &option_value, (socklen_t) sizeof(option_value)) < 0) { - perror("setsockopt: reuseaddr"); - return -1; - } -#ifdef SO_REUSEPORT - if (setsockopt(mcast_socket, SOL_SOCKET, SO_REUSEPORT, (char *) &option_value, sizeof(option_value)) < 0) { - perror("setsockopt: reuseport"); - return -1; - } -#endif - initialized = 1; - return 0; -} \ No newline at end of file diff --git a/trick_source/trick_utils/connection_handlers/ClientListener.cpp b/trick_source/trick_utils/connection_handlers/TCPClientListener.cpp similarity index 70% rename from trick_source/trick_utils/connection_handlers/ClientListener.cpp rename to trick_source/trick_utils/connection_handlers/TCPClientListener.cpp index 9d7cfc96..12b5e158 100644 --- a/trick_source/trick_utils/connection_handlers/ClientListener.cpp +++ b/trick_source/trick_utils/connection_handlers/TCPClientListener.cpp @@ -9,12 +9,12 @@ #include #include -#include "trick/ClientListener.hh" +#include "trick/TCPClientListener.hh" -Trick::ClientListener::ClientListener () : ClientListener (new SystemInterface()) {} -Trick::ClientListener::ClientListener (SystemInterface * system_interface) : _listen_socket(-1), _hostname(""), _port(0), _client_tag(""), _initialized(false), _system_interface(system_interface) {} +Trick::TCPClientListener::TCPClientListener () : TCPClientListener (new SystemInterface()) {} +Trick::TCPClientListener::TCPClientListener (SystemInterface * system_interface) : _listen_socket(-1), _hostname(""), _port(0), _client_tag(""), _initialized(false), _system_interface(system_interface) {} -Trick::ClientListener::~ClientListener () { +Trick::TCPClientListener::~TCPClientListener () { // Clean up our socket if initialized if (_initialized) { close (_listen_socket); @@ -23,7 +23,7 @@ Trick::ClientListener::~ClientListener () { delete _system_interface; } -int Trick::ClientListener::initialize(std::string in_hostname, int in_port) { +int Trick::TCPClientListener::initialize(std::string in_hostname, int in_port) { if ((_listen_socket = _system_interface->socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror ("Server: Unable to open socket"); @@ -114,12 +114,12 @@ int Trick::ClientListener::initialize(std::string in_hostname, int in_port) { return 0; } -int Trick::ClientListener::initialize() { +int Trick::TCPClientListener::initialize() { return initialize("", 0); } -int Trick::ClientListener::setBlockMode(bool blocking) { +int Trick::TCPClientListener::setBlockMode(bool blocking) { if (!_initialized) return -1; @@ -147,7 +147,7 @@ int Trick::ClientListener::setBlockMode(bool blocking) { } -bool Trick::ClientListener::checkForNewConnections() { +bool Trick::TCPClientListener::checkForNewConnections() { if (!_initialized) return false; @@ -170,7 +170,7 @@ bool Trick::ClientListener::checkForNewConnections() { -std::string Trick::ClientListener::getHostname () { +std::string Trick::TCPClientListener::getHostname () { if (!_initialized) return ""; @@ -178,7 +178,7 @@ std::string Trick::ClientListener::getHostname () { } -int Trick::ClientListener::getPort() { +int Trick::TCPClientListener::getPort() { if (!_initialized) return -1; @@ -186,7 +186,7 @@ int Trick::ClientListener::getPort() { } -int Trick::ClientListener::disconnect() { +int Trick::TCPClientListener::disconnect() { if (!_initialized) { return -1; } @@ -198,23 +198,48 @@ int Trick::ClientListener::disconnect() { return 0; } -bool Trick::ClientListener::validateSourceAddress(std::string requested_source_address) { - struct addrinfo hints, *res; - memset(&hints, 0, sizeof hints); - hints.ai_family = AF_INET; - hints.ai_socktype = SOCK_STREAM; - hints.ai_protocol = 0; +bool Trick::TCPClientListener::validateSourceAddress(std::string in_hostname) { + // struct addrinfo hints, *res; + // memset(&hints, 0, sizeof hints); + // hints.ai_family = AF_INET; + // hints.ai_socktype = SOCK_STREAM; + // hints.ai_protocol = 0; - int err; - if ((err = _system_interface->getaddrinfo(requested_source_address.c_str(), 0, &hints, &res)) != 0) { - std::cerr << "Unable to lookup address: " << gai_strerror(err) << std::endl; + // int err; + // if ((err = _system_interface->getaddrinfo(requested_source_address.c_str(), 0, &hints, &res)) != 0) { + // std::cerr << "Unable to lookup address: " << gai_strerror(err) << std::endl; + // return false; + // } + + // Look up the hostname + char name[80]; + gethostname(name, (size_t) 80); + + struct hostent *ip_host ; + sockaddr_in s_in; + socklen_t s_in_size = sizeof(s_in) ; + + s_in.sin_family = AF_INET; + + if (in_hostname == "" || in_hostname == "localhost" || strcmp(in_hostname.c_str(),name) == 0) { + s_in.sin_addr.s_addr = INADDR_ANY; + _hostname = std::string(name); + } else if ( inet_pton(AF_INET, in_hostname.c_str(), (struct in_addr *)&s_in.sin_addr.s_addr) == 1 ) { + /* numeric character string address */ + _hostname = in_hostname; + } else if ( (ip_host = gethostbyname(in_hostname.c_str())) != NULL ) { + /* some name other than the default name was given */ + memcpy((void *) &(s_in.sin_addr.s_addr), (const void *) ip_host->h_addr, (size_t) ip_host->h_length); + _hostname = in_hostname; + } else { + perror("Server: Could not determine source address"); return false; } return true; } -int Trick::ClientListener::checkSocket() { +int Trick::TCPClientListener::checkSocket() { if (!_initialized) return -1; @@ -226,11 +251,11 @@ int Trick::ClientListener::checkSocket() { return 0; } -bool Trick::ClientListener::isInitialized() { +bool Trick::TCPClientListener::isInitialized() { return _initialized; } -Trick::TCPConnection * Trick::ClientListener::setUpNewConnection () { +Trick::TCPConnection * Trick::TCPClientListener::setUpNewConnection () { if (!_initialized) return NULL; @@ -238,8 +263,9 @@ Trick::TCPConnection * Trick::ClientListener::setUpNewConnection () { return connection; } -int Trick::ClientListener::restart () { +int Trick::TCPClientListener::restart () { _system_interface = new SystemInterface(); + return 0; } diff --git a/trick_source/trick_utils/connection_handlers/TCPConnection.cpp b/trick_source/trick_utils/connection_handlers/TCPConnection.cpp index 604c9976..a72ef7f4 100644 --- a/trick_source/trick_utils/connection_handlers/TCPConnection.cpp +++ b/trick_source/trick_utils/connection_handlers/TCPConnection.cpp @@ -5,6 +5,8 @@ #include #include +Trick::TCPConnection::TCPConnection () : TCPConnection(0, new SystemInterface()) {} + Trick::TCPConnection::TCPConnection (SystemInterface * system_interface) : TCPConnection(0, system_interface) {} Trick::TCPConnection::TCPConnection (int listen_socket) : TCPConnection(listen_socket, new SystemInterface()) {} @@ -53,10 +55,11 @@ int Trick::TCPConnection::write (const std::string& message) { return _system_interface->send(_socket, send_buf, message.length(), 0); } -std::string Trick::TCPConnection::read (int max_len) { +int Trick::TCPConnection::read (std::string& message, int max_len) { if (!_connected) { std::cerr << "Trying to read from a socket that is not connected" << std::endl; - return ""; + message = ""; + return 0; } char incoming_msg[max_len]; @@ -65,16 +68,19 @@ std::string Trick::TCPConnection::read (int max_len) { int nbytes = _system_interface->recv(_socket, incoming_msg, max_receive_length, MSG_PEEK); if (nbytes == 0 ) { - return std::string(""); + message = ""; + return 0; } if (nbytes == -1) { if (errno == EAGAIN) { - return std::string(""); + message = ""; + return 0; } else { std::string error_msg = "Error while reading from socket " + std::to_string(_socket); perror(error_msg.c_str()); - return std::string(""); + message = ""; + return -1; } } @@ -105,7 +111,8 @@ std::string Trick::TCPConnection::read (int max_len) { } } - return msg_stream.str(); + message = msg_stream.str(); + return message.size(); } @@ -126,7 +133,7 @@ int Trick::TCPConnection::setBlockMode(bool blocking) { int flag = _system_interface->fcntl(_socket, F_GETFL, 0); if (flag == -1) { - std::string error_message = "Unable to get flags for fd " + std::to_string(_socket) + " block mode to " + std::to_string(blocking); + std::string error_message = "Unable to get flags for fd " + std::to_string(_socket); perror (error_message.c_str()); return -1; } @@ -153,4 +160,42 @@ bool Trick::TCPConnection::isInitialized() { int Trick::TCPConnection::restart() { _system_interface = new SystemInterface(); + return 0; +} + +std::string Trick::TCPConnection::getClientTag () { + return _client_tag; +} + +int Trick::TCPConnection::setClientTag (std::string tag) { + _client_tag = tag; + return 0; +} + +std::string Trick::TCPConnection::getClientHostname() { + if (!_connected) { + return ""; + } + + struct sockaddr_in otherside; + socklen_t len = (socklen_t)sizeof(otherside); + + if (getpeername(_socket, (struct sockaddr*)&otherside, &len) != 0) + return ""; + + return inet_ntoa(otherside.sin_addr); +} + +int Trick::TCPConnection::getClientPort() { + if (!_connected) { + return 0; + } + + struct sockaddr_in otherside; + socklen_t len = (socklen_t)sizeof(otherside); + + if (getpeername(_socket, (struct sockaddr*)&otherside, &len) != 0) + return 0; + + return ntohs(otherside.sin_port); } \ No newline at end of file diff --git a/trick_source/trick_utils/connection_handlers/UDPConnection.cpp b/trick_source/trick_utils/connection_handlers/UDPConnection.cpp index d8be0276..e9076977 100644 --- a/trick_source/trick_utils/connection_handlers/UDPConnection.cpp +++ b/trick_source/trick_utils/connection_handlers/UDPConnection.cpp @@ -117,9 +117,9 @@ int Trick::UDPConnection::write (const std::string& message) { return _system_interface->sendto(_socket, send_buf, message.length(), 0, (struct sockaddr *) &_remote_serv_addr, sock_size ); } -std::string Trick::UDPConnection::read (int max_len) { +int Trick::UDPConnection::read (std::string& message, int max_len) { if (!_started) - return ""; + return 0; char incoming_msg[max_len]; int nbytes = _system_interface->recvfrom( _socket, incoming_msg, MAX_CMD_LEN, MSG_PEEK, NULL, NULL ) ; @@ -127,26 +127,36 @@ std::string Trick::UDPConnection::read (int max_len) { return 0; } - if (nbytes != -1) { // -1 means socket is nonblocking and no data to read - /* find the last newline that is present on the socket */ - incoming_msg[nbytes] = '\0' ; - char *last_newline = rindex( incoming_msg , '\n') ; - - /* if there is a newline then there is a complete command on the socket */ - if ( last_newline != NULL ) { - socklen_t sock_size = sizeof(_remote_serv_addr); - /* Save the remote host information so we know where to send replies */ - /* only remove up to (and including) the last newline on the socket */ - int size = last_newline - incoming_msg + 1; - nbytes = _system_interface->recvfrom( _socket, incoming_msg, size, 0 , (struct sockaddr *) &_remote_serv_addr, &sock_size ) ; - } else { - nbytes = 0 ; + if (nbytes == -1) { + if (errno == EAGAIN || errno == EWOULDBLOCK) { + // There's nothing ready in the socket, just return an empty string + message = ""; + return 0; } + + // Otherwise, some other system error has occurred. Return an error. + return -1; } + /* find the last newline that is present on the socket */ + incoming_msg[nbytes] = '\0' ; + char *last_newline = rindex( incoming_msg , '\n') ; + + /* if there is a newline then there is a complete command on the socket */ + if ( last_newline != NULL ) { + socklen_t sock_size = sizeof(_remote_serv_addr); + /* Save the remote host information so we know where to send replies */ + /* only remove up to (and including) the last newline on the socket */ + int size = last_newline - incoming_msg + 1; + nbytes = _system_interface->recvfrom( _socket, incoming_msg, size, 0 , (struct sockaddr *) &_remote_serv_addr, &sock_size ) ; + } else { + nbytes = 0 ; + } + std::stringstream msg_stream; if ( nbytes > 0 ) { + // Strip out \r characers int msg_len = nbytes ; incoming_msg[msg_len] = '\0' ; @@ -158,7 +168,8 @@ std::string Trick::UDPConnection::read (int max_len) { } } - return msg_stream.str(); + message = msg_stream.str(); + return message.size(); } int Trick::UDPConnection::disconnect () { @@ -178,7 +189,7 @@ int Trick::UDPConnection::setBlockMode(bool blocking) { int flag = _system_interface->fcntl(_socket, F_GETFL, 0); if (flag == -1) { - std::string error_message = "Unable to get flags for fd " + std::to_string(_socket) + " block mode to " + std::to_string(blocking); + std::string error_message = "Unable to get flags for fd " + std::to_string(_socket); perror (error_message.c_str()); return -1; } @@ -200,6 +211,7 @@ int Trick::UDPConnection::setBlockMode(bool blocking) { int Trick::UDPConnection::restart() { _system_interface = new SystemInterface(); + return 0; } bool Trick::UDPConnection::isInitialized() { @@ -212,4 +224,29 @@ int Trick::UDPConnection::getPort() { std::string Trick::UDPConnection::getHostname() { return _hostname; +} + +std::string Trick::UDPConnection::getClientTag () { + return _client_tag; +} + +int Trick::UDPConnection::setClientTag (std::string tag) { + _client_tag = tag; + return 0; +} + +std::string Trick::UDPConnection::getClientHostname() { + if (!_initialized) { + return ""; + } + + return inet_ntoa(_remote_serv_addr.sin_addr); +} + +int Trick::UDPConnection::getClientPort() { + if (!_initialized) { + return 0; + } + + return ntohs(_remote_serv_addr.sin_port); } \ No newline at end of file diff --git a/trick_source/trick_utils/connection_handlers/test/MulticastGroup_test.cpp b/trick_source/trick_utils/connection_handlers/test/MulticastGroup_test.cpp new file mode 100644 index 00000000..a664f9c9 --- /dev/null +++ b/trick_source/trick_utils/connection_handlers/test/MulticastGroup_test.cpp @@ -0,0 +1,136 @@ +#include + +#include "trick/MulticastGroup.hh" +#include "SystemInterfaceMock/SystemInterfaceMock.hh" + +class MulticastGroupTest : public testing::Test { + + protected: + MulticastGroupTest() : system_context(new SystemInferfaceMock()), mcast (system_context) {} + ~MulticastGroupTest(){ + mcast.disconnect(); + } + + SystemInferfaceMock * system_context; + Trick::MulticastGroup mcast; +}; + +TEST_F(MulticastGroupTest, not_initialized) { + // ARRANGE + // ACT + // ASSERT + EXPECT_EQ(mcast.isInitialized(), 0); +} + +TEST_F(MulticastGroupTest, initialize) { + // ARRANGE + // ACT + mcast.initialize(); + // ASSERT + EXPECT_EQ(mcast.isInitialized(), 1); +} + +TEST_F(MulticastGroupTest, initialize_socket_fails) { + // ARRANGE + system_context->register_socket_impl([](int a, int b, int c) { + errno = EPERM; + return -1; + }); + + // ACT + mcast.initialize(); + // ASSERT + EXPECT_EQ(mcast.isInitialized(), 0); +} + +TEST_F(MulticastGroupTest, initialize_sockopt_reuseaddr_fails) { + // ARRANGE + system_context->register_setsockopt_impl([](int sockfd, int level, int optname, const void *optval, socklen_t optlen) { + if (optname == SO_REUSEADDR) { + errno = EINVAL; + return -1; + } + return 0; + }); + + // ACT + mcast.initialize(); + // ASSERT + EXPECT_EQ(mcast.isInitialized(), 0); +} + +TEST_F(MulticastGroupTest, initialize_sockopt_reuseport_fails) { + // ARRANGE + system_context->register_setsockopt_impl([](int sockfd, int level, int optname, const void *optval, socklen_t optlen) { + if (optname == SO_REUSEPORT) { + errno = EINVAL; + return -1; + } + return 0; + }); + + // ACT + mcast.initialize(); + // ASSERT + EXPECT_EQ(mcast.isInitialized(), 0); +} + +TEST_F(MulticastGroupTest, add_address) { + // ARRANGE + // ACT + int result = mcast.addAddress("239.3.14.15", 9265); + + // ASSERT + EXPECT_EQ(result, 0); +} + +TEST_F(MulticastGroupTest, add_address_fails) { + // ARRANGE + system_context->register_inet_addr_impl([](const char * addr) { + return -1; + }); + + // ACT + int result = mcast.addAddress("239.3.14.15", 9265); + + // ASSERT + EXPECT_EQ(result, -1); +} + +TEST_F(MulticastGroupTest, broadcast_uninitialized) { + // ARRANGE + // ACT + int result = mcast.broadcast("Some message"); + + // ASSERT + EXPECT_EQ(result, -1); +} + +TEST_F(MulticastGroupTest, broadcast) { + // ARRANGE + mcast.initialize(); + mcast.addAddress("239.3.14.15", 9265); + + // ACT + int result = mcast.broadcast("Some message"); + + // ASSERT + EXPECT_EQ(result, 0); +} + + +TEST_F(MulticastGroupTest, broadcast_send_fails) { + // ARRANGE + system_context->register_sendto_impl([](int socket, const void * buffer, size_t length, int flags, const struct sockaddr * dest_addr, socklen_t dest_len) { + return -1; + }); + + mcast.initialize(); + mcast.addAddress("239.3.14.15", 9265); + + // ACT + int result = mcast.broadcast("Some message"); + + // ASSERT + EXPECT_EQ(result, 0); +} \ No newline at end of file diff --git a/trick_source/trick_utils/connection_handlers/test/SystemInterfaceMock/SystemInterfaceMock.hh b/trick_source/trick_utils/connection_handlers/test/SystemInterfaceMock/SystemInterfaceMock.hh index 2463582c..5d3b8f6f 100644 --- a/trick_source/trick_utils/connection_handlers/test/SystemInterfaceMock/SystemInterfaceMock.hh +++ b/trick_source/trick_utils/connection_handlers/test/SystemInterfaceMock/SystemInterfaceMock.hh @@ -37,6 +37,8 @@ typedef std::function recv_func_type; typedef std::function recvfrom_func_type; +typedef std::function inet_addr_func_type; + class SystemInferfaceMock : public SystemInterface { public: @@ -57,6 +59,7 @@ class SystemInferfaceMock : public SystemInterface { real_sendto_impl(); real_recv_impl(); real_recvfrom_impl(); + real_inet_addr_impl(); } void set_all_real () { @@ -76,6 +79,7 @@ class SystemInferfaceMock : public SystemInterface { real_sendto_impl(); real_recv_impl(); real_recvfrom_impl(); + real_inet_addr_impl(); } void set_all_noop() { @@ -95,6 +99,7 @@ class SystemInferfaceMock : public SystemInterface { noop_sendto_impl(); noop_recv_impl(); noop_recvfrom_impl(); + noop_inet_addr_impl(); } @@ -118,7 +123,7 @@ class SystemInferfaceMock : public SystemInterface { // bind implementation public: - virtual int bind (int socket, struct sockaddr * address, socklen_t address_len) override { return bind_impl( socket, address, address_len); } + virtual int bind (int socket, const struct sockaddr * address, socklen_t address_len) override { return bind_impl( socket, address, address_len); } void register_bind_impl (bind_func_type impl) { bind_impl = impl; } void real_bind_impl () { bind_impl = [](int socket, const struct sockaddr * address, socklen_t address_len) -> int { return ::bind( socket, address, address_len); }; } void noop_bind_impl () { bind_impl = [](int socket, const struct sockaddr * address, socklen_t address_len) -> int { return 0; }; } @@ -233,6 +238,15 @@ class SystemInferfaceMock : public SystemInterface { private: recvfrom_func_type recvfrom_impl; + // inet_addr implementation + public: + virtual in_addr_t inet_addr (const char * cp) override { return inet_addr_impl( cp); } + void register_inet_addr_impl (inet_addr_func_type impl) { inet_addr_impl = impl; } + void real_inet_addr_impl () { inet_addr_impl = [](const char * cp) -> in_addr_t { return ::inet_addr( cp); }; } + void noop_inet_addr_impl () { inet_addr_impl = [](const char * cp) -> in_addr_t { return 0; }; } + private: + inet_addr_func_type inet_addr_impl; + }; #endif diff --git a/trick_source/trick_utils/connection_handlers/test/ClientListener_test.cpp b/trick_source/trick_utils/connection_handlers/test/TCPClientListener_test.cpp similarity index 79% rename from trick_source/trick_utils/connection_handlers/test/ClientListener_test.cpp rename to trick_source/trick_utils/connection_handlers/test/TCPClientListener_test.cpp index 02f9f125..0a4bf7a4 100644 --- a/trick_source/trick_utils/connection_handlers/test/ClientListener_test.cpp +++ b/trick_source/trick_utils/connection_handlers/test/TCPClientListener_test.cpp @@ -12,26 +12,26 @@ #include #include -#include "trick/ClientListener.hh" +#include "trick/TCPClientListener.hh" #include "SystemInterfaceMock/SystemInterfaceMock.hh" -class ClientListenerTest : public testing::Test { +class TCPClientListenerTest : public testing::Test { protected: - ClientListenerTest() : system_context(new SystemInferfaceMock()), listener (system_context) {} - ~ClientListenerTest(){} + TCPClientListenerTest() : system_context(new SystemInferfaceMock()), listener (system_context) {} + ~TCPClientListenerTest(){} SystemInferfaceMock * system_context; - Trick::ClientListener listener; + Trick::TCPClientListener listener; }; -TEST_F( ClientListenerTest, initialized ) { +TEST_F( TCPClientListenerTest, initialized ) { EXPECT_EQ(listener.isInitialized(), false); } -TEST_F( ClientListenerTest, initialize_localhost_0 ) { +TEST_F( TCPClientListenerTest, initialize_localhost_0 ) { // ARRANGE // Look up the hostname char name[80]; @@ -45,7 +45,7 @@ TEST_F( ClientListenerTest, initialize_localhost_0 ) { EXPECT_EQ(listener.getHostname(), std::string(name)); } -TEST_F( ClientListenerTest, initialize_localhost_54321 ) { +TEST_F( TCPClientListenerTest, initialize_localhost_54321 ) { // ARRANGE // ACT listener.initialize("localhost", 54321); @@ -55,7 +55,7 @@ TEST_F( ClientListenerTest, initialize_localhost_54321 ) { EXPECT_EQ(listener.getPort(), 54321); } -TEST_F( ClientListenerTest, initialize_no_args ) { +TEST_F( TCPClientListenerTest, initialize_no_args ) { // ARRANGE // ACT listener.initialize(); @@ -65,7 +65,7 @@ TEST_F( ClientListenerTest, initialize_no_args ) { EXPECT_GT(listener.getPort(), 1000); } -TEST_F( ClientListenerTest, initialize_localhost_numerical_54321 ) { +TEST_F( TCPClientListenerTest, initialize_localhost_numerical_54321 ) { // ARRANGE // ACT listener.initialize("127.0.0.1", 54321); @@ -75,7 +75,7 @@ TEST_F( ClientListenerTest, initialize_localhost_numerical_54321 ) { EXPECT_EQ(listener.getPort(), 54321); } -TEST_F( ClientListenerTest, initialize_invalid_hostname ) { +TEST_F( TCPClientListenerTest, initialize_invalid_hostname ) { // ARRANGE // ACT listener.initialize("some_invalid_hostname", 0); @@ -84,7 +84,7 @@ TEST_F( ClientListenerTest, initialize_invalid_hostname ) { EXPECT_EQ(listener.isInitialized(), false); } -TEST_F( ClientListenerTest, failed_socket ) { +TEST_F( TCPClientListenerTest, failed_socket ) { // ARRANGE system_context->register_socket_impl([](int a, int b, int c) { errno = EPERM; @@ -98,7 +98,7 @@ TEST_F( ClientListenerTest, failed_socket ) { EXPECT_EQ(listener.isInitialized(), false); } -TEST_F( ClientListenerTest, failed_setsockopt_reuseaddr ) { +TEST_F( TCPClientListenerTest, failed_setsockopt_reuseaddr ) { // ARRANGE system_context->register_setsockopt_impl([](int sockfd, int level, int optname, const void *optval, socklen_t optlen) { errno = EINVAL; @@ -112,7 +112,7 @@ TEST_F( ClientListenerTest, failed_setsockopt_reuseaddr ) { EXPECT_EQ(listener.isInitialized(), false); } -TEST_F( ClientListenerTest, failed_setsockopt_buffering ) { +TEST_F( TCPClientListenerTest, failed_setsockopt_buffering ) { // ARRANGE system_context->register_setsockopt_impl([](int sockfd, int level, int optname, const void *optval, socklen_t optlen) { if (level == IPPROTO_TCP && optname == TCP_NODELAY) { @@ -130,7 +130,7 @@ TEST_F( ClientListenerTest, failed_setsockopt_buffering ) { EXPECT_EQ(listener.isInitialized(), false); } -TEST_F( ClientListenerTest, failed_bind ) { +TEST_F( TCPClientListenerTest, failed_bind ) { // ARRANGE system_context->register_bind_impl([](int sockfd, const struct sockaddr *addr,socklen_t addrlen) { errno = EADDRINUSE; @@ -144,7 +144,7 @@ TEST_F( ClientListenerTest, failed_bind ) { EXPECT_EQ(listener.isInitialized(), false); } -TEST_F( ClientListenerTest, failed_sockname ) { +TEST_F( TCPClientListenerTest, failed_sockname ) { // ARRANGE system_context->register_getsockname_impl([](int sockfd, struct sockaddr *addr, socklen_t *addrlen) { ((struct sockaddr_in *) addr)->sin_port = htons(1234); @@ -158,7 +158,7 @@ TEST_F( ClientListenerTest, failed_sockname ) { EXPECT_EQ(listener.isInitialized(), false); } -TEST_F( ClientListenerTest, failed_listen ) { +TEST_F( TCPClientListenerTest, failed_listen ) { // ARRANGE system_context->register_listen_impl([](int sockfd, int backlog) { errno = EADDRINUSE; @@ -172,7 +172,7 @@ TEST_F( ClientListenerTest, failed_listen ) { EXPECT_EQ(listener.isInitialized(), false); } -TEST_F( ClientListenerTest, checkForNewConnections_uninitialized ) { +TEST_F( TCPClientListenerTest, checkForNewConnections_uninitialized ) { // ARRANGE // ACT bool result = listener.checkForNewConnections(); @@ -181,7 +181,7 @@ TEST_F( ClientListenerTest, checkForNewConnections_uninitialized ) { EXPECT_EQ(result, false); } -TEST_F( ClientListenerTest, checkForNewConnections ) { +TEST_F( TCPClientListenerTest, checkForNewConnections ) { // ARRANGE int socket_fd; system_context->register_socket_impl([&socket_fd](int a, int b, int c) { @@ -201,7 +201,7 @@ TEST_F( ClientListenerTest, checkForNewConnections ) { EXPECT_EQ(result, true); } -TEST_F( ClientListenerTest, checkForNewConnections_select_error ) { +TEST_F( TCPClientListenerTest, checkForNewConnections_select_error ) { // ARRANGE system_context->register_select_impl([](int nfds, fd_set *readfds, fd_set *writefds,fd_set *exceptfds, struct timeval *timeout) { return -1; @@ -215,7 +215,7 @@ TEST_F( ClientListenerTest, checkForNewConnections_select_error ) { EXPECT_EQ(result, false); } -TEST_F( ClientListenerTest, setBlockMode_false ) { +TEST_F( TCPClientListenerTest, setBlockMode_false ) { // ARRANGE int socket_fd; system_context->register_socket_impl([&socket_fd](int a, int b, int c) { @@ -234,7 +234,7 @@ TEST_F( ClientListenerTest, setBlockMode_false ) { EXPECT_TRUE(flag & O_NONBLOCK); } -TEST_F( ClientListenerTest, setBlockMode_nonblocking) { +TEST_F( TCPClientListenerTest, setBlockMode_nonblocking) { // ARRANGE int socket_fd; system_context->register_socket_impl([&socket_fd](int a, int b, int c) { @@ -254,7 +254,7 @@ TEST_F( ClientListenerTest, setBlockMode_nonblocking) { } -TEST_F( ClientListenerTest, setBlockMode_blocking) { +TEST_F( TCPClientListenerTest, setBlockMode_blocking) { // ARRANGE int socket_fd; system_context->register_socket_impl([&socket_fd](int a, int b, int c) { @@ -273,7 +273,7 @@ TEST_F( ClientListenerTest, setBlockMode_blocking) { EXPECT_FALSE(flag & O_NONBLOCK); } -TEST_F( ClientListenerTest, setBlockMode_fcntl_getfl_fail) { +TEST_F( TCPClientListenerTest, setBlockMode_fcntl_getfl_fail) { // ARRANGE int socket_fd; system_context->register_socket_impl([&socket_fd](int a, int b, int c) { @@ -293,7 +293,7 @@ TEST_F( ClientListenerTest, setBlockMode_fcntl_getfl_fail) { EXPECT_EQ(status, -1); } -TEST_F( ClientListenerTest, setBlockMode_fcntl_setfl_fail) { +TEST_F( TCPClientListenerTest, setBlockMode_fcntl_setfl_fail) { // ARRANGE int socket_fd; system_context->register_socket_impl([&socket_fd](int a, int b, int c) { @@ -318,7 +318,7 @@ TEST_F( ClientListenerTest, setBlockMode_fcntl_setfl_fail) { } -TEST_F( ClientListenerTest, validateSourceAddress_localhost) { +TEST_F( TCPClientListenerTest, validateSourceAddress_localhost) { // ARRANGE // ACT bool status = listener.validateSourceAddress("localhost"); @@ -327,7 +327,7 @@ TEST_F( ClientListenerTest, validateSourceAddress_localhost) { EXPECT_EQ(status, true); } -TEST_F( ClientListenerTest, validateSourceAddress_junk) { +TEST_F( TCPClientListenerTest, validateSourceAddress_junk) { // ARRANGE // ACT bool status = listener.validateSourceAddress("alsdkfjgalkdj"); @@ -336,7 +336,7 @@ TEST_F( ClientListenerTest, validateSourceAddress_junk) { EXPECT_EQ(status, false); } -TEST_F( ClientListenerTest, checkSocket) { +TEST_F( TCPClientListenerTest, checkSocket) { // ARRANGE listener.initialize(); int port = listener.getPort(); @@ -349,7 +349,7 @@ TEST_F( ClientListenerTest, checkSocket) { EXPECT_EQ(listener.getPort(), port); } -TEST_F( ClientListenerTest, checkSocket_uninitialized) { +TEST_F( TCPClientListenerTest, checkSocket_uninitialized) { // ARRANGE // ACT int status = listener.checkSocket(); @@ -358,7 +358,7 @@ TEST_F( ClientListenerTest, checkSocket_uninitialized) { EXPECT_EQ(status, -1); } -TEST_F( ClientListenerTest, disconnect) { +TEST_F( TCPClientListenerTest, disconnect) { // ARRANGE listener.initialize(); @@ -369,7 +369,7 @@ TEST_F( ClientListenerTest, disconnect) { EXPECT_EQ(status, 0); } -TEST_F( ClientListenerTest, disconnect_uninitialized) { +TEST_F( TCPClientListenerTest, disconnect_uninitialized) { // ARRANGE // ACT int status = listener.disconnect(); @@ -378,7 +378,7 @@ TEST_F( ClientListenerTest, disconnect_uninitialized) { EXPECT_EQ(status, -1); } -TEST_F( ClientListenerTest, setupNewConnection) { +TEST_F( TCPClientListenerTest, setupNewConnection) { // ARRANGE listener.initialize(); @@ -389,7 +389,7 @@ TEST_F( ClientListenerTest, setupNewConnection) { EXPECT_TRUE(connection != NULL); } -TEST_F( ClientListenerTest, setupNewConnection_uninitialized) { +TEST_F( TCPClientListenerTest, setupNewConnection_uninitialized) { // ARRANGE // ACT diff --git a/trick_source/trick_utils/connection_handlers/test/TCPConnection_test.cpp b/trick_source/trick_utils/connection_handlers/test/TCPConnection_test.cpp index 2c9c92f1..2494a55d 100644 --- a/trick_source/trick_utils/connection_handlers/test/TCPConnection_test.cpp +++ b/trick_source/trick_utils/connection_handlers/test/TCPConnection_test.cpp @@ -107,13 +107,11 @@ TEST_F( TCPConnectionTest, setBlockMode_fcntl_getfl_fail) { socket_fd = ::socket(AF_INET, SOCK_STREAM, 0); return socket_fd; }); - + connection.start(); system_context->register_fcntl_impl([](int a, int b, int c) { errno = EACCES; return -1; }); - connection.start(); - // ACT int status = connection.setBlockMode(true); @@ -131,6 +129,9 @@ TEST_F( TCPConnectionTest, setBlockMode_fcntl_setfl_fail) { socket_fd = ::socket(AF_INET, SOCK_STREAM, 0); return socket_fd; }); + + connection.start(); + system_context->register_fcntl_impl([](int a, int cmd, int c) { if (cmd == F_SETFL) { errno = EBADF; @@ -139,8 +140,6 @@ TEST_F( TCPConnectionTest, setBlockMode_fcntl_setfl_fail) { return 0; }); - connection.start(); - // ACT int status = connection.setBlockMode(true); @@ -240,10 +239,12 @@ TEST_F( TCPConnectionTest, read_nonewline ) { connection.start(); // ACT - std::string result = connection.read(); + std::string result; + int nbytes = connection.read(result); // ASSERT ASSERT_EQ(result, std::string("")); + ASSERT_EQ(nbytes, 0); } @@ -263,12 +264,14 @@ TEST_F( TCPConnectionTest, read ) { connection.start(); // ACT - std::string result = connection.read(); + std::string result; + int nbytes = connection.read(result); // ASSERT std::string expected = "Here is a complete message from a socket\n"; expected += '\0'; ASSERT_EQ(result, expected); + ASSERT_EQ(nbytes, expected.size()); } @@ -285,11 +288,14 @@ TEST_F( TCPConnectionTest, read_nodata ) { connection.start(); // ACT - std::string result = connection.read(); + std::string result; + int nbytes = connection.read(result); + // ASSERT std::string expected = ""; ASSERT_EQ(result, expected); + EXPECT_EQ(nbytes, 0); } TEST_F( TCPConnectionTest, read_other_error ) { @@ -306,11 +312,14 @@ TEST_F( TCPConnectionTest, read_other_error ) { connection.start(); // ACT - std::string result = connection.read(); + std::string result; + int nbytes = connection.read(result); // ASSERT std::string expected = ""; ASSERT_EQ(result, expected); + EXPECT_EQ(nbytes, -1); + } @@ -318,11 +327,14 @@ TEST_F( TCPConnectionTest, read_other_error ) { TEST_F( TCPConnectionTest, read_uninitialized ) { // ARRANGE // ACT - std::string result = connection.read(); + std::string result; + int nbytes = connection.read(result); // ASSERT std::string expected = ""; ASSERT_EQ(result, expected); + ASSERT_EQ(nbytes, 0); + } diff --git a/trick_source/trick_utils/connection_handlers/test/UDPConnection_test.cpp b/trick_source/trick_utils/connection_handlers/test/UDPConnection_test.cpp index af6924f0..a7c7623f 100644 --- a/trick_source/trick_utils/connection_handlers/test/UDPConnection_test.cpp +++ b/trick_source/trick_utils/connection_handlers/test/UDPConnection_test.cpp @@ -317,10 +317,12 @@ TEST_F( UDPConnectionTest, read_nonewline ) { // ACT - std::string result = connection.read(); + std::string result; + int nbytes = connection.read(result); // ASSERT ASSERT_EQ(result, std::string("")); + ASSERT_EQ(nbytes, 0); } @@ -338,12 +340,15 @@ TEST_F( UDPConnectionTest, read ) { connection.start(); // ACT - std::string result = connection.read(); + std::string result; + int nbytes = connection.read(result); // ASSERT std::string expected = "Here is a complete message from a socket\n"; expected += '\0'; ASSERT_EQ(result, expected); + ASSERT_EQ(nbytes, expected.size()); + } @@ -358,11 +363,13 @@ TEST_F( UDPConnectionTest, read_nodata ) { connection.start(); // ACT - std::string result = connection.read(); + std::string result; + int nbytes = connection.read(result); // ASSERT std::string expected = ""; ASSERT_EQ(result, expected); + ASSERT_EQ(nbytes, 0); } TEST_F( UDPConnectionTest, read_other_error ) { @@ -376,19 +383,24 @@ TEST_F( UDPConnectionTest, read_other_error ) { connection.start(); // ACT - std::string result = connection.read(); + std::string result; + int nbytes = connection.read(result); // ASSERT std::string expected = ""; ASSERT_EQ(result, expected); + ASSERT_EQ(nbytes, -1); + } TEST_F( UDPConnectionTest, read_uninitialized ) { // ARRANGE // ACT - std::string result = connection.read(); + std::string result; + int nbytes = connection.read(result); // ASSERT std::string expected = ""; ASSERT_EQ(result, expected); + ASSERT_EQ(nbytes, 0); } diff --git a/trick_source/trick_utils/var_binary_parser/src/var_binary_parser.cc b/trick_source/trick_utils/var_binary_parser/src/var_binary_parser.cc index 4255efa8..d8503316 100644 --- a/trick_source/trick_utils/var_binary_parser/src/var_binary_parser.cc +++ b/trick_source/trick_utils/var_binary_parser/src/var_binary_parser.cc @@ -272,24 +272,6 @@ const size_t ParsedBinaryMessage::variable_name_length_size = 4; const size_t ParsedBinaryMessage::variable_type_size = 4; const size_t ParsedBinaryMessage::variable_size_size = 4; -// Sometimes, we need to reconstruct the size from the message. -// Risk of segfaulting here if the bytes are not correctly formed -int ParsedBinaryMessage::parse (char * raw_bytes) { - std::vector bytes; - unsigned int i = 0; - for ( ; i < message_indicator_size + message_size_size; i++) { - bytes.push_back(raw_bytes[i]); - } - - unsigned int message_size = bytesToInt(std::vector (bytes.begin() + message_indicator_size, bytes.begin() + message_indicator_size + message_size_size), _byteswap); - for ( ; i < message_size + message_indicator_size; i++) { - bytes.push_back(raw_bytes[i]); - } - - return parse(bytes); -} - - int ParsedBinaryMessage::parse (const std::vector& bytes) { if (bytes.size() < header_size) { throw MalformedMessageException(std::string("Not enough bytes in message to contain header: expected at least 12, got " + std::to_string(bytes.size()))); diff --git a/trick_source/trick_utils/var_binary_parser/test/TEST_var_binary_parser.cc b/trick_source/trick_utils/var_binary_parser/test/TEST_var_binary_parser.cc index 1ea19406..0572371e 100644 --- a/trick_source/trick_utils/var_binary_parser/test/TEST_var_binary_parser.cc +++ b/trick_source/trick_utils/var_binary_parser/test/TEST_var_binary_parser.cc @@ -3,7 +3,6 @@ #include #include #include -// #include #include "trick/var_binary_parser.hh" // int hi = 161 @@ -837,7 +836,7 @@ TEST (BinaryParserTest, ParseLong) { // apparently this can be different by platform so we need to be careful here size_t long_size = sizeof(long); std::vector bytes; - for (int i = 0; i < long_size-1; i++) { + for (unsigned int i = 0; i < long_size-1; i++) { bytes.push_back(0x00); } bytes.push_back(0x80); @@ -871,7 +870,7 @@ TEST (BinaryParserTest, ParseUnsignedLong) { // apparently this can be different by platform so we need to be careful here size_t long_size = sizeof(unsigned long); std::vector bytes; - for (int i = 0; i < long_size-1; i++) { + for (unsigned int i = 0; i < long_size-1; i++) { bytes.push_back(0xFF); } bytes.push_back(0x7F); @@ -904,7 +903,7 @@ TEST (BinaryParserTest, ParseLongLong) { // apparently this can be different by platform so we need to be careful here size_t long_long_size = sizeof(long long); std::vector bytes; - for (int i = 0; i < long_long_size-1; i++) { + for (unsigned int i = 0; i < long_long_size-1; i++) { bytes.push_back(0x00); } bytes.push_back(0x80); @@ -937,7 +936,7 @@ TEST (BinaryParserTest, ParseUnsignedLongLong) { // apparently this can be different by platform so we need to be careful here size_t long_long_size = sizeof(unsigned long long); std::vector bytes; - for (int i = 0; i < long_long_size-1; i++) { + for (unsigned int i = 0; i < long_long_size-1; i++) { bytes.push_back(0xFF); } bytes.push_back(0x7F); @@ -1055,7 +1054,7 @@ TEST (BinaryParserTest, ParseWChar) { wchar_t test_wchar = L'J'; std::vector bytes; - for (int i = 0; i < sizeof(wchar_t); i++) { + for (unsigned int i = 0; i < sizeof(wchar_t); i++) { bytes.push_back((unsigned char)((test_wchar >> (i*8)) & 0xFF)); } @@ -1066,11 +1065,17 @@ TEST (BinaryParserTest, ParseWChar) { TEST (BinaryParserTest, ParseWCharWrongType) { Var variable; - std::vector bytes = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14, 0xc0}; + wchar_t test_wchar = L'J'; + std::vector bytes; + + for (unsigned int i = 0; i < sizeof(wchar_t); i++) { + bytes.push_back((unsigned char)((test_wchar >> (i*8)) & 0xFF)); + } + variable.setValue(bytes, 8, TRICK_INTEGER, false); try { - variable.getValue(); + variable.getValue(); FAIL() << "Expected exception thrown"; } catch(ParseTypeException& ex) { diff --git a/trickops.py b/trickops.py index a2703249..a04c1843 100644 --- a/trickops.py +++ b/trickops.py @@ -21,16 +21,33 @@ class SimTestWorkflow(TrickWorkflow): trick_dir=trick_top_level, config_file=(trick_top_level + "/test_sims.yml"), cpus=self.cpus, quiet=quiet) def run( self ): build_jobs = self.get_jobs(kind='build') - # Two sims have runs that require ordering via phases: + + # This is awful but I can't think of another way around it + # SIM_test_varserver has 2 tests that should return the code for SIG_USR1, the number is different on Mac vs Linux + # so it can't be hardcoded in the input yml file. Maybe this is a case having a label on a run would be cleaner? + import signal + run_names = ["Run test/SIM_test_varserv RUN_test/err1_test.py", "Run test/SIM_test_varserv RUN_test/err2_test.py"] + for job in [job for job in self.get_jobs(kind='run') if job.name in run_names]: + job._expected_exit_status = signal.SIGUSR1.value + + # Several sims have runs that require ordering via phases: # - SIM_stls dumps a checkpoint that is then read in and checked by a subsequent run # - SIM_checkpoint_data_recording dumps checkpoints that are read by subsequent runs - first_run_jobs = self.get_jobs(kind='run', phase=-1) # Get all jobs with early phase -1 - remaining_run_jobs = self.get_jobs(kind='run', phase=0) # Get all jobs with default phase 0 - analysis_jobs = self.get_jobs(kind='analyze') + # - SIM_test_varserver has 3 runs that cannot be concurrent + # - SIM_mc_generation generates runs and then runs them + phases = [-1, 0, 1, 2, 3] + analysis_jobs = self.get_jobs(kind='analyze') builds_status = self.execute_jobs(build_jobs, max_concurrent=self.cpus, header='Executing all sim builds.') - first_phase_run_status = self.execute_jobs(first_run_jobs, max_concurrent=self.cpus, header="Executing first phase runs.", job_timeout=1000) - runs_status = self.execute_jobs(remaining_run_jobs, max_concurrent=self.cpus, header='Executing remaining runs.', job_timeout=1000) + + jobs = build_jobs + + run_status = 0 + for phase in phases: + run_jobs = self.get_jobs(kind='run', phase=phase) + this_status = self.execute_jobs(run_jobs, max_concurrent=self.cpus, header="Executing phase " + str(phase) + " runs.", job_timeout=1000) + run_status = run_status or this_status + jobs += run_jobs comparison_result = self.compare() analysis_status = self.execute_jobs(analysis_jobs, max_concurrent=self.cpus, header='Executing all analysis.') @@ -39,7 +56,6 @@ class SimTestWorkflow(TrickWorkflow): self.status_summary() # Print a Succinct summary # Dump failing logs - jobs = build_jobs + first_run_jobs + remaining_run_jobs for job in jobs: if job.get_status() == Job.Status.FAILED or job.get_status() == Job.Status.TIMEOUT: print ("*"*120) @@ -54,7 +70,7 @@ class SimTestWorkflow(TrickWorkflow): print(open(job.log_file, "r").read()) print ("*"*120, "\n\n\n") - return (builds_status or runs_status or first_phase_run_status or len(self.config_errors) > 0 or comparison_result or analysis_status) + return (builds_status or run_status or len(self.config_errors) > 0 or comparison_result or analysis_status) if __name__ == "__main__": parser = argparse.ArgumentParser(description='Build, run, and compare all test sims for Trick',