aboutsummaryrefslogtreecommitdiff
path: root/src/testing/testing.c
diff options
context:
space:
mode:
authorSree Harsha Totakura <totakura@in.tum.de>2013-03-31 20:48:06 +0000
committerSree Harsha Totakura <totakura@in.tum.de>2013-03-31 20:48:06 +0000
commit36291b73fab94a984ae4f7314f983e18492238be (patch)
treec5a2d753c04ebedcf236b0b31018789afbaf7e85 /src/testing/testing.c
parent1517375e5b33fb42d6cd50fcbbc8d990ddd5b81a (diff)
downloadgnunet-36291b73fab94a984ae4f7314f983e18492238be.tar.gz
gnunet-36291b73fab94a984ae4f7314f983e18492238be.zip
support for asynchronous peer start/stop and service start/stop using ARM API
Diffstat (limited to 'src/testing/testing.c')
-rw-r--r--src/testing/testing.c196
1 files changed, 113 insertions, 83 deletions
diff --git a/src/testing/testing.c b/src/testing/testing.c
index 216dddfea..a96df9674 100644
--- a/src/testing/testing.c
+++ b/src/testing/testing.c
@@ -190,7 +190,7 @@ struct GNUNET_TESTING_Peer
190 */ 190 */
191 struct GNUNET_CONFIGURATION_Handle *cfg; 191 struct GNUNET_CONFIGURATION_Handle *cfg;
192 192
193 GNUNET_TESTING_PeerStartCallback cb; 193 GNUNET_TESTING_PeerStatusCallback cb;
194 194
195 void *cb_cls; 195 void *cb_cls;
196 196
@@ -997,6 +997,45 @@ GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system,
997 997
998 998
999/** 999/**
1000 * Wrapper over GNUNET_TESTING_peer_configure() to set the
1001 * GNUNET_TESTING_PeerStatusCallback() for using functions
1002 * GNUNET_TESTING_peer_start2() and GNUNET_TESTING_peer_stop2()
1003 *
1004 * @param system system to use to coordinate resource usage
1005 * @param cfg configuration to use; will be UPDATED (to reflect needed
1006 * changes in port numbers and paths)
1007 * @param key_number number of the hostkey to use for the peer
1008 * @param id identifier for the daemon, will be set, can be NULL
1009 * @param emsg set to freshly allocated error message (set to NULL on success),
1010 * can be NULL
1011 * @param status_cb the status callback to call upon peer start and stop
1012 * @return handle to the peer, NULL on error
1013 */
1014struct GNUNET_TESTING_Peer *
1015GNUNET_TESTING_peer_configure2 (struct GNUNET_TESTING_System *system,
1016 struct GNUNET_CONFIGURATION_Handle *cfg,
1017 uint32_t key_number,
1018 struct GNUNET_PeerIdentity *id,
1019 char **emsg,
1020 GNUNET_TESTING_PeerStatusCallback status_cb,
1021 void *cls)
1022{
1023 struct GNUNET_TESTING_Peer *peer;
1024
1025 peer = GNUNET_TESTING_peer_configure (system,
1026 cfg,
1027 key_number,
1028 id,
1029 emsg);
1030 if (NULL == peer)
1031 return NULL;
1032 peer->cb = status_cb;
1033 peer->cb_cls = cls;
1034 return peer;
1035}
1036
1037
1038/**
1000 * Obtain the peer identity from a peer handle. 1039 * Obtain the peer identity from a peer handle.
1001 * 1040 *
1002 * @param peer peer handle for which we want the peer's identity 1041 * @param peer peer handle for which we want the peer's identity
@@ -1055,51 +1094,64 @@ GNUNET_TESTING_peer_start (struct GNUNET_TESTING_Peer *peer)
1055} 1094}
1056 1095
1057 1096
1058void 1097/**
1098 * Start a service at a peer using its ARM service
1099 *
1100 * @param peer the peer whose service has to be started
1101 * @param service_name name of the service to start
1102 * @param timeout how long should the ARM API try to send the request to start
1103 * the service
1104 * @param cont the callback to call with result and status from ARM API
1105 * @param cont_cls the closure for the above callback
1106 * @return GNUNET_OK upon successfully queuing the service start request;
1107 * GNUNET_SYSERR upon error
1108 */
1109int
1059GNUNET_TESTING_peer_service_start (struct GNUNET_TESTING_Peer *peer, 1110GNUNET_TESTING_peer_service_start (struct GNUNET_TESTING_Peer *peer,
1060 const char *service_name, 1111 const char *service_name,
1112 struct GNUNET_TIME_Relative timeout,
1061 GNUNET_ARM_ResultCallback cont, 1113 GNUNET_ARM_ResultCallback cont,
1062 void *cont_cls) 1114 void *cont_cls)
1063{ 1115{
1116 if (NULL == peer->ah)
1117 return GNUNET_SYSERR;
1064 GNUNET_ARM_request_service_start (peer->ah, 1118 GNUNET_ARM_request_service_start (peer->ah,
1065 service_name, 1119 service_name,
1066 GNUNET_OS_INHERIT_STD_ALL, 1120 GNUNET_OS_INHERIT_STD_ALL,
1067 GNUNET_TIME_UNIT_MINUTES, 1121 timeout,
1068 cont, 1122 cont, cont_cls);
1069 cont_cls); 1123 return GNUNET_OK;
1070
1071} 1124}
1072 1125
1073 1126
1074void GNUNET_TESTING_peer_service_stop (struct GNUNET_TESTING_Peer *peer, 1127/**
1075 const char *service_name, 1128 * Stop a service at a peer using its ARM service
1076 GNUNET_ARM_ResultCallback cont, 1129 *
1077 void *cont_cls) 1130 * @param peer the peer whose service has to be stopped
1131 * @param service_name name of the service to stop
1132 * @param timeout how long should the ARM API try to send the request to stop
1133 * the service
1134 * @param cont the callback to call with result and status from ARM API
1135 * @param cont_cls the closure for the above callback
1136 * @return GNUNET_OK upon successfully queuing the service stop request;
1137 * GNUNET_SYSERR upon error
1138 */
1139int
1140GNUNET_TESTING_peer_service_stop (struct GNUNET_TESTING_Peer *peer,
1141 const char *service_name,
1142 struct GNUNET_TIME_Relative timeout,
1143 GNUNET_ARM_ResultCallback cont,
1144 void *cont_cls)
1078{ 1145{
1079 GNUNET_ARM_request_service_stop (peer->ah, service_name, 1146 if (NULL == peer->ah)
1080 GNUNET_TIME_UNIT_MINUTES, 1147 return GNUNET_SYSERR;
1148 GNUNET_ARM_request_service_stop (peer->ah,
1149 service_name,
1150 timeout,
1081 cont, cont_cls); 1151 cont, cont_cls);
1152 return GNUNET_OK;
1082} 1153}
1083 1154
1084static void
1085arm_start_result_cb (void *cls,
1086 struct GNUNET_ARM_Handle *arm,
1087 enum GNUNET_ARM_RequestStatus rs,
1088 const char *service,
1089 enum GNUNET_ARM_Result result)
1090{
1091 struct GNUNET_TESTING_Peer *peer = cls;
1092
1093 if ((GNUNET_ARM_REQUEST_SENT_OK != rs)
1094 || ! ((GNUNET_ARM_RESULT_STARTING == result)
1095 || (GNUNET_ARM_RESULT_IS_STARTING_ALREADY == result)
1096 || (GNUNET_ARM_RESULT_IS_STARTED_ALREADY == result)))
1097 {
1098 peer->cb (peer->cb_cls, peer, GNUNET_NO);
1099 return;
1100 }
1101 peer->cb (peer->cb_cls, peer, GNUNET_OK);
1102}
1103 1155
1104/** 1156/**
1105 * Function called whenever we connect to or disconnect from ARM. 1157 * Function called whenever we connect to or disconnect from ARM.
@@ -1119,23 +1171,33 @@ conn_status (void *cls, struct GNUNET_ARM_Handle *arm,
1119} 1171}
1120 1172
1121 1173
1174/**
1175 * Start a peer asynchronously using ARM API. Peer's startup is signaled
1176 * through the GNUNET_TESTING_PeerStatusCallback() given to
1177 * GNUNET_TESTING_peer_configure2(). To use this function the peer must be
1178 * configured earlier using GNUNET_TESTING_peer_configure2();
1179 *
1180 * @param peer the peer to start
1181 * @param timeout how long to wait before giving up to start the peer
1182 * @return GNUNET_OK upon successfully giving the request to the ARM API (this
1183 * does not mean that the peer is successfully started); GNUNET_SYSERR
1184 * upon any error.
1185 */
1122int 1186int
1123GNUNET_TESTING_peer_start2 (struct GNUNET_TESTING_Peer *peer, 1187GNUNET_TESTING_peer_start2 (struct GNUNET_TESTING_Peer *peer,
1124 GNUNET_TESTING_PeerStartCallback cb, 1188 struct GNUNET_TIME_Relative timeout)
1125 void *cb_cls)
1126{ 1189{
1127 if (NULL != peer->ah) 1190 if (NULL != peer->ah)
1128 { 1191 {
1129 GNUNET_break (0); 1192 GNUNET_break (0);
1130 return GNUNET_SYSERR; 1193 return GNUNET_SYSERR;
1131 } 1194 }
1132 GNUNET_assert (NULL != (peer->cb = cb)); 1195 GNUNET_assert (NULL != peer->cb);
1133 peer->cb_cls = cb_cls;
1134 peer->ah = GNUNET_ARM_connect (peer->cfg, &conn_status, peer); 1196 peer->ah = GNUNET_ARM_connect (peer->cfg, &conn_status, peer);
1135 if (NULL == peer->ah) 1197 if (NULL == peer->ah)
1136 return GNUNET_SYSERR; 1198 return GNUNET_SYSERR;
1137 //GNUNET_TESTING_peer_service_start (peer, "arm", &arm_start_result_cb, peer); 1199 //GNUNET_TESTING_peer_service_start (peer, "arm", &arm_start_result_cb, peer);
1138 GNUNET_TESTING_peer_service_start (peer, "arm", NULL, NULL); 1200 GNUNET_TESTING_peer_service_start (peer, "arm", timeout, NULL, NULL);
1139 return GNUNET_OK; 1201 return GNUNET_OK;
1140} 1202}
1141 1203
@@ -1200,58 +1262,28 @@ GNUNET_TESTING_peer_stop (struct GNUNET_TESTING_Peer *peer)
1200 return GNUNET_SYSERR; 1262 return GNUNET_SYSERR;
1201 return GNUNET_OK; 1263 return GNUNET_OK;
1202} 1264}
1203
1204static void
1205arm_stop_result_cb (void *cls,
1206 struct GNUNET_ARM_Handle *arm,
1207 enum GNUNET_ARM_RequestStatus rs,
1208 const char *service,
1209 enum GNUNET_ARM_Result result)
1210{
1211 struct GNUNET_TESTING_Peer *peer = cls;
1212
1213 if ((GNUNET_ARM_REQUEST_SENT_OK != rs)
1214 || ! ((GNUNET_ARM_RESULT_STOPPED == result)
1215 || (GNUNET_ARM_RESULT_STOPPING == result)
1216 || (GNUNET_ARM_RESULT_IS_STOPPING_ALREADY == result)
1217 || (GNUNET_ARM_RESULT_IS_STOPPED_ALREADY == result)))
1218 {
1219 peer->cb (peer->cb_cls, peer, GNUNET_NO);
1220 return;
1221 }
1222 peer->cb (peer->cb_cls, peer, GNUNET_OK);
1223}
1224
1225
1226static void
1227arm_service_monitor (void *cls,
1228 struct GNUNET_ARM_MonitorHandle *arm,
1229 const char *service,
1230 enum GNUNET_ARM_ServiceStatus status)
1231{
1232 struct GNUNET_TESTING_Peer *peer = cls;
1233
1234 peer->mh = arm;
1235 if (GNUNET_ARM_SERVICE_STOPPED != status)
1236 return;
1237 if (0 != strcasecmp (service, "arm"))
1238 return;
1239 peer->cb (peer->cb_cls, peer, GNUNET_OK);
1240}
1241 1265
1242 1266
1267/**
1268 * Stop a peer asynchronously using ARM API. Peer's shutdown is signaled
1269 * through the GNUNET_TESTING_PeerStatusCallback() given to
1270 * GNUNET_TESTING_peer_configure2(). To use this function the peer must be
1271 * configured earlier using GNUNET_TESTING_peer_configure2();
1272 *
1273 * @param peer the peer to stop
1274 * @param timeout how long to wait before giving up to stop the peer
1275 * @return GNUNET_OK upon successfully giving the request to the ARM API (this
1276 * does not mean that the peer is successfully stopped); GNUNET_SYSERR
1277 * upon any error.
1278 */
1243int 1279int
1244GNUNET_TESTING_peer_stop2 (struct GNUNET_TESTING_Peer *peer, 1280GNUNET_TESTING_peer_stop2 (struct GNUNET_TESTING_Peer *peer,
1245 GNUNET_TESTING_PeerStartCallback cb, 1281 struct GNUNET_TIME_Relative timeout)
1246 void *cb_cls)
1247{ 1282{
1248 if (NULL == peer->ah) 1283 if (NULL == peer->ah)
1249 return GNUNET_SYSERR; 1284 return GNUNET_SYSERR;
1250 GNUNET_assert (NULL != (peer->cb = cb)); 1285 GNUNET_assert (NULL != peer->cb);
1251 peer->cb_cls = cb_cls; 1286 GNUNET_TESTING_peer_service_stop (peer, "arm", timeout, NULL, NULL);
1252 /* if (NULL == peer->mh) */
1253 /* peer->mh = GNUNET_ARM_monitor (peer->cfg, &arm_service_monitor, peer); */
1254 GNUNET_TESTING_peer_service_stop (peer, "arm", NULL, NULL);
1255 return GNUNET_OK; 1287 return GNUNET_OK;
1256} 1288}
1257 1289
@@ -1268,8 +1300,6 @@ GNUNET_TESTING_peer_destroy (struct GNUNET_TESTING_Peer *peer)
1268{ 1300{
1269 if (NULL != peer->main_process) 1301 if (NULL != peer->main_process)
1270 GNUNET_TESTING_peer_stop (peer); 1302 GNUNET_TESTING_peer_stop (peer);
1271 if (NULL != peer->mh)
1272 GNUNET_ARM_monitor_disconnect_and_free (peer->mh);
1273 if (NULL != peer->ah) 1303 if (NULL != peer->ah)
1274 GNUNET_ARM_disconnect_and_free (peer->ah); 1304 GNUNET_ARM_disconnect_and_free (peer->ah);
1275 GNUNET_free (peer->cfgfile); 1305 GNUNET_free (peer->cfgfile);