aboutsummaryrefslogtreecommitdiff
path: root/src/transport/transport-testing-send.c
blob: 60fed23a694ab1a362c1545b07be26e9e8fc4e96 (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
     This file is part of GNUnet.
     Copyright (C) 2016 GNUnet e.V.

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, 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
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
*/
/**
 * @file transport-testing-send.c
 * @brief convenience transmission function for tests
 * @author Christian Grothoff
 */
#include "transport-testing.h"

/**
 * Acceptable transmission delay.
 */
#define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)


static size_t
notify_ready (void *cls,
              size_t size,
              void *buf)
{ 
  struct TRANSPORT_TESTING_SendJob *sj = cls;
  struct GNUNET_TRANSPORT_TESTING_PeerContext *sender = sj->sender;
  struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver = sj->receiver;
  struct GNUNET_TRANSPORT_TESTING_Handle *tth = sender->tth;
  uint16_t msize = sj->msize;
  struct GNUNET_TRANSPORT_TESTING_TestMessage *test;

  sj->th = NULL;
  GNUNET_CONTAINER_DLL_remove (tth->sj_head,
			       tth->sj_tail,
			       sj);
  if (NULL == buf)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Timeout occurred while waiting for transmit_ready\n");
    GNUNET_SCHEDULER_shutdown ();
    GNUNET_free (sj);
    return 0;
  }

  GNUNET_assert (size >= msize);
  if (NULL != buf)
  {
    memset (buf, sj->num, msize);
    test = buf;
    test->header.size = htons (msize);
    test->header.type = htons (sj->mtype);
    test->num = htonl (sj->num);
  }

  {
    char *ps = GNUNET_strdup (GNUNET_i2s (&sender->id));

    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                "Sending message %u from %u (%s) with type %u and size %u bytes to peer %u (%s)\n",
		(unsigned int) sj->num,
		sender->no,
		ps,
                sj->mtype,
                msize,
                receiver->no,
                GNUNET_i2s (&receiver->id));
    GNUNET_free (ps);
  }
  if (NULL != sj->cont)
    GNUNET_SCHEDULER_add_now (sj->cont,
			      sj->cont_cls);
  GNUNET_free (sj);
  return msize;
}


/**
 * Return @a cx in @a cls.
 */
static void
find_cr (void *cls,   
	 struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cx)
{
  struct GNUNET_TRANSPORT_TESTING_ConnectRequest **cr = cls;

  *cr = cx;
}


/**
 * Send a test message of type @a mtype and size @a msize from
 * peer @a sender to peer @a receiver.  The peers should be
 * connected when this function is called.
 *
 * @param sender the sending peer
 * @param receiver the receiving peer
 * @param mtype message type to use
 * @param msize size of the message, at least `sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage)`
 * @param num unique message number
 * @param cont continuation to call after transmission
 * @param cont_cls closure for @a cont
 * @return #GNUNET_OK if message was queued,
 *         #GNUNET_NO if peers are not connected
 *         #GNUNET_SYSERR if @a msize is illegal
 */
int
GNUNET_TRANSPORT_TESTING_send (struct GNUNET_TRANSPORT_TESTING_PeerContext *sender,
			       struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
			       uint16_t mtype,
			       uint16_t msize,
			       uint32_t num,
			       GNUNET_SCHEDULER_TaskCallback cont,
			       void *cont_cls)
{
  struct GNUNET_TRANSPORT_TESTING_Handle *tth = sender->tth;
  struct TRANSPORT_TESTING_SendJob *sj;
  struct GNUNET_TRANSPORT_TESTING_ConnectRequest *cr;

  if (msize < sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage))
  {
    GNUNET_break (0);
    return GNUNET_SYSERR;
  }
  cr = NULL;
  GNUNET_TRANSPORT_TESTING_find_connecting_context (sender,
						    receiver,
						    &find_cr,
						    &cr);
  if (NULL == cr)
    GNUNET_TRANSPORT_TESTING_find_connecting_context (receiver,
						      sender,
						      &find_cr,
						      &cr);
  if ( (NULL == cr) ||
       (GNUNET_YES != cr->connected) )
  {
    GNUNET_break (0);
    return GNUNET_NO;
  }
  sj = GNUNET_new (struct TRANSPORT_TESTING_SendJob);
  sj->num = num;
  sj->sender = sender;
  sj->receiver = receiver;
  sj->cont = cont;
  sj->cont_cls = cont_cls;
  sj->mtype = mtype;
  sj->msize = msize;
  GNUNET_CONTAINER_DLL_insert (tth->sj_head,
			       tth->sj_tail,
			       sj);
  {
    char *receiver_s = GNUNET_strdup (GNUNET_i2s (&receiver->id));

    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                "Sending message from peer %u (`%s') -> peer %u (`%s') !\n",
                sender->no,
                GNUNET_i2s (&sender->id),
                receiver->no,
                receiver_s);
    GNUNET_free (receiver_s);
  }
  sj->th = GNUNET_TRANSPORT_notify_transmit_ready (sender->th,
						   &receiver->id,
						   msize,
						   TIMEOUT_TRANSMIT,
						   &notify_ready,
						   sj);
  GNUNET_assert (NULL != sj->th);
  return GNUNET_OK;
}


/**
 * Task that sends a test message from the 
 * first peer to the second peer.
 *
 * @param ccc context which should contain at least two peers, the
 *        first two of which should be currently connected
 * @param size desired message size
 * @param cont continuation to call after transmission
 * @param cont_cls closure for @a cont
 */
static void
do_send (struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc,
	 uint16_t size,
	 GNUNET_SCHEDULER_TaskCallback cont,
	 void *cont_cls)
{
  int ret;

  ccc->global_ret = GNUNET_SYSERR;
  ret = GNUNET_TRANSPORT_TESTING_send (ccc->p[0],
				       ccc->p[1],
				       GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE,
				       size,
				       ccc->send_num_gen++,
				       cont,
				       cont_cls);
  GNUNET_assert (GNUNET_SYSERR != ret);
  if (GNUNET_NO == ret)
  {
    GNUNET_break (0);
    ccc->global_ret = GNUNET_SYSERR;
    GNUNET_SCHEDULER_shutdown ();
  }
}


/**
 * Task that sends a minimalistic test message from the 
 * first peer to the second peer.
 *
 * @param cls the `struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext`
 *        which should contain at least two peers, the first two
 *        of which should be currently connected
 */
void
GNUNET_TRANSPORT_TESTING_simple_send (void *cls)
{
  struct GNUNET_TRANSPORT_TESTING_SendClosure *sc = cls;
  int done;
  size_t msize;

  if (0 < sc->num_messages)
  {
    sc->num_messages--;
    done = (0 == sc->num_messages);
  }
  else
  {
    done = 0; /* infinite loop */
  }
  msize = sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage);
  if (NULL != sc->get_size_cb)
    msize = sc->get_size_cb (sc->num_messages);
  /* if this was the last message, call the continuation,
     otherwise call this function again */
  do_send (sc->ccc,
	   msize,
	   done ? sc->cont : &GNUNET_TRANSPORT_TESTING_simple_send,
	   done ? sc->cont_cls : sc);
}


/**
 * Task that sends a large test message from the 
 * first peer to the second peer.
 *
 * @param cls the `struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext`
 *        which should contain at least two peers, the first two
 *        of which should be currently connected
 */
void
GNUNET_TRANSPORT_TESTING_large_send (void *cls)
{
  struct GNUNET_TRANSPORT_TESTING_SendClosure *sc = cls;
  int done;
  size_t msize;

  if (0 < sc->num_messages)
  {
    sc->num_messages--;
    done = (0 == sc->num_messages);
  }
  else
  {
    done = 0; /* infinite loop */
  }
  msize = 2600;
  if (NULL != sc->get_size_cb)
    msize = sc->get_size_cb (sc->num_messages);
  /* if this was the last message, call the continuation,
     otherwise call this function again */
  do_send (sc->ccc,
	   msize,
	   done ? sc->cont : &GNUNET_TRANSPORT_TESTING_large_send,
	   done ? sc->cont_cls : sc);
}

/* end of transport-testing-send.c */