Marine systems simulation
CTCP_client.h
1#ifndef CTCP_C_H
2#define CTCP_C_H
3
4#include <string>
5#include "SimObject.h"
6#include <Eigen/Eigen>
7#include <CPrintDuringExec.h>
8
9#ifdef _WIN32 // Windows
10#include <winsock2.h>
11#else // Linux
12#include <sys/types.h>
13#include <sys/socket.h>
14#include <netinet/in.h>
15#include <arpa/inet.h>
16#include <netdb.h>
17#include <unistd.h>
18#include <errno.h>
19#define SOCKET int
20#endif
21
22
23namespace Interface {
24
81class CTCP_client : public SimObject
82{
83public:
84 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
85
86 // Constructor
87 CTCP_client(std::string sSimObjectName, ISimObjectCreator* pCreator);
88 virtual void FinalSetup(const double dT, const double* const adX, ISimObjectCreator* const pCreator);
89 virtual void InitialConditionSetup(const double T, const double *const currentIC, double* updatedIC, ISimObjectCreator* creator);
90 virtual ~CTCP_client();
91
92 // Calculate the state derivatives.
93 virtual void OdeFcn(const double dT, const double* const adX, double* const adXDot, const bool bIsMajorTimeStep);
94
95 // Set up a tcp connection for a spesific IP address and port number
96 bool SetupTCP(const std::string& IPString, const int& port);
97
98 // Send a tcp message
99 bool SendMsg(const double dT, const double* const adX);
100
101 // Receive a tcp message
102 bool ReceiveMsg();
103
104 // Receive a tcp message in multiple rounds
105 bool ReceiveMsgMR();
106
107#ifdef FH_VISUALIZATION
108 // Initial specification of the simObject geometry for rendering.
109 virtual void RenderInit(Ogre::Root* const ogreRoot, ISimObjectCreator* const creator) {};
110
111 // Updating the simObject for rendering the next frame.
112 virtual void RenderUpdate(const double dT, const double* const adX) {};
113#endif
114
115protected:
116 // FhSim logger
117 CPrintDuringExec* m_Logger;
118
119 // Input ports
120 ISignalPort* m_InSig;
121 // Number of messages to send
122 int m_InNum;
123 // Input buffer
124 double* m_InMsg;
125
126 // Output ports
127 virtual const double* OutMsg(const double dT, const double* const adX);
128 // Number of messages to receive
129 int m_OutNum;
130 // Output buffer
131 double* m_OutMsg;
132
133 // Buffer for sending and receiving messages
134 char* m_MsgBuf;
135
136 // Server information
137 std::string m_TCPName;
138 std::string m_IPString;
139 int m_PortNum;
140
141 // Socket information
142#ifdef _WIN32
143 WSADATA m_WsaData;
144#endif
145 SOCKET m_ConnectSocket;
146
147 // To check if sending and reveiving messages successfully
148 bool m_TCP;
149
150 // Number of Bytes sent and received
151 int m_RecResult;
152 int m_SendResult;
153
154 // Options
155 bool m_RecMR; // True to receive a tcp message in multiple rounds
156 bool m_ShowTCPRst; // True to show TCP results on the screen
157 double m_ComDT; // Communication interval
158 double m_ComNT; // Time for next communication
159};
160
161};
162
163#endif
Definition: CTCP_client.h:82