mail[Wesnoth-commits] r44093 - in /trunk/src: network_ana.cpp network_manager_ana.cpp network_manager_ana.hpp


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by billybiset on July 10, 2010 - 23:15:
Author: billynux
Date: Sat Jul 10 23:15:56 2010
New Revision: 44093

URL: http://svn.gna.org/viewcvs/wesnoth?rev=44093&view=rev
Log:
Started to work on the server side of ana, implemented accept_connection.

Modified:
    trunk/src/network_ana.cpp
    trunk/src/network_manager_ana.cpp
    trunk/src/network_manager_ana.hpp

Modified: trunk/src/network_ana.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/network_ana.cpp?rev=44093&r1=44092&r2=44093&view=diff
==============================================================================
--- trunk/src/network_ana.cpp (original)
+++ trunk/src/network_ana.cpp Sat Jul 10 23:15:56 2010
@@ -199,7 +199,6 @@
         if ( create_server != NO_SERVER )
         {
             ana::net_id server_id = ana_manager.create_server( );
-
             ana_manager.run_server( server_id, port);
         }
     }
@@ -253,7 +252,7 @@
 
     connection accept_connection()
     {
-        throw std::runtime_error("TODO:Not implemented accept_connection");
+        return ana_manager.new_connection_id();
     }
 
     bool disconnect(connection /*s*/)
@@ -299,8 +298,9 @@
         return receive_data(cfg,connection_num, size_t(0), NULL); // <- just 
call the previous version without timeouts
     }
 
-    connection receive_data(std::vector<char>& /*buf*/, bandwidth_in_ptr* 
/*bandwidth_in*/)
-    {
+    connection receive_data(std::vector<char>& buf, bandwidth_in_ptr* 
/*bandwidth_in*/)
+    {
+        std::cout << "DEBUG: Trying to read to a buffer of size " << 
buf.size() <<".\n";
         throw std::runtime_error("TODO:Not implemented receive_data2");
     }
 

Modified: trunk/src/network_manager_ana.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/network_manager_ana.cpp?rev=44093&r1=44092&r2=44093&view=diff
==============================================================================
--- trunk/src/network_manager_ana.cpp (original)
+++ trunk/src/network_manager_ana.cpp Sat Jul 10 23:15:56 2010
@@ -424,6 +424,14 @@
     return boost::get<ana::client*>(base_);
 }
 
+ana::detail::listener* ana_component::listener() const
+{
+    if( is_server_ )
+        return server();
+    else
+        return client();
+}
+
 bool ana_component::is_server() const
 {
     return is_server_;
@@ -498,7 +506,8 @@
 // Begin clients_manager  implementation 
---------------------------------------------------------------
 
 clients_manager::clients_manager() :
-    ids_()
+    ids_(),
+    pending_ids_()
 {
 }
 
@@ -510,12 +519,28 @@
 void clients_manager::handle_connect(ana::error_code error, ana::net_id 
client)
 {
     if (! error )
+    {
         ids_.insert(client);
+        pending_ids_.insert( network::connection( client ) );
+    }
 }
 
 void clients_manager::handle_disconnect(ana::error_code /*error*/, 
ana::net_id client)
 {
     ids_.erase(client);
+    pending_ids_.erase( network::connection( client ) );
+}
+
+bool clients_manager::has_connection_pending() const
+{
+    return ! pending_ids_.empty();
+}
+
+network::connection clients_manager::get_pending_connection_id()
+{
+    const network::connection result = *pending_ids_.begin();
+    pending_ids_.erase( pending_ids_.begin() );
+    return result;
 }
 
 // Begin ana_network_manager implementation 
------------------------------------------------------------
@@ -625,6 +650,23 @@
     }
 }
 
+network::connection ana_network_manager::new_connection_id( )
+{
+    ana_component_set::iterator it;
+
+    for (it = components_.begin(); it != components_.end(); ++it)
+    {
+        if ( (*it)->is_server() )
+        {
+            clients_manager* clients_mgr = server_manager_[ (*it)->server() 
];
+            if ( clients_mgr->has_connection_pending() )
+                return clients_mgr->get_pending_connection_id();
+        }
+    }
+
+    // No new connection
+    return 0;
+}
 const ana::stats* ana_network_manager::get_stats( network::connection 
connection_num )
 {
     ana::net_id id( connection_num );
@@ -656,13 +698,19 @@
     std::stringstream ss;
     ss << port;
 
-    std::set<ana_component*>::iterator it;
+    ana_component_set::iterator it;
 
     it = std::find_if( components_.begin(), components_.end(),
                         boost::bind(&ana_component::get_id, _1) == id );
 
-    if ( it != components_.end())
-        return (*it)->server()->run( ss.str() );
+    if ( it == components_.end())
+        throw std::runtime_error("No server with this id.");
+    else
+        if ( (*it)->is_server() )
+            (*it)->server()->run( ss.str() );
+        else
+            throw std::runtime_error("Component is not a server.");
+
 }
 
 std::string ana_network_manager::ip_address( network::connection id )
@@ -808,14 +856,15 @@
         return 0;
     else
     {
-        ana::client* const client = (*it)->client(); // TODO: server support
-
         ana_receive_handler handler;
-        client->set_listener_handler( &handler );
-
-        handler.wait_completion( client, timeout_ms );
-
-        client->set_listener_handler( this );
+        (*it)->listener()->set_listener_handler( &handler );
+
+        if ( (*it)->is_server() )
+            handler.wait_completion( (*it)->server(), timeout_ms );
+        else
+            handler.wait_completion( (*it)->client(), timeout_ms );
+
+        (*it)->listener()->set_listener_handler( this );
 
         if ( handler.error() )
             return 0;

Modified: trunk/src/network_manager_ana.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/network_manager_ana.hpp?rev=44093&r1=44092&r2=44093&view=diff
==============================================================================
--- trunk/src/network_manager_ana.hpp (original)
+++ trunk/src/network_manager_ana.hpp Sat Jul 10 23:15:56 2010
@@ -74,6 +74,12 @@
          */
         ana::client* client() const;
 
+        /**
+         * Get the pointer to an ana::listener object for this component.
+         * Both an ana::client an the ana::server are listeners.
+         */
+        ana::detail::listener* listener() const;
+
         /** Returns true iff this component is a server. */
         bool is_server() const;
 
@@ -339,12 +345,17 @@
         /** Returns the amount of components connected to this server. */
         size_t client_amount() const;
 
+        bool has_connection_pending() const;
+
+        network::connection get_pending_connection_id();
+
     private:
         virtual void handle_connect(ana::error_code error, ana::net_id 
client);
 
         virtual void handle_disconnect(ana::error_code /*error*/, 
ana::net_id client);
 
-        std::set<ana::net_id> ids_;
+        std::set< ana::net_id >         ids_;
+        std::set< network::connection > pending_ids_;
 };
 
 /**
@@ -370,6 +381,8 @@
          * @returns The ID of the new created client, as a 
network::connection number.
          */
         network::connection create_client_and_connect(std::string host, int 
port);
+
+        network::connection new_connection_id( );
 
         /**
          * Get the associated stats of a given component.
@@ -475,7 +488,7 @@
         ana::timer*                connect_timer_;
         ana_component_set          components_;
 
-        std::map< ana::server*, const clients_manager* > server_manager_;
+        std::map< ana::server*, clients_manager* > server_manager_;
 };
 
 #endif




Related Messages


Powered by MHonArc, Updated Sun Jul 11 01:20:08 2010