blob: cefd73c03bec54b5395e9169a6582233eb3c3e29 [file] [log] [blame]
Zhenkai Zhue25def92012-03-12 16:39:25 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012 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>
Chaoyi Bian3e1eb162012-04-03 16:59:32 -070019 * Chaoyi Bian <bcy@pku.edu.cn>
Zhenkai Zhue25def92012-03-12 16:39:25 -070020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
22
Chaoyi Bian9d8768a2012-03-14 17:23:36 -070023#include "sync-app-socket.h"
Chaoyi Bian2f2d6412012-06-04 16:53:08 -070024#include <boost/shared_array.hpp>
Zhenkai Zhue25def92012-03-12 16:39:25 -070025using namespace std;
26using namespace Sync;
27
Chaoyi Bian2f2d6412012-06-04 16:53:08 -070028struct SyncAppSocketStruct;
29struct MissingDataInfoC
30{
31 const char *prefix;
32 int session;
33 int low;
34 int high;
35};
36
37class CallbackHolder {
Chaoyi Bian98135192012-03-27 18:34:11 -070038private:
Alexander Afanasyev03a58b72012-03-12 18:11:56 -070039 void (*m_callback)(const char *, const char *);
Chaoyi Bian98135192012-03-27 18:34:11 -070040
41public:
Chaoyi Bian2f2d6412012-06-04 16:53:08 -070042 CallbackHolder(void (*callback)(const char*, const char*))
43 :m_callback(callback){};
44
45 void callbackWrapper(const string &name, const string &data) {
46 if (m_callback != NULL)
47 (*m_callback)(name.c_str(), data.c_str());
Alexander Afanasyev03a58b72012-03-12 18:11:56 -070048 }
Zhenkai Zhue25def92012-03-12 16:39:25 -070049};
50
Chaoyi Bian2f2d6412012-06-04 16:53:08 -070051class UpdateCallbackHolder {
52private:
53 void (*m_callback)(const struct MissingDataInfoC*, const int, const SyncAppSocketStruct*);
54
55public:
56 UpdateCallbackHolder(void (*callback)(
57 const MissingDataInfoC*,
58 const int,
59 const SyncAppSocketStruct*))
60 :m_callback(callback){};
61
62 void callbackWrapper(const vector<MissingDataInfo> &mdi, SyncAppSocket* socket) {
63 boost::shared_array<MissingDataInfoC> mdic(new MissingDataInfoC[mdi.size()]);
64 int i;
65
66 for (i = 0; i < mdi.size(); i++)
67 {
68 mdic[i].prefix = mdi[i].prefix.c_str();
69 mdic[i].session = mdi[i].low.getSession();
70 mdic[i].low = mdi[i].low.getSeq();
71 mdic[i].high = mdi[i].high.getSeq();
72 }
73 if (m_callback != NULL)
74 (*m_callback)(mdic.get(), i, (const SyncAppSocketStruct*) socket);
75 }
76};
77
78class RemoveCallbackHolder {
79private:
80 void (*m_callback)(const char*);
81
82public:
83 RemoveCallbackHolder(void (*callback)(const char*)):m_callback(callback){};
84 void callbackWrapper(const string &prefix) {
85 if (m_callback != NULL)
86 (*m_callback)(prefix.c_str());
87 }
88};
Chaoyi Bian9d8768a2012-03-14 17:23:36 -070089
90extern "C"
Zhenkai Zhue25def92012-03-12 16:39:25 -070091SyncAppSocketStruct *
Chaoyi Bian2f2d6412012-06-04 16:53:08 -070092create_sync_app_socket(const char *prefix,
93 void (*updatecallback)(const MissingDataInfoC*, const int, const SyncAppSocketStruct*),
94 void (*removecallback)(const char *))
Alexander Afanasyev03a58b72012-03-12 18:11:56 -070095{
Chaoyi Bian2f2d6412012-06-04 16:53:08 -070096 boost::shared_ptr<UpdateCallbackHolder> uh(new UpdateCallbackHolder(updatecallback));
97 boost::shared_ptr<RemoveCallbackHolder> rh(new RemoveCallbackHolder(removecallback));
98 boost::function<void (const vector<MissingDataInfo>&, SyncAppSocket*)> ucb = bind(&UpdateCallbackHolder::callbackWrapper, uh, _1, _2);
99 boost::function<void (const string&)> rcb = bind(&RemoveCallbackHolder::callbackWrapper, rh, _1);
100 SyncAppSocket *sock = new SyncAppSocket(prefix, ucb, rcb);
Alexander Afanasyev03a58b72012-03-12 18:11:56 -0700101 return (SyncAppSocketStruct *) sock;
102}
Zhenkai Zhue25def92012-03-12 16:39:25 -0700103
Chaoyi Bian9d8768a2012-03-14 17:23:36 -0700104extern "C"
Zhenkai Zhue25def92012-03-12 16:39:25 -0700105void
106delete_sync_app_socket(SyncAppSocketStruct **sock) {
Alexander Afanasyev03a58b72012-03-12 18:11:56 -0700107 SyncAppSocket *temp = *((SyncAppSocket **)sock);
108 delete temp;
109 temp = NULL;
110}
Zhenkai Zhue25def92012-03-12 16:39:25 -0700111
112// assume char *buf ends with '\0', or otherwise it's going to crash;
Chaoyi Bian9d8768a2012-03-14 17:23:36 -0700113// should fix this "feature"
114extern "C"
115int
116sync_app_socket_publish(SyncAppSocketStruct *sock, const char *prefix, int session, const char *buf, int freshness)
Alexander Afanasyev03a58b72012-03-12 18:11:56 -0700117{
Chaoyi Bian2f2d6412012-06-04 16:53:08 -0700118 SyncAppSocket *temp = (SyncAppSocket*) sock;
119 return temp->publishString(prefix, session, buf, freshness);
Alexander Afanasyev03a58b72012-03-12 18:11:56 -0700120}
Zhenkai Zhue25def92012-03-12 16:39:25 -0700121
Chaoyi Bian9d8768a2012-03-14 17:23:36 -0700122extern "C"
Zhenkai Zhue25def92012-03-12 16:39:25 -0700123void
124sync_app_socket_remove(SyncAppSocketStruct *sock, const char *prefix)
Alexander Afanasyev03a58b72012-03-12 18:11:56 -0700125{
Chaoyi Bian2f2d6412012-06-04 16:53:08 -0700126 SyncAppSocket *temp = (SyncAppSocket*) sock;
Alexander Afanasyev03a58b72012-03-12 18:11:56 -0700127 temp->remove(prefix);
128}
Chaoyi Bian2f2d6412012-06-04 16:53:08 -0700129
130extern "C"
131void
132sync_app_socket_fetch(SyncAppSocketStruct *sock, const char *prefix, int session, int sequence,
133 void (*callback)(const char*, const char*), int retry = 0)
134{
135 SyncAppSocket *temp = (SyncAppSocket*) sock;
136 string s(prefix);
137 SeqNo seq(session, sequence);
138 boost::shared_ptr<CallbackHolder> h(new CallbackHolder(callback));
139 boost::function<void (const string&, const string&)> cb = bind(&CallbackHolder::callbackWrapper, h, _1, _2);
140
141 temp->fetchString(s, seq, cb, retry);
142}