blob: 064107bedb97b841f657a95712086d1a818b898e [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>
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -080015#include <boost/lexical_cast.hpp>
Jeff Thompsonb605b5d2013-07-30 15:12:56 -070016
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070017namespace ndn {
Jeff Thompson0cb7aee2013-07-16 16:18:06 -070018
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070019class Transport {
20public:
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -080021 struct Error : public std::runtime_error { inline Error(const boost::system::error_code &code, const std::string &msg); };
22
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080023 typedef ptr_lib::function<void (const Block &wire)> ReceiveCallback;
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080024 typedef ptr_lib::function<void ()> ErrorCallback;
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080025
26 inline
27 Transport();
28
29 inline virtual
30 ~Transport();
31
Jeff Thompson2ed62fb2013-07-16 18:10:30 -070032 /**
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080033 * Connect transport
34 *
35 * @throws If connection cannot be established
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080036 */
37 inline virtual void
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080038 connect(boost::asio::io_service &io_service,
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -080039 const ReceiveCallback &receiveCallback);
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080040
41 /**
42 * Close the connection.
Jeff Thompson10e34382013-08-22 13:34:46 -070043 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070044 virtual void
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080045 close() = 0;
46
Jeff Thompson432c8be2013-08-09 16:16:08 -070047 /**
48 * Set data to the host
49 * @param data A pointer to the buffer of data to send.
50 * @param dataLength The number of bytes in data.
51 */
Jeff Thompson0050abe2013-09-17 12:50:25 -070052 virtual void
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080053 send(const Block &wire) = 0;
Jeff Thompson7098dd62013-08-06 14:42:02 -070054
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080055 inline bool
56 isConnected();
57
58protected:
59 inline void
60 receive(const Block &wire);
Jeff Thompsona4056972013-08-22 11:52:21 -070061
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080062protected:
63 boost::asio::io_service *ioService_;
64 bool isConnected_;
65 ReceiveCallback receiveCallback_;
Jeff Thompsonfcf347d2013-07-15 11:30:44 -070066};
67
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080068inline
69Transport::Transport()
70 : ioService_(0)
71 , isConnected_(false)
72{
73}
74
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -080075inline Transport::Error::Error(const boost::system::error_code &code, const std::string &msg)
76 : std::runtime_error(msg + (code.value() ? " (" + code.category().message(code.value()) + ")" : ""))
77{
78}
79
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080080inline
81Transport::~Transport()
82{
83}
84
85inline void
Alexander Afanasyeva557d5a2013-12-28 21:59:03 -080086Transport::connect(boost::asio::io_service &ioService,
Alexander Afanasyev3ae2da22013-12-29 15:50:04 -080087 const ReceiveCallback &receiveCallback)
Alexander Afanasyeve2e0d752014-01-03 13:30:30 -080088{
89 ioService_ = &ioService;
90 receiveCallback_ = receiveCallback;
91}
92
93inline bool
94Transport::isConnected()
95{
96 return isConnected_;
97}
98
99inline void
100Transport::receive(const Block &wire)
101{
102 receiveCallback_(wire);
103}
104
Jeff Thompsonfcf347d2013-07-15 11:30:44 -0700105}
106
107#endif