9b26cf36c40da05ab2bbfb506f35bc636f94b86e
[oweals/gnunet.git] / src / peerstore / gnunet-peerstore.c
1 /*
2      This file is part of GNUnet.
3      (C)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file peerstore/gnunet-peerstore.c
23  * @brief peerstore tool
24  * @author Omar Tarabai
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_peerstore_service.h"
29
30 static int ret;
31
32 /**
33  * option '-t'
34  */
35 static int test;
36
37 /*
38  * Handle to PEERSTORE service
39  */
40 struct GNUNET_PEERSTORE_Handle *peerstore_handle;
41
42 /**
43  * Run on shutdown
44  *
45  * @param cls unused
46  * @param tc scheduler context
47  */
48 static void
49 shutdown_task (void *cls,
50          const struct GNUNET_SCHEDULER_TaskContext *tc)
51 {
52   if(NULL != peerstore_handle)
53   {
54     GNUNET_PEERSTORE_disconnect(peerstore_handle);
55     peerstore_handle = NULL;
56   }
57 }
58
59 void test_cont(void *cls, const char *emsg)
60 {
61   char *req = cls;
62
63   printf("Received a response to request: %s\n", req);
64   if(NULL != emsg)
65   {
66     printf("Response: %s\n", emsg);
67   }
68 }
69
70 /**
71  * Main function that will be run by the scheduler.
72  *
73  * @param cls closure
74  * @param args remaining command-line arguments
75  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
76  * @param cfg configuration
77  */
78 static void
79 run (void *cls,
80      char *const *args,
81      const char *cfgfile,
82      const struct GNUNET_CONFIGURATION_Handle *cfg)
83 {
84
85   peerstore_handle = NULL;
86   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
87                                   &shutdown_task,
88                                   NULL);
89   peerstore_handle = GNUNET_PEERSTORE_connect(cfg);
90   GNUNET_assert(NULL != peerstore_handle);
91   if(GNUNET_YES == test)
92   {
93     struct GNUNET_PeerIdentity pid;
94     memset (&pid, 32, sizeof (pid));
95     GNUNET_PEERSTORE_store(peerstore_handle,
96         &pid,
97         "subsub",
98         "value",
99         5,
100         GNUNET_TIME_UNIT_FOREVER_REL,
101         &test_cont,
102         "Req1");
103     GNUNET_PEERSTORE_store(peerstore_handle,
104             &pid,
105             "subsub",
106             "value",
107             5,
108             GNUNET_TIME_UNIT_FOREVER_REL,
109             &test_cont,
110             "Req2");
111   }
112
113   ret = 0;
114 }
115
116 /**
117  * The main function to peerstore.
118  *
119  * @param argc number of arguments from the command line
120  * @param argv command line arguments
121  * @return 0 ok, 1 on error
122  */
123 int
124 main (int argc, char *const *argv)
125 {
126   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
127     {'t', "test", NULL,
128         gettext_noop("TESTING"),
129     0, &GNUNET_GETOPT_set_one, &test},
130     GNUNET_GETOPT_OPTION_END
131   };
132   return (GNUNET_OK ==
133           GNUNET_PROGRAM_run (argc,
134                               argv,
135                               "gnunet-peerstore [options [value]]",
136                               gettext_noop
137                               ("peerstore"),
138                               options, &run, NULL)) ? ret : 1;
139 }
140
141 /* end of gnunet-peerstore.c */