merge branch 'refactoring-scheduler'
[oweals/gnunet.git] / src / rps / rps-test_util.c
1 /*
2      This file is part of GNUnet.
3      Copyright (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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file rps/rps-test_util.c
23  * @brief Some utils faciliating the view into the internals for the sampler
24  *        needed for evaluation
25  *
26  * @author Julius Bünger
27  */
28
29 #include "platform.h"
30 #include "gnunet_util_lib.h"
31
32 #include <inttypes.h>
33
34 #define LOG(kind, ...) GNUNET_log_from(kind,"rps-sampler",__VA_ARGS__)
35
36 #ifndef TO_FILE
37 #define TO_FILE
38 #endif /* TO_FILE */
39
40 #ifdef TO_FILE
41 void
42 to_file_ (char *file_name, char *line)
43 {
44   struct GNUNET_DISK_FileHandle *f;
45   char output_buffer[512];
46   //size_t size;
47   int size;
48   size_t size2;
49
50
51   if (NULL == (f = GNUNET_DISK_file_open (file_name,
52                                           GNUNET_DISK_OPEN_APPEND |
53                                           GNUNET_DISK_OPEN_WRITE |
54                                           GNUNET_DISK_OPEN_CREATE,
55                                           GNUNET_DISK_PERM_USER_READ |
56                                           GNUNET_DISK_PERM_USER_WRITE |
57                                           GNUNET_DISK_PERM_GROUP_READ |
58                                           GNUNET_DISK_PERM_OTHER_READ)))
59   {
60     LOG (GNUNET_ERROR_TYPE_WARNING,
61          "Not able to open file %s\n",
62          file_name);
63     return;
64   }
65   size = GNUNET_snprintf (output_buffer,
66                           sizeof (output_buffer),
67                           "%llu %s\n",
68                           GNUNET_TIME_absolute_get ().abs_value_us,
69                           line);
70   if (0 > size)
71   {
72     LOG (GNUNET_ERROR_TYPE_WARNING,
73          "Failed to write string to buffer (size: %i)\n",
74          size);
75     return;
76   }
77
78   size2 = GNUNET_DISK_file_write (f, output_buffer, size);
79   if (size != size2)
80   {
81     LOG (GNUNET_ERROR_TYPE_WARNING,
82          "Unable to write to file! (Size: %u, size2: %u)\n",
83          size,
84          size2);
85
86     if (GNUNET_YES != GNUNET_DISK_file_close (f))
87       LOG (GNUNET_ERROR_TYPE_WARNING,
88            "Unable to close file\n");
89
90     return;
91   }
92
93   if (GNUNET_YES != GNUNET_DISK_file_close (f))
94     LOG (GNUNET_ERROR_TYPE_WARNING,
95          "Unable to close file\n");
96 }
97
98 char *
99 auth_key_to_string (struct GNUNET_CRYPTO_AuthKey auth_key)
100 {
101   int size;
102   size_t name_buf_size;
103   char *end;
104   char *buf;
105   char *name_buf;
106   size_t keylen = (sizeof (struct GNUNET_CRYPTO_AuthKey)) * 8;
107
108   name_buf_size = 512 * sizeof (char);
109   name_buf = GNUNET_malloc (name_buf_size);
110
111   if (keylen % 5 > 0)
112     keylen += 5 - keylen % 5;
113   keylen /= 5;
114   buf = GNUNET_malloc (keylen + 1);
115
116   end = GNUNET_STRINGS_data_to_string (&(auth_key.key),
117       sizeof (struct GNUNET_CRYPTO_AuthKey),
118       buf,
119       keylen);
120
121   if (NULL == end)
122   {
123     GNUNET_free (buf);
124     GNUNET_break (0);
125   }
126   else
127   {
128     *end = '\0';
129   }
130
131   size = GNUNET_snprintf (name_buf, name_buf_size, "sampler_el-%s", buf);
132   if (0 > size)
133     LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to create name_buf\n");
134
135   GNUNET_free (buf);
136
137   return name_buf;
138 }
139
140
141 struct GNUNET_CRYPTO_AuthKey
142 string_to_auth_key (const char *str)
143 {
144   struct GNUNET_CRYPTO_AuthKey auth_key;
145
146   if (GNUNET_OK !=
147       GNUNET_STRINGS_string_to_data (str,
148                                      strlen (str),
149                                      &auth_key.key,
150                                      sizeof (struct GNUNET_CRYPTO_AuthKey)))
151   {
152     LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to convert string to data\n");
153   }
154
155   return auth_key;
156 }
157
158
159 char *
160 create_file (const char *name)
161 {
162   int size;
163   size_t name_buf_size;
164   char *name_buf;
165   char *prefix;
166   char *file_name;
167
168   prefix = "/tmp/rps/";
169   name_buf_size = (strlen (prefix) + strlen (name) + 2) * sizeof (char);
170   name_buf = GNUNET_malloc (name_buf_size);
171
172   size = GNUNET_snprintf (name_buf, name_buf_size, "%s%s", prefix, name);
173   if (0 > size)
174     LOG (GNUNET_ERROR_TYPE_WARNING, "Failed to create name_buf\n");
175
176   if (GNUNET_YES != GNUNET_DISK_directory_create (prefix))
177   {
178     LOG (GNUNET_ERROR_TYPE_WARNING,
179          "Could not create directory %s.\n",
180          prefix);
181   }
182
183   if (NULL == strstr (name, "sampler_el"))
184   {/* only append random string to sampler */
185     if (NULL == (file_name = GNUNET_DISK_mktemp (name_buf)))
186           LOG (GNUNET_ERROR_TYPE_WARNING, "Could not create file\n");
187
188   GNUNET_free (name_buf);
189   return file_name;
190   }
191
192   return name_buf;
193 }
194
195 #endif /* TO_FILE */
196
197 /* end of gnunet-service-rps.c */