blob: 27bceefcbefc779d8f8baa698a1d174663520b50 [file] [log] [blame]
// Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "common/server_message.h"
#include "common/clock_interface.h"
#include "http_server/server_interface.h"
#include <string>
#include <map>
#include <glib.h>
#include <base/command_line.h>
#include <base/files/file_path.h>
#include <base/memory/scoped_ptr.h>
#include <base/threading/simple_thread.h>
namespace p2p {
namespace http_server {
class Server : public ServerInterface {
// Constructs a new Server object.
// This constructor doesn't start the server - to start listening on
// the socket, the Start() method will need to be called.
// If |port| is 0, the kernel assigns a random free port that can be
// retrieved with Port(). The Server will report messages as
// P2PServerMessage strunct on the |message_fd|.
Server(const base::FilePath& directory, uint16 port, int message_fd,
ConnectionDelegateFactory delegate_factory);
virtual ~Server();
// ServerInterface override methods.
virtual bool Start();
virtual void Stop();
virtual void SetMaxDownloadRate(int64_t bytes_per_sec);
virtual uint16 Port();
virtual int NumConnections();
virtual p2p::common::ClockInterface* Clock();
virtual void ConnectionTerminated(ConnectionDelegateInterface* delegate);
virtual void ReportServerMessage(p2p::util::P2PServerMessageType msg_type,
int64_t value);
// Callback used clients connect to our listening socket.
static gboolean OnIOChannelActivity(GIOChannel* source,
GIOCondition condition,
gpointer data);
// Updates number of connections. May be called from any thread.
// As a side-effect, prints the number of connection on stdout
// for reporting to higher-level code.
void UpdateNumConnections(int delta_num_connections);
// Clock used for time-keeping and sleeping.
scoped_ptr<p2p::common::ClockInterface> clock_;
// Thread pool used for worker threads.
base::DelegateSimpleThreadPool thread_pool_;
// The path of the directory we're serving .p2p files from.
base::FilePath directory_;
// The file descriptor for the directory corresponding to |directory_|.
int dirfd_;
// The TCP port to listen on.
uint16 port_;
// The socket where the P2PServerMessage is reported.
int message_fd_;
// The maximum download rate of 0 if there is no limit.
int64_t max_download_rate_;
// Set to true only if the server is running.
bool started_;
// The file descriptor for the socket we're listening on.
int listen_fd_;
// The GLib source id for our socket.
guint listen_source_id_;
// The current number of connected clients.
int num_connections_;
// Object-wide lock.
base::Lock lock_;
// A ConnectionDelegateInterface factory used to serve the connections.
ConnectionDelegateFactory* delegate_factory_;
} // namespace http_server
} // namespace p2p