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