remote_client.cpp 1.47 KB
Newer Older
1
#include <network/remote_client.hpp>
louiz’'s avatar
louiz’ committed
2

3
RemoteClient::RemoteClient(boost::asio::io_service& io_service,
4 5
                           Server<RemoteClient>* server):
  RemoteClientBase(io_service),
6
  server(server)
louiz’'s avatar
louiz’ committed
7 8 9
{
}

louiz’'s avatar
louiz’ committed
10
RemoteClient::~RemoteClient()
louiz’'s avatar
louiz’ committed
11
{
12
  log_info("Deleting remote client " << this->id);
13 14
}

louiz’'s avatar
louiz’ committed
15 16
void RemoteClient::install_callbacks()
{
17 18
  this->install_callback("AUTH", std::bind(&RemoteClient::auth_callback, this, std::placeholders::_1));
  this->install_callback("TRANSFER", std::bind(&RemoteClient::transfer_callback, this, std::placeholders::_1));
louiz’'s avatar
louiz’ committed
19 20
}

louiz’'s avatar
louiz’ committed
21
void RemoteClient::auth_callback(Message*)
louiz’'s avatar
louiz’ committed
22
{
23 24
}

25
void RemoteClient::transfer_callback(Message* received_message)
26
{
27
  this->send_file(std::string(received_message->body, received_message->body_size));
28 29 30 31 32
}

void RemoteClient::on_auth_success()
{
  log_debug("on_auth_success");
louiz’'s avatar
louiz’ committed
33 34
}

35 36 37 38 39 40 41 42 43 44 45 46
void RemoteClient::send_file(const std::string& filename)
{
  TransferSender* sender = new TransferSender(this, filename);
  if (sender->start() == true)
    {
      this->senders.push_back(sender);
      sender->send_next_chunk();
    }
  else
    delete sender;
}

47 48 49 50
void RemoteClient::on_connection_closed()
{
  this->server->remove_client(this);
}
51

louiz’'s avatar
louiz’ committed
52
void RemoteClient::on_transfer_ended(const TransferSender* transfer)
53 54 55 56 57
{
  log_debug("on_transfer_ended");
  std::vector<TransferSender*>::iterator it;
  for (it = this->senders.begin(); it < this->senders.end(); ++it)
    if (*it == transfer)
58
      {
louiz’'s avatar
louiz’ committed
59 60
     this->senders.erase(it);
     break ;
61
      }
62 63
  delete transfer;
}