blob: bdfdd2cd9514b881e1347110235004350c0fd85f [file] [log] [blame]
Alexander Afanasyevf278db32013-01-21 14:41:01 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
19 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
20 */
21
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080022#ifndef CCNX_WRAPPER_H
23#define CCNX_WRAPPER_H
24
Zhenkai Zhu7f4258e2012-12-29 19:55:13 -080025#include <boost/thread/locks.hpp>
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080026#include <boost/thread/recursive_mutex.hpp>
27#include <boost/thread/thread.hpp>
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080028
29#include "ccnx-common.h"
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080030#include "ccnx-name.h"
Zhenkai Zhua6472e02013-01-06 14:33:37 -080031#include "ccnx-selectors.h"
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080032#include "ccnx-closure.h"
Alexander Afanasyevf278db32013-01-21 14:41:01 -080033#include "ccnx-pco.h"
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080034
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080035namespace Ccnx {
36
Alexander Afanasyevf278db32013-01-21 14:41:01 -080037struct CcnxOperationException : virtual boost::exception, virtual std::exception { };
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080038
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080039class CcnxWrapper
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080040{
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080041public:
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080042 const static int MAX_FRESHNESS = 2147; // max value for ccnx
43 const static int DEFAULT_FRESHNESS = 60;
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080044 typedef boost::function<void (const Name &)> InterestCallback;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080045
46 CcnxWrapper();
47 virtual ~CcnxWrapper();
48
49 virtual int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080050 setInterestFilter (const Name &prefix, const InterestCallback &interestCallback);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080051
52 virtual void
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080053 clearInterestFilter (const Name &prefix);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080054
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080055 virtual int
Alexander Afanasyevd6c2a902013-01-19 21:24:30 -080056 sendInterest (const Name &interest, const Closure &closure, const Selectors &selector = Selectors());
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080057
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080058 virtual int
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080059 publishData (const Name &name, const unsigned char *buf, size_t len, int freshness = DEFAULT_FRESHNESS/* max value for ccnx*/);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080060
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080061 int
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080062 publishData (const Name &name, const Bytes &content, int freshness = DEFAULT_FRESHNESS/* max value for ccnx*/);
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080063
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080064 static Name
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080065 getLocalPrefix ();
66
Zhenkai Zhu43eb2732012-12-28 00:48:26 -080067 Bytes
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080068 createContentObject(const Name &name, const void *buf, size_t len, int freshness = DEFAULT_FRESHNESS/* max value for ccnx*/);
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080069
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080070 int
Zhenkai Zhubad089c2012-12-28 10:28:27 -080071 putToCcnd (const Bytes &contentObject);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080072
Zhenkai Zhu90ef9df2013-01-04 22:31:59 -080073private:
74 CcnxWrapper(const CcnxWrapper &other) {}
75
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080076protected:
77 void
78 connectCcnd();
79
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080080 /// @cond include_hidden
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080081 void
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080082 ccnLoop ();
83
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080084 /// @endcond
85
86protected:
Alexander Afanasyev66f4c492013-01-20 23:32:50 -080087 typedef boost::shared_mutex Lock;
88 typedef boost::unique_lock<Lock> WriteLock;
89 typedef boost::shared_lock<Lock> ReadLock;
90
91 typedef boost::recursive_mutex RecLock;
92 typedef boost::unique_lock<RecLock> UniqueRecLock;
93
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080094 ccn* m_handle;
Zhenkai Zhu7f4258e2012-12-29 19:55:13 -080095 RecLock m_mutex;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080096 boost::thread m_thread;
97 bool m_running;
98 bool m_connected;
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -080099 std::map<Name, InterestCallback> m_registeredInterests;
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800100};
101
102typedef boost::shared_ptr<CcnxWrapper> CcnxWrapperPtr;
103
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -0800104
105} // Ccnx
106
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800107#endif