3 This file is part of R²C²
4 Copyright © 2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
8 #ifndef LIBR2C2_CENTRALSTATION_H_
9 #define LIBR2C2_CENTRALSTATION_H_
11 #include <msp/net/streamsocket.h>
12 #include <msp/time/timestamp.h>
17 class CentralStation: public Driver
28 operator bool() const;
33 typedef std::map<std::string, std::string> AttribMap;
34 typedef std::map<unsigned, AttribMap> ObjectMap;
40 operator bool() const;
46 NERROR_UNKNOWNCOMMAND = 10,
47 NERROR_UNKNOWNARG = 11,
48 NERROR_UNKNOWNPARAM = 12,
49 NERROR_UNKNOWNID = 15,
51 NERROR_NOTALLOWED = 22,
52 NERROR_NOCONTROL = 25,
103 typedef std::map<unsigned, unsigned> AddressMap;
104 typedef std::map<unsigned, Locomotive> LocoMap;
105 typedef std::map<unsigned, Turnout> TurnoutMap;
106 typedef std::map<unsigned, Sensor> SensorMap;
108 Msp::Net::StreamSocket socket;
109 std::list<std::string> cmd_queue;
110 unsigned pending_commands;
113 std::string in_buffer;
115 AddressMap loco_addr;
118 AddressMap turnout_addr;
119 bool turnouts_synced;
121 std::vector<unsigned> s88;
125 CentralStation(const std::string &);
128 virtual void set_power(bool);
129 virtual bool get_power() const { return power; }
130 virtual void halt(bool);
131 virtual bool is_halted() const { return halted; }
133 virtual const char *enumerate_protocols(unsigned) const;
134 virtual unsigned get_protocol_speed_steps(const std::string &) const;
136 virtual void add_loco(unsigned, const std::string &, const VehicleType &);
137 virtual void set_loco_speed(unsigned, unsigned);
138 virtual void set_loco_reverse(unsigned, bool);
139 virtual void set_loco_function(unsigned, unsigned, bool);
141 virtual void add_turnout(unsigned, const TrackType &);
142 virtual void set_turnout(unsigned, unsigned);
143 virtual unsigned get_turnout(unsigned) const;
145 virtual void add_sensor(unsigned);
146 virtual void set_sensor(unsigned, bool) { }
147 virtual bool get_sensor(unsigned) const;
150 virtual void flush();
153 void command(const std::string &, bool = false);
155 void process_reply(const Message &);
156 void process_event(const Message &);
157 void process_object(unsigned, const Message::AttribMap &);
158 Protocol map_protocol(const std::string &) const;
160 unsigned map_address(const std::map<unsigned, T> &, const AddressMap &, unsigned) const;
161 void skip(std::string::iterator &, const std::string::iterator &, const std::string &) const;
162 std::string parse_token(std::string::iterator &, const std::string::iterator &, const std::string &) const;
163 Tag parse_tag(std::string::iterator &, const std::string::iterator &) const;
164 Message parse_message(std::string::iterator &, const std::string::iterator &) const;