REST/NAMESTORE: rework API
[oweals/gnunet.git] / src / transport / test_communicator_unix.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2019 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21 /**
22  * @file transport/test_communicator_unix.c
23  * @brief test the unix communicator
24  * @author Julius Bünger
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "transport-testing2.h"
29 #include "gnunet_ats_transport_service.h"
30 #include "gnunet_signatures.h"
31 #include "transport.h"
32
33 #include <inttypes.h>
34
35 /**
36  * TODO
37  * - start two communicators
38  * - act like transport services
39  *   - get_server_addresses (service.c)
40  *   - open_listen_socket (service.c)
41  *   - GNUNET_MQ_queue_for_callbacks (service.c)
42  * - let them communicate
43  *
44  */
45
46
47
48 #define LOG(kind,...) GNUNET_log_from (kind, "test_transport_communicator_unix", __VA_ARGS__)
49
50 #define NUM_PEERS 2
51
52 static struct GNUNET_PeerIdentity peer_id[NUM_PEERS];
53
54 static struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_hs[NUM_PEERS];
55
56 //static char *addresses[NUM_PEERS];
57
58 static void
59 communicator_available_cb (void *cls,
60                            struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
61                            enum GNUNET_TRANSPORT_CommunicatorCharacteristics cc,
62                            char *address_prefix)
63 {
64   LOG (GNUNET_ERROR_TYPE_DEBUG,
65       "Communicator available. (cc: %u, prefix: %s)\n",
66       cc,
67       address_prefix);
68 }
69
70
71 static void
72 add_address_cb (void *cls,
73                 struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
74                 const char *address,
75                 struct GNUNET_TIME_Relative expiration,
76                 uint32_t aid,
77                 enum GNUNET_NetworkType nt)
78 {
79   LOG (GNUNET_ERROR_TYPE_DEBUG,
80       "New address. (addr: %s, expir: %" PRIu32 ", ID: %" PRIu32 ", nt: %u\n",
81       address,
82       expiration.rel_value_us,
83       aid,
84       nt);
85   //addresses[1] = GNUNET_strdup (address);
86   GNUNET_TRANSPORT_TESTING_transport_communicator_open_queue (tc_hs[0],
87                                                               &peer_id[1],
88                                                               address);
89 }
90
91
92 static void
93 queue_create_reply_cb (void *cls,
94                        struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
95                        int success)
96 {
97   if (GNUNET_YES == success)
98     LOG (GNUNET_ERROR_TYPE_DEBUG,
99         "Got Queue!\n");
100   else
101     LOG (GNUNET_ERROR_TYPE_DEBUG,
102         "Failed getting queue!\n");
103 }
104
105
106 static void
107 add_queue_cb (void *cls,
108               struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h,
109               struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue)
110 {
111   LOG (GNUNET_ERROR_TYPE_DEBUG,
112       "Got Queue!\n");
113 }
114
115
116 static void
117 run (void *cls)
118 {
119   struct GNUNET_CONFIGURATION_Handle *cfg = cls;
120
121   tc_hs[0] = GNUNET_TRANSPORT_TESTING_transport_communicator_service_start (
122       "transport",
123       "test_communicator_1.conf",
124       &communicator_available_cb,
125       NULL,
126       &queue_create_reply_cb,
127       &add_queue_cb,
128       NULL); /* cls */
129   tc_hs[1] = GNUNET_TRANSPORT_TESTING_transport_communicator_service_start (
130       "transport",
131       "test_communicator_2.conf",
132       &communicator_available_cb,
133       &add_address_cb,
134       NULL,
135       &add_queue_cb,
136       NULL); /* cls */
137 }
138
139 int
140 main (int argc,
141       char *const *argv)
142 {
143   char *cfg_filename;
144   char *opt_cfg_filename;
145   const char *xdg;
146   char *loglev;
147   char *logfile;
148   struct GNUNET_CONFIGURATION_Handle *cfg;
149
150   struct GNUNET_GETOPT_CommandLineOption service_options[] = {
151     GNUNET_GETOPT_option_cfgfile (&opt_cfg_filename),
152     GNUNET_GETOPT_option_help (NULL),
153     GNUNET_GETOPT_option_loglevel (&loglev),
154     GNUNET_GETOPT_option_logfile (&logfile),
155     GNUNET_GETOPT_OPTION_END
156   };
157
158   if (GNUNET_OK != GNUNET_log_setup ("test_communicator_unix",
159                                      loglev,
160                                      logfile))
161   {
162     GNUNET_break (0);
163     return GNUNET_SYSERR;
164   }
165
166   xdg = getenv ("XDG_CONFIG_HOME");
167   if (NULL != xdg)
168     GNUNET_asprintf (&cfg_filename,
169                      "%s%s%s",
170                      xdg,
171                      DIR_SEPARATOR_STR,
172                      GNUNET_OS_project_data_get ()->config_file);
173   else
174     cfg_filename = GNUNET_strdup (GNUNET_OS_project_data_get ()->user_config_file);
175   cfg = GNUNET_CONFIGURATION_create ();
176   if (NULL != opt_cfg_filename)
177   {
178     if ( (GNUNET_YES !=
179           GNUNET_DISK_file_test (opt_cfg_filename)) ||
180        (GNUNET_SYSERR ==
181           GNUNET_CONFIGURATION_load (cfg,
182                                      opt_cfg_filename)) )
183     {
184       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
185                   _("Malformed configuration file `%s', exit ...\n"),
186                     opt_cfg_filename);
187       return GNUNET_SYSERR;
188     }
189   }
190   else
191   {
192     if (GNUNET_YES ==
193         GNUNET_DISK_file_test (cfg_filename))
194     {
195       if (GNUNET_SYSERR ==
196           GNUNET_CONFIGURATION_load (cfg,
197                                      cfg_filename))
198       {
199         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
200                     _("Malformed configuration file `%s', exit ...\n"),
201                     cfg_filename);
202         return GNUNET_SYSERR;
203       }
204     }
205     else
206     {
207       if (GNUNET_SYSERR ==
208           GNUNET_CONFIGURATION_load (cfg,
209                                      NULL))
210       {
211         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
212                     _("Malformed configuration, exit ...\n"));
213         return GNUNET_SYSERR;
214       }
215     }
216   }
217   GNUNET_SCHEDULER_run (&run,
218                         cfg);
219 }
220