]> git.tdb.fi Git - r2c2.git/blobdiff - source/network/server.cpp
Remove status from Train and turn it into a TrainAI
[r2c2.git] / source / network / server.cpp
index 43d22ffef9016f26b376e691d840840bea13a940..df40b3f11ea23d03bf9ca1cb2bb3cdb36b36d6f5 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of R²C²
-Copyright © 2009-2010  Mikkosoft Productions, Mikko Rasa
+Copyright © 2009-2011  Mikkosoft Productions, Mikko Rasa
 Distributed under the GPL
 */
 
@@ -77,6 +77,10 @@ void Server::train_added(Train &train)
        pkt.loco_type = train.get_locomotive_type().get_article_number().str();
        pkt.name = train.get_name();
        send(pkt);
+
+       TrainStatus *status = new TrainStatus(train);
+       status->set_tag("server:status");
+       status->signal_changed.connect(sigc::bind<0>(sigc::mem_fun(this, &Server::train_status_changed), sigc::ref(train)));
 }
 
 void Server::train_control_changed(const Train &train, const string &control, float value)
@@ -117,8 +121,17 @@ template<typename P>
 void Server::send(const P &pkt)
 {
        for(vector<Connection *>::const_iterator i=connections.begin(); i!=connections.end(); ++i)
-               if(!(*i)->stale)
-                       (*i)->comm.send(pkt);
+               if(!(*i)->stale && (*i)->comm.is_handshake_done())
+               {
+                       try
+                       {
+                               (*i)->comm.send(pkt);
+                       }
+                       catch(...)
+                       {
+                               (*i)->stale = true;
+                       }
+               }
 }
 
 
@@ -185,10 +198,11 @@ void Server::Connection::handshake_done()
                        pkt.functions = train.get_functions();
                        comm.send(pkt);
                }
+               if(TrainStatus *status = dynamic_cast<TrainStatus *>(train.get_tagged_ai("server:status")))
                {
                        TrainStatusPacket pkt;
                        pkt.address = train.get_address();
-                       pkt.status = train.get_status();
+                       pkt.status = status->get_status();
                        comm.send(pkt);
                }
                if(train.get_route())