mail[Wesnoth-commits] r43534 - in /trunk/src: CMakeLists.txt ana/src/CMakeLists.txt network_ana.cpp


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

Header


Content

Posted by billybiset on June 17, 2010 - 23:11:
Author: billynux
Date: Thu Jun 17 23:11:46 2010
New Revision: 43534

URL: http://svn.gna.org/viewcvs/wesnoth?rev=43534&view=rev
Log:
Added network_ana.cpp to work on implementing the network using ana and an 
option in cmake to use it.

Added:
    trunk/src/network_ana.cpp   (with props)
Modified:
    trunk/src/CMakeLists.txt
    trunk/src/ana/src/CMakeLists.txt

Modified: trunk/src/CMakeLists.txt
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/CMakeLists.txt?rev=43534&r1=43533&r2=43534&view=diff
==============================================================================
--- trunk/src/CMakeLists.txt (original)
+++ trunk/src/CMakeLists.txt Thu Jun 17 23:11:46 2010
@@ -38,6 +38,16 @@
   include_directories( ${ZLIB_INCLUDE_DIR} )
 endif()
 
+option ( USE_ANA_NETWORK "Compile network module using ana" OFF )
+
+set( network_implementation_files network.cpp network_worker.cpp )
+
+if( USE_ANA_NETWORK )
+    set( network_implementation_files network_ana.cpp )
+    message( STATUS "Compiling network module with ana: 
${network_implementation_files}" )
+    find_package( Boost 1.35 REQUIRED COMPONENTS system )
+endif ( USE_ANA_NETWORK )
+
 # needed to get include paths in the subfolders correct
 include_directories( ${CMAKE_SOURCE_DIR}/src/ )
 # needed to have the generated config.h used, too
@@ -59,6 +69,7 @@
                ${SDL_LIBRARY}
                ${Boost_IOSTREAMS_LIBRARY}
                ${Boost_REGEX_LIBRARY}
+        ${Boost_SYSTEM_LIBRARIES}
        )
 endif(MSVC)
 
@@ -400,8 +411,7 @@
     multiplayer_wait.cpp
     multiplayer_connect.cpp
     multiplayer_create.cpp
-    network.cpp
-    network_worker.cpp
+    ${network_implementation_files} # network.cpp and network_worker.cpp are 
included by default (without USE_ANA_NETWORK)
     persist_context.cpp
     persist_var.cpp
     playcampaign.cpp

Modified: trunk/src/ana/src/CMakeLists.txt
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/ana/src/CMakeLists.txt?rev=43534&r1=43533&r2=43534&view=diff
==============================================================================
--- trunk/src/ana/src/CMakeLists.txt (original)
+++ trunk/src/ana/src/CMakeLists.txt Thu Jun 17 23:11:46 2010
@@ -2,7 +2,7 @@
 
 include_directories( ../api )
 
-find_package( Boost 1.35 REQUIRED COMPONENTS thread system)
+find_package( Boost 1.35 REQUIRED COMPONENTS thread bind)
 
 set ( common_srcs asio_listener.cpp )
 

Added: trunk/src/network_ana.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/network_ana.cpp?rev=43534&view=auto
==============================================================================
--- trunk/src/network_ana.cpp (added)
+++ trunk/src/network_ana.cpp Thu Jun 17 23:11:46 2010
@@ -1,0 +1,460 @@
+/* $Id$ */
+
+/**
+ * @file network_ana.hpp
+ * @brief Network implementation using ana.
+ *
+ * Copyright (C) 2010 Guillermo Biset.
+ *
+ * Part of the Battle for Wesnoth Project http://www.wesnoth.org/
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * or at your option any later version.
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY.
+ *
+ * See the COPYING file for more details.
+ */
+
+#include "ana/ana.hpp"
+#include "network.hpp"
+
+#include "global.hpp"
+
+#include "gettext.hpp"
+#include "log.hpp"
+#include "serialization/string_utils.hpp"
+#include "thread.hpp"
+#include "util.hpp"
+
+#include "filesystem.hpp"
+
+#include <cerrno>
+#include <queue>
+#include <iomanip>
+#include <set>
+#include <cstring>
+
+#include <signal.h>
+#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
+#undef INADDR_ANY
+#undef INADDR_BROADCAST
+#undef INADDR_NONE
+#include <windows.h>
+#else
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>  // for TCP_NODELAY
+#ifdef __BEOS__
+#include <socket.h>
+#else
+#include <fcntl.h>
+#endif
+#define SOCKET int
+#endif
+
+static lg::log_domain log_network("network");
+#define DBG_NW LOG_STREAM(debug, log_network)
+#define LOG_NW LOG_STREAM(info, log_network)
+#define WRN_NW LOG_STREAM(warn, log_network)
+#define ERR_NW LOG_STREAM(err, log_network)
+// Only warnings and not errors to avoid DoS by log flooding
+
+// TODO: just a temporary typedef
+typedef ana::client_id TCPsocket;
+
+namespace {
+
+// We store the details of a connection in a map that must be looked up by 
its handle.
+// This allows a connection to be disconnected and then recovered,
+// but the handle remains the same, so it's all seamless to the user.
+struct connection_details {
+    connection_details(TCPsocket sock, const std::string& host, int port)
+        : sock(sock), host(host), port(port), remote_handle(0),
+          connected_at( 0 ) // TODO: connected_at(SDL_GetTicks())
+    {}
+
+    TCPsocket sock;
+    std::string host;
+    int port;
+
+    // The remote handle is the handle assigned to this connection by the 
remote host.
+    // Is 0 before a handle has been assigned.
+    int remote_handle;
+
+    int connected_at;
+};
+
+typedef std::map<network::connection,connection_details> connection_map;
+connection_map connections;
+
+
+} // end anon namespace
+
+static connection_details& get_connection_details(network::connection handle)
+{
+    const connection_map::iterator i = connections.find(handle);
+    if(i == connections.end()) {
+        throw network::error(_("invalid network handle"));
+    }
+
+    return i->second;
+}
+
+
+static void check_error()
+{
+    throw("TODO:Not implemented");
+}
+
+namespace {
+
+// SDLNet_SocketSet socket_set = 0;
+typedef std::set<TCPsocket> socket_set_type;
+socket_set_type socket_set;
+std::set<network::connection> waiting_sockets;
+typedef std::vector<network::connection> sockets_list;
+sockets_list sockets;
+
+
+struct partial_buffer {
+    partial_buffer() :
+        buf(),
+        upto(0)
+    {
+    }
+
+    std::vector<char> buf;
+    size_t upto;
+};
+
+std::deque<network::connection> disconnection_queue;
+std::set<network::connection> bad_sockets;
+
+// network_worker_pool::manager* worker_pool_man = NULL;
+
+} // end anon namespace
+
+namespace network {
+
+/**
+ * Amount of seconds after the last server ping when we assume to have timed 
out.
+ * When set to '0' ping timeout isn't checked.
+ * Gets set in preferences::manager according to the preferences file.
+ */
+unsigned int ping_timeout = 0;
+
+connection_stats::connection_stats(int sent, int received, int connected_at)
+       : bytes_sent(sent), bytes_received(received), time_connected(0 - 
connected_at)
+                                                    // TODO: s/SDLGetTicks/0/
+{}
+
+connection_stats get_connection_stats(connection connection_num)
+{
+    connection_details& details = get_connection_details(connection_num);
+    return connection_stats(get_send_stats(connection_num).total,
+                            get_receive_stats(connection_num).total,
+                            details.connected_at);
+}
+
+error::error(const std::string& msg, connection sock) : message(msg), 
socket(sock)
+{
+    if(socket) {
+        bad_sockets.insert(socket);
+    }
+}
+
+void error::disconnect()
+{
+    if(socket) network::disconnect(socket);
+}
+
+pending_statistics get_pending_stats()
+{
+    throw("TODO:Not implemented");
+}
+
+manager::manager(size_t /*min_threads*/, size_t /*max_threads*/) : 
free_(true)
+{
+    throw("TODO:Not implemented");
+}
+
+manager::~manager()
+{
+    throw("TODO:Not implemented");
+}
+
+void set_raw_data_only()
+{
+    throw("TODO:Not implemented");
+}
+
+server_manager::server_manager(int /*port*/, CREATE_SERVER 
/*create_server*/) : free_(false), connection_(0)
+{
+    throw("TODO:Not implemented");
+}
+
+server_manager::~server_manager()
+{
+    stop();
+}
+
+void server_manager::stop()
+{
+    throw("TODO:Not implemented");
+}
+
+bool server_manager::is_running() const
+{
+    throw("TODO:Not implemented");
+}
+
+size_t nconnections()
+{
+    return sockets.size();
+}
+
+bool is_server()
+{
+    throw("TODO:Not implemented");
+}
+
+connection connect(const std::string& /*host*/, int /*port*/)
+{
+    throw("TODO:Not implemented");
+}
+
+connection connect(const std::string& /*host*/, int /*port*/, 
threading::waiter& /*waiter*/)
+{
+    throw("TODO:Not implemented");
+}
+
+namespace {
+
+connection accept_connection_pending(std::vector<TCPsocket>& 
/*pending_sockets*/,
+                                     socket_set_type& /*pending_socket_set*/)
+{
+    throw("TODO:Not implemented");
+}
+
+} //anon namespace
+
+connection accept_connection()
+{
+    throw("TODO:Not implemented");
+}
+
+bool disconnect(connection /*s*/)
+{
+    throw("TODO:Not implemented");
+}
+
+void queue_disconnect(network::connection sock)
+{
+    disconnection_queue.push_back(sock);
+}
+
+connection receive_data(config& /*cfg*/,
+                        connection /*connection_num*/,
+                        unsigned int /*timeout*/,
+                        bandwidth_in_ptr* /*bandwidth_in*/)
+{
+    throw("TODO:Not implemented");
+}
+
+connection receive_data(config& /*cfg*/, connection /*connection_num*/,
+                        bool* /*gzipped*/,
+                        bandwidth_in_ptr* /*bandwidth_in*/)
+{
+    throw("TODO:Not implemented");
+}
+
+connection receive_data(std::vector<char>& /*buf*/, bandwidth_in_ptr* 
/*bandwidth_in*/)
+{
+    throw("TODO:Not implemented");
+}
+
+struct bandwidth_stats {
+    int out_packets;
+    int out_bytes;
+    int in_packets;
+    int in_bytes;
+    int day;
+    const static size_t type_width = 16;
+    const static size_t packet_width = 7;
+    const static size_t bytes_width = 10;
+    bandwidth_stats& operator+=(const bandwidth_stats& a)
+    {
+        out_packets += a.out_packets;
+        out_bytes += a.out_bytes;
+        in_packets += a.in_packets;
+        in_bytes += a.in_bytes;
+
+        return *this;
+    }
+};
+
+typedef std::map<const std::string, bandwidth_stats> bandwidth_map;
+typedef std::vector<bandwidth_map> hour_stats_vector;
+hour_stats_vector hour_stats(24);
+
+static bandwidth_map::iterator add_bandwidth_entry(const std::string& 
packet_type)
+{
+    time_t now = time(0);
+    struct tm * timeinfo = localtime(&now);
+    int hour = timeinfo->tm_hour;
+    int day = timeinfo->tm_mday;
+    assert(hour < 24 && hour >= 0);
+    std::pair<bandwidth_map::iterator,bool> insertion = 
hour_stats[hour].insert(std::make_pair(packet_type, bandwidth_stats()));
+    bandwidth_map::iterator inserted = insertion.first;
+    if (!insertion.second && day != inserted->second.day)
+    {
+        // clear previuos day stats
+        hour_stats[hour].clear();
+        //insert again to cleared map
+        insertion = hour_stats[hour].insert(std::make_pair(packet_type, 
bandwidth_stats()));
+        inserted = insertion.first;
+    }
+
+    inserted->second.day = day;
+    return inserted;
+}
+
+typedef boost::shared_ptr<bandwidth_stats> bandwidth_stats_ptr;
+
+
+struct bandwidth_stats_output {
+    bandwidth_stats_output(std::stringstream& ss) : ss_(ss), totals_(new 
bandwidth_stats())
+    {}
+    void operator()(const bandwidth_map::value_type& stats)
+    {
+        // name
+        ss_ << " " << std::setw(bandwidth_stats::type_width) <<  stats.first 
<< "| "
+            << std::setw(bandwidth_stats::packet_width)<< 
stats.second.out_packets << "| "
+            << std::setw(bandwidth_stats::bytes_width) << 
stats.second.out_bytes/1024 << "| "
+            << std::setw(bandwidth_stats::packet_width)<< 
stats.second.in_packets << "| "
+            << std::setw(bandwidth_stats::bytes_width) << 
stats.second.in_bytes/1024 << "\n";
+        *totals_ += stats.second;
+    }
+    void output_totals()
+    {
+        (*this)(std::make_pair(std::string("total"), *totals_));
+    }
+    private:
+    std::stringstream& ss_;
+    bandwidth_stats_ptr totals_;
+};
+
+std::string get_bandwidth_stats_all()
+{
+    std::string result;
+    for (int hour = 0; hour < 24; ++hour)
+    {
+        result += get_bandwidth_stats(hour);
+    }
+    return result;
+}
+
+std::string get_bandwidth_stats()
+{
+    time_t now = time(0);
+    struct tm * timeinfo = localtime(&now);
+    int hour = timeinfo->tm_hour - 1;
+    if (hour < 0)
+        hour = 23;
+    return get_bandwidth_stats(hour);
+}
+
+std::string get_bandwidth_stats(int hour)
+{
+    assert(hour < 24 && hour >= 0);
+    std::stringstream ss;
+
+    ss << "Hour stat starting from " << hour << "\n " << std::left << 
std::setw(bandwidth_stats::type_width) <<  "Type of packet" << "| "
+        << std::setw(bandwidth_stats::packet_width)<< "out #"  << "| "
+        << std::setw(bandwidth_stats::bytes_width) << "out kb" << "| " /* 
Are these bytes or bits? base10 or base2? */
+        << std::setw(bandwidth_stats::packet_width)<< "in #"  << "| "
+        << std::setw(bandwidth_stats::bytes_width) << "in kb" << "\n";
+
+    bandwidth_stats_output outputer(ss);
+    std::for_each(hour_stats[hour].begin(), hour_stats[hour].end(), 
outputer);
+
+    outputer.output_totals();
+    return ss.str();
+}
+
+void add_bandwidth_out(const std::string& packet_type, size_t len)
+{
+    bandwidth_map::iterator itor = add_bandwidth_entry(packet_type);
+    itor->second.out_bytes += len;
+    ++(itor->second.out_packets);
+}
+
+void add_bandwidth_in(const std::string& packet_type, size_t len)
+{
+    bandwidth_map::iterator itor = add_bandwidth_entry(packet_type);
+    itor->second.in_bytes += len;
+    ++(itor->second.in_packets);
+}
+
+    bandwidth_in::~bandwidth_in()
+    {
+        add_bandwidth_in(type_, len_);
+    }
+
+void send_file(const std::string& /*filename*/, connection 
/*connection_num*/, const std::string& /*packet_type*/)
+{
+    throw("TODO:Not implemented");
+}
+
+/**
+ * @todo Note the gzipped parameter should be removed later, we want to send
+ * all data gzipped. This can be done once the campaign server is also 
updated
+ * to work with gzipped data.
+ */
+size_t send_data(const config& /*cfg*/,
+                 connection /*connection_num*/,
+                 const bool /*gzipped*/,
+                 const std::string& /*packet_type*/)
+{
+    throw("TODO:Not implemented");
+}
+
+void send_raw_data(const char* /*buf*/,
+                   int /*len*/,
+                   connection /*connection_num*/,
+                   const std::string& /*packet_type*/)
+{
+    throw("TODO:Not implemented");
+}
+
+void process_send_queue(connection, size_t)
+{
+    check_error();
+}
+
+/** @todo Note the gzipped parameter should be removed later. */
+void send_data_all_except(const config& /*cfg*/,
+                          connection /*connection_num*/,
+                          const bool /*gzipped*/,
+                          const std::string& /*packet_type*/)
+{
+}
+
+std::string ip_address(connection /*connection_num*/)
+{
+    throw("TODO:Not implemented");
+}
+
+statistics get_send_stats(connection /*handle*/)
+{
+    throw("TODO:Not implemented");
+}
+statistics get_receive_stats(connection /*handle*/)
+{
+    throw("TODO:Not implemented");
+}
+
+} // end namespace network

Propchange: trunk/src/network_ana.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: trunk/src/network_ana.cpp
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"




Related Messages


Powered by MHonArc, Updated Thu Jun 17 23:20:14 2010