]> git.mxchange.org Git - flightgear.git/blob - src/MultiPlayer/multiplaymgr.cxx
Fixed an property tieing issue on sim reset.
[flightgear.git] / src / MultiPlayer / multiplaymgr.cxx
1 //////////////////////////////////////////////////////////////////////
2 //
3 // multiplaymgr.cxx
4 //
5 // Written by Duncan McCreanor, started February 2003.
6 // duncan.mccreanor@airservicesaustralia.com
7 //
8 // Copyright (C) 2003  Airservices Australia
9 // Copyright (C) 2005  Oliver Schroeder
10 // Copyright (C) 2006  Mathias Froehlich
11 //
12 // This program is free software; you can redistribute it and/or
13 // modify it under the terms of the GNU General Public License as
14 // published by the Free Software Foundation; either version 2 of the
15 // License, or (at your option) any later version.
16 //
17 // This program is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 // General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with this program; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
25 //
26 // $Id$
27 //  
28 //////////////////////////////////////////////////////////////////////
29
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33
34 #include <iostream>
35 #include <algorithm>
36 #include <cstring>
37 #include <osg/Math>             // isNaN
38
39 #include <simgear/misc/stdint.hxx>
40 #include <simgear/timing/timestamp.hxx>
41 #include <simgear/debug/logstream.hxx>
42 #include <simgear/props/props.hxx>
43
44 #include <AIModel/AIManager.hxx>
45 #include <Main/fg_props.hxx>
46 #include "multiplaymgr.hxx"
47 #include "mpmessages.hxx"
48
49 using namespace std;
50
51 #define MAX_PACKET_SIZE 1200
52 #define MAX_TEXT_SIZE 128
53
54 // These constants are provided so that the ident 
55 // command can list file versions
56 const char sMULTIPLAYMGR_BID[] = "$Id$";
57 const char sMULTIPLAYMGR_HID[] = MULTIPLAYTXMGR_HID;
58
59 // A static map of protocol property id values to property paths,
60 // This should be extendable dynamically for every specific aircraft ...
61 // For now only that static list
62 const FGMultiplayMgr::IdPropertyList
63 FGMultiplayMgr::sIdPropertyList[] = {
64   {100, "surface-positions/left-aileron-pos-norm",  simgear::props::FLOAT},
65   {101, "surface-positions/right-aileron-pos-norm", simgear::props::FLOAT},
66   {102, "surface-positions/elevator-pos-norm",      simgear::props::FLOAT},
67   {103, "surface-positions/rudder-pos-norm",        simgear::props::FLOAT},
68   {104, "surface-positions/flap-pos-norm",          simgear::props::FLOAT},
69   {105, "surface-positions/speedbrake-pos-norm",    simgear::props::FLOAT},
70   {106, "gear/tailhook/position-norm",              simgear::props::FLOAT},
71   {107, "gear/launchbar/position-norm",             simgear::props::FLOAT},
72   {108, "gear/launchbar/state",                     simgear::props::STRING},
73   {109, "gear/launchbar/holdback-position-norm",    simgear::props::FLOAT},
74   {110, "canopy/position-norm",                     simgear::props::FLOAT},
75   {111, "surface-positions/wing-pos-norm",          simgear::props::FLOAT},
76   {112, "surface-positions/wing-fold-pos-norm",     simgear::props::FLOAT},
77
78   {200, "gear/gear[0]/compression-norm",           simgear::props::FLOAT},
79   {201, "gear/gear[0]/position-norm",              simgear::props::FLOAT},
80   {210, "gear/gear[1]/compression-norm",           simgear::props::FLOAT},
81   {211, "gear/gear[1]/position-norm",              simgear::props::FLOAT},
82   {220, "gear/gear[2]/compression-norm",           simgear::props::FLOAT},
83   {221, "gear/gear[2]/position-norm",              simgear::props::FLOAT},
84   {230, "gear/gear[3]/compression-norm",           simgear::props::FLOAT},
85   {231, "gear/gear[3]/position-norm",              simgear::props::FLOAT},
86   {240, "gear/gear[4]/compression-norm",           simgear::props::FLOAT},
87   {241, "gear/gear[4]/position-norm",              simgear::props::FLOAT},
88
89   {300, "engines/engine[0]/n1",  simgear::props::FLOAT},
90   {301, "engines/engine[0]/n2",  simgear::props::FLOAT},
91   {302, "engines/engine[0]/rpm", simgear::props::FLOAT},
92   {310, "engines/engine[1]/n1",  simgear::props::FLOAT},
93   {311, "engines/engine[1]/n2",  simgear::props::FLOAT},
94   {312, "engines/engine[1]/rpm", simgear::props::FLOAT},
95   {320, "engines/engine[2]/n1",  simgear::props::FLOAT},
96   {321, "engines/engine[2]/n2",  simgear::props::FLOAT},
97   {322, "engines/engine[2]/rpm", simgear::props::FLOAT},
98   {330, "engines/engine[3]/n1",  simgear::props::FLOAT},
99   {331, "engines/engine[3]/n2",  simgear::props::FLOAT},
100   {332, "engines/engine[3]/rpm", simgear::props::FLOAT},
101   {340, "engines/engine[4]/n1",  simgear::props::FLOAT},
102   {341, "engines/engine[4]/n2",  simgear::props::FLOAT},
103   {342, "engines/engine[4]/rpm", simgear::props::FLOAT},
104   {350, "engines/engine[5]/n1",  simgear::props::FLOAT},
105   {351, "engines/engine[5]/n2",  simgear::props::FLOAT},
106   {352, "engines/engine[5]/rpm", simgear::props::FLOAT},
107   {360, "engines/engine[6]/n1",  simgear::props::FLOAT},
108   {361, "engines/engine[6]/n2",  simgear::props::FLOAT},
109   {362, "engines/engine[6]/rpm", simgear::props::FLOAT},
110   {370, "engines/engine[7]/n1",  simgear::props::FLOAT},
111   {371, "engines/engine[7]/n2",  simgear::props::FLOAT},
112   {372, "engines/engine[7]/rpm", simgear::props::FLOAT},
113   {380, "engines/engine[8]/n1",  simgear::props::FLOAT},
114   {381, "engines/engine[8]/n2",  simgear::props::FLOAT},
115   {382, "engines/engine[8]/rpm", simgear::props::FLOAT},
116   {390, "engines/engine[9]/n1",  simgear::props::FLOAT},
117   {391, "engines/engine[9]/n2",  simgear::props::FLOAT},
118   {392, "engines/engine[9]/rpm", simgear::props::FLOAT},
119
120   {800, "rotors/main/rpm", simgear::props::FLOAT},
121   {801, "rotors/tail/rpm", simgear::props::FLOAT},
122   {810, "rotors/main/blade[0]/position-deg",  simgear::props::FLOAT},
123   {811, "rotors/main/blade[1]/position-deg",  simgear::props::FLOAT},
124   {812, "rotors/main/blade[2]/position-deg",  simgear::props::FLOAT},
125   {813, "rotors/main/blade[3]/position-deg",  simgear::props::FLOAT},
126   {820, "rotors/main/blade[0]/flap-deg",  simgear::props::FLOAT},
127   {821, "rotors/main/blade[1]/flap-deg",  simgear::props::FLOAT},
128   {822, "rotors/main/blade[2]/flap-deg",  simgear::props::FLOAT},
129   {823, "rotors/main/blade[3]/flap-deg",  simgear::props::FLOAT},
130   {830, "rotors/tail/blade[0]/position-deg",  simgear::props::FLOAT},
131   {831, "rotors/tail/blade[1]/position-deg",  simgear::props::FLOAT},
132
133   {900, "sim/hitches/aerotow/tow/length",                       simgear::props::FLOAT},
134   {901, "sim/hitches/aerotow/tow/elastic-constant",             simgear::props::FLOAT},
135   {902, "sim/hitches/aerotow/tow/weight-per-m-kg-m",            simgear::props::FLOAT},
136   {903, "sim/hitches/aerotow/tow/dist",                         simgear::props::FLOAT},
137   {904, "sim/hitches/aerotow/tow/connected-to-property-node",   simgear::props::BOOL},
138   {905, "sim/hitches/aerotow/tow/connected-to-ai-or-mp-callsign",   simgear::props::STRING},
139   {906, "sim/hitches/aerotow/tow/brake-force",                  simgear::props::FLOAT},
140   {907, "sim/hitches/aerotow/tow/end-force-x",                  simgear::props::FLOAT},
141   {908, "sim/hitches/aerotow/tow/end-force-y",                  simgear::props::FLOAT},
142   {909, "sim/hitches/aerotow/tow/end-force-z",                  simgear::props::FLOAT},
143   {930, "sim/hitches/aerotow/is-slave",                         simgear::props::BOOL},
144   {931, "sim/hitches/aerotow/speed-in-tow-direction",           simgear::props::FLOAT},
145   {932, "sim/hitches/aerotow/open",                             simgear::props::BOOL},
146   {933, "sim/hitches/aerotow/local-pos-x",                      simgear::props::FLOAT},
147   {934, "sim/hitches/aerotow/local-pos-y",                      simgear::props::FLOAT},
148   {935, "sim/hitches/aerotow/local-pos-z",                      simgear::props::FLOAT},
149
150   {1001, "controls/flight/slats",  simgear::props::FLOAT},
151   {1002, "controls/flight/speedbrake",  simgear::props::FLOAT},
152   {1003, "controls/flight/spoilers",  simgear::props::FLOAT},
153   {1004, "controls/gear/gear-down",  simgear::props::FLOAT},
154   {1005, "controls/lighting/nav-lights",  simgear::props::FLOAT},
155   {1006, "controls/armament/station[0]/jettison-all",  simgear::props::BOOL},
156
157   {1100, "sim/model/variant", simgear::props::INT},
158   {1101, "sim/model/livery/file", simgear::props::STRING},
159
160   {1200, "environment/wildfire/data", simgear::props::STRING},
161   {1201, "environment/contrail", simgear::props::INT},
162
163   {1300, "tanker", simgear::props::INT},
164
165   {10001, "sim/multiplay/transmission-freq-hz",  simgear::props::STRING},
166   {10002, "sim/multiplay/chat",  simgear::props::STRING},
167
168   {10100, "sim/multiplay/generic/string[0]", simgear::props::STRING},
169   {10101, "sim/multiplay/generic/string[1]", simgear::props::STRING},
170   {10102, "sim/multiplay/generic/string[2]", simgear::props::STRING},
171   {10103, "sim/multiplay/generic/string[3]", simgear::props::STRING},
172   {10104, "sim/multiplay/generic/string[4]", simgear::props::STRING},
173   {10105, "sim/multiplay/generic/string[5]", simgear::props::STRING},
174   {10106, "sim/multiplay/generic/string[6]", simgear::props::STRING},
175   {10107, "sim/multiplay/generic/string[7]", simgear::props::STRING},
176   {10108, "sim/multiplay/generic/string[8]", simgear::props::STRING},
177   {10109, "sim/multiplay/generic/string[9]", simgear::props::STRING},
178   {10110, "sim/multiplay/generic/string[10]", simgear::props::STRING},
179   {10111, "sim/multiplay/generic/string[11]", simgear::props::STRING},
180   {10112, "sim/multiplay/generic/string[12]", simgear::props::STRING},
181   {10113, "sim/multiplay/generic/string[13]", simgear::props::STRING},
182   {10114, "sim/multiplay/generic/string[14]", simgear::props::STRING},
183   {10115, "sim/multiplay/generic/string[15]", simgear::props::STRING},
184   {10116, "sim/multiplay/generic/string[16]", simgear::props::STRING},
185   {10117, "sim/multiplay/generic/string[17]", simgear::props::STRING},
186   {10118, "sim/multiplay/generic/string[18]", simgear::props::STRING},
187   {10119, "sim/multiplay/generic/string[19]", simgear::props::STRING},
188
189   {10200, "sim/multiplay/generic/float[0]", simgear::props::FLOAT},
190   {10201, "sim/multiplay/generic/float[1]", simgear::props::FLOAT},
191   {10202, "sim/multiplay/generic/float[2]", simgear::props::FLOAT},
192   {10203, "sim/multiplay/generic/float[3]", simgear::props::FLOAT},
193   {10204, "sim/multiplay/generic/float[4]", simgear::props::FLOAT},
194   {10205, "sim/multiplay/generic/float[5]", simgear::props::FLOAT},
195   {10206, "sim/multiplay/generic/float[6]", simgear::props::FLOAT},
196   {10207, "sim/multiplay/generic/float[7]", simgear::props::FLOAT},
197   {10208, "sim/multiplay/generic/float[8]", simgear::props::FLOAT},
198   {10209, "sim/multiplay/generic/float[9]", simgear::props::FLOAT},
199   {10210, "sim/multiplay/generic/float[10]", simgear::props::FLOAT},
200   {10211, "sim/multiplay/generic/float[11]", simgear::props::FLOAT},
201   {10212, "sim/multiplay/generic/float[12]", simgear::props::FLOAT},
202   {10213, "sim/multiplay/generic/float[13]", simgear::props::FLOAT},
203   {10214, "sim/multiplay/generic/float[14]", simgear::props::FLOAT},
204   {10215, "sim/multiplay/generic/float[15]", simgear::props::FLOAT},
205   {10216, "sim/multiplay/generic/float[16]", simgear::props::FLOAT},
206   {10217, "sim/multiplay/generic/float[17]", simgear::props::FLOAT},
207   {10218, "sim/multiplay/generic/float[18]", simgear::props::FLOAT},
208   {10219, "sim/multiplay/generic/float[19]", simgear::props::FLOAT},
209
210   {10300, "sim/multiplay/generic/int[0]", simgear::props::INT},
211   {10301, "sim/multiplay/generic/int[1]", simgear::props::INT},
212   {10302, "sim/multiplay/generic/int[2]", simgear::props::INT},
213   {10303, "sim/multiplay/generic/int[3]", simgear::props::INT},
214   {10304, "sim/multiplay/generic/int[4]", simgear::props::INT},
215   {10305, "sim/multiplay/generic/int[5]", simgear::props::INT},
216   {10306, "sim/multiplay/generic/int[6]", simgear::props::INT},
217   {10307, "sim/multiplay/generic/int[7]", simgear::props::INT},
218   {10308, "sim/multiplay/generic/int[8]", simgear::props::INT},
219   {10309, "sim/multiplay/generic/int[9]", simgear::props::INT},
220   {10310, "sim/multiplay/generic/int[10]", simgear::props::INT},
221   {10311, "sim/multiplay/generic/int[11]", simgear::props::INT},
222   {10312, "sim/multiplay/generic/int[12]", simgear::props::INT},
223   {10313, "sim/multiplay/generic/int[13]", simgear::props::INT},
224   {10314, "sim/multiplay/generic/int[14]", simgear::props::INT},
225   {10315, "sim/multiplay/generic/int[15]", simgear::props::INT},
226   {10316, "sim/multiplay/generic/int[16]", simgear::props::INT},
227   {10317, "sim/multiplay/generic/int[17]", simgear::props::INT},
228   {10318, "sim/multiplay/generic/int[18]", simgear::props::INT},
229   {10319, "sim/multiplay/generic/int[19]", simgear::props::INT}
230 };
231
232 const unsigned
233 FGMultiplayMgr::numProperties = (sizeof(FGMultiplayMgr::sIdPropertyList)
234                                  / sizeof(FGMultiplayMgr::sIdPropertyList[0]));
235
236 // Look up a property ID using binary search.
237 namespace
238 {
239   struct ComparePropertyId
240   {
241     bool operator()(const FGMultiplayMgr::IdPropertyList& lhs,
242                     const FGMultiplayMgr::IdPropertyList& rhs)
243     {
244       return lhs.id < rhs.id;
245     }
246     bool operator()(const FGMultiplayMgr::IdPropertyList& lhs,
247                     unsigned id)
248     {
249       return lhs.id < id;
250     }
251     bool operator()(unsigned id,
252                     const FGMultiplayMgr::IdPropertyList& rhs)
253     {
254       return id < rhs.id;
255     }
256   };
257     
258 }
259 const FGMultiplayMgr::IdPropertyList* FGMultiplayMgr::findProperty(unsigned id)
260 {
261   std::pair<const IdPropertyList*, const IdPropertyList*> result
262     = std::equal_range(sIdPropertyList, sIdPropertyList + numProperties, id,
263                        ComparePropertyId());
264   if (result.first == result.second) {
265     return 0;
266   } else {
267     return result.first;
268   }
269 }
270
271 namespace
272 {
273   bool verifyProperties(const xdr_data_t* data, const xdr_data_t* end)
274   {
275     using namespace simgear;
276     const xdr_data_t* xdr = data;
277     while (xdr < end) {
278       unsigned id = XDR_decode_uint32(*xdr);
279       const FGMultiplayMgr::IdPropertyList* plist
280         = FGMultiplayMgr::findProperty(id);
281     
282       if (plist) {
283         xdr++;
284         // How we decode the remainder of the property depends on the type
285         switch (plist->type) {
286         case props::INT:
287         case props::BOOL:
288         case props::LONG:
289           xdr++;
290           break;
291         case props::FLOAT:
292         case props::DOUBLE:
293           {
294             float val = XDR_decode_float(*xdr);
295             if (osg::isNaN(val))
296               return false;
297             xdr++;
298             break;
299           }
300         case props::STRING:
301         case props::UNSPECIFIED:
302           {
303             // String is complicated. It consists of
304             // The length of the string
305             // The string itself
306             // Padding to the nearest 4-bytes.
307             // XXX Yes, each byte is padded out to a word! Too late
308             // to change...
309             uint32_t length = XDR_decode_uint32(*xdr);
310             xdr++;
311             // Old versions truncated the string but left the length
312             // unadjusted.
313             if (length > MAX_TEXT_SIZE)
314               length = MAX_TEXT_SIZE;
315             xdr += length;
316             // Now handle the padding
317             while ((length % 4) != 0)
318               {
319                 xdr++;
320                 length++;
321                 //cout << "0";
322               }
323           }
324           break;
325         default:
326           // cerr << "Unknown Prop type " << id << " " << type << "\n";
327           xdr++;
328           break;
329         }            
330       }
331       else {
332         // give up; this is a malformed property list.
333         return false;
334       }
335     }
336     return true;
337   }
338 }
339 //////////////////////////////////////////////////////////////////////
340 //
341 //  MultiplayMgr constructor
342 //
343 //////////////////////////////////////////////////////////////////////
344 FGMultiplayMgr::FGMultiplayMgr() 
345 {
346   mSocket        = 0;
347   mInitialised   = false;
348   mHaveServer    = false;
349 } // FGMultiplayMgr::FGMultiplayMgr()
350 //////////////////////////////////////////////////////////////////////
351
352 //////////////////////////////////////////////////////////////////////
353 //
354 //  MultiplayMgr destructor
355 //
356 //////////////////////////////////////////////////////////////////////
357 FGMultiplayMgr::~FGMultiplayMgr() 
358 {
359   Close();
360 } // FGMultiplayMgr::~FGMultiplayMgr()
361 //////////////////////////////////////////////////////////////////////
362
363 //////////////////////////////////////////////////////////////////////
364 //
365 //  Initialise object
366 //
367 //////////////////////////////////////////////////////////////////////
368 void
369 FGMultiplayMgr::init (void) 
370 {
371   //////////////////////////////////////////////////
372   //  Initialise object if not already done
373   //////////////////////////////////////////////////
374   if (mInitialised) {
375     SG_LOG(SG_NETWORK, SG_WARN, "FGMultiplayMgr::init - already initialised");
376     return;
377   }
378   //////////////////////////////////////////////////
379   //  Set members from property values
380   //////////////////////////////////////////////////
381   short rxPort = fgGetInt("/sim/multiplay/rxport");
382   string rxAddress = fgGetString("/sim/multiplay/rxhost");
383   short txPort = fgGetInt("/sim/multiplay/txport");
384   string txAddress = fgGetString("/sim/multiplay/txhost");
385   mCallsign = fgGetString("/sim/multiplay/callsign");
386   if (txPort > 0 && !txAddress.empty()) {
387     mServer.set(txAddress.c_str(), txPort);
388     if (strncmp (mServer.getHost(), "0.0.0.0", 8) == 0) {
389       mHaveServer = false;
390       SG_LOG(SG_NETWORK, SG_DEBUG,
391         "FGMultiplayMgr - could not resolve '"
392         << txAddress << "', Multiplayermode disabled");
393     } else {
394       mHaveServer = true;
395     }
396     if (rxPort <= 0)
397       rxPort = txPort;
398   }
399   if (rxPort <= 0) {
400     SG_LOG(SG_NETWORK, SG_DEBUG,
401       "FGMultiplayMgr - No receiver port, Multiplayermode disabled");
402     return;
403   }
404   if (mCallsign.empty())
405     mCallsign = "JohnDoe"; // FIXME: use getpwuid
406   SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-txaddress= "<<txAddress);
407   SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-txport= "<<txPort );
408   SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-rxaddress="<<rxAddress );
409   SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-rxport= "<<rxPort);
410   SG_LOG(SG_NETWORK,SG_INFO,"FGMultiplayMgr::init-callsign= "<<mCallsign);
411   Close(); // Should Init be called twice, close Socket first
412            // A memory leak was reported here by valgrind
413   mSocket = new simgear::Socket();
414   if (!mSocket->open(false)) {
415     SG_LOG( SG_NETWORK, SG_DEBUG,
416             "FGMultiplayMgr::init - Failed to create data socket" );
417     return;
418   }
419   mSocket->setBlocking(false);
420   if (mSocket->bind(rxAddress.c_str(), rxPort) != 0) {
421     perror("bind");
422     SG_LOG( SG_NETWORK, SG_DEBUG,
423             "FGMultiplayMgr::Open - Failed to bind receive socket" );
424     return;
425   }
426   
427   mInitialised = true;
428 } // FGMultiplayMgr::init()
429 //////////////////////////////////////////////////////////////////////
430
431 //////////////////////////////////////////////////////////////////////
432 //
433 //  Closes and deletes the local player object. Closes
434 //  and deletes the tx socket. Resets the object state to unitialised.
435 //
436 //////////////////////////////////////////////////////////////////////
437 void
438 FGMultiplayMgr::Close (void) 
439 {
440   mMultiPlayerMap.clear();
441
442   if (mSocket) {
443     mSocket->close();
444     delete mSocket;
445     mSocket = 0;
446   }
447   mInitialised = false;
448 } // FGMultiplayMgr::Close(void)
449 //////////////////////////////////////////////////////////////////////
450
451 //////////////////////////////////////////////////////////////////////
452 //
453 //  Description: Sends the position data for the local position.
454 //
455 //////////////////////////////////////////////////////////////////////
456
457 /**
458  * The buffer that holds a multi-player message, suitably aligned.
459  */
460 union FGMultiplayMgr::MsgBuf
461 {
462     MsgBuf()
463     {
464         memset(&Msg, 0, sizeof(Msg));
465     }
466
467     T_MsgHdr* msgHdr()
468     {
469         return &Header;
470     }
471
472     const T_MsgHdr* msgHdr() const
473     {
474         return reinterpret_cast<const T_MsgHdr*>(&Header);
475     }
476
477     T_PositionMsg* posMsg()
478     {
479         return reinterpret_cast<T_PositionMsg*>(Msg + sizeof(T_MsgHdr));
480     }
481
482     const T_PositionMsg* posMsg() const
483     {
484         return reinterpret_cast<const T_PositionMsg*>(Msg + sizeof(T_MsgHdr));
485     }
486
487     xdr_data_t* properties()
488     {
489         return reinterpret_cast<xdr_data_t*>(Msg + sizeof(T_MsgHdr)
490                                              + sizeof(T_PositionMsg));
491     }
492
493     const xdr_data_t* properties() const
494     {
495         return reinterpret_cast<const xdr_data_t*>(Msg + sizeof(T_MsgHdr)
496                                                    + sizeof(T_PositionMsg));
497     }
498     /**
499      * The end of the properties buffer.
500      */
501     xdr_data_t* propsEnd()
502     {
503         return reinterpret_cast<xdr_data_t*>(Msg + MAX_PACKET_SIZE);
504     };
505
506     const xdr_data_t* propsEnd() const
507     {
508         return reinterpret_cast<const xdr_data_t*>(Msg + MAX_PACKET_SIZE);
509     };
510     /**
511      * The end of properties actually in the buffer. This assumes that
512      * the message header is valid.
513      */
514     xdr_data_t* propsRecvdEnd()
515     {
516         return reinterpret_cast<xdr_data_t*>(Msg + Header.MsgLen);
517     }
518
519     const xdr_data_t* propsRecvdEnd() const
520     {
521         return reinterpret_cast<const xdr_data_t*>(Msg + Header.MsgLen);
522     }
523     
524     xdr_data2_t double_val;
525     char Msg[MAX_PACKET_SIZE];
526     T_MsgHdr Header;
527 };
528
529 bool
530 FGMultiplayMgr::isSane(const FGExternalMotionData& motionInfo)
531 {
532     // check for corrupted data (NaNs)
533     bool isCorrupted = false;
534     isCorrupted |= ((osg::isNaN(motionInfo.time           )) ||
535                     (osg::isNaN(motionInfo.lag            )) ||
536                     (osg::isNaN(motionInfo.orientation(3) )));
537     for (unsigned i = 0; (i < 3)&&(!isCorrupted); ++i)
538     {
539         isCorrupted |= ((osg::isNaN(motionInfo.position(i)      ))||
540                         (osg::isNaN(motionInfo.orientation(i)   ))||
541                         (osg::isNaN(motionInfo.linearVel(i))    )||
542                         (osg::isNaN(motionInfo.angularVel(i))   )||
543                         (osg::isNaN(motionInfo.linearAccel(i))  )||
544                         (osg::isNaN(motionInfo.angularAccel(i)) ));
545     }
546     return !isCorrupted;
547 }
548
549 void
550 FGMultiplayMgr::SendMyPosition(const FGExternalMotionData& motionInfo)
551 {
552   if ((! mInitialised) || (! mHaveServer))
553         return;
554
555   if (! mHaveServer) {
556       SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::SendMyPosition - no server");
557       return;
558   }
559
560   if (!isSane(motionInfo))
561   {
562       // Current local data is invalid (NaN), so stop MP transmission.
563       // => Be nice to older FG versions (no NaN checks) and don't waste bandwidth.
564       SG_LOG(SG_NETWORK, SG_ALERT, "FGMultiplayMgr::SendMyPosition - "
565               << "Local data is invalid (NaN). Data not transmitted.");
566       return;
567   }
568
569   static MsgBuf msgBuf;
570   static unsigned msgLen = 0;
571   T_PositionMsg* PosMsg = msgBuf.posMsg();
572
573   strncpy(PosMsg->Model, fgGetString("/sim/model/path"), MAX_MODEL_NAME_LEN);
574   PosMsg->Model[MAX_MODEL_NAME_LEN - 1] = '\0';
575   if (fgGetBool("/sim/freeze/replay-state", true))
576   {
577       // do not send position updates during replay
578       for (unsigned i = 0 ; i < 3; ++i)
579       {
580           // no movement during replay
581           PosMsg->linearVel[i] = XDR_encode_float (0.0);
582           PosMsg->angularVel[i] = XDR_encode_float (0.0);
583           PosMsg->linearAccel[i] = XDR_encode_float (0.0);
584           PosMsg->angularAccel[i] = XDR_encode_float (0.0);
585       }
586       // all other data remains unchanged (resend last state) 
587   }
588   else
589   {
590       PosMsg->time = XDR_encode_double (motionInfo.time);
591       PosMsg->lag = XDR_encode_double (motionInfo.lag);
592       for (unsigned i = 0 ; i < 3; ++i)
593         PosMsg->position[i] = XDR_encode_double (motionInfo.position(i));
594       SGVec3f angleAxis;
595       motionInfo.orientation.getAngleAxis(angleAxis);
596       for (unsigned i = 0 ; i < 3; ++i)
597         PosMsg->orientation[i] = XDR_encode_float (angleAxis(i));
598       for (unsigned i = 0 ; i < 3; ++i)
599         PosMsg->linearVel[i] = XDR_encode_float (motionInfo.linearVel(i));
600       for (unsigned i = 0 ; i < 3; ++i)
601         PosMsg->angularVel[i] = XDR_encode_float (motionInfo.angularVel(i));
602       for (unsigned i = 0 ; i < 3; ++i)
603         PosMsg->linearAccel[i] = XDR_encode_float (motionInfo.linearAccel(i));
604       for (unsigned i = 0 ; i < 3; ++i)
605         PosMsg->angularAccel[i] = XDR_encode_float (motionInfo.angularAccel(i));
606
607       xdr_data_t* ptr = msgBuf.properties();
608       std::vector<FGPropertyData*>::const_iterator it;
609       it = motionInfo.properties.begin();
610       //cout << "OUTPUT PROPERTIES\n";
611       xdr_data_t* msgEnd = msgBuf.propsEnd();
612       while (it != motionInfo.properties.end() && ptr + 2 < msgEnd) {
613         
614         // First element is the ID. Write it out when we know we have room for
615         // the whole property.
616         xdr_data_t id =  XDR_encode_uint32((*it)->id);
617         // The actual data representation depends on the type
618         switch ((*it)->type) {
619           case simgear::props::INT:
620           case simgear::props::BOOL:
621           case simgear::props::LONG:
622             *ptr++ = id;
623             *ptr++ = XDR_encode_uint32((*it)->int_value);
624             //cout << "Prop:" << (*it)->id << " " << (*it)->type << " "<< (*it)->int_value << "\n";
625             break;
626           case simgear::props::FLOAT:
627           case simgear::props::DOUBLE:
628             *ptr++ = id;
629             *ptr++ = XDR_encode_float((*it)->float_value);
630             //cout << "Prop:" << (*it)->id << " " << (*it)->type << " "<< (*it)->float_value << "\n";
631             break;
632           case simgear::props::STRING:
633           case simgear::props::UNSPECIFIED:
634             {
635               // String is complicated. It consists of
636               // The length of the string
637               // The string itself
638               // Padding to the nearest 4-bytes.        
639               const char* lcharptr = (*it)->string_value;
640               
641               if (lcharptr != 0)
642               {
643                 // Add the length         
644                 ////cout << "String length: " << strlen(lcharptr) << "\n";
645                 uint32_t len = strlen(lcharptr);
646                 if (len > MAX_TEXT_SIZE)
647                   len = MAX_TEXT_SIZE;
648                 // XXX This should not be using 4 bytes per character!
649                 // If there's not enough room for this property, drop it
650                 // on the floor.
651                 if (ptr + 2 + ((len + 3) & ~3) > msgEnd)
652                     goto escape;
653                 //cout << "String length unint32: " << len << "\n";
654                 *ptr++ = id;
655                 *ptr++ = XDR_encode_uint32(len);
656                 if (len != 0)
657                 {
658                   // Now the text itself
659                   // XXX This should not be using 4 bytes per character!
660                   int lcount = 0;
661                   while ((*lcharptr != '\0') && (lcount < MAX_TEXT_SIZE)) 
662                   {
663                     *ptr++ = XDR_encode_int8(*lcharptr);
664                     lcharptr++;
665                     lcount++;          
666                   }
667     
668                   //cout << "Prop:" << (*it)->id << " " << (*it)->type << " " << len << " " << (*it)->string_value;
669     
670                   // Now pad if required
671                   while ((lcount % 4) != 0)
672                   {
673                     *ptr++ = XDR_encode_int8(0);
674                     lcount++;          
675                     //cout << "0";
676                   }
677                   
678                   //cout << "\n";
679                 }
680               }
681               else
682               {
683                 // Nothing to encode
684                 *ptr++ = id;
685                 *ptr++ = XDR_encode_uint32(0);
686                 //cout << "Prop:" << (*it)->id << " " << (*it)->type << " 0\n";
687               }
688             }
689             break;
690             
691           default:
692             //cout << " Unknown Type: " << (*it)->type << "\n";
693             *ptr++ = id;
694             *ptr++ = XDR_encode_float((*it)->float_value);;
695             //cout << "Prop:" << (*it)->id << " " << (*it)->type << " "<< (*it)->float_value << "\n";
696             break;
697         }
698             
699         ++it;
700       }
701   escape:
702       msgLen = reinterpret_cast<char*>(ptr) - msgBuf.Msg;
703       FillMsgHdr(msgBuf.msgHdr(), POS_DATA_ID, msgLen);
704   }
705   if (msgLen>0)
706       mSocket->sendto(msgBuf.Msg, msgLen, 0, &mServer);
707   SG_LOG(SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::SendMyPosition");
708 } // FGMultiplayMgr::SendMyPosition()
709
710 //////////////////////////////////////////////////////////////////////
711
712 //////////////////////////////////////////////////////////////////////
713 //
714 //  Name: SendTextMessage
715 //  Description: Sends a message to the player. The message must
716 //  contain a valid and correctly filled out header and optional
717 //  message body.
718 //
719 //////////////////////////////////////////////////////////////////////
720 void
721 FGMultiplayMgr::SendTextMessage(const string &MsgText)
722 {
723   if (!mInitialised || !mHaveServer)
724     return;
725
726   T_MsgHdr MsgHdr;
727   FillMsgHdr(&MsgHdr, CHAT_MSG_ID);
728   //////////////////////////////////////////////////
729   // Divide the text string into blocks that fit
730   // in the message and send the blocks.
731   //////////////////////////////////////////////////
732   unsigned iNextBlockPosition = 0;
733   T_ChatMsg ChatMsg;
734   
735   char Msg[sizeof(T_MsgHdr) + sizeof(T_ChatMsg)];
736   while (iNextBlockPosition < MsgText.length()) {
737     strncpy (ChatMsg.Text, 
738              MsgText.substr(iNextBlockPosition, MAX_CHAT_MSG_LEN - 1).c_str(),
739              MAX_CHAT_MSG_LEN);
740     ChatMsg.Text[MAX_CHAT_MSG_LEN - 1] = '\0';
741     memcpy (Msg, &MsgHdr, sizeof(T_MsgHdr));
742     memcpy (Msg + sizeof(T_MsgHdr), &ChatMsg, sizeof(T_ChatMsg));
743     mSocket->sendto (Msg, sizeof(T_MsgHdr) + sizeof(T_ChatMsg), 0, &mServer);
744     iNextBlockPosition += MAX_CHAT_MSG_LEN - 1;
745
746   }
747   
748   
749 } // FGMultiplayMgr::SendTextMessage ()
750 //////////////////////////////////////////////////////////////////////
751
752 //////////////////////////////////////////////////////////////////////
753 //
754 //  Name: ProcessData
755 //  Description: Processes data waiting at the receive socket. The
756 //  processing ends when there is no more data at the socket.
757 //  
758 //////////////////////////////////////////////////////////////////////
759 void
760 FGMultiplayMgr::update(double) 
761 {
762   if (!mInitialised)
763     return;
764
765   /// Just for expiry
766   long stamp = SGTimeStamp::now().getSeconds();
767
768   //////////////////////////////////////////////////
769   //  Read the receive socket and process any data
770   //////////////////////////////////////////////////
771   ssize_t bytes;
772   do {
773     MsgBuf msgBuf;
774     //////////////////////////////////////////////////
775     //  Although the recv call asks for 
776     //  MAX_PACKET_SIZE of data, the number of bytes
777     //  returned will only be that of the next
778     //  packet waiting to be processed.
779     //////////////////////////////////////////////////
780     simgear::IPAddress SenderAddress;
781     bytes = mSocket->recvfrom(msgBuf.Msg, sizeof(msgBuf.Msg), 0,
782                               &SenderAddress);
783     //////////////////////////////////////////////////
784     //  no Data received
785     //////////////////////////////////////////////////
786     if (bytes <= 0) {
787       if (errno != EAGAIN && errno != 0) // MSVC output "NoError" otherwise
788         perror("FGMultiplayMgr::MP_ProcessData");
789       break;
790     }
791     if (bytes <= static_cast<ssize_t>(sizeof(T_MsgHdr))) {
792       SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "
793               << "received message with insufficient data" );
794       break;
795     }
796     //////////////////////////////////////////////////
797     //  Read header
798     //////////////////////////////////////////////////
799     T_MsgHdr* MsgHdr = msgBuf.msgHdr();
800     MsgHdr->Magic       = XDR_decode_uint32 (MsgHdr->Magic);
801     MsgHdr->Version     = XDR_decode_uint32 (MsgHdr->Version);
802     MsgHdr->MsgId       = XDR_decode_uint32 (MsgHdr->MsgId);
803     MsgHdr->MsgLen      = XDR_decode_uint32 (MsgHdr->MsgLen);
804     MsgHdr->ReplyPort   = XDR_decode_uint32 (MsgHdr->ReplyPort);
805     MsgHdr->Callsign[MAX_CALLSIGN_LEN -1] = '\0';
806     if (MsgHdr->Magic != MSG_MAGIC) {
807       SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "
808               << "message has invalid magic number!" );
809       break;
810     }
811     if (MsgHdr->Version != PROTO_VER) {
812       SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "
813               << "message has invalid protocol number!" );
814       break;
815     }
816     if (MsgHdr->MsgLen != bytes) {
817       SG_LOG(SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "
818              << "message from " << MsgHdr->Callsign << " has invalid length!");
819       break;
820     }
821     //////////////////////////////////////////////////
822     //  Process messages
823     //////////////////////////////////////////////////
824     switch (MsgHdr->MsgId) {
825     case CHAT_MSG_ID:
826       ProcessChatMsg(msgBuf, SenderAddress);
827       break;
828     case POS_DATA_ID:
829       ProcessPosMsg(msgBuf, SenderAddress, stamp);
830       break;
831     case UNUSABLE_POS_DATA_ID:
832     case OLD_OLD_POS_DATA_ID:
833     case OLD_PROP_MSG_ID:
834     case OLD_POS_DATA_ID:
835       break;
836     default:
837       SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "
838               << "Unknown message Id received: " << MsgHdr->MsgId );
839       break;
840     }
841   } while (bytes > 0);
842
843   // check for expiry
844   MultiPlayerMap::iterator it = mMultiPlayerMap.begin();
845   while (it != mMultiPlayerMap.end()) {
846     if (it->second->getLastTimestamp() + 10 < stamp) {
847       std::string name = it->first;
848       it->second->setDie(true);
849       mMultiPlayerMap.erase(it);
850       it = mMultiPlayerMap.upper_bound(name);
851     } else
852       ++it;
853   }
854 } // FGMultiplayMgr::ProcessData(void)
855 //////////////////////////////////////////////////////////////////////
856
857 //////////////////////////////////////////////////////////////////////
858 //
859 //  handle a position message
860 //
861 //////////////////////////////////////////////////////////////////////
862 void
863 FGMultiplayMgr::ProcessPosMsg(const FGMultiplayMgr::MsgBuf& Msg,
864                               const simgear::IPAddress& SenderAddress, long stamp)
865 {
866   const T_MsgHdr* MsgHdr = Msg.msgHdr();
867   if (MsgHdr->MsgLen < sizeof(T_MsgHdr) + sizeof(T_PositionMsg)) {
868     SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "
869             << "Position message received with insufficient data" );
870     return;
871   }
872   const T_PositionMsg* PosMsg = Msg.posMsg();
873   FGExternalMotionData motionInfo;
874   motionInfo.time = XDR_decode_double(PosMsg->time);
875   motionInfo.lag = XDR_decode_double(PosMsg->lag);
876   for (unsigned i = 0; i < 3; ++i)
877     motionInfo.position(i) = XDR_decode_double(PosMsg->position[i]);
878   SGVec3f angleAxis;
879   for (unsigned i = 0; i < 3; ++i)
880     angleAxis(i) = XDR_decode_float(PosMsg->orientation[i]);
881   motionInfo.orientation = SGQuatf::fromAngleAxis(angleAxis);
882   for (unsigned i = 0; i < 3; ++i)
883     motionInfo.linearVel(i) = XDR_decode_float(PosMsg->linearVel[i]);
884   for (unsigned i = 0; i < 3; ++i)
885     motionInfo.angularVel(i) = XDR_decode_float(PosMsg->angularVel[i]);
886   for (unsigned i = 0; i < 3; ++i)
887     motionInfo.linearAccel(i) = XDR_decode_float(PosMsg->linearAccel[i]);
888   for (unsigned i = 0; i < 3; ++i)
889     motionInfo.angularAccel(i) = XDR_decode_float(PosMsg->angularAccel[i]);
890
891   // sanity check: do not allow injection of corrupted data (NaNs)
892   if (!isSane(motionInfo))
893   {
894       // drop this message, keep old position until receiving valid data
895       SG_LOG(SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::ProcessPosMsg - "
896               << "Position message with invalid data (NaN) received from "
897               << MsgHdr->Callsign);
898       return;
899   }
900
901   //cout << "INPUT MESSAGE\n";
902
903   // There was a bug in 1.9.0 and before: T_PositionMsg was 196 bytes
904   // on 32 bit architectures and 200 bytes on 64 bit, and this
905   // structure is put directly on the wire. By looking at the padding,
906   // we can sort through the mess, mostly:
907   // If padding is 0 (which is not a valid property type), then the
908   // message was produced by a new client or an old 64 bit client that
909   // happened to have 0 on the stack;
910   // Else if the property list starting with the padding word is
911   // well-formed, then the client is probably an old 32 bit client and
912   // we'll go with that;
913   // Else it is an old 64-bit client and properties start after the
914   // padding.
915   // There is a chance that we could be fooled by garbage in the
916   // padding looking like a valid property, so verifyProperties() is
917   // strict about the validity of the property values.
918   const xdr_data_t* xdr = Msg.properties();
919   if (PosMsg->pad != 0) {
920     if (verifyProperties(&PosMsg->pad, Msg.propsRecvdEnd()))
921       xdr = &PosMsg->pad;
922     else if (!verifyProperties(xdr, Msg.propsRecvdEnd()))
923       goto noprops;
924   }
925   while (xdr < Msg.propsRecvdEnd()) {
926     // simgear::props::Type type = simgear::props::UNSPECIFIED;
927     
928     // First element is always the ID
929     unsigned id = XDR_decode_uint32(*xdr);
930     //cout << pData->id << " ";
931     xdr++;
932     
933     // Check the ID actually exists and get the type
934     const IdPropertyList* plist = findProperty(id);
935     
936     if (plist)
937     {
938       FGPropertyData* pData = new FGPropertyData;
939       pData->id = id;
940       pData->type = plist->type;
941       // How we decode the remainder of the property depends on the type
942       switch (pData->type) {
943         case simgear::props::INT:
944         case simgear::props::BOOL:
945         case simgear::props::LONG:
946           pData->int_value = XDR_decode_uint32(*xdr);
947           xdr++;
948           //cout << pData->int_value << "\n";
949           break;
950         case simgear::props::FLOAT:
951         case simgear::props::DOUBLE:
952           pData->float_value = XDR_decode_float(*xdr);
953           xdr++;
954           //cout << pData->float_value << "\n";
955           break;
956         case simgear::props::STRING:
957         case simgear::props::UNSPECIFIED:
958           {
959             // String is complicated. It consists of
960             // The length of the string
961             // The string itself
962             // Padding to the nearest 4-bytes.    
963             uint32_t length = XDR_decode_uint32(*xdr);
964             xdr++;
965             //cout << length << " ";
966             // Old versions truncated the string but left the length unadjusted.
967             if (length > MAX_TEXT_SIZE)
968               length = MAX_TEXT_SIZE;
969             pData->string_value = new char[length + 1];
970             //cout << " String: ";
971             for (unsigned i = 0; i < length; i++)
972               {
973                 pData->string_value[i] = (char) XDR_decode_int8(*xdr);
974                 xdr++;
975                 //cout << pData->string_value[i];
976               }
977
978             pData->string_value[length] = '\0';
979
980             // Now handle the padding
981             while ((length % 4) != 0)
982               {
983                 xdr++;
984                 length++;
985                 //cout << "0";
986               }
987             //cout << "\n";
988           }
989           break;
990
991         default:
992           pData->float_value = XDR_decode_float(*xdr);
993           SG_LOG(SG_NETWORK, SG_DEBUG, "Unknown Prop type " << pData->id << " " << pData->type);
994           xdr++;
995           break;
996       }
997
998       motionInfo.properties.push_back(pData);
999     }
1000     else
1001     {
1002       // We failed to find the property. We'll try the next packet immediately.
1003       SG_LOG(SG_NETWORK, SG_INFO, "FGMultiplayMgr::ProcessPosMsg - "
1004              "message from " << MsgHdr->Callsign << " has unknown property id "
1005              << id); 
1006     }
1007   }
1008  noprops:
1009   FGAIMultiplayer* mp = getMultiplayer(MsgHdr->Callsign);
1010   if (!mp)
1011     mp = addMultiplayer(MsgHdr->Callsign, PosMsg->Model);
1012   mp->addMotionInfo(motionInfo, stamp);
1013 } // FGMultiplayMgr::ProcessPosMsg()
1014 //////////////////////////////////////////////////////////////////////
1015
1016 //////////////////////////////////////////////////////////////////////
1017 //
1018 //  handle a chat message
1019 //  FIXME: display chat message within flightgear
1020 //
1021 //////////////////////////////////////////////////////////////////////
1022 void
1023 FGMultiplayMgr::ProcessChatMsg(const MsgBuf& Msg,
1024                                const simgear::IPAddress& SenderAddress)
1025 {
1026   const T_MsgHdr* MsgHdr = Msg.msgHdr();
1027   if (MsgHdr->MsgLen < sizeof(T_MsgHdr) + 1) {
1028     SG_LOG( SG_NETWORK, SG_DEBUG, "FGMultiplayMgr::MP_ProcessData - "
1029             << "Chat message received with insufficient data" );
1030     return;
1031   }
1032   
1033   char *chatStr = new char[MsgHdr->MsgLen - sizeof(T_MsgHdr)];
1034   const T_ChatMsg* ChatMsg
1035       = reinterpret_cast<const T_ChatMsg *>(Msg.Msg + sizeof(T_MsgHdr));
1036   strncpy(chatStr, ChatMsg->Text,
1037           MsgHdr->MsgLen - sizeof(T_MsgHdr));
1038   chatStr[MsgHdr->MsgLen - sizeof(T_MsgHdr) - 1] = '\0';
1039   
1040   SG_LOG (SG_NETWORK, SG_WARN, "Chat [" << MsgHdr->Callsign << "]"
1041            << " " << chatStr);
1042
1043   delete [] chatStr;
1044 } // FGMultiplayMgr::ProcessChatMsg ()
1045 //////////////////////////////////////////////////////////////////////
1046
1047 void
1048 FGMultiplayMgr::FillMsgHdr(T_MsgHdr *MsgHdr, int MsgId, unsigned _len)
1049 {
1050   uint32_t len;
1051   switch (MsgId) {
1052   case CHAT_MSG_ID:
1053     len = sizeof(T_MsgHdr) + sizeof(T_ChatMsg);
1054     break;
1055   case POS_DATA_ID:
1056     len = _len;
1057     break;
1058   default:
1059     len = sizeof(T_MsgHdr);
1060     break;
1061   }
1062   MsgHdr->Magic           = XDR_encode_uint32(MSG_MAGIC);
1063   MsgHdr->Version         = XDR_encode_uint32(PROTO_VER);
1064   MsgHdr->MsgId           = XDR_encode_uint32(MsgId);
1065   MsgHdr->MsgLen          = XDR_encode_uint32(len);
1066   MsgHdr->ReplyAddress    = 0; // Are obsolete, keep them for the server for
1067   MsgHdr->ReplyPort       = 0; // now
1068   strncpy(MsgHdr->Callsign, mCallsign.c_str(), MAX_CALLSIGN_LEN);
1069   MsgHdr->Callsign[MAX_CALLSIGN_LEN - 1] = '\0';
1070 }
1071
1072 FGAIMultiplayer*
1073 FGMultiplayMgr::addMultiplayer(const std::string& callsign,
1074                                const std::string& modelName)
1075 {
1076   if (0 < mMultiPlayerMap.count(callsign))
1077     return mMultiPlayerMap[callsign].get();
1078
1079   FGAIMultiplayer* mp = new FGAIMultiplayer;
1080   mp->setPath(modelName.c_str());
1081   mp->setCallSign(callsign);
1082   mMultiPlayerMap[callsign] = mp;
1083
1084   FGAIManager *aiMgr = (FGAIManager*)globals->get_subsystem("ai_model");
1085   if (aiMgr) {
1086     aiMgr->attach(mp);
1087
1088     /// FIXME: that must follow the attach ATM ...
1089     for (unsigned i = 0; i < numProperties; ++i)
1090       mp->addPropertyId(sIdPropertyList[i].id, sIdPropertyList[i].name);
1091   }
1092
1093   return mp;
1094 }
1095
1096 FGAIMultiplayer*
1097 FGMultiplayMgr::getMultiplayer(const std::string& callsign)
1098 {
1099   if (0 < mMultiPlayerMap.count(callsign))
1100     return mMultiPlayerMap[callsign].get();
1101   else
1102     return 0;
1103 }