blob: 2445b8c15df6bde806338c5f703d53a499d41f7b [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{
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
Chaoyi Bian9d8768a2012-03-14 17:23:36 -070035struct SyncAppSocketStruct;
36
37extern "C"
Zhenkai Zhue25def92012-03-12 16:39:25 -070038SyncAppSocketStruct *
39create_sync_app_socket(const char *prefix, void (*callback)(const char *, const char *))
Alexander Afanasyev03a58b72012-03-12 18:11:56 -070040{
41 CallbackHolder holder;
42 holder.m_callback = callback;
43 boost::function<void (string, string)> cb = bind(&CallbackHolder::callbackWrapper, &holder, _1, _2);
44 SyncAppSocket *sock = new SyncAppSocket(prefix, cb);
45 return (SyncAppSocketStruct *) sock;
46}
Zhenkai Zhue25def92012-03-12 16:39:25 -070047
Chaoyi Bian9d8768a2012-03-14 17:23:36 -070048extern "C"
Zhenkai Zhue25def92012-03-12 16:39:25 -070049void
50delete_sync_app_socket(SyncAppSocketStruct **sock) {
Alexander Afanasyev03a58b72012-03-12 18:11:56 -070051 SyncAppSocket *temp = *((SyncAppSocket **)sock);
52 delete temp;
53 temp = NULL;
54}
Zhenkai Zhue25def92012-03-12 16:39:25 -070055
56// assume char *buf ends with '\0', or otherwise it's going to crash;
Chaoyi Bian9d8768a2012-03-14 17:23:36 -070057// should fix this "feature"
58extern "C"
59int
60sync_app_socket_publish(SyncAppSocketStruct *sock, const char *prefix, int session, const char *buf, int freshness)
Alexander Afanasyev03a58b72012-03-12 18:11:56 -070061{
62 SyncAppSocket *temp = (SyncAppSocket *)sock;
63 return temp->publish(prefix, session, buf, freshness);
64}
Zhenkai Zhue25def92012-03-12 16:39:25 -070065
Chaoyi Bian9d8768a2012-03-14 17:23:36 -070066extern "C"
Zhenkai Zhue25def92012-03-12 16:39:25 -070067void
68sync_app_socket_remove(SyncAppSocketStruct *sock, const char *prefix)
Alexander Afanasyev03a58b72012-03-12 18:11:56 -070069{
70 SyncAppSocket *temp = (SyncAppSocket *)sock;
71 temp->remove(prefix);
72}