Marine systems simulation
Loading...
Searching...
No Matches
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
94class CTCP : public SimObject
95{
96public:
97 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
98
99 // Constructor
100 CTCP(std::string sSimObjectName, ISimObjectCreator* pCreator);
101 virtual void FinalSetup(const double dT, const double* const adX, ISimObjectCreator* const pCreator);
102 virtual void InitialConditionSetup(const double T, const double *const currentIC, double* updatedIC, ISimObjectCreator* creator);
103 virtual ~CTCP();
104
105 // Calculate the state derivatives.
106 virtual void OdeFcn(const double dT, const double* const adX, double* const adXDot, const bool bIsMajorTimeStep);
107
108 // Set up a tcp connection for a spesific IP address and port number
109 bool SetupTCP(const std::string& IPString, const int& port);
110
111 // Send a tcp message
112 bool SendMsg(const double dT, const double* const adX);
113
114 // Receive a tcp message
115 bool ReceiveMsg();
116
117 // Receive a tcp message in multiple rounds
118 bool ReceiveMsgMR();
119
120#ifdef FH_VISUALIZATION
122 virtual void RenderInit(Ogre::Root* const ogreRoot, ISimObjectCreator* const creator) {};
123
125 virtual void RenderUpdate(const double dT, const double* const adX) {};
126#endif
127
128protected:
129 // FhSim logger
130 CPrintDuringExec* m_Logger;
131
132 // Input ports
133 ISignalPort* m_InSig;
134 // Number of messages to send
135 int m_InNum;
136 // Input buffer
137 double* m_InMsg;
138
139 // Output ports
140 virtual const double* OutMsg(const double dT, const double* const adX);
141 // Number of messages to receive
142 int m_OutNum;
143 // Output buffer
144 double* m_OutMsg;
145
146 // Buffer for sending and receiving messages
147 char* m_MsgBuf;
148
149 // Server information
150 std::string m_TCPName;
151 std::string m_IPString;
152 int m_PortNum;
153
154 // Socket information
155#ifdef _WIN32
156 WSADATA m_WsaData;
157#endif
158 SOCKET m_ListenSocket;
159 SOCKET m_ClientSocket;
160
161 // To check if sending and reveiving messages successfully
162 bool m_TCP;
163
164 // Number of Bytes sent and received
165 int m_RecResult;
166 int m_SendResult;
167
168 // Options
169 bool m_RecMR; // True to receive a tcp message in multiple rounds
170 bool m_ShowTCPRst; // True to show TCP results on the screen
171 double m_ComDT; // Communication interval
172 double m_ComNT; // Time for next communication
173};
174
175};
176
177#endif
Definition CTCP.h:95