mail[Wesnoth-commits] r44797 - in /trunk/src/ana: ./ api/ apps/chat/ src/


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

Header


Content

Posted by billybiset on July 28, 2010 - 11:12:
Author: billynux
Date: Wed Jul 28 11:12:32 2010
New Revision: 44797

URL: http://svn.gna.org/viewcvs/wesnoth?rev=44797&view=rev
Log:
Added methods to wait for an incoming message (in server and client versions) 
to ANA. The test chat application has a feature to try it out.

Modified:
    trunk/src/ana/api/ana.hpp
    trunk/src/ana/apps/chat/client.cpp
    trunk/src/ana/new-wesnoth-network-api.hpp
    trunk/src/ana/src/asio_client.cpp
    trunk/src/ana/src/asio_client.hpp
    trunk/src/ana/src/asio_listener.cpp
    trunk/src/ana/src/asio_listener.hpp
    trunk/src/ana/src/asio_server.cpp
    trunk/src/ana/src/asio_server.hpp

Modified: trunk/src/ana/api/ana.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ana/api/ana.hpp?rev=44797&r1=44796&r2=44797&view=diff
==============================================================================
--- trunk/src/ana/api/ana.hpp (original)
+++ trunk/src/ana/api/ana.hpp Wed Jul 28 11:12:32 2010
@@ -242,9 +242,11 @@
                 }
 
             private:
-                bool raw_data_   /** The component is in raw data mode.*/ ;
-
-                const net_id     id_ /** This component's net_id. */ ;
+                /** The component is in raw data mode.*/
+                bool raw_data_;
+
+                /** This component's net_id. */
+                const net_id     id_;
         };
 
         /**
@@ -511,6 +513,30 @@
 
         /** Returns a pointer to an ana::stats object of a connected client. 
*/
         virtual const stats* get_client_stats( net_id, stat_type ) const = 0;
+
+        /**
+         * Signal the server that you are waiting for a message from a given 
client in a certain
+         * period of time.
+         *
+         * The time parameter indicates how long you are willing to wait.
+         *
+         * If a message is received before this time period then this call 
will be insignificant.
+         * However, if no such message is received, the appropiate call to 
handle_receive will be
+         * made with ana::timeout_error as the error_code parameter.
+         *
+         * @param id : The ana::net_id of the client you are expecting the 
message from. If the
+         *             id is invalid, this call will have no effect.
+         * @param time : The amount of time you are willing to wait.
+         *
+         * Use the methods described in the ana::time namespace to create 
time lapses.
+         *
+         * Examples:
+         *     - client->waiting_for_message( ana::time::seconds( 5 ) );
+         *
+         * @sa error_code
+         * @sa ana::time
+         */
+        virtual void expecting_message( net_id, size_t ms_until_timeout ) = 
0;
 
         /** Standard destructor. */
         virtual ~server() {}
@@ -552,6 +578,26 @@
             /** Returns the string representing the ip address of the 
connected client. */
             virtual std::string ip_address() const = 0;
 
+            /**
+             * Signal the client proxy that you are waiting for a message 
from the actual client
+             * before a given time.
+             *
+             * The time parameter indicates how long you are willing to wait.
+             *
+             * If a message is received before this time period then this 
call will be
+             * insignificant.
+             * However, if no such message is received, the appropiate call 
to handle_receive will
+             * be made with ana::timeout_error as the error_code parameter.
+             *
+             * Use the methods described in the ana::time namespace to 
create time lapses.
+             *
+             * Examples:
+             *     - client->waiting_for_message( ana::time::seconds( 5 ) );
+             *
+             * @sa error_code
+             */
+            virtual void expecting_message( size_t ms_until_timeout ) = 0;
+
             // Allow server objects to invoke run_listener directly.
             using detail::listener::run_listener;
 
@@ -642,6 +688,24 @@
         virtual void cancel_pending( )                             = 0;
 
         /**
+         * Signal the client that you are waiting for a message from the 
server before a given time.
+         *
+         * The time parameter indicates how long you are willing to wait.
+         *
+         * If a message is received before this time period then this call 
will be insignificant.
+         * However, if no such message is received, the appropiate call to 
handle_receive will be
+         * made with ana::timeout_error as the error_code parameter.
+         *
+         * Use the methods described in the ana::time namespace to create 
time lapses.
+         *
+         * Examples:
+         *     - client->waiting_for_message( ana::time::seconds( 5 ) );
+         *
+         * @sa error_code
+         */
+        virtual void expecting_message( size_t ms_until_timeout ) = 0;
+
+        /**
          * Set a timeout value for connection attempts. If attempting to 
connect through a proxy
          * this value will be used once for the whole connection attempt.
          *

Modified: trunk/src/ana/apps/chat/client.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ana/apps/chat/client.cpp?rev=44797&r1=44796&r2=44797&view=diff
==============================================================================
--- trunk/src/ana/apps/chat/client.cpp (original)
+++ trunk/src/ana/apps/chat/client.cpp Wed Jul 28 11:12:32 2010
@@ -108,6 +108,20 @@
             return msg.substr(pos+1);
         }
 
+        double get_seconds(const std::string& msg)
+        {
+            size_t pos = msg.find(" ");
+
+            std::stringstream ss( msg.substr(pos+1) );
+
+            double result;
+
+            ss >> result;
+
+            return result;
+        }
+
+
         void parse_command(const std::string& msg)
         {
             if (msg[1] == 'n') //Lame: assume name command
@@ -153,6 +167,11 @@
                                             << std::setw(8) << 
day_stats->bytes_out() <<"|\n"
                     << 
"+-----------------+-----------------+-----------------+\n";
             }
+            else if ( msg[1] == 'h' )
+            {
+                double seconds = get_seconds( msg );
+                client_->expecting_message( ana::time::seconds( seconds ) );
+            }
         }
 
         void run_input()
@@ -205,6 +224,7 @@
                              "    '/quit'      : Quit. \n"
                              "    '/who'       : List connected users. \n" <<
                              "    '/stats'     : Print full network stats. 
\n" <<
+                             "    '/hold secs' : Wait secs for a new 
message. \n" <<
                              "    '/name name' : Change name." << std::endl;
 
                 run_input();
@@ -245,6 +265,8 @@
                           << std::endl << name_ << " : ";
                 std::cout.flush();
             }
+            else if ( error == ana::timeout_error )
+                std::cerr << "\nTimeout for waiting message.\n";
         }
 
         virtual void handle_send( ana::error_code error, net_id client, 
ana::operation_id op_id)

Modified: trunk/src/ana/new-wesnoth-network-api.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ana/new-wesnoth-network-api.hpp?rev=44797&r1=44796&r2=44797&view=diff
==============================================================================
--- trunk/src/ana/new-wesnoth-network-api.hpp (original)
+++ trunk/src/ana/new-wesnoth-network-api.hpp Wed Jul 28 11:12:32 2010
@@ -274,7 +274,7 @@
              * If a message is received before this time period the 
appropiate call to
              * handle_receive will be made with ana::timeout_error.
              */
-            void waiting_for_message( ana::net_id, time );
+            void waiting_for_message( ana::net_id, size_t ms_until_timeout );
 
             /**
              * Get network stats for the server, the parameter indicates the 
time period

Modified: trunk/src/ana/src/asio_client.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ana/src/asio_client.cpp?rev=44797&r1=44796&r2=44797&view=diff
==============================================================================
--- trunk/src/ana/src/asio_client.cpp (original)
+++ trunk/src/ana/src/asio_client.cpp Wed Jul 28 11:12:32 2010
@@ -253,6 +253,11 @@
     connect_timeout_ms_ = ms;
 }
 
+void asio_client::expecting_message( size_t ms_until_timeout )
+{
+    wait_for_incoming_message( ms_until_timeout );
+}
+
 void asio_client::disconnect_listener()
 {
     io_service_.stop();

Modified: trunk/src/ana/src/asio_client.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ana/src/asio_client.hpp?rev=44797&r1=44796&r2=44797&view=diff
==============================================================================
--- trunk/src/ana/src/asio_client.hpp (original)
+++ trunk/src/ana/src/asio_client.hpp Wed Jul 28 11:12:32 2010
@@ -102,6 +102,8 @@
 
         virtual void set_connect_timeout( size_t ms );
 
+        virtual void expecting_message( size_t ms_until_timeout );
+
         void handle_connect(const boost::system::error_code& ec,
                             tcp::resolver::iterator endpoint_iterator,
                             ana::connection_handler*,

Modified: trunk/src/ana/src/asio_listener.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ana/src/asio_listener.cpp?rev=44797&r1=44796&r2=44797&view=diff
==============================================================================
--- trunk/src/ana/src/asio_listener.cpp (original)
+++ trunk/src/ana/src/asio_listener.cpp Wed Jul 28 11:12:32 2010
@@ -40,12 +40,26 @@
 asio_listener::asio_listener( ) :
     disconnected_( false ),
     listener_( NULL ),
-    raw_mode_buffer_size_( ana::INITIAL_RAW_MODE_BUFFER_SIZE )
+    raw_mode_buffer_size_( ana::INITIAL_RAW_MODE_BUFFER_SIZE ),
+    next_message_timer_( NULL )
 {
 }
 
 asio_listener::~asio_listener()
 {
+    delete next_message_timer_;
+}
+
+void asio_listener::wait_for_incoming_message( size_t ms_to_timeout, 
ana::net_id id )
+{
+    if ( (next_message_timer_ == NULL) && ( ms_to_timeout > 0 ) )
+    {
+        next_message_timer_ = new ana::timer( socket().get_io_service() );
+
+        next_message_timer_->wait(  ms_to_timeout,
+                                    
boost::bind(&asio_listener::handle_timeout, this,
+                                                
boost::asio::placeholders::error, id) );
+    }
 }
 
 void asio_listener::disconnect( boost::system::error_code error)
@@ -167,6 +181,8 @@
             if ( accumulated == buffer->size() )
             {
                 listener_->handle_receive( ec, id(), buffer );
+                delete next_message_timer_;
+                next_message_timer_ = NULL;
                 listen_one_message();
             }
             else
@@ -184,6 +200,14 @@
     }
 }
 
+void asio_listener::handle_timeout( const boost::system::error_code& 
error_code, ana::net_id id)
+{
+    delete next_message_timer_;
+    next_message_timer_ = NULL;
+
+    if ( error_code != ana::operation_aborted )
+        listener_->handle_receive( ana::timeout_error, id, 
ana::detail::read_buffer() );
+}
 
 void asio_listener::handle_raw_buffer( ana::detail::read_buffer buf,
                                        const boost::system::error_code& ec,
@@ -198,6 +222,8 @@
             buf->resize( read_size );
             log_conditional_receive( buf );
             listener_->handle_receive( ec, id(), buf );
+            delete next_message_timer_;
+            next_message_timer_ = NULL;
             listen_one_message();
         }
     }

Modified: trunk/src/ana/src/asio_listener.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ana/src/asio_listener.hpp?rev=44797&r1=44796&r2=44797&view=diff
==============================================================================
--- trunk/src/ana/src/asio_listener.hpp (original)
+++ trunk/src/ana/src/asio_listener.hpp Wed Jul 28 11:12:32 2010
@@ -53,6 +53,8 @@
     protected:
         virtual tcp::socket& socket() = 0;
 
+        void wait_for_incoming_message( size_t ms_to_timeout, ana::net_id id 
= 0  );
+
     private:
         virtual void disconnect_listener()                   {}
 
@@ -76,6 +78,8 @@
                                   size_t accumulated,
                                   size_t last_msg_size);
 
+        void handle_timeout( const boost::system::error_code&, ana::net_id);
+
         void handle_raw_buffer( ana::detail::read_buffer, const 
boost::system::error_code&, size_t);
 
         /*attr*/
@@ -83,6 +87,8 @@
         ana::listener_handler*     listener_;
         char                       header_[ana::HEADER_LENGTH];
         size_t                     raw_mode_buffer_size_;
+
+        ana::timer*                next_message_timer_;
 };
 
 #endif

Modified: trunk/src/ana/src/asio_server.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ana/src/asio_server.cpp?rev=44797&r1=44796&r2=44797&view=diff
==============================================================================
--- trunk/src/ana/src/asio_server.cpp (original)
+++ trunk/src/ana/src/asio_server.cpp Wed Jul 28 11:12:32 2010
@@ -394,6 +394,17 @@
     }
 }
 
+void asio_server::expecting_message( net_id id, size_t ms_until_timeout )
+{
+    std::list<ana::server::client_proxy*>::iterator it;
+
+    it = std::find_if( client_proxies_.begin(), client_proxies_.end(),
+                       boost::bind( &client_proxy::id, _1) == id );
+
+    if ( it != client_proxies_.end() )
+        (*it)->expecting_message( ms_until_timeout );
+}
+
 void asio_server::set_header_first_mode( ana::net_id id )
 {
     std::list<ana::server::client_proxy*>::const_iterator it;
@@ -471,3 +482,8 @@
 {
     return new ana::timer( socket_.get_io_service() );
 }
+
+void asio_server::asio_client_proxy::expecting_message( size_t 
ms_until_timeout )
+{
+    wait_for_incoming_message( ms_until_timeout, id() );
+}

Modified: trunk/src/ana/src/asio_server.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ana/src/asio_server.hpp?rev=44797&r1=44796&r2=44797&view=diff
==============================================================================
--- trunk/src/ana/src/asio_server.hpp (original)
+++ trunk/src/ana/src/asio_server.hpp Wed Jul 28 11:12:32 2010
@@ -87,6 +87,8 @@
 
                 virtual const ana::stats* get_stats( ana::stat_type type ) 
const;
 
+                virtual void expecting_message( size_t ms_until_timeout );
+
                 void log_conditional_receive( const 
ana::detail::read_buffer& buffer );
 
                 tcp::socket           socket_;
@@ -157,6 +159,8 @@
 
         virtual void set_raw_data_mode( ana::net_id id );
 
+        virtual void expecting_message( ana::net_id, size_t ms_until_timeout 
);
+
         void handle_accept (const boost::system::error_code& ec,
                             asio_client_proxy*               client,
                             ana::connection_handler*         handler);




Related Messages


Powered by MHonArc, Updated Wed Jul 28 12:20:17 2010