Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 1 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 2 | * Copyright (C) 2013 Regents of the University of California. |
| 3 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
Jeff Thompson | b9e3c8e | 2013-08-02 11:42:51 -0700 | [diff] [blame] | 7 | #ifndef NDN_FACE_HPP |
Jeff Thompson | a0d18c9 | 2013-08-06 13:55:32 -0700 | [diff] [blame] | 8 | #define NDN_FACE_HPP |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 9 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 10 | #include "node.hpp" |
Jeff Thompson | fb29cda | 2013-08-24 10:26:54 -0700 | [diff] [blame] | 11 | #include "transport/tcp-transport.hpp" |
Jeff Thompson | beb8b7d | 2013-07-16 15:49:21 -0700 | [diff] [blame] | 12 | |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 13 | namespace ndn { |
| 14 | |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 15 | /** |
| 16 | * The Face class provides the main methods for NDN communication. |
| 17 | */ |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 18 | class Face { |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 19 | public: |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 20 | /** |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 21 | * Create a new Face for communication with an NDN hub with the given Transport object and connectionInfo. |
| 22 | * @param transport A shared_ptr to a Transport object used for communication. |
| 23 | * @param transport A shared_ptr to a Transport::ConnectionInfo to be used to connect to the transport. |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 24 | */ |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 25 | Face(const ptr_lib::shared_ptr<Transport>& transport, const ptr_lib::shared_ptr<const Transport::ConnectionInfo>& connectionInfo) |
Jeff Thompson | 10e3438 | 2013-08-22 13:34:46 -0700 | [diff] [blame] | 26 | : node_(transport, connectionInfo) |
Jeff Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 27 | { |
Jeff Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 28 | } |
| 29 | |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 30 | /** |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 31 | * Create a new Face for communication with an NDN hub at host:port using the default TcpTransport. |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 32 | * @param host The host of the NDN hub. |
Jeff Thompson | fb29cda | 2013-08-24 10:26:54 -0700 | [diff] [blame] | 33 | * @param port The port of the NDN hub. If omitted. use 9695. |
Jeff Thompson | fe08e5a | 2013-08-13 11:15:59 -0700 | [diff] [blame] | 34 | */ |
Jeff Thompson | fb29cda | 2013-08-24 10:26:54 -0700 | [diff] [blame] | 35 | Face(const char *host, unsigned short port = 9695) |
| 36 | : node_(ptr_lib::make_shared<TcpTransport>(), |
| 37 | ptr_lib::make_shared<TcpTransport::ConnectionInfo>(host, port)) |
Jeff Thompson | b7d059d | 2013-07-31 10:59:51 -0700 | [diff] [blame] | 38 | { |
| 39 | } |
Jeff Thompson | fb29cda | 2013-08-24 10:26:54 -0700 | [diff] [blame] | 40 | |
Jeff Thompson | 4fe4551 | 2013-08-23 14:06:38 -0700 | [diff] [blame] | 41 | /** |
| 42 | * Send the Interest through the transport, read the entire response and call onData(interest, data). |
| 43 | * @param interest A reference to the Interest. This copies the Interest. |
| 44 | * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to |
| 45 | * use func_lib::ref() as appropriate. |
| 46 | * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it. |
| 47 | * This copies the function object, so you may need to use func_lib::ref() as appropriate. |
| 48 | */ |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 49 | void expressInterest(const Interest& interest, const OnData& onData, const OnTimeout& onTimeout = OnTimeout()) |
Jeff Thompson | 4fe4551 | 2013-08-23 14:06:38 -0700 | [diff] [blame] | 50 | { |
| 51 | node_.expressInterest(interest, onData, onTimeout); |
| 52 | } |
| 53 | |
| 54 | /** |
Jeff Thompson | c172be3 | 2013-07-16 15:08:05 -0700 | [diff] [blame] | 55 | * Encode name as an Interest. If interestTemplate is not 0, use its interest selectors. |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 56 | * Send the interest through the transport, read the entire response and call onData(interest, data). |
Jeff Thompson | 4fe4551 | 2013-08-23 14:06:38 -0700 | [diff] [blame] | 57 | * @param name A reference to a Name for the interest. This copies the Name. |
Jeff Thompson | c172be3 | 2013-07-16 15:08:05 -0700 | [diff] [blame] | 58 | * @param interestTemplate if not 0, copy interest selectors from the template. This does not keep a pointer to the Interest object. |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 59 | * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to |
| 60 | * use func_lib::ref() as appropriate. |
| 61 | * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it. |
| 62 | * This copies the function object, so you may need to use func_lib::ref() as appropriate. |
Jeff Thompson | c172be3 | 2013-07-16 15:08:05 -0700 | [diff] [blame] | 63 | */ |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 64 | void expressInterest(const Name& name, const Interest *interestTemplate, const OnData& onData, const OnTimeout& onTimeout = OnTimeout()); |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * Encode name as an Interest, using a default interest lifetime. |
| 68 | * Send the interest through the transport, read the entire response and call onData(interest, data). |
Jeff Thompson | 4fe4551 | 2013-08-23 14:06:38 -0700 | [diff] [blame] | 69 | * @param name A reference to a Name for the interest. This copies the Name. |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 70 | * @param onData A function object to call when a matching data packet is received. This copies the function object, so you may need to |
| 71 | * use func_lib::ref() as appropriate. |
| 72 | * @param onTimeout A function object to call if the interest times out. If onTimeout is an empty OnTimeout(), this does not use it. |
| 73 | * This copies the function object, so you may need to use func_lib::ref() as appropriate. |
| 74 | */ |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 75 | void expressInterest(const Name& name, const OnData& onData, const OnTimeout& onTimeout = OnTimeout()) |
Jeff Thompson | 7aec025 | 2013-08-22 17:29:57 -0700 | [diff] [blame] | 76 | { |
Jeff Thompson | fb29cda | 2013-08-24 10:26:54 -0700 | [diff] [blame] | 77 | expressInterest(name, 0, onData, onTimeout); |
Jeff Thompson | cdf7e25 | 2013-07-31 12:41:47 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 80 | /** |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 81 | * Register prefix with the connected NDN hub and call onInterest when a matching interest is received. |
| 82 | * @param prefix A reference to a Name for the prefix to register. This copies the Name. |
| 83 | * @param onInterest A function object to call when a matching interest is received. This copies the function object, so you may need to |
| 84 | * use func_lib::ref() as appropriate. |
| 85 | * @param flags The flags for finer control of which interests are forward to the application. |
| 86 | */ |
Jeff Thompson | 1656e6a | 2013-08-29 18:01:48 -0700 | [diff] [blame] | 87 | void registerPrefix(const Name& prefix, const OnInterest& onInterest, int flags = 0) |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 88 | { |
| 89 | node_.registerPrefix(prefix, onInterest, flags); |
| 90 | } |
Jeff Thompson | 86507bc | 2013-08-23 20:51:38 -0700 | [diff] [blame] | 91 | |
| 92 | /** |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 93 | * Process any data to receive or call timeout callbacks. |
Jeff Thompson | c7e0744 | 2013-08-19 15:25:43 -0700 | [diff] [blame] | 94 | * This is non-blocking and will return immediately if there is no data to receive. |
| 95 | * You should repeatedly call this from an event loop, with calls to sleep as needed so that the loop doesn't use 100% of the CPU. |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 96 | * @throw This may throw an exception for reading data or in the callback for processing the data. If you |
| 97 | * call this from an main event loop, you may want to catch and log/disregard all exceptions. |
| 98 | */ |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 99 | void processEvents() |
| 100 | { |
| 101 | // Just call Node's processEvents. |
| 102 | node_.processEvents(); |
| 103 | } |
Jeff Thompson | 432c8be | 2013-08-09 16:16:08 -0700 | [diff] [blame] | 104 | |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 105 | /** |
| 106 | * Shut down and disconnect this Face. |
| 107 | */ |
Jeff Thompson | 517ffa8 | 2013-08-05 16:04:34 -0700 | [diff] [blame] | 108 | void shutdown(); |
| 109 | |
Jeff Thompson | b982b6d | 2013-07-15 18:15:45 -0700 | [diff] [blame] | 110 | private: |
Jeff Thompson | bf50a1a | 2013-08-20 18:01:01 -0700 | [diff] [blame] | 111 | Node node_; |
Jeff Thompson | aa4e6db | 2013-07-15 17:25:23 -0700 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | } |
| 115 | |
| 116 | #endif |