blob: 1233ec9d140ec1e66b244c71b8c0b643a3e067f3 [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>
19 * 卞超轶 Chaoyi Bian <bcy@pku.edu.cn>
20 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
22
Chaoyi Bian9d8768a2012-03-14 17:23:36 -070023#include "sync-app-socket.h"
Zhenkai Zhue25def92012-03-12 16:39:25 -070024using namespace std;
25using namespace Sync;
26
27class CallbackHolder{
Chaoyi Bian98135192012-03-27 18:34:11 -070028private:
Alexander Afanasyev03a58b72012-03-12 18:11:56 -070029 void (*m_callback)(const char *, const char *);
Chaoyi Bian98135192012-03-27 18:34:11 -070030
31public:
32 CallbackHolder(void (*callback)(const char*, const char*)):m_callback(callback){};
Alexander Afanasyev03a58b72012-03-12 18:11:56 -070033 void callbackWrapper(string name, string data) {
34 m_callback(name.c_str(), data.c_str());
35 }
Zhenkai Zhue25def92012-03-12 16:39:25 -070036};
37
Chaoyi Bian9d8768a2012-03-14 17:23:36 -070038struct SyncAppSocketStruct;
39
40extern "C"
Zhenkai Zhue25def92012-03-12 16:39:25 -070041SyncAppSocketStruct *
42create_sync_app_socket(const char *prefix, void (*callback)(const char *, const char *))
Alexander Afanasyev03a58b72012-03-12 18:11:56 -070043{
Chaoyi Bian98135192012-03-27 18:34:11 -070044 CallbackHolder holder(callback);
Alexander Afanasyev03a58b72012-03-12 18:11:56 -070045 boost::function<void (string, string)> cb = bind(&CallbackHolder::callbackWrapper, &holder, _1, _2);
46 SyncAppSocket *sock = new SyncAppSocket(prefix, cb);
47 return (SyncAppSocketStruct *) sock;
48}
Zhenkai Zhue25def92012-03-12 16:39:25 -070049
Chaoyi Bian9d8768a2012-03-14 17:23:36 -070050extern "C"
Zhenkai Zhue25def92012-03-12 16:39:25 -070051void
52delete_sync_app_socket(SyncAppSocketStruct **sock) {
Alexander Afanasyev03a58b72012-03-12 18:11:56 -070053 SyncAppSocket *temp = *((SyncAppSocket **)sock);
54 delete temp;
55 temp = NULL;
56}
Zhenkai Zhue25def92012-03-12 16:39:25 -070057
58// assume char *buf ends with '\0', or otherwise it's going to crash;
Chaoyi Bian9d8768a2012-03-14 17:23:36 -070059// should fix this "feature"
60extern "C"
61int
62sync_app_socket_publish(SyncAppSocketStruct *sock, const char *prefix, int session, const char *buf, int freshness)
Alexander Afanasyev03a58b72012-03-12 18:11:56 -070063{
64 SyncAppSocket *temp = (SyncAppSocket *)sock;
65 return temp->publish(prefix, session, buf, freshness);
66}
Zhenkai Zhue25def92012-03-12 16:39:25 -070067
Chaoyi Bian9d8768a2012-03-14 17:23:36 -070068extern "C"
Zhenkai Zhue25def92012-03-12 16:39:25 -070069void
70sync_app_socket_remove(SyncAppSocketStruct *sock, const char *prefix)
Alexander Afanasyev03a58b72012-03-12 18:11:56 -070071{
72 SyncAppSocket *temp = (SyncAppSocket *)sock;
73 temp->remove(prefix);
74}