blob: 0fc03e172227218084c1b8dfd39b314e52836b05 [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
23#include "sync-app-socket-c.h"
24using namespace std;
25using namespace Sync;
26
27class CallbackHolder{
Alexander Afanasyev03a58b72012-03-12 18:11:56 -070028public:
29 void (*m_callback)(const char *, const char *);
30 void callbackWrapper(string name, string data) {
31 m_callback(name.c_str(), data.c_str());
32 }
Zhenkai Zhue25def92012-03-12 16:39:25 -070033};
34
35SyncAppSocketStruct *
36create_sync_app_socket(const char *prefix, void (*callback)(const char *, const char *))
Alexander Afanasyev03a58b72012-03-12 18:11:56 -070037{
38 CallbackHolder holder;
39 holder.m_callback = callback;
40 boost::function<void (string, string)> cb = bind(&CallbackHolder::callbackWrapper, &holder, _1, _2);
41 SyncAppSocket *sock = new SyncAppSocket(prefix, cb);
42 return (SyncAppSocketStruct *) sock;
43}
Zhenkai Zhue25def92012-03-12 16:39:25 -070044
45
46void
47delete_sync_app_socket(SyncAppSocketStruct **sock) {
Alexander Afanasyev03a58b72012-03-12 18:11:56 -070048 SyncAppSocket *temp = *((SyncAppSocket **)sock);
49 delete temp;
50 temp = NULL;
51}
Zhenkai Zhue25def92012-03-12 16:39:25 -070052
53// assume char *buf ends with '\0', or otherwise it's going to crash;
54// should fix this "feature"
55bool
56sync_app_socket_publish(SyncAppSocketStruct *sock, const char *prefix, uint32_t session, const char *buf, int freshness)
Alexander Afanasyev03a58b72012-03-12 18:11:56 -070057{
58 SyncAppSocket *temp = (SyncAppSocket *)sock;
59 return temp->publish(prefix, session, buf, freshness);
60}
Zhenkai Zhue25def92012-03-12 16:39:25 -070061
62void
63sync_app_socket_remove(SyncAppSocketStruct *sock, const char *prefix)
Alexander Afanasyev03a58b72012-03-12 18:11:56 -070064{
65 SyncAppSocket *temp = (SyncAppSocket *)sock;
66 temp->remove(prefix);
67}