remote_client.cpp 3.56 KB
Newer Older
1
#include <master_server/remote_client.hpp>
2 3 4
#include <master_server/master_to_client_server.hpp>

#include <database/database.hpp>
5
#include "master.pb.h"
louiz’'s avatar
louiz’ committed
6

louiz’'s avatar
louiz’ committed
7 8
boost::asio::ssl::context RemoteClient::context(boost::asio::ssl::context::tlsv1);

9
RemoteClient::RemoteClient():
louiz’'s avatar
louiz’ committed
10
  RemoteClientBase<TLSSocket>(RemoteClient::context),
11 12 13
  user(nullptr),
  server(nullptr),
  senders{}
louiz’'s avatar
louiz’ committed
14 15 16
{
}

louiz’'s avatar
louiz’ committed
17
RemoteClient::~RemoteClient()
louiz’'s avatar
louiz’ committed
18
{
19
  log_info("Deleting remote client " << this->id);
20 21
}

louiz’'s avatar
louiz’ committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
void RemoteClient::start()
{
  log_debug("Doing handshake now " << this->get_id());
  this->get_stream().async_handshake(TLSSocket::ssl_socket_type::server,
                     [this](const boost::system::error_code& error)
                         {
                           if (error)
                             {
                               log_error("Handshake failed: " << error << " (id)" << this->get_id());
                               this->get_stream().shutdown();
                             }
                           else
                             RemoteClientBase::start();
                         });
}

38
void RemoteClient::set_server(MasterToClientServer* server)
39 40 41 42 43 44 45 46 47
{
  this->server = server;
}

const db::User* RemoteClient::get_user() const
{
  return this->user.get();
}

louiz’'s avatar
louiz’ committed
48 49
void RemoteClient::install_callbacks()
{
50 51
  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
52 53
}

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
void RemoteClient::auth_callback(Message* message)
{
  auto srl = message->parse_body_to_protobuf_object<ser::master::Authenticate>();
  ser::master::AuthResponse response;
  auto db = this->server->get_database();
  auto maybe_user = db->get_user_by_login(srl.login());
  if (!maybe_user)
    response.set_result(ser::master::AuthResponse::ERROR_INVALID_LOGIN);
  else
    {
      auto user = *maybe_user;
      auto res = db->check_user_password(user, srl.password());
      if (std::get<0>(res) == true)
        {
          this->user = std::make_unique<db::User>(user);
          response.set_result(ser::master::AuthResponse::SUCCESS);
        }
      else
        response.set_result(ser::master::AuthResponse::ERROR_INVALID_PASSWORD);
    }
  this->send_message("AUTH_RESPONSE", response);
  if (this->user)
    this->on_user_logged_in();
}


void RemoteClient::on_user_logged_in()
louiz’'s avatar
louiz’ committed
81
{
82
  // TODO send various information, the friend list, the news, etc
83 84
}

85
void RemoteClient::transfer_callback(Message* received_message)
86
{
87
  this->send_file(std::string(received_message->body, received_message->body_size));
88 89 90 91 92
}

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

95 96
void RemoteClient::send_file(const std::string& filename)
{
97 98 99 100 101
  TransferSender* sender = new TransferSender(this, filename,
                                              [this](const TransferSender* ts)
                                              {
                                                this->on_transfer_ended(ts);
                                              });
102 103 104 105 106 107 108 109 110
  if (sender->start() == true)
    {
      this->senders.push_back(sender);
      sender->send_next_chunk();
    }
  else
    delete sender;
}

111 112 113 114
void RemoteClient::on_connection_closed()
{
  this->server->remove_client(this);
}
115

louiz’'s avatar
louiz’ committed
116
void RemoteClient::on_transfer_ended(const TransferSender* transfer)
117 118 119 120 121
{
  log_debug("on_transfer_ended");
  std::vector<TransferSender*>::iterator it;
  for (it = this->senders.begin(); it < this->senders.end(); ++it)
    if (*it == transfer)
122
      {
louiz’'s avatar
louiz’ committed
123 124
     this->senders.erase(it);
     break ;
125
      }
126 127
  delete transfer;
}