1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 Module: FGfdmSocket.cpp
6 Purpose: Encapsulates a socket
7 Called by: FGOutput, et. al.
9 ------------- Copyright (C) 1999 Jon S. Berndt (jsb@hal-pc.org) -------------
11 This program is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free Software
13 Foundation; either version 2 of the License, or (at your option) any later
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
23 Place - Suite 330, Boston, MA 02111-1307, USA.
25 Further information about the GNU General Public License can also be found on
26 the world wide web at http://www.gnu.org.
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
30 This class excapsulates a socket for simple data writing
33 --------------------------------------------------------------------------------
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40 #include "FGfdmSocket.h"
44 static const char *IdSrc = "$Id$";
45 static const char *IdHdr = ID_FDMSOCKET;
47 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
51 FGfdmSocket::FGfdmSocket(string address, int port)
53 sckt = sckt_in = size = 0;
56 #if defined(__BORLANDC__) || defined(_MSC_VER) || defined(__MINGW32__)
59 wsaReturnCode = WSAStartup(MAKEWORD(1,1), &wsaData);
60 if (wsaReturnCode == 0) cout << "Winsock DLL loaded ..." << endl;
61 else cout << "Winsock DLL not initialized ..." << endl;
64 if (address.find_first_not_of("0123456789.",0) != address.npos) {
65 if ((host = gethostbyname(address.c_str())) == NULL) {
66 cout << "Could not get host net address by name..." << endl;
69 if ((host = gethostbyaddr(address.c_str(), address.size(), PF_INET)) == NULL) {
70 cout << "Could not get host net address by number..." << endl;
75 cout << "Got host net address..." << endl;
76 sckt = socket(AF_INET, SOCK_STREAM, 0);
78 if (sckt >= 0) { // successful
79 memset(&scktName, 0, sizeof(struct sockaddr_in));
80 scktName.sin_family = AF_INET;
81 scktName.sin_port = htons(port);
82 memcpy(&scktName.sin_addr, host->h_addr_list[0], host->h_length);
83 int len = sizeof(struct sockaddr_in);
84 if (connect(sckt, (struct sockaddr*)&scktName, len) == 0) { // successful
85 cout << "Successfully connected to socket ..." << endl;
87 } else { // unsuccessful
88 cout << "Could not connect to socket ..." << endl;
90 } else { // unsuccessful
91 cout << "Could not create socket for FDM, error = " << errno << endl;
97 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99 FGfdmSocket::FGfdmSocket(int port)
103 unsigned long NoBlock = true;
105 #if defined(__BORLANDC__) || defined(_MSC_VER) || defined(__MINGW32__)
108 wsaReturnCode = WSAStartup(MAKEWORD(1,1), &wsaData);
109 if (wsaReturnCode == 0) cout << "Winsock DLL loaded ..." << endl;
110 else cerr << "Winsock DLL not initialized ..." << endl;
113 sckt = socket(AF_INET, SOCK_STREAM, 0);
115 if (sckt >= 0) { // successful
116 memset(&scktName, 0, sizeof(struct sockaddr_in));
117 scktName.sin_family = AF_INET;
118 scktName.sin_port = htons(port);
119 // memcpy(&scktName.sin_addr, host->h_addr_list[0], host->h_length);
120 int len = sizeof(struct sockaddr_in);
121 if (bind(sckt, (struct sockaddr*)&scktName, len) == 0) { // successful
122 cout << "Successfully bound to socket ..." << endl;
123 if (listen(sckt, 5) >= 0) { // successful listen()
124 #if defined(__BORLANDC__) || defined(_MSC_VER) || defined(__MINGW32__)
125 ioctlsocket(sckt, FIONBIO, &NoBlock);
126 sckt_in = accept(sckt, (struct sockaddr*)&scktName, &len);
128 ioctl(sckt, FIONBIO, &NoBlock);
129 sckt_in = accept(sckt, (struct sockaddr*)&scktName, (socklen_t*)&len);
132 cerr << "Could not listen ..." << endl;
135 } else { // unsuccessful
136 cerr << "Could not bind to socket ..." << endl;
138 } else { // unsuccessful
139 cerr << "Could not create socket for FDM, error = " << errno << endl;
145 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
147 FGfdmSocket::~FGfdmSocket()
150 if (sckt) shutdown(sckt,2);
151 if (sckt_in) shutdown(sckt_in,2);
160 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
162 string FGfdmSocket::Receive(void)
165 int len = sizeof(struct sockaddr_in);
168 unsigned long NoBlock = true;
169 string data = ""; // todo: should allocate this with a standard size as a
170 // class attribute and pass as a reference?
173 #if defined(__BORLANDC__) || defined(_MSC_VER) || defined(__MINGW32__)
174 sckt_in = accept(sckt, (struct sockaddr*)&scktName, &len);
176 sckt_in = accept(sckt, (struct sockaddr*)&scktName, (socklen_t*)&len);
179 #if defined(__BORLANDC__) || defined(_MSC_VER) || defined(__MINGW32__)
180 ioctlsocket(sckt_in, FIONBIO,&NoBlock);
182 ioctl(sckt_in, FIONBIO, &NoBlock);
184 send(sckt_in, "Connected to JSBSim server\nJSBSim> ", 35, 0);
189 while ((num_chars = recv(sckt_in, buf, 1024, 0)) > 0) {
190 data += string(buf).substr(0,num_chars);
191 total_chars += num_chars;
194 #if defined(_MSC_VER)
195 // when nothing received and the error isn't "would block"
196 // then assume that the client has closed the socket.
197 if (num_chars == 0) {
198 DWORD err = WSAGetLastError ();
199 if (err != WSAEWOULDBLOCK) {
200 printf ("Socket Closed. back to listening\n");
201 closesocket (sckt_in);
208 return data.substr(0, total_chars);
211 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
213 int FGfdmSocket::Reply(string text)
215 int num_chars_sent=0;
218 num_chars_sent = send(sckt_in, text.c_str(), text.size(), 0);
219 send(sckt_in, "JSBSim> ", 8, 0);
221 cerr << "Socket reply must be to a valid socket" << endl;
224 return num_chars_sent;
227 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
229 void FGfdmSocket::Close(void)
234 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
236 void FGfdmSocket::Clear(void)
242 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244 void FGfdmSocket::Clear(string s)
247 size = buffer.size();
250 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
252 void FGfdmSocket::Append(const char* item)
254 if (size == 0) buffer += string(item);
255 else buffer += string(",") + string(item);
259 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
261 void FGfdmSocket::Append(double item)
265 sprintf(s,"%12.7f",item);
267 if (size == 0) buffer += string(s);
268 else buffer += string(",") + string(s);
272 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
274 void FGfdmSocket::Append(long item)
278 sprintf(s,"%12ld",item);
280 if (size == 0) buffer += string(s);
281 else buffer += string(",") + string(s);
285 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
287 void FGfdmSocket::Send(void)
289 buffer += string("\n");
290 if ((send(sckt,buffer.c_str(),buffer.size(),0)) <= 0) {
296 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
297 // The bitmasked value choices are as follows:
298 // unset: In this case (the default) JSBSim would only print
299 // out the normally expected messages, essentially echoing
300 // the config files as they are read. If the environment
301 // variable is not set, debug_lvl is set to 1 internally
302 // 0: This requests JSBSim not to output any messages
304 // 1: This value explicity requests the normal JSBSim
306 // 2: This value asks for a message to be printed out when
307 // a class is instantiated
308 // 4: When this value is set, a message is displayed when a
309 // FGModel object executes its Run() method
310 // 8: When this value is set, various runtime state variables
311 // are printed out periodically
312 // 16: When set various parameters are sanity checked and
313 // a message is printed out when they go out of bounds
315 void FGfdmSocket::Debug(int from)
317 if (debug_lvl <= 0) return;
319 if (debug_lvl & 1) { // Standard console startup message output
320 if (from == 0) { // Constructor
323 if (debug_lvl & 2 ) { // Instantiation/Destruction notification
324 if (from == 0) cout << "Instantiated: FGfdmSocket" << endl;
325 if (from == 1) cout << "Destroyed: FGfdmSocket" << endl;
327 if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
329 if (debug_lvl & 8 ) { // Runtime state variables
331 if (debug_lvl & 16) { // Sanity checking
333 if (debug_lvl & 64) {
334 if (from == 0) { // Constructor
335 cout << IdSrc << endl;
336 cout << IdHdr << endl;