transfer_sender.hpp 1.78 KB
Newer Older
1 2 3 4
/** @addtogroup Network
 *  @{
 */

louiz’'s avatar
louiz’ committed
5
/**
louiz’'s avatar
louiz’ committed
6
 * Represent an outgoing file transfer. The file is sent by chunks, each in a
louiz’'s avatar
louiz’ committed
7 8 9 10 11 12 13 14 15 16
 * different message. The transfer has an ID that is used in the message's
 * command name, that way the receiver just needs to add a callback on that
 * ID to receive all the chunks for that specific file. Every chunk is sent as
 * soon as the previous one was correctly sent on the socket.
 * Two entities (e.g. one client with one server) can have any number of
 * simultaneous file transfers, without any confilct or delay. And a server
 * can send files to any number of clients.
 * @class TransferSender
 */

17
#include <string>
18
#include <boost/asio.hpp>
19 20
#include <boost/filesystem/fstream.hpp>
#include <boost/filesystem.hpp>
21 22 23 24 25

class TLSSocket;

template <typename SocketType>
class MessageHandler;
26 27 28 29

#ifndef __TRANSFER_SENDER_HPP__
# define __TRANSFER_SENDER_HPP__

30
static const boost::filesystem::path FILES_TO_SEND_DIRECTORY("./send/");
31 32 33 34 35

class TransferSender
{
public:
  static unsigned long int current_id;
36 37
  TransferSender(MessageHandler<TLSSocket>* client, const std::string&,
                 std::function<void(const TransferSender*)>&& end_callback);
38 39 40 41 42 43 44 45 46 47 48 49 50 51
  ~TransferSender();
  /**
   * Try to start the file transfer, returns true if there was
   * no error opening the file and reading, false otherwise (in
   * that case this object should immediately be deleted by the RemoteClient
   * object.
   */
  bool start();
  void send_next_chunk();

private:
  TransferSender(const TransferSender&);
  TransferSender& operator=(const TransferSender&);

52
  MessageHandler<TLSSocket>* client;
53
  const std::string filename;
54 55 56
  std::function<void(const TransferSender*)> end_callback;

  boost::filesystem::ifstream file;
57
  int length;
louiz’'s avatar
louiz’ committed
58
  std::string id;
59 60 61 62
};

#endif // __TRANSFER_SENDER_HPP__
/**@}*/