]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGfdmSocket.cpp
Updated to match changes in radiostack.[ch]xx
[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 static const char *IdSrc = "$Id$";
43 static const char *IdHdr = ID_FDMSOCKET;
44
45 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
46 CLASS IMPLEMENTATION
47 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
48
49 FGfdmSocket::FGfdmSocket(string address, int port)
50 {
51   size = 0;
52   connected = false;
53
54 #if defined(__BORLANDC__) || defined(_MSC_VER) || defined(__MINGW32__)
55     WSADATA wsaData;
56     int wsaReturnCode;
57     wsaReturnCode = WSAStartup(MAKEWORD(1,1), &wsaData);
58     if (wsaReturnCode == 0) cout << "Winsock DLL loaded ..." << endl;
59     else cout << "Winsock DLL not initialized ..." << endl;
60   #endif
61
62   if (address.find_first_not_of("0123456789.",0) != address.npos) {
63     if ((host = gethostbyname(address.c_str())) == NULL) {
64       cout << "Could not get host net address by name..." << endl;
65     }
66   } else {
67     if ((host = gethostbyaddr(address.c_str(), address.size(), PF_INET)) == NULL) {
68       cout << "Could not get host net address by number..." << endl;
69     }
70   }
71
72   if (host != NULL) {
73     cout << "Got host net address..." << endl;
74     sckt = socket(AF_INET, SOCK_STREAM, 0);
75
76     if (sckt >= 0) {  // successful
77       memset(&scktName, 0, sizeof(struct sockaddr_in));
78       scktName.sin_family = AF_INET;
79       scktName.sin_port = htons(port);
80       memcpy(&scktName.sin_addr, host->h_addr_list[0], host->h_length);
81       int len = sizeof(struct sockaddr_in);
82       if (connect(sckt, (struct sockaddr*)&scktName, len) == 0) {   // successful
83         cout << "Successfully connected to socket ..." << endl;
84         connected = true;
85       } else {                // unsuccessful
86         cout << "Could not connect to socket ..." << endl;
87       }
88     } else {          // unsuccessful
89       cout << "Could not create socket for FDM, error = " << errno << endl;
90     }
91   }
92   Debug(0);
93 }
94
95 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
96
97 FGfdmSocket::~FGfdmSocket()
98 {
99   #ifndef macintosh
100   if (sckt) shutdown(sckt,2);
101   #endif
102   
103   #ifdef __BORLANDC__
104     WSACleanup();
105   #endif
106   Debug(1);
107 }
108
109 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110
111 void FGfdmSocket::Clear(void)
112 {
113   buffer = "";
114   size = 0;
115 }
116
117 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
118
119 void FGfdmSocket::Append(const char* item)
120 {
121   if (size == 0) buffer += string(item);
122   else buffer += string(",") + string(item);
123   size++;
124 }
125
126 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
127
128 void FGfdmSocket::Append(double item)
129 {
130   char s[25];
131
132   sprintf(s,"%12.7f",item);
133
134   if (size == 0) buffer += string(s);
135   else buffer += string(",") + string(s);
136   size++;
137 }
138
139 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140
141 void FGfdmSocket::Append(long item)
142 {
143   char s[25];
144
145   sprintf(s,"%12ld",item);
146
147   if (size == 0) buffer += string(s);
148   else buffer += string(",") + string(s);
149   size++;
150 }
151
152 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
153
154 void FGfdmSocket::Send(void)
155 {
156   buffer += string("\n");
157   if ((send(sckt,buffer.c_str(),buffer.size(),0)) <= 0) {
158     perror("send");
159   } else {
160   }
161 }
162
163 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
164 //    The bitmasked value choices are as follows:
165 //    unset: In this case (the default) JSBSim would only print
166 //       out the normally expected messages, essentially echoing
167 //       the config files as they are read. If the environment
168 //       variable is not set, debug_lvl is set to 1 internally
169 //    0: This requests JSBSim not to output any messages
170 //       whatsoever.
171 //    1: This value explicity requests the normal JSBSim
172 //       startup messages
173 //    2: This value asks for a message to be printed out when
174 //       a class is instantiated
175 //    4: When this value is set, a message is displayed when a
176 //       FGModel object executes its Run() method
177 //    8: When this value is set, various runtime state variables
178 //       are printed out periodically
179 //    16: When set various parameters are sanity checked and
180 //       a message is printed out when they go out of bounds
181
182 void FGfdmSocket::Debug(int from)
183 {
184   if (debug_lvl <= 0) return;
185
186   if (debug_lvl & 1) { // Standard console startup message output
187     if (from == 0) { // Constructor
188     }
189   }
190   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
191     if (from == 0) cout << "Instantiated: FGfdmSocket" << endl;
192     if (from == 1) cout << "Destroyed:    FGfdmSocket" << endl;
193   }
194   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
195   }
196   if (debug_lvl & 8 ) { // Runtime state variables
197   }
198   if (debug_lvl & 16) { // Sanity checking
199   }
200   if (debug_lvl & 64) {
201     if (from == 0) { // Constructor
202       cout << IdSrc << endl;
203       cout << IdHdr << endl;
204     }
205   }
206 }
207