remote_client.hpp 2.34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/** @addtogroup Network
 *  @{
 */

/**
 * Represents one single remote client. It is kept in a list, by the Server
 * object. Using the CommandHandler interface, it executes callbacks
 * upon receiving command from the client, and sends commands when
 * we need to communicate something to it.
 * @class RemoteClient
 */

louiz’'s avatar
louiz’ committed
13 14 15 16 17
#include <boost/asio.hpp>
#include <boost/bind.hpp>

#ifndef __REMOTE_CLIENT_HPP__
# define __REMOTE_CLIENT_HPP__
louiz’'s avatar
louiz’ committed
18

19 20 21

#include <database/user.hpp>
#include <network/command_handler.hpp>
22
#include <network/command.hpp>
louiz’'s avatar
louiz’ committed
23 24
#include <network/timed_event_handler.hpp>
#include <network/timed_event.hpp>
25
#include <network/transfer_sender.hpp>
26 27 28

class Server;

louiz’'s avatar
louiz’ committed
29 30
using boost::asio::ip::tcp;

louiz’'s avatar
louiz’ committed
31
class RemoteClient: public CommandHandler, public TimedEventHandler
louiz’'s avatar
louiz’ committed
32 33
{
public:
34
  RemoteClient(boost::asio::io_service&, Server*);
louiz’'s avatar
louiz’ committed
35
  ~RemoteClient();
36 37 38
  /**
   * starts the client (install the read handler, etc)
   */
39
  void start();
40 41 42
  /**
   * @return tcp::socket&
   */
louiz’'s avatar
louiz’ committed
43
  tcp::socket& get_socket(void);
44 45 46 47
  /**
   * The number of clients is incremented each time
   * a new client is accepted.
   */
48
  static unsigned long int clients_number;
louiz’'s avatar
louiz’ committed
49

50
  virtual void on_connection_closed();
51

louiz’'s avatar
louiz’ committed
52
  virtual boost::asio::io_service& get_io_service();
53 54 55 56 57 58 59 60 61 62
  /**
   * Sends a file to the remote client.
   * @param filename The file to send.
   */
  void send_file(const std::string&);
  /**
   * To be called whenever a file transfer ends.
   * Removes the TransferSender from the list.
   * @param transfer The TransferSender to remove from the list.
   */
louiz’'s avatar
louiz’ committed
63
  void on_transfer_ended(const TransferSender*);
64 65 66 67 68 69
  /**
   * Called when the client is successfully authenticated.
   * For example, checks if there are news to send, or offline messages, etc
   */
  void on_auth_success();

70
  User* get_user();
71

louiz’'s avatar
louiz’ committed
72
private:
73 74 75 76 77
  /**
   * Creates the default callbacks associated with a network command.
   * It is executed whenever that command is received.
   * See CommandHandler for details
   */
louiz’'s avatar
louiz’ committed
78
  void install_callbacks();
louiz’'s avatar
louiz’ committed
79
  void install_read_handler(void);
80 81
  void auth_callback(Command*);
  void transfer_callback(Command*);
82 83 84 85 86 87 88
  const unsigned long int number;
  /**
   * A pointer to the server, to call its method when the RemoteClient
   * has to be deleted.
   */
  Server* server;
  User* user;
89 90 91 92
  /**
   * A list of all the current file transfers with tha client.
   */
  std::vector<TransferSender*> senders;
louiz’'s avatar
louiz’ committed
93 94 95
};

#endif
96
/**@}*/