aboutsummaryrefslogtreecommitdiff
path: root/src/transport/transport-testing2.c
blob: df48e778297853e655c877b6fdf9931fb1122847 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
     This file is part of GNUnet.
     Copyright (C) 2019 GNUnet e.V.

     GNUnet is free software: you can redistribute it and/or modify it
     under the terms of the GNU Affero General Public License as published
     by the Free Software Foundation, either version 3 of the License,
     or (at your option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     Affero General Public License for more details.

     You should have received a copy of the GNU Affero General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

     SPDX-License-Identifier: AGPL3.0-or-later
*/

/**
 * @file transport/transport-testing2.c
 * @brief functions related to testing-tng
 * @author Christian Grothoff
 * @author Julius Bünger
 */
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_protocols.h"
#include "gnunet_constants.h"
#include "transport-testing2.h"
#include "gnunet_ats_transport_service.h"
#include "gnunet_signatures.h"
#include "transport.h"


#define LOG(kind,...) GNUNET_log_from (kind, "transport-testing2", __VA_ARGS__)


/**
 * @brief Check whether incoming msg indicating available communicator is
 * correct
 *
 * @param cls Closure
 * @param msg Message struct
 *
 * @return GNUNET_YES in case message is correct
 */
static int
check_communicator_available (void *cls,
    const struct GNUNET_TRANSPORT_CommunicatorAvailableMessage *msg)
{
  LOG (GNUNET_ERROR_TYPE_DEBUG,
      "check_communicator_available()\n");
  return GNUNET_YES;
}


/**
 * @brief Handle new communicator
 *
 * @param cls Closure
 * @param msg Message struct
 */
static void
handle_communicator_available (void *cls,
    const struct GNUNET_TRANSPORT_CommunicatorAvailableMessage *msg)
{
  GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback communicator_available = cls;
  LOG (GNUNET_ERROR_TYPE_DEBUG,
      "handle_communicator_available()\n");
  if (NULL != communicator_available)
  {
    LOG (GNUNET_ERROR_TYPE_DEBUG,
        "calling communicator_available()\n");
    communicator_available (NULL, msg);
  }
  //GNUNET_SERVICE_client_continue (client);
}


/**
 * @brief Shut down the service
 *
 * @param cls Closure - Handle to the service
 */
static void
shutdown_service (void *cls)
{
  struct GNUNET_SERVICE_Handle *h = cls;

  GNUNET_SERVICE_stop (h);
}


/**
 * @brief Start the communicator part of the transport service
 *
 * @param communicator_available Callback to be called when a new communicator
 * becomes available
 * @param cfg Configuration
 */
static void
transport_communicator_start (GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback communicator_available,
                              struct GNUNET_CONFIGURATION_Handle *cfg)
{
  struct GNUNET_MQ_MessageHandler mh[] = {
    GNUNET_MQ_hd_var_size (communicator_available,
        GNUNET_MESSAGE_TYPE_TRANSPORT_NEW_COMMUNICATOR,
        struct GNUNET_TRANSPORT_CommunicatorAvailableMessage,
        &communicator_available),
    //GNUNET_MQ_hd_var_size (communicator_backchannel,
    //    GNUNET_MESSAGE_TYPE_TRANSPORT_COMMUNICATOR_BACKCHANNEL,
    //    struct GNUNET_TRANSPORT_CommunicatorBackchannel,
    //    NULL),
    //GNUNET_MQ_hd_var_size (add_address,
    //    GNUNET_MESSAGE_TYPE_TRANSPORT_ADD_ADDRESS,
    //    struct GNUNET_TRANSPORT_AddAddressMessage,
    //    NULL),
    //GNUNET_MQ_hd_fixed_size (del_address,
    //                         GNUNET_MESSAGE_TYPE_TRANSPORT_DEL_ADDRESS,
    //                         struct GNUNET_TRANSPORT_DelAddressMessage,
    //                         NULL),
    //GNUNET_MQ_hd_var_size (incoming_msg,
    //    GNUNET_MESSAGE_TYPE_TRANSPORT_INCOMING_MSG,
    //    struct GNUNET_TRANSPORT_IncomingMessage,
    //    NULL),
    //GNUNET_MQ_hd_fixed_size (queue_create_ok,
    //      GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE_OK,
    //      struct GNUNET_TRANSPORT_CreateQueueResponse,
    //      NULL),
    //GNUNET_MQ_hd_fixed_size (queue_create_fail,
    //      GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_CREATE_FAIL,
    //      struct GNUNET_TRANSPORT_CreateQueueResponse,
    //      NULL),
    //GNUNET_MQ_hd_var_size (add_queue_message,
    //    GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_SETUP,
    //    struct GNUNET_TRANSPORT_AddQueueMessage,
    //    NULL),
    //GNUNET_MQ_hd_fixed_size (del_queue_message,
    //                         GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_TEARDOWN,
    //                         struct GNUNET_TRANSPORT_DelQueueMessage,
    //                         NULL),
    //GNUNET_MQ_hd_fixed_size (send_message_ack,
    //                         GNUNET_MESSAGE_TYPE_TRANSPORT_SEND_MSG_ACK,
    //                         struct GNUNET_TRANSPORT_SendMessageToAck,
    //                         NULL),
  };
  struct GNUNET_SERVICE_Handle *h;

  h = GNUNET_SERVICE_start ("transport",
                            cfg,
                            NULL,
                            NULL,
                            NULL,
                            mh);
  if (NULL == h)
    LOG (GNUNET_ERROR_TYPE_ERROR,
         "Failed starting service!\n");
  else
  {
    LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Started service\n");
    GNUNET_SCHEDULER_add_shutdown (&shutdown_service, h);
  }
}


/**
 * @brief Start the communicator
 *
 * @param cfgname Name of the communicator
 */
static void
communicator_start (const char *cfgname)
{
  char *binary;
  struct GNUNET_CONFIGURATION_Handle *cfg;
  struct GNUNET_OS_Process *proc;

  LOG (GNUNET_ERROR_TYPE_DEBUG,
      "communicator_start\n");
  binary = GNUNET_OS_get_libexec_binary_path ("gnunet-communicator-unix");
  cfg = GNUNET_CONFIGURATION_create ();
  proc =
    GNUNET_OS_start_process (GNUNET_YES,
                             GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
                             NULL, NULL, NULL,
                             binary,
                             "./gnunet-communicator-unix",
                             "-c",
                             cfgname,
                             NULL);
  if (NULL == proc)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Failed to start communicator!");
    return;
  }
  GNUNET_assert (GNUNET_OK ==
                 GNUNET_CONFIGURATION_load (cfg,
                                            cfgname));
  LOG (GNUNET_ERROR_TYPE_DEBUG,
      "started communicator\n");
  GNUNET_free (binary);
}


/**
 * @brief Start communicator part of transport service and communicator
 *
 * @param service_name Name of the service
 * @param cfg Configuration handle
 * @param communicator_available Callback that is called when a new
 * communicator becomes available
 * @param cb_cls Closure to @p communicator_available
 *
 * @return Handle to the communicator duo
 */
struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *
GNUNET_TRANSPORT_TESTING_transport_communicator_service_start
  (const char *service_name,
   const char *cfg_filename,
   GNUNET_TRANSPORT_TESTING_CommunicatorAvailableCallback communicator_available,
   //GNUNET_TRANSPORT_TESTING_Callback2 cb2,
   //GNUNET_TRANSPORT_TESTING_Callback3 cb3,
   //GNUNET_TRANSPORT_TESTING_Callback4 cb4,
   void *cb_cls)
{
  struct GNUNET_CONFIGURATION_Handle *cfg;

  cfg = GNUNET_CONFIGURATION_create ();
  if ( (GNUNET_SYSERR ==
        GNUNET_CONFIGURATION_load (cfg,
                                   cfg_filename)) )
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                _("Malformed configuration file `%s', exit ...\n"),
                  cfg_filename);
    return NULL;
  }
  /* Start communicator part of service */
  transport_communicator_start (communicator_available, cfg);

  /* Schedule start communicator */
  communicator_start ("test_communicator_1.conf");
}

//void
//GNUNET_TRANSPORT_TESTING_transport_communicator_open_queue
//  (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tch,
//   const char *address);
//
//struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorTransmission *
//GNUNET_TRANSPORT_TESTING_transport_communicator_send
//  (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tcq,
//   const struct GNUNET_MessageHeader *hdr,
//   GNUNET_TRANSPORT_TESTING_SuccessStatus cb, void *cb_cls);