~ubuntu-branches/ubuntu/intrepid/gnunet/intrepid

« back to all changes in this revision

Viewing changes to src/applications/dht/tools/dht-join.c

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Kyheng
  • Date: 2005-11-20 03:09:15 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051120030915-4b3lwgjqtugsjhcy
Tags: 0.7.0a-1
* New upstream release.
* debian/po/fr.po: Updated french translation. Thanks to Eric (Closes: #336769).
* debian/control: Updated dependencies to follow the libstdc++ allocator change.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 *
26
26
 * Todo:
27
27
 * - test
28
 
 * - add options (verbose reporting of DHT operations, leave-timeout)
29
28
 */
30
29
 
31
30
#include "platform.h"
44
43
      gettext_noop("allow SIZE bytes of memory for the local table") },
45
44
    { 't', "table", "NAME",
46
45
      gettext_noop("join table called NAME") },
47
 
    { 'T', "timeout", "VALUE",
48
 
      gettext_noop("when leaving table, use VALUEs to migrate data") },
49
46
    HELP_VERSION,
50
47
    HELP_VERBOSE,
51
48
    HELP_END,
57
54
 
58
55
static int parseOptions(int argc,
59
56
                        char ** argv) {
60
 
  int c;  
 
57
  int c;
61
58
 
62
59
  while (1) {
63
60
    int option_index = 0;
64
61
    static struct GNoption long_options[] = {
65
62
      LONG_DEFAULT_OPTIONS,
66
63
      { "memory", 1, 0, 'm' },
67
 
      { "table", 1, 0, 't' },      
 
64
      { "table", 1, 0, 't' },
68
65
      { "verbose", 0, 0, 'V' },
69
66
      { 0,0,0,0 }
70
67
    };
71
68
    c = GNgetopt_long(argc,
72
69
                      argv,
73
70
                      "vhH:c:L:dt:m:T:V",
74
 
                      long_options, 
75
 
                      &option_index);    
76
 
    if (c == -1) 
 
71
                      long_options,
 
72
                      &option_index);
 
73
    if (c == -1)
77
74
      break;  /* No more flags to process */
78
75
    if (YES == parseDefaultOptions(c, GNoptarg))
79
76
      continue;
80
77
    switch(c) {
81
 
    case 'h': 
82
 
      printHelp(); 
 
78
    case 'h':
 
79
      printHelp();
83
80
      return SYSERR;
84
81
    case 'm': {
85
82
      unsigned int max;
86
83
      if (1 != sscanf(GNoptarg, "%ud", &max)) {
87
 
        LOG(LOG_FAILURE, 
88
 
            _("You must pass a number to the '%s' option.\n"),
 
84
        LOG(LOG_FAILURE,
 
85
            _("You must pass a number to the `%s' option.\n"),
89
86
            "-m");
90
87
        return SYSERR;
91
88
      } else {  
100
97
                                         "TABLE",
101
98
                                         GNoptarg));
102
99
      break;
103
 
    case 'T': {
104
 
      unsigned int max;
105
 
      if (1 != sscanf(GNoptarg, "%ud", &max)) {
106
 
        LOG(LOG_FAILURE, 
107
 
            _("You must pass a number to the '%s' option.\n"),
108
 
            "-T");
109
 
        return SYSERR;
110
 
      } else {  
111
 
        setConfigurationInt("DHT-JOIN",
112
 
                            "TIMEOUT",
113
 
                            max);
114
 
      }
115
 
      break;
116
 
    }
117
 
    case 'v': 
 
100
    case 'v':
118
101
      printf("dht-join v0.0.0\n");
119
102
      return SYSERR;
120
103
    case 'V':
127
110
      return SYSERR;
128
111
    } /* end of parsing commandline */
129
112
  } /* while (1) */
130
 
  if (argc - GNoptind != 0) 
131
 
    LOG(LOG_WARNING, 
 
113
  if (argc - GNoptind != 0)
 
114
    LOG(LOG_WARNING,
132
115
        _("Superflous arguments (ignored).\n"));
133
116
  return OK;
134
117
}
145
128
  }
146
129
}
147
130
 
148
 
#define LOGRET(ret) dump(_("Call to '%s' returns %d.\n"), __FUNCTION__, ret)
149
 
#define LOGKEY(key) do { EncName kn; hash2enc(key, &kn); dump(_("Call to '%s' with key '%s'.\n"), __FUNCTION__, &kn); } while (0)
150
 
#define LOGVAL(val) dump(_("Call to '%s' with value '%.*s' (%d bytes).\n"), __FUNCTION__, (val == NULL) ? 0 : val->dataLength, (val == NULL) ? NULL : val->data, (val == NULL) ? 0 : val->dataLength)
 
131
#define LOGRET(ret) dump(_("Call to `%s' returns %d.\n"), __FUNCTION__, ret)
 
132
#define LOGKEY(key) do { EncName kn; hash2enc(key, &kn); dump(_("Call to `%s' with key `%s'.\n"), __FUNCTION__, &kn); } while (0)
 
133
#define LOGVAL(val) dump(_("Call to `%s' with value '%.*s' (%d bytes).\n"), __FUNCTION__, (val == NULL) ? 0 : &val[1], (val == NULL) ? NULL : &val[1], (val == NULL) ? 0 : (ntohl(val->size) - sizeof(DataContainer)))
151
134
 
152
135
static int lookup(void * closure,
153
 
                 const HashCode160 * key,
154
 
                 unsigned int maxResults,
155
 
                 DHT_DataContainer * results,
156
 
                 int flags) {
 
136
                  unsigned int type,
 
137
                  unsigned int prio,
 
138
                  unsigned int keyCount,
 
139
                  const HashCode512 * keys,
 
140
                  DataProcessor processor,
 
141
                  void * pclosure) {
157
142
  int ret;
158
 
  DHT_Datastore * cls = (DHT_Datastore*) closure;  
159
 
  LOGKEY(key);
160
 
  ret = cls->lookup(cls->closure,
161
 
                    key,
162
 
                    maxResults,
163
 
                    results,
164
 
                    flags);
165
 
  if (ret >= 1)
166
 
    LOGVAL(results);
167
 
  LOGRET(ret);  
 
143
  Blockstore * cls = (Blockstore*) closure;
 
144
  LOGKEY(&keys[0]);
 
145
  ret = cls->get(cls->closure,
 
146
                 type,
 
147
                 prio,
 
148
                 keyCount,
 
149
                 keys,
 
150
                 processor,
 
151
                 pclosure);
 
152
  LOGRET(ret);
168
153
  return ret;
169
154
}
170
 
  
 
155
 
171
156
static int store(void * closure,
172
 
                 const HashCode160 * key,
173
 
                 const DHT_DataContainer * value,
174
 
                 int flags) {
 
157
                 const HashCode512 * key,
 
158
                 const DataContainer * value,
 
159
                 unsigned int prio) {
175
160
  int ret;
176
 
  DHT_Datastore * cls = (DHT_Datastore*) closure;
 
161
  Blockstore * cls = (Blockstore*) closure;
177
162
  LOGKEY(key);
178
163
  LOGVAL(value);
179
 
  ret = cls->store(cls->closure,
180
 
                   key,
181
 
                   value,
182
 
                   flags);
 
164
  ret = cls->put(cls->closure,
 
165
                 key,
 
166
                 value,
 
167
                 prio);
183
168
  LOGRET(ret);
184
169
  return ret;
185
170
}
186
171
 
187
172
static int removeDS(void * closure,
188
 
                    const HashCode160 * key,
189
 
                    const DHT_DataContainer * value,
190
 
                    int flags) {
 
173
                    const HashCode512 * key,
 
174
                    const DataContainer * value) {
191
175
  int ret;
192
 
  DHT_Datastore * cls = (DHT_Datastore*) closure;
 
176
  Blockstore * cls = (Blockstore*) closure;
193
177
  LOGKEY(key);
194
178
  LOGVAL(value);
195
 
  ret = cls->remove(cls->closure,
196
 
                    key,
197
 
                    value,
198
 
                    flags);
 
179
  ret = cls->del(cls->closure,
 
180
                 key,
 
181
                 value);
199
182
  LOGRET(ret);
200
183
  return ret;
201
184
}
202
185
 
203
 
static int iterate(void * closure,               
204
 
                   int flags,
205
 
                   DHT_DataProcessor processor,
 
186
static int iterate(void * closure,
 
187
                   DataProcessor processor,
206
188
                   void * parg) {
207
189
  int ret;
208
 
  DHT_Datastore * cls = (DHT_Datastore*) closure;
 
190
  Blockstore * cls = (Blockstore*) closure;
209
191
  ret = cls->iterate(cls->closure,
210
 
                     flags,
211
192
                     processor,
212
193
                     parg);
213
194
  LOGRET(ret);
214
195
  return ret;
215
196
}
216
197
 
217
 
int main(int argc, 
 
198
int main(int argc,
218
199
         char **argv) {
219
200
  char * tableName;
220
 
  int flags;
221
201
  unsigned int mem;
222
 
  HashCode160 table;
223
 
  DHT_Datastore myStore;
 
202
  HashCode512 table;
 
203
  Blockstore myStore;
224
204
 
225
 
  if (SYSERR == initUtil(argc, argv, &parseOptions)) 
 
205
  if (SYSERR == initUtil(argc, argv, &parseOptions))
226
206
    return 0;
227
207
 
228
 
  tableName = getConfigurationString("DHT-JOIN", 
 
208
  tableName = getConfigurationString("DHT-JOIN",
229
209
                                     "TABLE");
230
210
  if (tableName == NULL) {
231
 
    printf(_("No table name specified, using '%s'.\n"),
 
211
    printf(_("No table name specified, using `%s'.\n"),
232
212
           "test");
233
213
    tableName = STRDUP("test");
234
214
  }
242
222
  mem = getConfigurationInt("DHT-JOIN",
243
223
                            "MEMORY");
244
224
  if (mem == 0) mem = 65536; /* default: use 64k */
245
 
  myStore.closure = create_datastore_memory(mem);
246
 
  myStore.lookup = &lookup;
247
 
  myStore.store = &store;
248
 
  myStore.remove = &removeDS;
 
225
  myStore.closure = create_blockstore_memory(mem);
 
226
  myStore.get = &lookup;
 
227
  myStore.put = &store;
 
228
  myStore.del = &removeDS;
249
229
  myStore.iterate = &iterate;
250
230
 
251
 
  flags = 1; /* one replica */
252
 
  
253
231
  DHT_LIB_init();
254
232
  initializeShutdownHandlers();
255
233
  if (OK != DHT_LIB_join(&myStore,
256
 
                         &table,
257
 
                         0,
258
 
                         flags)) {
 
234
                         &table)) {
259
235
    LOG(LOG_WARNING,
260
236
        _("Error joining DHT.\n"));
261
 
    destroy_datastore_memory((DHT_Datastore*)myStore.closure);
 
237
    destroy_blockstore_memory((Blockstore*)myStore.closure);
262
238
    doneShutdownHandlers();
263
239
    DHT_LIB_done();
264
240
    return 1;
267
243
  printf(_("Joined DHT.  Press CTRL-C to leave.\n"));
268
244
  /* wait for CTRL-C */
269
245
  wait_for_shutdown();
270
 
  
271
 
  /* shutdown */ 
272
 
  if (OK != DHT_LIB_leave(&table,
273
 
                          getConfigurationInt("DHT-JOIN",
274
 
                                              "TIMEOUT") * cronSECONDS,
275
 
                          0)) {
 
246
 
 
247
  /* shutdown */
 
248
  if (OK != DHT_LIB_leave(&table)) {
276
249
    LOG(LOG_WARNING,
277
250
        _("Error leaving DHT.\n"));
278
 
    destroy_datastore_memory((DHT_Datastore*)myStore.closure);
 
251
    destroy_blockstore_memory((Blockstore*)myStore.closure);
279
252
    doneShutdownHandlers();
280
253
    DHT_LIB_done();
281
254
    return 1;
282
255
  } else {
283
 
    destroy_datastore_memory((DHT_Datastore*)myStore.closure);
 
256
    destroy_blockstore_memory((Blockstore*)myStore.closure);
284
257
    doneShutdownHandlers();
285
258
    DHT_LIB_done();
286
259
    return 0;