]> git.tdb.fi Git - r2c2.git/blobdiff - source/network/server.cpp
Export routes over network
[r2c2.git] / source / network / server.cpp
index 17142f8dcdaeacf5340e710ff8258c00773846ae..cd52b48502b1e5345a9a749e7a36c024f978940d 100644 (file)
@@ -7,8 +7,10 @@ Distributed under the GPL
 
 #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;
@@ -23,12 +25,7 @@ Server::Server(TrafficManager &tm):
 {
        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));
@@ -52,7 +49,9 @@ void Server::train_added(Train &train)
 {
        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;
@@ -71,6 +70,15 @@ void Server::train_speed_changed(const Train &train, unsigned speed)
        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;
@@ -79,6 +87,15 @@ void Server::train_function_changed(const Train &train, unsigned, bool)
        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;
@@ -113,6 +130,14 @@ Server::Connection::~Connection()
 
 void Server::Connection::handshake_done()
 {
+       const set<Route *> &routes = server.trfc_mgr.get_layout().get_routes();
+       for(set<Route *>::const_iterator i=routes.begin(); i!=routes.end(); ++i)
+       {
+               RouteInfoPacket pkt;
+               pkt.name = (*i)->get_name();
+               comm.send(pkt);
+       }
+
        const list<Train *> &trains = server.trfc_mgr.get_trains();
        for(list<Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
        {
@@ -144,6 +169,13 @@ void Server::Connection::handshake_done()
                        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);
+               }
        }
 }
 
@@ -155,20 +187,61 @@ void Server::Connection::end_of_file()
 
 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