#include <msp/net/inet.h>
#include "libmarklin/control.h"
+#include "libmarklin/layout.h"
#include "libmarklin/locomotive.h"
#include "libmarklin/locotype.h"
+#include "libmarklin/route.h"
#include "server.h"
using namespace std;
{
const list<Train *> &trains = trfc_mgr.get_trains();
for(list<Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
- {
- Locomotive &loco = (*i)->get_locomotive();
- (*i)->signal_target_speed_changed.connect(sigc::bind<0>(sigc::mem_fun(this, &Server::train_speed_changed), sigc::ref(**i)));
- loco.signal_function_changed.connect(sigc::bind<0>(sigc::mem_fun(this, &Server::train_function_changed), sigc::ref(**i)));
- (*i)->signal_status_changed.connect(sigc::bind<0>(sigc::mem_fun(this, &Server::train_status_changed), sigc::ref(**i)));
- }
+ train_added(**i);
listen_sock.listen(Net::InetAddr(0, 8315), 4);
listen_sock.signal_data_available.connect(sigc::mem_fun(this, &Server::incoming_connection));
{
Locomotive &loco = train.get_locomotive();
train.signal_target_speed_changed.connect(sigc::bind<0>(sigc::mem_fun(this, &Server::train_speed_changed), sigc::ref(train)));
+ loco.signal_reverse_changed.connect(sigc::bind<0>(sigc::mem_fun(this, &Server::train_reverse_changed), sigc::ref(train)));
loco.signal_function_changed.connect(sigc::bind<0>(sigc::mem_fun(this, &Server::train_function_changed), sigc::ref(train)));
+ train.signal_route_changed.connect(sigc::bind<0>(sigc::mem_fun(this, &Server::train_route_changed), sigc::ref(train)));
train.signal_status_changed.connect(sigc::bind<0>(sigc::mem_fun(this, &Server::train_status_changed), sigc::ref(train)));
TrainInfoPacket pkt;
send(pkt);
}
+void Server::train_reverse_changed(const Train &train, bool reverse)
+{
+ TrainSpeedPacket pkt;
+ pkt.address = train.get_locomotive().get_address();
+ pkt.speed = train.get_target_speed();
+ pkt.reverse = reverse;
+ send(pkt);
+}
+
void Server::train_function_changed(const Train &train, unsigned, bool)
{
TrainFunctionPacket pkt;
send(pkt);
}
+void Server::train_route_changed(const Train &train, const Route *route)
+{
+ TrainRoutePacket pkt;
+ pkt.address = train.get_locomotive().get_address();
+ if(route)
+ pkt.route = route->get_name();
+ send(pkt);
+}
+
void Server::train_status_changed(const Train &train, const string &status)
{
TrainStatusPacket pkt;
void Server::Connection::handshake_done()
{
+ const map<string, Route *> &routes = server.trfc_mgr.get_layout().get_routes();
+ for(map<string, Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
+ {
+ RouteInfoPacket pkt;
+ pkt.name = i->first;
+ comm.send(pkt);
+ }
+
const list<Train *> &trains = server.trfc_mgr.get_trains();
for(list<Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
{
pkt.status = (*i)->get_status();
comm.send(pkt);
}
+ if((*i)->get_route())
+ {
+ TrainRoutePacket pkt;
+ pkt.address = loco.get_address();
+ pkt.route = (*i)->get_route()->get_name();
+ comm.send(pkt);
+ }
}
}
void Server::Connection::receive(const TrainSpeedPacket &pkt)
{
- Locomotive &loco = server.trfc_mgr.get_control().get_locomotive(pkt.address);
- Train &train = server.trfc_mgr.get_train_by_locomotive(loco);
- if(pkt.reverse!=loco.get_reverse())
- train.set_reverse(pkt.reverse);
- else
- train.set_speed(pkt.speed);
+ try
+ {
+ Locomotive &loco = server.trfc_mgr.get_control().get_locomotive(pkt.address);
+ Train &train = server.trfc_mgr.get_train_by_locomotive(loco);
+ if(pkt.reverse!=loco.get_reverse())
+ train.set_reverse(pkt.reverse);
+ else
+ train.set_speed(pkt.speed);
+ }
+ catch(const Exception &e)
+ {
+ error(e.what());
+ }
}
void Server::Connection::receive(const TrainFunctionPacket &pkt)
{
- Locomotive &loco = server.trfc_mgr.get_control().get_locomotive(pkt.address);
- for(unsigned i=0; i<9; ++i)
- if(((pkt.functions^loco.get_functions())>>i)&1)
- loco.set_function(i, (pkt.functions>>i)&1);
+ try
+ {
+ Locomotive &loco = server.trfc_mgr.get_control().get_locomotive(pkt.address);
+ for(unsigned i=0; i<9; ++i)
+ if(((pkt.functions^loco.get_functions())>>i)&1)
+ loco.set_function(i, (pkt.functions>>i)&1);
+ }
+ catch(const Exception &e)
+ {
+ error(e.what());
+ }
+}
+
+void Server::Connection::receive(const TrainRoutePacket &pkt)
+{
+ try
+ {
+ Locomotive &loco = server.trfc_mgr.get_control().get_locomotive(pkt.address);
+ Train &train = server.trfc_mgr.get_train_by_locomotive(loco);
+ if(pkt.route.empty())
+ train.set_route(0);
+ else
+ {
+ Route &route = server.trfc_mgr.get_layout().get_route(pkt.route);
+ train.set_route(&route);
+ }
+ }
+ catch(const Exception &e)
+ {
+ error(e.what());
+ }
+}
+
+void Server::Connection::error(const string &msg)
+{
+ ErrorPacket pkt;
+ pkt.message = msg;
+ comm.send(pkt);
}
} // namespace Marklin