blob: 8bfa3552f8b006d469b58b1d9a2854dfe2021ffc [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;
21
22 inline
23 Transport();
24
25 inline virtual
26 ~Transport();
27
Jeff Thompson2ed62fb2013-07-16 18:10:30 -070028 /**
Jeff Thompson10e34382013-08-22 13:34:46 -070029 * Connect according to the info in ConnectionInfo, and processEvents() will use elementListener.
30 * @param connectionInfo A reference to an object of a subclass of ConnectionInfo.
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080031 */
32 inline virtual void
33 connect(boost::asio::io_service &io_service, const ReceiveCallback &receiveCallback);
34
35 /**
36 * Close the connection.
Jeff Thompson10e34382013-08-22 13:34:46 -070037 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070038 virtual void
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080039 close() = 0;
40
Jeff Thompson432c8be2013-08-09 16:16:08 -070041 /**
42 * Set data to the host
43 * @param data A pointer to the buffer of data to send.
44 * @param dataLength The number of bytes in data.
45 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070046 virtual void
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080047 send(const Block &wire) = 0;
Jeff Thompson7098dd62013-08-06 14:42:02 -070048
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080049 inline bool
50 isConnected();
51
52protected:
53 inline void
54 receive(const Block &wire);
Jeff Thompsona4056972013-08-22 11:52:21 -070055
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080056protected:
57 boost::asio::io_service *ioService_;
58 bool isConnected_;
59 ReceiveCallback receiveCallback_;
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070060};
61
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080062inline
63Transport::Transport()
64 : ioService_(0)
65 , isConnected_(false)
66{
67}
68
69inline
70Transport::~Transport()
71{
72}
73
74inline void
75Transport::connect(boost::asio::io_service &ioService, const ReceiveCallback &receiveCallback)
76{
77 ioService_ = &ioService;
78 receiveCallback_ = receiveCallback;
79}
80
81inline bool
82Transport::isConnected()
83{
84 return isConnected_;
85}
86
87inline void
88Transport::receive(const Block &wire)
89{
90 receiveCallback_(wire);
91}
92
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070093}
94
95#endif