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(); |
Chaoyi Bian | 87832b7 | 2012-06-05 21:35:51 -0700 | [diff] [blame^] | 69 | 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 Bian | 2f2d641 | 2012-06-04 16:53:08 -0700 | [diff] [blame] | 74 | 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 | |
| 81 | class RemoveCallbackHolder { |
| 82 | private: |
| 83 | void (*m_callback)(const char*); |
| 84 | |
| 85 | public: |
| 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 Bian | 9d8768a | 2012-03-14 17:23:36 -0700 | [diff] [blame] | 92 | |
| 93 | extern "C" |
Zhenkai Zhu | e25def9 | 2012-03-12 16:39:25 -0700 | [diff] [blame] | 94 | SyncAppSocketStruct * |
Chaoyi Bian | 2f2d641 | 2012-06-04 16:53:08 -0700 | [diff] [blame] | 95 | create_sync_app_socket(const char *prefix, |
| 96 | void (*updatecallback)(const MissingDataInfoC*, const int, const SyncAppSocketStruct*), |
| 97 | void (*removecallback)(const char *)) |
Alexander Afanasyev | 03a58b7 | 2012-03-12 18:11:56 -0700 | [diff] [blame] | 98 | { |
Chaoyi Bian | 2f2d641 | 2012-06-04 16:53:08 -0700 | [diff] [blame] | 99 | 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 Afanasyev | 03a58b7 | 2012-03-12 18:11:56 -0700 | [diff] [blame] | 104 | return (SyncAppSocketStruct *) sock; |
| 105 | } |
Zhenkai Zhu | e25def9 | 2012-03-12 16:39:25 -0700 | [diff] [blame] | 106 | |
Chaoyi Bian | 9d8768a | 2012-03-14 17:23:36 -0700 | [diff] [blame] | 107 | extern "C" |
Zhenkai Zhu | e25def9 | 2012-03-12 16:39:25 -0700 | [diff] [blame] | 108 | void |
| 109 | delete_sync_app_socket(SyncAppSocketStruct **sock) { |
Alexander Afanasyev | 03a58b7 | 2012-03-12 18:11:56 -0700 | [diff] [blame] | 110 | SyncAppSocket *temp = *((SyncAppSocket **)sock); |
| 111 | delete temp; |
| 112 | temp = NULL; |
| 113 | } |
Zhenkai Zhu | e25def9 | 2012-03-12 16:39:25 -0700 | [diff] [blame] | 114 | |
| 115 | // 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] | 116 | // should fix this "feature" |
| 117 | extern "C" |
| 118 | int |
Chaoyi Bian | b97ce6b | 2012-06-05 10:42:34 -0700 | [diff] [blame] | 119 | sync_app_socket_publish(const SyncAppSocketStruct *sock, const char *prefix, int session, const char *buf, int freshness) |
Alexander Afanasyev | 03a58b7 | 2012-03-12 18:11:56 -0700 | [diff] [blame] | 120 | { |
Chaoyi Bian | 2f2d641 | 2012-06-04 16:53:08 -0700 | [diff] [blame] | 121 | SyncAppSocket *temp = (SyncAppSocket*) sock; |
| 122 | return temp->publishString(prefix, session, buf, freshness); |
Alexander Afanasyev | 03a58b7 | 2012-03-12 18:11:56 -0700 | [diff] [blame] | 123 | } |
Zhenkai Zhu | e25def9 | 2012-03-12 16:39:25 -0700 | [diff] [blame] | 124 | |
Chaoyi Bian | 9d8768a | 2012-03-14 17:23:36 -0700 | [diff] [blame] | 125 | extern "C" |
Zhenkai Zhu | e25def9 | 2012-03-12 16:39:25 -0700 | [diff] [blame] | 126 | void |
Chaoyi Bian | b97ce6b | 2012-06-05 10:42:34 -0700 | [diff] [blame] | 127 | sync_app_socket_remove(const SyncAppSocketStruct *sock, const char *prefix) |
Alexander Afanasyev | 03a58b7 | 2012-03-12 18:11:56 -0700 | [diff] [blame] | 128 | { |
Chaoyi Bian | 2f2d641 | 2012-06-04 16:53:08 -0700 | [diff] [blame] | 129 | SyncAppSocket *temp = (SyncAppSocket*) sock; |
Alexander Afanasyev | 03a58b7 | 2012-03-12 18:11:56 -0700 | [diff] [blame] | 130 | temp->remove(prefix); |
| 131 | } |
Chaoyi Bian | 2f2d641 | 2012-06-04 16:53:08 -0700 | [diff] [blame] | 132 | |
| 133 | extern "C" |
| 134 | void |
Chaoyi Bian | b97ce6b | 2012-06-05 10:42:34 -0700 | [diff] [blame] | 135 | sync_app_socket_fetch(const SyncAppSocketStruct *sock, const char *prefix, int session, int sequence, |
| 136 | void (*callback)(const char*, const char*), int retry) |
Chaoyi Bian | 2f2d641 | 2012-06-04 16:53:08 -0700 | [diff] [blame] | 137 | { |
| 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 Bian | b97ce6b | 2012-06-05 10:42:34 -0700 | [diff] [blame] | 146 | |