JSBSim Flight Dynamics Model  1.0 (02 March 2017)
An Open Source Flight Dynamics and Control Software Library in C++
FGUDPOutputSocket.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 
3  Module: FGUDPOutputSocket.cpp
4  Author: David Culp
5  Date started: 03/31/15
6  Purpose: Manage output of property values to a UDP socket
7  Called by: FGOutput
8 
9  ------------- Copyright (C) 2015 David Culp ----------------
10 
11  This program is free software; you can redistribute it and/or modify it under
12  the terms of the GNU Lesser 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 Lesser General Public License for more
19  details.
20 
21  You should have received a copy of the GNU Lesser 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 Lesser 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 sends comma-separated strings over a UDP socket. The format is that required
31 by the QtJSBSim application.
32 
33 HISTORY
34 --------------------------------------------------------------------------------
35 03/31/15 DC Created
36 
37 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38 INCLUDES
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40 
41 #include <cstring>
42 #include <cstdlib>
43 
44 #include "FGUDPOutputSocket.h"
45 #include "FGFDMExec.h"
46 #include "input_output/FGPropertyManager.h"
47 #include "input_output/FGXMLElement.h"
48 
49 using namespace std;
50 
51 namespace JSBSim {
52 
53 IDENT(IdSrc,"$Id: FGUDPOutputSocket.cpp,v 1.2 2015/08/16 13:19:52 bcoconni Exp $");
54 IDENT(IdHdr,ID_UDPOUTPUTSOCKET);
55 
56 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57 CLASS IMPLEMENTATION
58 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
59 
60 FGUDPOutputSocket::FGUDPOutputSocket(FGFDMExec* fdmex) :
61  FGOutputType(fdmex),
62  socket(0)
63 {
64  FDMExec = fdmex;
65  PropertyManager = fdmex->GetPropertyManager();
66  root = PropertyManager->GetNode();
67  root->SetDouble("simulation/null", 0.0);
68  SockName = "localhost";
69  SockPort = 5138;
70 }
71 
72 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 
75 {
76  delete socket;
77 }
78 
79 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 
82 {
83 
84  Element *property_element = el->FindElement("property");
85 
86  while (property_element) {
87  string property_str = property_element->GetDataLine();
88  FGPropertyNode* node = PropertyManager->GetNode(property_str);
89  if (!node) {
90  node = PropertyManager->GetNode("simulation/null");
91  }
92  OutputProperties.push_back(node);
93  property_element = el->FindNextElement("property");
94  }
95 
96  double outRate = 1.0;
97  if (!el->GetAttributeValue("rate").empty()) {
98  outRate = el->GetAttributeValueAsNumber("rate");
99  }
100  SetRateHz(outRate);
101 
102  SockPort = atoi(el->GetAttributeValue("port").c_str());
103  if (SockPort == 0) {
104  cerr << endl << "No port assigned for output." << endl;
105  return false;
106  }
107 
108  return true;
109 }
110 
111 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112 
114 {
115  if (FGOutputType::InitModel()) {
116  delete socket;
117  socket = new FGfdmSocket(SockName, SockPort, FGfdmSocket::ptUDP);
118 
119  if (socket == 0) return false;
120  if (!socket->GetConnectStatus()) return false;
121 
122  return true;
123  }
124 
125  return false;
126 }
127 
128 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
129 
131 {
132 
133  if (socket == 0) return;
134  if (!socket->GetConnectStatus()) return;
135 
136  socket->Clear();
137  socket->Append(FDMExec->GetSimTime());
138 
139  for (unsigned int i=0;i<OutputProperties.size();i++) {
140  socket->Append(OutputProperties[i]->getDoubleValue());
141  }
142 
143  socket->Send();
144 }
145 
146 }
std::string GetAttributeValue(const std::string &key)
Retrieves an attribute.
bool InitModel(void)
Initializes the instance.
virtual bool InitModel(void)
Init the output model according to its configitation.
Class wrapper for property handling.
STL namespace.
void Print(void)
Generates and sends the output datagram.
Element * FindElement(const std::string &el="")
Searches for a specified element.
bool SetDouble(const std::string &name, double val)
Set a double value for a property.
virtual bool Load(Element *el)
Init the output directives from an XML file.
FGPropertyManager * GetPropertyManager(void)
Returns a pointer to the property manager object.
Definition: FGFDMExec.cpp:1099
void SetRateHz(double rtHz)
Set the output rate for this output instances.
double GetAttributeValueAsNumber(const std::string &key)
Retrieves an attribute value as a double precision real number.
std::string GetDataLine(unsigned int i=0)
Gets a line of data belonging to an element.
Abstract class to provide functions generic to all the output directives.
Definition: FGOutputType.h:95
double GetSimTime(void) const
Returns the cumulative simulation time in seconds.
Definition: FGFDMExec.h:533
Element * FindNextElement(const std::string &el="")
Searches for the next element as specified.
Encapsulates the JSBSim simulation executive.
Definition: FGFDMExec.h:189
Encapsulates an object that enables JSBSim to communicate via socket (input and/or output)...
Definition: FGfdmSocket.h:89