blob: 517a8323eb7a20447cbfd23a94d0355a9693cf5b [file] [log] [blame]
Jeff Thompsonfa306642013-06-17 15:06:57 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013, Regents of the University of California
4 * Alexander Afanasyev
5 * Zhenkai Zhu
6 *
7 * BSD license, See the LICENSE file for more information
8 *
9 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
10 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
11 */
12
13#ifndef NDN_WRAPPER_H
14#define NDN_WRAPPER_H
15
16#include <boost/thread/locks.hpp>
17#include <boost/thread/recursive_mutex.hpp>
18#include <boost/thread/thread.hpp>
19
20#include "ndn.cxx/common.h"
21#include "ndn.cxx/fields/name.h"
22#include "ndn.cxx/interest.h"
23#include "ndn.cxx/closure.h"
24#include "ndn.cxx/pco.h"
25
26class Executor;
27typedef boost::shared_ptr<Executor> ExecutorPtr;
28
29namespace ndn {
30
31class Verifier;
32class Wrapper
33{
34public:
35 const static int MAX_FRESHNESS = 2147; // max value for ccnx
36 const static int DEFAULT_FRESHNESS = 60;
37 typedef boost::function<void (InterestPtr)> InterestCallback;
38
39 Wrapper();
40 ~Wrapper();
41
42 void
43 start (); // called automatically in constructor
44
45 /**
46 * @brief Because of uncertainty with executor, in some case it is necessary to call shutdown explicitly (see test-server-and-fetch.cc)
47 */
48 void
49 shutdown (); // called in destructor, but can called manually
50
51 int
52 setInterestFilter (const Name &prefix, const InterestCallback &interestCallback, bool record = true);
53
54 void
55 clearInterestFilter (const Name &prefix, bool record = true);
56
57 int
58 sendInterest (const Interest &interest, const Closure &closure);
59
60 int
61 publishData (const Name &name, const unsigned char *buf, size_t len, int freshness = DEFAULT_FRESHNESS, const Name &keyName=Name());
62
63 inline int
64 publishData (const Name &name, const Bytes &content, int freshness = DEFAULT_FRESHNESS, const Name &keyName=Name());
65
66 inline int
67 publishData (const Name &name, const std::string &content, int freshness = DEFAULT_FRESHNESS, const Name &keyName=Name());
68
69 int
70 publishUnsignedData(const Name &name, const unsigned char *buf, size_t len, int freshness = DEFAULT_FRESHNESS);
71
72 inline int
73 publishUnsignedData(const Name &name, const Bytes &content, int freshness = DEFAULT_FRESHNESS);
74
75 inline int
76 publishUnsignedData(const Name &name, const std::string &content, int freshness = DEFAULT_FRESHNESS);
77
78 static Name
79 getLocalPrefix ();
80
81 Bytes
82 createContentObject(const Name &name, const void *buf, size_t len, int freshness = DEFAULT_FRESHNESS, const Name &keyNameParam=Name());
83
84 int
85 putToCcnd (const Bytes &contentObject);
86
87 bool
88 verify(PcoPtr &pco, double maxWait = 1 /*seconds*/);
89
90 PcoPtr
91 get (const Interest &interest, double maxWait = 4.0/*seconds*/);
92
93private:
94 Wrapper(const Wrapper &other) {}
95
96protected:
97 void
98 connectCcnd();
99
100 /// @cond include_hidden
101 void
102 ccnLoop ();
103
104 /// @endcond
105
106protected:
107 typedef boost::shared_mutex Lock;
108 typedef boost::unique_lock<Lock> WriteLock;
109 typedef boost::shared_lock<Lock> ReadLock;
110
111 typedef boost::recursive_mutex RecLock;
112 typedef boost::unique_lock<RecLock> UniqueRecLock;
113
114 ccn* m_handle;
115 RecLock m_mutex;
116 boost::thread m_thread;
117 bool m_running;
118 bool m_connected;
119 std::map<Name, InterestCallback> m_registeredInterests;
120 ExecutorPtr m_executor;
121 Verifier *m_verifier;
122};
123
124typedef boost::shared_ptr<Wrapper> WrapperPtr;
125
126/**
127 * @brief Namespace holding all exceptions that can be fired by the library
128 */
129namespace Error
130{
131struct ndnOperation : boost::exception, std::exception { };
132}
133
134inline int
135Wrapper::publishData (const Name &name, const Bytes &content, int freshness, const Name &keyName)
136{
137 return publishData(name, head(content), content.size(), freshness, keyName);
138}
139
140inline int
141Wrapper::publishData (const Name &name, const std::string &content, int freshness, const Name &keyName)
142{
143 return publishData(name, reinterpret_cast<const unsigned char *> (content.c_str ()), content.size (), freshness, keyName);
144}
145
146inline int
147Wrapper::publishUnsignedData(const Name &name, const Bytes &content, int freshness)
148{
149 return publishUnsignedData(name, head(content), content.size(), freshness);
150}
151
152inline int
153Wrapper::publishUnsignedData(const Name &name, const std::string &content, int freshness)
154{
155 return publishUnsignedData(name, reinterpret_cast<const unsigned char *> (content.c_str ()), content.size (), freshness);
156}
157
158
159} // ndn
160
161#endif