blob: 59af6d19a4f86d2d1f033232dcaaece19a62cf02 [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompsonfcf347d2013-07-15 11:30:44 -07002/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompsonfcf347d2013-07-15 11:30:44 -07005 * See COPYING for copyright and distribution information.
6 */
7
8#ifndef NDN_TRANSPORT_HPP
Jeff Thompson2d27e2f2013-08-09 12:55:00 -07009#define NDN_TRANSPORT_HPP
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070010
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080011#include <ndn-cpp/common.hpp>
12
Jeff Thompsonb605b5d2013-07-30 15:12:56 -070013#include <vector>
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080014#include <boost/asio.hpp>
Jeff Thompsonb605b5d2013-07-30 15:12:56 -070015
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070016namespace ndn {
Jeff Thompson0cb7aee2013-07-16 16:18:06 -070017
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070018class Transport {
19public:
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080020 typedef ptr_lib::function<void (const Block &wire)> ReceiveCallback;
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080021 typedef ptr_lib::function<void ()> ErrorCallback;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080022
23 inline
24 Transport();
25
26 inline virtual
27 ~Transport();
28
Jeff Thompson2ed62fb2013-07-16 18:10:30 -070029 /**
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080030 * Connect transport
31 *
32 * @throws If connection cannot be established
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080033 */
34 inline virtual void
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080035 connect(boost::asio::io_service &io_service,
36 const ReceiveCallback &receiveCallback,
37 const ErrorCallback &errorCallback);
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080038
39 /**
40 * Close the connection.
Jeff Thompson10e34382013-08-22 13:34:46 -070041 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070042 virtual void
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080043 close() = 0;
44
Jeff Thompson432c8be2013-08-09 16:16:08 -070045 /**
46 * Set data to the host
47 * @param data A pointer to the buffer of data to send.
48 * @param dataLength The number of bytes in data.
49 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070050 virtual void
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080051 send(const Block &wire) = 0;
Jeff Thompson7098dd62013-08-06 14:42:02 -070052
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080053 inline bool
54 isConnected();
55
56protected:
57 inline void
58 receive(const Block &wire);
Jeff Thompsona4056972013-08-22 11:52:21 -070059
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080060protected:
61 boost::asio::io_service *ioService_;
62 bool isConnected_;
63 ReceiveCallback receiveCallback_;
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080064 ErrorCallback errorCallback_;
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070065};
66
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080067inline
68Transport::Transport()
69 : ioService_(0)
70 , isConnected_(false)
71{
72}
73
74inline
75Transport::~Transport()
76{
77}
78
79inline void
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080080Transport::connect(boost::asio::io_service &ioService,
81 const ReceiveCallback &receiveCallback,
82 const ErrorCallback &errorCallback)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080083{
84 ioService_ = &ioService;
85 receiveCallback_ = receiveCallback;
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080086 errorCallback_ = errorCallback;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080087}
88
89inline bool
90Transport::isConnected()
91{
92 return isConnected_;
93}
94
95inline void
96Transport::receive(const Block &wire)
97{
98 receiveCallback_(wire);
99}
100
Jeff Thompsonfcf347d2013-07-15 11:30:44 -0700101}
102
103#endif