Zhenkai Zhu | e25def9 | 2012-03-12 16:39:25 -0700 | [diff] [blame] | 1 | /* -*- 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 Bian | 3e1eb16 | 2012-04-03 16:59:32 -0700 | [diff] [blame] | 19 | * Chaoyi Bian <bcy@pku.edu.cn> |
Zhenkai Zhu | e25def9 | 2012-03-12 16:39:25 -0700 | [diff] [blame] | 20 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 21 | */ |
| 22 | |
Chaoyi Bian | 9d8768a | 2012-03-14 17:23:36 -0700 | [diff] [blame] | 23 | #include "sync-app-socket.h" |
Chaoyi Bian | 2f2d641 | 2012-06-04 16:53:08 -0700 | [diff] [blame] | 24 | #include <boost/shared_array.hpp> |
Zhenkai Zhu | e25def9 | 2012-03-12 16:39:25 -0700 | [diff] [blame] | 25 | using namespace std; |
| 26 | using namespace Sync; |
| 27 | |
Chaoyi Bian | 2f2d641 | 2012-06-04 16:53:08 -0700 | [diff] [blame] | 28 | struct SyncAppSocketStruct; |
| 29 | struct MissingDataInfoC |
| 30 | { |
| 31 | const char *prefix; |
| 32 | int session; |
| 33 | int low; |
| 34 | int high; |
| 35 | }; |
| 36 | |
| 37 | class CallbackHolder { |
Chaoyi Bian | 9813519 | 2012-03-27 18:34:11 -0700 | [diff] [blame] | 38 | private: |
Alexander Afanasyev | 03a58b7 | 2012-03-12 18:11:56 -0700 | [diff] [blame] | 39 | void (*m_callback)(const char *, const char *); |
Chaoyi Bian | 9813519 | 2012-03-27 18:34:11 -0700 | [diff] [blame] | 40 | |
| 41 | public: |
Chaoyi Bian | 2f2d641 | 2012-06-04 16:53:08 -0700 | [diff] [blame] | 42 | 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 Afanasyev | 03a58b7 | 2012-03-12 18:11:56 -0700 | [diff] [blame] | 48 | } |
Zhenkai Zhu | e25def9 | 2012-03-12 16:39:25 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
Chaoyi Bian | 2f2d641 | 2012-06-04 16:53:08 -0700 | [diff] [blame] | 51 | class UpdateCallbackHolder { |
| 52 | private: |
| 53 | void (*m_callback)(const struct MissingDataInfoC*, const int, const SyncAppSocketStruct*); |
| 54 | |
| 55 | public: |
| 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 | |
| 78 | class RemoveCallbackHolder { |
| 79 | private: |
| 80 | void (*m_callback)(const char*); |
| 81 | |
| 82 | public: |
| 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 Bian | 9d8768a | 2012-03-14 17:23:36 -0700 | [diff] [blame] | 89 | |
| 90 | extern "C" |
Zhenkai Zhu | e25def9 | 2012-03-12 16:39:25 -0700 | [diff] [blame] | 91 | SyncAppSocketStruct * |
Chaoyi Bian | 2f2d641 | 2012-06-04 16:53:08 -0700 | [diff] [blame] | 92 | create_sync_app_socket(const char *prefix, |
| 93 | void (*updatecallback)(const MissingDataInfoC*, const int, const SyncAppSocketStruct*), |
| 94 | void (*removecallback)(const char *)) |
Alexander Afanasyev | 03a58b7 | 2012-03-12 18:11:56 -0700 | [diff] [blame] | 95 | { |
Chaoyi Bian | 2f2d641 | 2012-06-04 16:53:08 -0700 | [diff] [blame] | 96 | 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 Afanasyev | 03a58b7 | 2012-03-12 18:11:56 -0700 | [diff] [blame] | 101 | return (SyncAppSocketStruct *) sock; |
| 102 | } |
Zhenkai Zhu | e25def9 | 2012-03-12 16:39:25 -0700 | [diff] [blame] | 103 | |
Chaoyi Bian | 9d8768a | 2012-03-14 17:23:36 -0700 | [diff] [blame] | 104 | extern "C" |
Zhenkai Zhu | e25def9 | 2012-03-12 16:39:25 -0700 | [diff] [blame] | 105 | void |
| 106 | delete_sync_app_socket(SyncAppSocketStruct **sock) { |
Alexander Afanasyev | 03a58b7 | 2012-03-12 18:11:56 -0700 | [diff] [blame] | 107 | SyncAppSocket *temp = *((SyncAppSocket **)sock); |
| 108 | delete temp; |
| 109 | temp = NULL; |
| 110 | } |
Zhenkai Zhu | e25def9 | 2012-03-12 16:39:25 -0700 | [diff] [blame] | 111 | |
| 112 | // assume char *buf ends with '\0', or otherwise it's going to crash; |
Chaoyi Bian | 9d8768a | 2012-03-14 17:23:36 -0700 | [diff] [blame] | 113 | // should fix this "feature" |
| 114 | extern "C" |
| 115 | int |
| 116 | sync_app_socket_publish(SyncAppSocketStruct *sock, const char *prefix, int session, const char *buf, int freshness) |
Alexander Afanasyev | 03a58b7 | 2012-03-12 18:11:56 -0700 | [diff] [blame] | 117 | { |
Chaoyi Bian | 2f2d641 | 2012-06-04 16:53:08 -0700 | [diff] [blame] | 118 | SyncAppSocket *temp = (SyncAppSocket*) sock; |
| 119 | return temp->publishString(prefix, session, buf, freshness); |
Alexander Afanasyev | 03a58b7 | 2012-03-12 18:11:56 -0700 | [diff] [blame] | 120 | } |
Zhenkai Zhu | e25def9 | 2012-03-12 16:39:25 -0700 | [diff] [blame] | 121 | |
Chaoyi Bian | 9d8768a | 2012-03-14 17:23:36 -0700 | [diff] [blame] | 122 | extern "C" |
Zhenkai Zhu | e25def9 | 2012-03-12 16:39:25 -0700 | [diff] [blame] | 123 | void |
| 124 | sync_app_socket_remove(SyncAppSocketStruct *sock, const char *prefix) |
Alexander Afanasyev | 03a58b7 | 2012-03-12 18:11:56 -0700 | [diff] [blame] | 125 | { |
Chaoyi Bian | 2f2d641 | 2012-06-04 16:53:08 -0700 | [diff] [blame] | 126 | SyncAppSocket *temp = (SyncAppSocket*) sock; |
Alexander Afanasyev | 03a58b7 | 2012-03-12 18:11:56 -0700 | [diff] [blame] | 127 | temp->remove(prefix); |
| 128 | } |
Chaoyi Bian | 2f2d641 | 2012-06-04 16:53:08 -0700 | [diff] [blame] | 129 | |
| 130 | extern "C" |
| 131 | void |
| 132 | sync_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 | } |