blob: e23c8d1b63080c31a2fea747625b3d5a08918918 [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();
Chaoyi Bian87832b72012-06-05 21:35:51 -070069 mdic[i].session = mdi[i].high.getSession();
70 if (mdi[i].low.getSession() != mdi[i].high.getSession())
71 mdic[i].low = 0;
72 else
73 mdic[i].low = mdi[i].low.getSeq();
Chaoyi Bian2f2d6412012-06-04 16:53:08 -070074 mdic[i].high = mdi[i].high.getSeq();
75 }
76 if (m_callback != NULL)
77 (*m_callback)(mdic.get(), i, (const SyncAppSocketStruct*) socket);
78 }
79};
80
81class RemoveCallbackHolder {
82private:
83 void (*m_callback)(const char*);
84
85public:
86 RemoveCallbackHolder(void (*callback)(const char*)):m_callback(callback){};
87 void callbackWrapper(const string &prefix) {
88 if (m_callback != NULL)
89 (*m_callback)(prefix.c_str());
90 }
91};
Chaoyi Bian9d8768a2012-03-14 17:23:36 -070092
93extern "C"
Zhenkai Zhue25def92012-03-12 16:39:25 -070094SyncAppSocketStruct *
Chaoyi Bian2f2d6412012-06-04 16:53:08 -070095create_sync_app_socket(const char *prefix,
96 void (*updatecallback)(const MissingDataInfoC*, const int, const SyncAppSocketStruct*),
97 void (*removecallback)(const char *))
Alexander Afanasyev03a58b72012-03-12 18:11:56 -070098{
Chaoyi Bian2f2d6412012-06-04 16:53:08 -070099 boost::shared_ptr<UpdateCallbackHolder> uh(new UpdateCallbackHolder(updatecallback));
100 boost::shared_ptr<RemoveCallbackHolder> rh(new RemoveCallbackHolder(removecallback));
101 boost::function<void (const vector<MissingDataInfo>&, SyncAppSocket*)> ucb = bind(&UpdateCallbackHolder::callbackWrapper, uh, _1, _2);
102 boost::function<void (const string&)> rcb = bind(&RemoveCallbackHolder::callbackWrapper, rh, _1);
103 SyncAppSocket *sock = new SyncAppSocket(prefix, ucb, rcb);
Alexander Afanasyev03a58b72012-03-12 18:11:56 -0700104 return (SyncAppSocketStruct *) sock;
105}
Zhenkai Zhue25def92012-03-12 16:39:25 -0700106
Chaoyi Bian9d8768a2012-03-14 17:23:36 -0700107extern "C"
Zhenkai Zhue25def92012-03-12 16:39:25 -0700108void
109delete_sync_app_socket(SyncAppSocketStruct **sock) {
Alexander Afanasyev03a58b72012-03-12 18:11:56 -0700110 SyncAppSocket *temp = *((SyncAppSocket **)sock);
111 delete temp;
112 temp = NULL;
113}
Zhenkai Zhue25def92012-03-12 16:39:25 -0700114
115// assume char *buf ends with '\0', or otherwise it's going to crash;
Chaoyi Bian9d8768a2012-03-14 17:23:36 -0700116// should fix this "feature"
117extern "C"
118int
Chaoyi Bianb97ce6b2012-06-05 10:42:34 -0700119sync_app_socket_publish(const SyncAppSocketStruct *sock, const char *prefix, int session, const char *buf, int freshness)
Alexander Afanasyev03a58b72012-03-12 18:11:56 -0700120{
Chaoyi Bian2f2d6412012-06-04 16:53:08 -0700121 SyncAppSocket *temp = (SyncAppSocket*) sock;
122 return temp->publishString(prefix, session, buf, freshness);
Alexander Afanasyev03a58b72012-03-12 18:11:56 -0700123}
Zhenkai Zhue25def92012-03-12 16:39:25 -0700124
Chaoyi Bian9d8768a2012-03-14 17:23:36 -0700125extern "C"
Zhenkai Zhue25def92012-03-12 16:39:25 -0700126void
Chaoyi Bianb97ce6b2012-06-05 10:42:34 -0700127sync_app_socket_remove(const SyncAppSocketStruct *sock, const char *prefix)
Alexander Afanasyev03a58b72012-03-12 18:11:56 -0700128{
Chaoyi Bian2f2d6412012-06-04 16:53:08 -0700129 SyncAppSocket *temp = (SyncAppSocket*) sock;
Alexander Afanasyev03a58b72012-03-12 18:11:56 -0700130 temp->remove(prefix);
131}
Chaoyi Bian2f2d6412012-06-04 16:53:08 -0700132
133extern "C"
134void
Chaoyi Bianb97ce6b2012-06-05 10:42:34 -0700135sync_app_socket_fetch(const SyncAppSocketStruct *sock, const char *prefix, int session, int sequence,
136 void (*callback)(const char*, const char*), int retry)
Chaoyi Bian2f2d6412012-06-04 16:53:08 -0700137{
138 SyncAppSocket *temp = (SyncAppSocket*) sock;
139 string s(prefix);
140 SeqNo seq(session, sequence);
141 boost::shared_ptr<CallbackHolder> h(new CallbackHolder(callback));
142 boost::function<void (const string&, const string&)> cb = bind(&CallbackHolder::callbackWrapper, h, _1, _2);
143
144 temp->fetchString(s, seq, cb, retry);
145}
Chaoyi Bianb97ce6b2012-06-05 10:42:34 -0700146