Marine systems simulation
CTCP.h
1#ifndef CTCP_H
2#define CTCP_H
3
4#include <string>
5#include <Eigen/Eigen>
6#include <iostream>
7#include <iomanip>
8#include <fstream>
9#include <algorithm>
10#include <vector>
11#include <valarray>
12
13#include "SimObject.h"
14#include <CIntegratorOptions.h>
15#include <CPrintDuringExec.h>
16
17#include "sfh/text.h"
18#include "sfh/math/math.h"
19#include "sfh/sim/kinematics.h"
20#include "sfh/constants.h"
21
22#ifdef _WIN32 // Windows
23#include <winsock2.h>
24#include <ws2tcpip.h>
25#include <windows.h>
26#else // Linux
27#include <sys/types.h>
28#include <sys/socket.h>
29#include <netinet/in.h>
30#include <arpa/inet.h>
31#include <netdb.h>
32#include <unistd.h>
33#include <errno.h>
34#define SOCKET int
35#endif
36
37
38namespace Interface {
39
98class CTCP : public SimObject
99{
100public:
101 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
102
103 // Constructor
104 CTCP(std::string sSimObjectName, ISimObjectCreator* pCreator);
105 virtual void FinalSetup(const double dT, const double* const adX, ISimObjectCreator* const pCreator);
106 virtual void InitialConditionSetup(const double T, const double *const currentIC, double* updatedIC, ISimObjectCreator* creator);
107 virtual ~CTCP();
108
109 // Calculate the state derivatives.
110 virtual void OdeFcn(const double dT, const double* const adX, double* const adXDot, const bool bIsMajorTimeStep);
111
112 // Set up a tcp connection for a spesific IP address and port number
113 bool SetupTCP(const std::string& IPString, const int& port);
114
115 // Send a tcp message
116 bool SendMsg(const double dT, const double* const adX);
117
118 // Receive a tcp message
119 bool ReceiveMsg();
120
121 // Receive a tcp message in multiple rounds
122 bool ReceiveMsgMR();
123
124#ifdef FH_VISUALIZATION
126 virtual void RenderInit(Ogre::Root* const ogreRoot, ISimObjectCreator* const creator) {};
127
129 virtual void RenderUpdate(const double dT, const double* const adX) {};
130#endif
131
132protected:
133 // FhSim logger
134 CPrintDuringExec* m_Logger;
135
136 // Input ports
137 ISignalPort* m_InSig;
138 // Number of messages to send
139 int m_InNum;
140 // Input buffer
141 double* m_InMsg;
142
143 // Output ports
144 virtual const double* OutMsg(const double dT, const double* const adX);
145 // Number of messages to receive
146 int m_OutNum;
147 // Output buffer
148 double* m_OutMsg;
149
150 // Buffer for sending and receiving messages
151 char* m_MsgBuf;
152
153 // Server information
154 std::string m_TCPName;
155 std::string m_IPString;
156 int m_PortNum;
157
158 // Socket information
159#ifdef _WIN32
160 WSADATA m_WsaData;
161#endif
162 SOCKET m_ListenSocket;
163 SOCKET m_ClientSocket;
164
165 // To check if sending and reveiving messages successfully
166 bool m_TCP;
167
168 // Number of Bytes sent and received
169 int m_RecResult;
170 int m_SendResult;
171
172 // Options
173 bool m_RecMR; // True to receive a tcp message in multiple rounds
174 bool m_ShowTCPRst; // True to show TCP results on the screen
175 double m_ComDT; // Communication interval
176 double m_ComNT; // Time for next communication
177};
178
179};
180
181#endif
Definition: CTCP.h:99