]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/centralstation.cpp
833560ae727a5839151105b625da6e363a76a662
[r2c2.git] / source / libr2c2 / centralstation.cpp
1 #include <algorithm>
2 #include <msp/core/refptr.h>
3 #include <msp/io/print.h>
4 #include <msp/net/resolve.h>
5 #include <msp/strings/utils.h>
6 #include <msp/time/units.h>
7 #include <msp/time/utils.h>
8 #include "centralstation.h"
9 #include "tracktype.h"
10 #include "vehicletype.h"
11
12 using namespace std;
13 using namespace Msp;
14
15 namespace R2C2 {
16
17 CentralStation::CentralStation(const string &host):
18         socket(Net::INET),
19         pending_commands(0),
20         power(false),
21         halted(false),
22         locos_synced(false),
23         accessories_synced(false),
24         sensors_synced(false)
25 {
26         RefPtr<Net::SockAddr> addr = Net::resolve(host+":15471");
27         socket.connect(*addr);
28
29         IO::print("Connected to central station at %s\n", addr->str());
30
31         command("get(1, status)");
32         command("request(1, view)");
33         command("queryObjects(10, addr, name)");
34         command("queryObjects(11, addr)");
35         command("queryObjects(26)");
36 }
37
38 CentralStation::~CentralStation()
39 {
40         command("release(1, view)", true);
41         for(LocoMap::iterator i=locos.begin(); (i!=locos.end() && !(i->first&0x10000)); ++i)
42                 command(format("release(%d, view, control)", i->first));
43         for(AccessoryMap::iterator i=accessories.begin(); (i!=accessories.end() && !(i->first&0x10000)); ++i)
44                 command(format("release(%d, view, control)", i->first));
45         while(IO::poll(socket, IO::P_INPUT, 100*Time::msec))
46                 while(receive()) ;
47 }
48
49 void CentralStation::set_power(bool p)
50 {
51         power = p;
52         command(format("set(1, %s)", (power ? "go" : "stop")));
53 }
54
55 void CentralStation::halt(bool h)
56 {
57         halted = h;
58         if(halted)
59         {
60                 for(LocoMap::iterator i=locos.begin(); i!=locos.end(); ++i)
61                         if(i->second.speed)
62                                 set_loco_speed(i->first, 0);
63         }
64
65         signal_halt.emit(halted);
66 }
67
68 const char *CentralStation::enumerate_protocols(unsigned index) const
69 {
70         if(index==MM)
71                 return "MM";
72         else if(index==MM_27)
73                 return "MM-27";
74         else if(index==MFX)
75                 return "MFX";
76         else
77                 return 0;
78 }
79
80 unsigned CentralStation::get_protocol_speed_steps(const string &name) const
81 {
82         switch(map_protocol(name))
83         {
84         case MM: return 14;
85         case MM_27: return 27;
86         case MFX: return 126;
87         default: return 0;
88         }
89 }
90
91 void CentralStation::add_loco(unsigned addr, const string &proto_name, const VehicleType &type)
92 {
93         Protocol proto = map_protocol(proto_name);
94
95         unsigned id = map_address(locos, loco_addr, addr);
96         if(!id)
97         {
98                 Locomotive &loco = locos[addr|0x10000];
99                 loco.name = type.get_name();
100                 loco.protocol = proto;
101                 loco.address = addr;
102
103                 const VehicleType::FunctionMap &type_funcs = type.get_functions();
104                 for(VehicleType::FunctionMap::const_iterator i=type_funcs.begin(); i!=type_funcs.end(); ++i)
105                         loco.func_mask |= 1<<i->first;
106
107                 if(locos_synced && proto!=MFX)
108                         command("create(10)");
109         }
110         else
111                 command(format("request(%d, view, control, force)", id));
112 }
113
114 void CentralStation::set_loco_speed(unsigned addr, unsigned speed)
115 {
116         if(speed && halted)
117                 return;
118
119         unsigned id = map_address(locos, loco_addr, addr);
120         if(id)
121         {
122                 Locomotive &loco = locos[id];
123                 if(loco.protocol==MFX && speed)
124                         ++speed;
125                 command(format("set(%d, speedstep[%d])", id, speed));
126         }
127 }
128
129 void CentralStation::set_loco_reverse(unsigned addr, bool rev)
130 {
131         unsigned id = map_address(locos, loco_addr, addr);
132         if(id)
133                 command(format("set(%d, dir[%d])", id, rev));
134 }
135
136 void CentralStation::set_loco_function(unsigned addr, unsigned func, bool state)
137 {
138         unsigned id = map_address(locos, loco_addr, addr);
139         if(id)
140                 command(format("set(%d, func[%d, %d])", id, func, state));
141 }
142
143 void CentralStation::add_turnout(unsigned addr, const TrackType &type)
144 {
145         unsigned straight = type.get_paths();
146         bool left = false;
147         bool right = false;
148         bool cross = false;
149
150         const vector<TrackPart> &parts = type.get_parts();
151         for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
152         {
153                 TrackPoint start = i->get_point(0);
154                 TrackPoint end = i->get_point(i->get_length());
155                 if(end.dir>start.dir+0.01 || end.dir<start.dir-0.01)
156                 {
157                         (end.dir>start.dir ? left : right) = true;
158                         straight &= ~(1<<i->get_path());
159                 }
160                 else if(start.dir<-0.01 || start.dir>0.01)
161                         cross = true;
162         }
163
164         MagnetAccessory::Symbol symbol = MagnetAccessory::TURNOUT_LEFT;
165         if(cross)
166                 symbol = MagnetAccessory::TURNOUT_DOUBLESLIP;
167         else if(left && right)
168                 symbol = MagnetAccessory::TURNOUT_THREEWAY;
169         else if(left)
170                 symbol = (straight ? MagnetAccessory::TURNOUT_LEFT : MagnetAccessory::TURNOUT_CURVED_LEFT);
171         else if(right)
172                 symbol = (straight ? MagnetAccessory::TURNOUT_RIGHT : MagnetAccessory::TURNOUT_CURVED_RIGHT);
173
174         MagnetAccessory &turnout = add_accessory(addr, MagnetAccessory::TURNOUT, symbol);
175         turnout.bits = type.get_state_bits();
176 }
177
178 void CentralStation::set_turnout(unsigned addr, unsigned state)
179 {
180         set_accessory_state(addr, MagnetAccessory::TURNOUT, state);
181 }
182
183 unsigned CentralStation::get_turnout(unsigned addr) const
184 {
185         return get_accessory_state(addr, MagnetAccessory::TURNOUT);
186 }
187
188 CentralStation::MagnetAccessory &CentralStation::add_accessory(unsigned addr, MagnetAccessory::Type type, MagnetAccessory::Symbol symbol)
189 {
190         unsigned id = map_address(accessories, accessory_addr, addr);
191         if(!id)
192         {
193                 id = addr|0x10000;
194
195                 MagnetAccessory &accessory = accessories[id];
196                 accessory.address = addr;
197                 accessory.type = type;
198                 accessory.symbol = symbol;
199
200                 accessory_addr[addr] = id;
201
202                 if(accessories_synced)
203                         command("create(11, append)");
204
205                 return accessory;
206         }
207         else
208         {
209                 MagnetAccessory &accessory = accessories[id];
210                 command(format("request(%d, view, control)", id));
211                 if(accessory.symbol!=symbol)
212                         command(format("set(%d, symbol[%d])", symbol));
213
214                 return accessory;
215         }
216 }
217
218 void CentralStation::set_accessory_state(unsigned addr, MagnetAccessory::Type type, unsigned state)
219 {
220         unsigned id = map_address(accessories, accessory_addr, addr);
221         if(id)
222         {
223                 MagnetAccessory &accessory = accessories[id];
224                 if(accessory.type!=type)
225                         throw logic_error("accessory type conflict");
226
227                 unsigned mask = (1<<accessory.bits)-1;
228
229                 if(((state^accessory.state)&mask)==0 || !accessory.synced)
230                 {
231                         accessory.state = state;
232                         accessory_state_changed(accessory);
233                         return;
234                 }
235
236                 accessory.state = (accessory.state&mask) | (state&~mask);
237
238                 command(format("set(%d, state[%d])", id, state&mask));
239         }
240 }
241
242 unsigned CentralStation::get_accessory_state(unsigned addr, MagnetAccessory::Type type) const
243 {
244         unsigned id = map_address(accessories, accessory_addr, addr);
245         if(id)
246         {
247                 AccessoryMap::const_iterator i = accessories.find(id);
248                 if(i!=accessories.end() && i->second.type==type)
249                         return i->second.state;
250         }
251         return 0;
252 }
253
254 void CentralStation::accessory_state_changed(const MagnetAccessory &accessory) const
255 {
256         if(accessory.type==MagnetAccessory::TURNOUT)
257                 signal_turnout.emit(accessory.address, accessory.state);
258 }
259
260 void CentralStation::add_sensor(unsigned addr)
261 {
262         sensors.insert(SensorMap::value_type(addr, Sensor()));
263
264         if(sensors_synced)
265         {
266                 if(addr>s88.size()*16)
267                         command("create(26, add[0])");
268         }
269 }
270
271 bool CentralStation::get_sensor(unsigned addr) const
272 {
273         SensorMap::const_iterator i = sensors.find(addr);
274         if(i!=sensors.end())
275                 return i->second.state;
276         return false;
277 }
278
279 void CentralStation::tick()
280 {
281         while(Message msg = receive())
282         {
283                 if(msg.footer.code)
284                         IO::print("\033[31m*** ERROR: %s: %d %s ***\033[0m\n", msg.header.value, msg.footer.code, msg.footer.value);
285
286                 if(msg.header.type=="REPLY")
287                         process_reply(msg);
288                 else if(msg.header.type=="EVENT")
289                         process_event(msg);
290         }
291 }
292
293 void CentralStation::flush()
294 {
295 }
296
297 void CentralStation::command(const string &cmd, bool force)
298 {
299         if(pending_commands<10 || force)
300         {
301                 socket.write(cmd+"\r\n");
302                 ++pending_commands;
303         }
304         else
305                 cmd_queue.push_back(cmd);
306 }
307
308 CentralStation::Message CentralStation::receive()
309 {
310         while(IO::poll(socket, IO::P_INPUT, Time::zero))
311         {
312                 char rbuf[1024];
313                 unsigned len = socket.read(rbuf, sizeof(rbuf));
314                 if(!len)
315                         return Message();
316
317                 in_buffer.append(rbuf, len);
318         }
319
320         if(!in_buffer.empty())
321         {
322                 string::iterator iter = in_buffer.begin();
323                 if(Message msg = parse_message(iter, in_buffer.end()))
324                 {
325                         skip(iter, in_buffer.end(), "\r\n");
326                         in_buffer.erase(in_buffer.begin(), iter);
327
328                         if(msg.header.type=="REPLY" && pending_commands>0)
329                         {
330                                 --pending_commands;
331                                 if(!cmd_queue.empty())
332                                 {
333                                         command(cmd_queue.front());
334                                         cmd_queue.pop_front();
335                                 }
336                         }
337
338                         return msg;
339                 }
340         }
341
342         return Message();
343 }
344
345 void CentralStation::process_reply(const Message &msg)
346 {
347         if(!msg.header.value.compare(0, 4, "get("))
348         {
349                 for(Message::ObjectMap::const_iterator i=msg.content.begin(); i!=msg.content.end(); ++i)
350                 {
351                         if(accessories.count(i->first))
352                                 accessories[i->first].synced = true;
353
354                         process_object(i->first, i->second);
355                 }
356         }
357         else if(!msg.header.value.compare(0, 16, "queryObjects(10,"))
358         {
359                 for(Message::ObjectMap::const_iterator i=msg.content.begin(); i!=msg.content.end(); ++i)
360                 {
361                         LocoMap::iterator j = locos.find(i->first);
362                         if(j==locos.end())
363                         {
364                                 bool found = false;
365                                 Message::AttribMap::const_iterator k = i->second.find("addr");
366                                 if(k!=i->second.end())
367                                 {
368                                         unsigned addr = lexical_cast<unsigned>(k->second);
369
370                                         j = locos.find(addr|0x10000);
371                                         if(j!=locos.end())
372                                         {
373                                                 command(format("request(%d, view, control, force)", i->first));
374                                                 string cmd = format("get(%d, dir", i->first);
375                                                 for(unsigned l=0; j->second.func_mask>>l; ++l)
376                                                         if((j->second.func_mask>>l)&1)
377                                                                 cmd += format(", func[%d]", l);
378                                                 cmd += ')';
379                                                 command(cmd);
380
381                                                 locos.insert(LocoMap::value_type(i->first, j->second));
382                                                 locos.erase(j);
383
384                                                 found = true;
385                                         }
386                                 }
387
388                                 if(!found)
389                                         locos.insert(LocoMap::value_type(i->first, Locomotive()));
390                         }
391
392                         process_object(i->first, i->second);
393                 }
394
395                 locos_synced = true;
396
397                 if(locos.lower_bound(0x10000)!=locos.end())
398                         command("create(10)");
399         }
400         else if(!msg.header.value.compare(0, 16, "queryObjects(11,"))
401         {
402                 for(Message::ObjectMap::const_iterator i=msg.content.begin(); i!=msg.content.end(); ++i)
403                 {
404                         AccessoryMap::iterator j = accessories.find(i->first);
405                         if(j==accessories.end())
406                         {
407                                 bool found = false;
408                                 Message::AttribMap::const_iterator k = i->second.find("addr");
409                                 if(k!=i->second.end())
410                                 {
411                                         unsigned addr = lexical_cast<unsigned>(k->second);
412
413                                         j = accessories.find(addr|0x10000);
414                                         if(j!=accessories.end())
415                                         {
416                                                 command(format("request(%d, view, control)", i->first));
417                                                 command(format("set(%d, symbol[%d])", i->first, j->second.symbol));
418                                                 command(format("get(%d, state)", i->first));
419
420                                                 accessories.insert(AccessoryMap::value_type(i->first, j->second));
421                                                 accessories.erase(j);
422
423                                                 found = true;
424                                         }
425                                 }
426
427                                 if(!found)
428                                         accessories.insert(AccessoryMap::value_type(i->first, MagnetAccessory()));
429                         }
430
431                         process_object(i->first, i->second);
432                 }
433
434                 accessories_synced = true;
435
436                 for(AccessoryMap::const_iterator i=accessories.lower_bound(0x10000); i!=accessories.end(); ++i)
437                         command("create(11, append)");
438         }
439         else if(msg.header.value=="queryObjects(26)")
440         {
441                 s88.clear();
442                 for(Message::ObjectMap::const_iterator i=msg.content.begin(); i!=msg.content.end(); ++i)
443                 {
444                         s88.push_back(i->first);
445                         command(format("request(%d, view)", i->first));
446                         command(format("get(%d, state)", i->first));
447                 }
448
449                 sensors_synced = true;
450
451                 if(!sensors.empty())
452                 {
453                         unsigned high_addr = (--sensors.end())->first;
454                         if(high_addr>16*s88.size())
455                                 command("create(26, add[0])");
456                 }
457         }
458         else if(msg.header.value=="create(10)")
459         {
460                 Message::ObjectMap::const_iterator i = msg.content.find(10);
461                 if(i!=msg.content.end())
462                 {
463                         Message::AttribMap::const_iterator j = i->second.find("id");
464                         if(j!=i->second.end())
465                         {
466                                 unsigned id = lexical_cast<unsigned>(j->second);
467                                 LocoMap::iterator k = locos.lower_bound(0x10000);
468                                 if(k!=locos.end())
469                                 {
470                                         command(format("request(%d, view, control)", id));
471                                         command(format("set(%d, addr[%d], protocol[%s], name[\"%s\"])",
472                                                 id, k->second.address, (k->second.protocol==MM_27 ? "MM27" : "MM14"), k->second.name));
473                                         command("create(10, append)");
474
475                                         locos.insert(LocoMap::value_type(id, k->second));
476                                         locos.erase(k);
477                                 }
478                         }
479                 }
480
481                 if(locos.lower_bound(0x10000)!=locos.end())
482                         command("create(10)");
483         }
484         else if(!msg.header.value.compare(0, 10, "create(11,"))
485         {
486                 Message::ObjectMap::const_iterator i = msg.content.find(11);
487                 if(i!=msg.content.end())
488                 {
489                         Message::AttribMap::const_iterator j = i->second.find("id");
490                         if(j!=i->second.end())
491                         {
492                                 unsigned id = lexical_cast<unsigned>(j->second);
493                                 AccessoryMap::iterator k = accessories.lower_bound(0x10000);
494                                 if(k!=accessories.end())
495                                 {
496                                         command(format("request(%d, view, control)", id));
497                                         command(format("set(%d, addr[%d], symbol[%d], name1[\"Switch\"], name2[\"%d\"], name3[\"\"])",
498                                                 id, k->second.address, k->second.symbol, k->second.address));
499                                         command(format("set(%d, state[%d])", id, k->second.state&((1<<k->second.bits)-1)));
500
501                                         k->second.synced = true;
502                                         accessories.insert(AccessoryMap::value_type(id, k->second));
503                                         accessories.erase(k);
504                                 }
505                         }
506                 }
507         }
508         else if(!msg.header.value.compare(0, 10, "create(26,"))
509                 command("queryObjects(26)");
510 }
511
512 void CentralStation::process_event(const Message &msg)
513 {
514         for(Message::ObjectMap::const_iterator i=msg.content.begin(); i!=msg.content.end(); ++i)
515                 process_object(i->first, i->second);
516 }
517
518 void CentralStation::process_object(unsigned id, const Message::AttribMap &attribs)
519 {
520         if(id==1)
521         {
522                 for(Message::AttribMap::const_iterator i=attribs.begin(); i!=attribs.end(); ++i)
523                         if(i->first=="status")
524                         {
525                                 power = (i->second=="GO");
526                                 signal_power.emit(power);
527                         }
528         }
529         else if(locos.count(id))
530         {
531                 Locomotive &loco = locos[id];
532                 bool speed_changed = false;
533                 unsigned funcs_changed = 0;
534                 for(Message::AttribMap::const_iterator i=attribs.begin(); i!=attribs.end(); ++i)
535                 {
536                         if(i->first=="name")
537                                 loco.name = i->second.substr(1, i->second.size()-2);
538                         else if(i->first=="addr")
539                         {
540                                 loco_addr.erase(loco.address);
541                                 loco.address = lexical_cast<unsigned>(i->second);
542                                 loco_addr[loco.address] = id;
543                         }
544                         else if(i->first=="protocol")
545                         {
546                                 if(i->second=="MM")
547                                         loco.protocol = MM;
548                                 else if(i->second=="MM27")
549                                         loco.protocol = MM_27;
550                                 else if(i->second=="MFX")
551                                         loco.protocol = MFX;
552                         }
553                         else if(i->first=="speedstep")
554                         {
555                                 loco.speed = lexical_cast<unsigned>(i->second);
556                                 if(loco.protocol==MFX && loco.speed)
557                                         --loco.speed;
558                                 speed_changed = true;
559                         }
560                         else if(i->first=="dir")
561                         {
562                                 loco.reverse = i->second[0]!='0';
563                                 speed_changed = true;
564                         }
565                         else if(i->first=="func")
566                         {
567                                 vector<string> parts = split(i->second, ", ");
568                                 unsigned func = lexical_cast<unsigned>(parts[0]);
569                                 bool value = lexical_cast<unsigned>(parts[1]);
570                                 loco.funcs &= ~(1<<func);
571                                 if(value)
572                                         loco.funcs |= 1<<func;
573                                 funcs_changed |= 1<<func;
574                         }
575                         else if(i->first=="msg")
576                         {
577                                 if(i->second=="CONTROL_LOST")
578                                         command(format("request(%d, control, force)", id));
579                         }
580                 }
581
582                 if(speed_changed)
583                         signal_loco_speed.emit(loco.address, loco.speed, loco.reverse);
584                 for(unsigned i=0; funcs_changed>>i; ++i)
585                         if(funcs_changed&(1<<i))
586                                 signal_loco_function.emit(loco.address, i, loco.funcs&(1<<i));
587         }
588         else if(accessories.count(id))
589         {
590                 MagnetAccessory &accessory = accessories[id];
591                 bool state_changed = false;
592                 for(Message::AttribMap::const_iterator i=attribs.begin(); i!=attribs.end(); ++i)
593                 {
594                         if(i->first=="addr")
595                         {
596                                 accessory_addr.erase(accessory.address);
597                                 accessory.address = lexical_cast<unsigned>(i->second);
598                                 accessory_addr[accessory.address] = id;
599                         }
600                         else if(i->first=="state")
601                         {
602                                 unsigned state = lexical_cast<unsigned>(i->second);
603                                 unsigned mask = (1<<accessory.bits)-1;
604                                 accessory.state = (accessory.state&~mask) | (state&mask);
605                                 state_changed = true;
606                         }
607                 }
608
609                 if(state_changed)
610                         accessory_state_changed(accessory);
611         }
612         else if(find(s88.begin(), s88.end(), id)!=s88.end())
613         {
614                 unsigned base = 0;
615                 for(; (base<s88.size() && s88[base]!=id); ++base) ;
616
617                 for(Message::AttribMap::const_iterator i=attribs.begin(); i!=attribs.end(); ++i)
618                 {
619                         if(i->first=="state")
620                         {
621                                 unsigned state = lexical_cast<unsigned>(i->second, "%i");
622                                 for(unsigned j=0; j<16; ++j)
623                                 {
624                                         unsigned addr = base*16+j+1;
625                                         Sensor &sensor = sensors[addr];
626                                         bool s = state&(1<<j);
627                                         if(s!=sensor.state)
628                                         {
629                                                 sensor.state = s;
630                                                 signal_sensor.emit(addr, sensor.state);
631                                         }
632                                 }
633                         }
634                 }
635         }
636 }
637
638 CentralStation::Protocol CentralStation::map_protocol(const string &name) const
639 {
640         if(name=="MM")
641                 return MM;
642         else if(name=="MM-27")
643                 return MM_27;
644         else if(name=="MFX")
645                 return MFX;
646         else
647                 throw invalid_argument("CentralStation::map_protocol");
648 }
649
650 template<typename T>
651 unsigned CentralStation::map_address(const map<unsigned, T> &omap, const AddressMap &amap, unsigned addr) const
652 {
653         if(omap.count(addr))
654                 return addr;
655         else
656         {
657                 AddressMap::const_iterator i = amap.find(addr);
658                 if(i!=amap.end())
659                         return i->second;
660                 else
661                         return 0;
662         }
663 }
664
665 void CentralStation::skip(string::iterator &iter, const string::iterator &end, const string &what) const
666 {
667         for(; (iter!=end && what.find(*iter)!=string::npos); ++iter) ;
668 }
669
670 string CentralStation::parse_token(string::iterator &iter, const string::iterator &end, const string &stop) const
671 {
672         vector<char> parens;
673         bool quote = false;
674         string token;
675
676         skip(iter, end, stop);
677
678         for(; iter!=end; ++iter)
679         {
680                 if(stop.find(*iter)!=string::npos && parens.empty() && !quote)
681                         break;
682                 else if(*iter=='(' || *iter=='[')
683                         parens.push_back(*iter);
684                 else if((*iter==')' || *iter==']') && !parens.empty())
685                 {
686                         if((*iter==')' && parens.back()!='(') || (*iter==']' && parens.back()!='['))
687                                 IO::print("Mismatched parentheses\n");
688                         parens.pop_back();
689                 }
690                 else if(*iter=='"')
691                         quote = !quote;
692
693                 token += *iter;
694         }
695
696         return token;
697 }
698
699 CentralStation::Tag CentralStation::parse_tag(string::iterator &iter, const string::iterator &end) const
700 {
701         Tag tag;
702
703         for(; (iter!=end && *iter!='<'); ++iter) ;
704         if(iter==end)
705                 return Tag();
706
707         tag.type = parse_token(++iter, end, " >");
708         if(tag.type=="END")
709         {
710                 string code = parse_token(iter, end, " >");
711                 tag.code = lexical_cast<unsigned>(code);
712         }
713         skip(iter, end, " ");
714         tag.value = parse_token(iter, end, ">");
715         if(iter==end)
716                 return Tag();
717         ++iter;
718
719         return tag;
720 }
721
722 CentralStation::Message CentralStation::parse_message(string::iterator &iter, const string::iterator &end) const
723 {
724         Message msg;
725
726         msg.header = parse_tag(iter, end);
727
728         while(iter!=end)
729         {
730                 skip(iter, end, "\r\n");
731                 if(*iter=='<')
732                         break;
733
734                 string id = parse_token(iter, end, " \r\n<");
735                 Message::AttribMap &attribs = msg.content[lexical_cast<unsigned>(id)];
736                 while(iter!=end && *iter!='\n' && *iter!='\r')
737                 {
738                         string attr = parse_token(iter, end, " \r\n<");
739                         string::size_type open_bracket = attr.find('[');
740                         if(open_bracket!=string::npos)
741                         {
742                                 string::size_type close_bracket = attr.rfind(']');
743                                 string attr_name = attr.substr(0, open_bracket);
744                                 string attr_value = attr.substr(open_bracket+1, close_bracket-open_bracket-1);
745                                 attribs.insert(Message::AttribMap::value_type(attr_name, attr_value));
746                         }
747                         else
748                                 attribs.insert(Message::AttribMap::value_type(attr, string()));
749                 }
750         }
751
752         msg.footer = parse_tag(iter, end);
753         if(msg.footer.type.empty())
754                 return Message();
755
756         return msg;
757 }
758
759
760 CentralStation::Tag::Tag():
761         code(0)
762 { }
763
764 CentralStation::Tag::operator bool() const
765 {
766         return !type.empty();
767 }
768
769
770 CentralStation::Message::operator bool() const
771 {
772         return header && footer;
773 }
774
775
776 CentralStation::Locomotive::Locomotive():
777         address(0),
778         speed(0),
779         reverse(false),
780         func_mask(0),
781         funcs(0),
782         control(false)
783 { }
784
785
786 CentralStation::MagnetAccessory::MagnetAccessory():
787         address(0),
788         type(TURNOUT),
789         symbol(TURNOUT_LEFT),
790         state(0),
791         bits(1),
792         synced(false)
793 { }
794
795
796 CentralStation::Sensor::Sensor():
797         state(false)
798 { }
799
800 } // namespace R2C2