]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/FGfdmSocket.cpp
JSBSim tweaks.
[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 extern short debug_lvl;
46
47 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48 CLASS IMPLEMENTATION
49 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
50
51 FGfdmSocket::FGfdmSocket(string address, int port)
52 {
53   size = 0;
54
55   #if defined(__BORLANDC__) || defined(_MSC_VER)
56     WSADATA wsaData;
57     int PASCAL FAR wsaReturnCode;
58     wsaReturnCode = WSAStartup(MAKEWORD(1,1), &wsaData);
59     if (wsaReturnCode == 0) cout << "Winsock DLL loaded ..." << endl;
60     else cout << "Winsock DLL not initialized ..." << endl;
61   #endif
62
63   if (address.find_first_not_of("0123456789.",0) != address.npos) {
64     if ((host = gethostbyname(address.c_str())) == NULL) {
65       cout << "Could not get host net address by name..." << endl;
66     }
67   } else {
68     if ((host = gethostbyaddr(address.c_str(), address.size(), PF_INET)) == NULL) {
69       cout << "Could not get host net address by number..." << endl;
70     }
71   }
72
73   if (host != NULL) {
74     cout << "Got host net address..." << endl;
75     sckt = socket(AF_INET, SOCK_STREAM, 0);
76
77     if (sckt >= 0) {  // successful
78       memset(&scktName, 0, sizeof(struct sockaddr_in));
79       scktName.sin_family = AF_INET;
80       scktName.sin_port = htons(port);
81       memcpy(&scktName.sin_addr, host->h_addr_list[0], host->h_length);
82       int len = sizeof(struct sockaddr_in);
83       if (connect(sckt, (struct sockaddr*)&scktName, len) == 0) {   // successful
84         cout << "Successfully connected to socket ..." << endl;
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
93   if (debug_lvl & 2) cout << "Instantiated: FGfdmSocket" << endl;
94 }
95
96 FGfdmSocket::~FGfdmSocket()
97 {
98   #ifndef macintosh
99   if (sckt) shutdown(sckt,2);
100   #endif
101   
102   #ifdef __BORLANDC__
103     WSACleanup();
104   #endif
105 }
106
107 void FGfdmSocket::Clear(void)
108 {
109   buffer = "";
110   size = 0;
111 }
112
113 void FGfdmSocket::Append(const char* item)
114 {
115   if (size == 0) buffer += string(item);
116   else buffer += string(",") + string(item);
117   size++;
118 }
119
120 void FGfdmSocket::Append(float item)
121 {
122   char s[25];
123
124   sprintf(s,"%12.7f\0",item);
125
126   if (size == 0) buffer += string(s);
127   else buffer += string(",") + string(s);
128   size++;
129 }
130
131 void FGfdmSocket::Append(long item)
132 {
133   char s[25];
134
135   sprintf(s,"%12d\0",item);
136
137   if (size == 0) buffer += string(s);
138   else buffer += string(",") + string(s);
139   size++;
140 }
141
142 void FGfdmSocket::Send(void)
143 {
144   buffer += string("\n");
145   if ((send(sckt,buffer.c_str(),buffer.size(),0)) <= 0) {
146     perror("send");
147   } else {
148   }
149 }
150