| 1 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 2 | |
| 3 | Module: FGUDPInputSocket.cpp |
| 4 | Author: Dave Culp |
| 5 | Date started: 02/19/15 |
| 6 | Purpose: Manage input of data from a UDP socket |
| 7 | Called by: FGInput |
| 8 | |
| 9 | ------------- Copyright (C) 2015 Dave 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 establishes a UDP socket and reads data from it. |
| 31 | |
| 32 | HISTORY |
| 33 | -------------------------------------------------------------------------------- |
| 34 | 02/19/15 DC Created |
| 35 | |
| 36 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 37 | INCLUDES |
| 38 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 39 | |
| 40 | #include <cstring> |
| 41 | #include <cstdlib> |
| 42 | #include <sstream> |
| 43 | |
| 44 | #include "FGUDPInputSocket.h" |
| 45 | #include "FGFDMExec.h" |
| 46 | #include "input_output/FGXMLElement.h" |
| 47 | |
| 48 | using namespace std; |
| 49 | |
| 50 | namespace JSBSim { |
| 51 | |
| 52 | /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 53 | CLASS IMPLEMENTATION |
| 54 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ |
| 55 | |
| 56 | FGUDPInputSocket::FGUDPInputSocket(FGFDMExec* fdmex) : |
| 57 | FGInputSocket(fdmex), rate(20), oldTimeStamp(0.0) |
| 58 | { |
| 59 | SockPort = 5139; |
| 60 | SockProtocol = FGfdmSocket::ptUDP; |
| 61 | } |
| 62 | |
| 63 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 64 | |
| 65 | bool FGUDPInputSocket::Load(Element* el) |
| 66 | { |
| 67 | if (!FGInputSocket::Load(el)) |
| 68 | return false; |
| 69 | |
| 70 | rate = atoi(el->GetAttributeValue("rate").c_str()); |
| 71 | SetRate(0.5 + 1.0/(FDMExec->GetDeltaT()*rate)); |
| 72 | |
| 73 | Element *property_element = el->FindElement("property"); |
| 74 | |
| 75 | while (property_element) { |
| 76 | string property_str = property_element->GetDataLine(); |
| 77 | FGPropertyNode* node = PropertyManager->GetNode(property_str); |
| 78 | if (!node) { |
| 79 | cerr << fgred << highint << endl << " No property by the name " |
| 80 | << property_str << " can be found." << reset << endl; |
| 81 | } else { |
| 82 | InputProperties.push_back(node); |
| 83 | } |
| 84 | property_element = el->FindNextElement("property"); |
| 85 | } |
| 86 | |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 91 | |
| 92 | void FGUDPInputSocket::Read(bool Holding) |
| 93 | { |
| 94 | if (socket == 0) return; |
| 95 | |
| 96 | data = socket->Receive(); |
| 97 | |
| 98 | if (!data.empty()) { |
| 99 | |
| 100 | vector<string> tokens; |
| 101 | stringstream ss(data); |
| 102 | string temp; |
| 103 | while (getline(ss, temp, ',')) { |
| 104 | tokens.push_back(temp); |
| 105 | } |
| 106 | |
| 107 | vector<double> values; |
| 108 | |
| 109 | for (string& token : tokens) { |
| 110 | if (is_number(trim(token))) { |
| 111 | try { |
| 112 | values.push_back(atof_locale_c(token)); |
| 113 | } catch(BaseException& e) { |
| 114 | return; |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | if (values[0] < oldTimeStamp) { |
| 120 | return; |
| 121 | } else { |
| 122 | oldTimeStamp = values[0]; |
| 123 | } |
| 124 | |
| 125 | // the zeroeth value is the time stamp |
| 126 | if ((values.size() - 1) != InputProperties.size()) { |
| 127 | cerr << endl << "Mismatch between UDP input property and value counts." << endl; |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | for (unsigned int i=1; i<values.size(); i++) { |
| 132 | InputProperties[i-1]->setDoubleValue(values[i]); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | } |