]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGfdmSocket.cpp
Curt Olson:
[flightgear.git] / src / FDM / JSBSim / FGfdmSocket.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3  Module:       FGfdmSocket.cpp
4  Author:       Jon S. Berndt
5  Date started: 11/08/99
6  Purpose:      Encapsulates a socket
7  Called by:    FGOutput, et. al.
8
9  ------------- Copyright (C) 1999  Jon S. Berndt (jsb@hal-pc.org) -------------
10
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
14  version.
15
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
19  details.
20
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.
24
25  Further information about the GNU General Public License can also be found on
26  the world wide web at http://www.gnu.org.
27
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
30 This class excapsulates a socket for simple data writing
31
32 HISTORY
33 --------------------------------------------------------------------------------
34 11/08/99   JSB   Created
35
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 INCLUDES
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
39
40 #include "FGfdmSocket.h"
41
42 namespace JSBSim {
43
44 static const char *IdSrc = "$Id$";
45 static const char *IdHdr = ID_FDMSOCKET;
46
47 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48 CLASS IMPLEMENTATION
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
50
51 FGfdmSocket::FGfdmSocket(string address, int port)
52 {
53   size = 0;
54   connected = false;
55
56   #if defined(__BORLANDC__) || defined(_MSC_VER) || defined(__MINGW32__)
57     WSADATA wsaData;
58     int wsaReturnCode;
59     wsaReturnCode = WSAStartup(MAKEWORD(1,1), &wsaData);
60     if (wsaReturnCode == 0) cout << "Winsock DLL loaded ..." << endl;
61     else cout << "Winsock DLL not initialized ..." << endl;
62   #endif
63
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;
67     }
68   } else {
69     if ((host = gethostbyaddr(address.c_str(), address.size(), PF_INET)) == NULL) {
70       cout << "Could not get host net address by number..." << endl;
71     }
72   }
73
74   if (host != NULL) {
75     cout << "Got host net address..." << endl;
76     sckt = socket(AF_INET, SOCK_STREAM, 0);
77
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;
86         connected = true;
87       } else {                // unsuccessful
88         cout << "Could not connect to socket ..." << endl;
89       }
90     } else {          // unsuccessful
91       cout << "Could not create socket for FDM, error = " << errno << endl;
92     }
93   }
94   Debug(0);
95 }
96
97 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98
99 FGfdmSocket::~FGfdmSocket()
100 {
101   #ifndef macintosh
102   if (sckt) shutdown(sckt,2);
103   #endif
104   
105   #ifdef __BORLANDC__
106     WSACleanup();
107   #endif
108   Debug(1);
109 }
110
111 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112
113 void FGfdmSocket::Clear(void)
114 {
115   buffer = "";
116   size = 0;
117 }
118
119 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
120
121 void FGfdmSocket::Append(const char* item)
122 {
123   if (size == 0) buffer += string(item);
124   else buffer += string(",") + string(item);
125   size++;
126 }
127
128 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
129
130 void FGfdmSocket::Append(double item)
131 {
132   char s[25];
133
134   sprintf(s,"%12.7f",item);
135
136   if (size == 0) buffer += string(s);
137   else buffer += string(",") + string(s);
138   size++;
139 }
140
141 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142
143 void FGfdmSocket::Append(long item)
144 {
145   char s[25];
146
147   sprintf(s,"%12ld",item);
148
149   if (size == 0) buffer += string(s);
150   else buffer += string(",") + string(s);
151   size++;
152 }
153
154 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
155
156 void FGfdmSocket::Send(void)
157 {
158   buffer += string("\n");
159   if ((send(sckt,buffer.c_str(),buffer.size(),0)) <= 0) {
160     perror("send");
161   } else {
162   }
163 }
164
165 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166 //    The bitmasked value choices are as follows:
167 //    unset: In this case (the default) JSBSim would only print
168 //       out the normally expected messages, essentially echoing
169 //       the config files as they are read. If the environment
170 //       variable is not set, debug_lvl is set to 1 internally
171 //    0: This requests JSBSim not to output any messages
172 //       whatsoever.
173 //    1: This value explicity requests the normal JSBSim
174 //       startup messages
175 //    2: This value asks for a message to be printed out when
176 //       a class is instantiated
177 //    4: When this value is set, a message is displayed when a
178 //       FGModel object executes its Run() method
179 //    8: When this value is set, various runtime state variables
180 //       are printed out periodically
181 //    16: When set various parameters are sanity checked and
182 //       a message is printed out when they go out of bounds
183
184 void FGfdmSocket::Debug(int from)
185 {
186   if (debug_lvl <= 0) return;
187
188   if (debug_lvl & 1) { // Standard console startup message output
189     if (from == 0) { // Constructor
190     }
191   }
192   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
193     if (from == 0) cout << "Instantiated: FGfdmSocket" << endl;
194     if (from == 1) cout << "Destroyed:    FGfdmSocket" << endl;
195   }
196   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
197   }
198   if (debug_lvl & 8 ) { // Runtime state variables
199   }
200   if (debug_lvl & 16) { // Sanity checking
201   }
202   if (debug_lvl & 64) {
203     if (from == 0) { // Constructor
204       cout << IdSrc << endl;
205       cout << IdHdr << endl;
206     }
207   }
208 }
209 }