00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "libs/gateway_prot.h"
00019 #include "libs/gateway_rpc.h"
00020 #include "libs/sha1.h"
00021 #include "TcaRegistry.h"
00022
00023 static const char* APP_STRING = "tca";
00024 static const char* CLIB_STRING = "rpcgen";
00025
00026 static const int DHT_KEYLEN = 20;
00027
00028
00029
00030
00031 static void
00032 hash(const std::string& s, uint8 digest[DHT_KEYLEN])
00033 {
00034
00035 sha1_context ctx;
00036 sha1_starts(&ctx);
00037 sha1_update(&ctx, (unsigned char*)(s.c_str()), s.length());
00038 sha1_finish(&ctx, digest);
00039 }
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00053
00054
00055
00056 bool
00057 TcaRegistry::init_nodes()
00058 {
00059
00060
00061
00062
00063
00064
00065
00066 dht_nodes_.push_back(std::string("cloudburst.uwaterloo.ca"));
00067 dht_nodes_.push_back(std::string("blast.uwaterloo.ca"));
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 return true;
00082 }
00083
00084
00085 bool
00086 TcaRegistry::init_addrs()
00087 {
00088
00089
00090
00091
00092
00093
00094 printf("Initializing TcaRegistry...\n");
00095
00096 last_node_ = 0;
00097
00098 sockaddr_in addr;
00099 for (unsigned int i=0; i<dht_nodes_.size(); ++i)
00100 {
00101 if (test_node(dht_nodes_[i].c_str(), &addr))
00102 {
00103
00104 dht_addrs_.push_back(addr);
00105 }
00106 }
00107
00108 if (dht_addrs_.size() == 0) return false;
00109
00110 printf("...dht nodes available = %zu / %zu\n",
00111 dht_addrs_.size(), dht_nodes_.size());
00112 return true;
00113 }
00114
00115
00116
00117
00118
00119 bool
00120 TcaRegistry::write(const RegRecord& rr, int ttl)
00121 {
00122 CLIENT* p_node = get_node();
00123 if (p_node == NULL) return false;
00124
00125
00126
00127 uint8 key[DHT_KEYLEN];
00128 hash(rr.host_, key);
00129
00130
00131 bamboo_put_args args;
00132 memset(&args, 0, sizeof(args));
00133
00134 args.application = const_cast<char*>(APP_STRING);
00135 args.client_library = const_cast<char*>(CLIB_STRING);
00136 memcpy(args.key, key, DHT_KEYLEN);
00137
00138 args.value.bamboo_value_len = rr.link_addr_.length() + 1;
00139 args.value.bamboo_value_val = const_cast<char*>(rr.link_addr_.c_str());
00140
00141 args.ttl_sec = ttl;
00142
00143
00144
00145 bamboo_stat* res = bamboo_dht_proc_put_2(&args, p_node);
00146
00147
00148 return (*res == BAMBOO_OK);
00149 }
00150
00151
00152
00153
00154
00155
00156 bool
00157 TcaRegistry::read(RegRecord& rr)
00158 {
00159 CLIENT* p_node = get_node();
00160 if (p_node == NULL) return false;
00161
00162
00163
00164 uint8 key[DHT_KEYLEN];
00165 hash(rr.host_, key);
00166
00167
00168 bamboo_get_args args;
00169 memset(&args, 0, sizeof(args));
00170
00171 args.application = const_cast<char*>(APP_STRING);
00172 args.client_library = const_cast<char*>(CLIB_STRING);
00173 memcpy(args.key, key, DHT_KEYLEN);
00174
00175
00176
00177 args.maxvals = 1;
00178
00179 bamboo_get_res* res = bamboo_dht_proc_get_2(&args, p_node);
00180 if (res == NULL)
00181 {
00182 printf("TcaRegistry::read: get returned NULL\n");
00183 return false;
00184 }
00185
00186 int n_values = res->values.values_len;
00187
00188 if (n_values != 1)
00189 {
00190
00191 return false;
00192 }
00193
00194 bamboo_value* p_val = &res->values.values_val[0];
00195
00196 rr.link_addr_ = p_val->bamboo_value_val;
00197 printf("TcaRegistry::read: succeeded! value=%s\n", rr.link_addr_.c_str());
00198
00199 return true;
00200 }
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232 CLIENT*
00233 TcaRegistry::get_node()
00234 {
00235
00236
00237
00238
00239
00240
00241
00242 CLIENT* p_node = NULL;
00243
00244 for (unsigned int i = last_node_ + 1; i != last_node_; ++i)
00245 {
00246 if (i == dht_addrs_.size()) i = 0;
00247 p_node = get_connection(&dht_addrs_[i]);
00248 if (p_node)
00249 {
00250 last_node_ = i;
00251 break;
00252 }
00253 }
00254
00255 return p_node;
00256 }
00257
00258
00259