1010
1111typedef struct State {
1212 uint32_t id ;
13+ Tox * tox ;
1314 bool self_online ;
1415 bool friend_online ;
1516 bool invited_next ;
@@ -56,7 +57,7 @@ static void handle_conference_invite(Tox *tox, const Tox_Event_Conference_Invite
5657
5758 {
5859 Tox_Err_Conference_Join err ;
59- state -> conference = tox_conference_join (tox , friend_number , cookie , length , & err );
60+ state -> conference = tox_conference_join (state -> tox , friend_number , cookie , length , & err );
6061 ck_assert_msg (err == TOX_ERR_CONFERENCE_JOIN_OK , "failed to join a conference: err = %d" , err );
6162 fprintf (stderr , "tox%u Joined conference %u\n" , state -> id , state -> conference );
6263 state -> joined = true;
@@ -89,7 +90,7 @@ static void handle_conference_peer_list_changed(Tox *tox, const Tox_Event_Confer
8990 state -> id , conference_number );
9091
9192 Tox_Err_Conference_Peer_Query err ;
92- uint32_t count = tox_conference_peer_count (tox , conference_number , & err );
93+ uint32_t count = tox_conference_peer_count (state -> tox , conference_number , & err );
9394
9495 if (err != TOX_ERR_CONFERENCE_PEER_QUERY_OK ) {
9596 fprintf (stderr , "ERROR: %d\n" , err );
@@ -107,7 +108,7 @@ static void handle_conference_connected(Tox *tox, const Tox_Event_Conference_Con
107108 // We're tox2, so now we invite tox3.
108109 if (state -> id == 2 && !state -> invited_next ) {
109110 Tox_Err_Conference_Invite err ;
110- tox_conference_invite (tox , 1 , state -> conference , & err );
111+ tox_conference_invite (state -> tox , 1 , state -> conference , & err );
111112 ck_assert_msg (err == TOX_ERR_CONFERENCE_INVITE_OK , "tox2 failed to invite tox3: err = %d" , err );
112113
113114 state -> invited_next = true;
@@ -126,14 +127,12 @@ static void iterate_one(
126127}
127128
128129static void iterate3_wait (
129- Tox * tox1 , State * state1 ,
130- Tox * tox2 , State * state2 ,
131- Tox * tox3 , State * state3 ,
130+ State * state1 , State * state2 , State * state3 ,
132131 const Tox_Dispatch * dispatch , int interval )
133132{
134- iterate_one (tox1 , state1 , dispatch );
135- iterate_one (tox2 , state2 , dispatch );
136- iterate_one (tox3 , state3 , dispatch );
133+ iterate_one (state1 -> tox , state1 , dispatch );
134+ iterate_one (state2 -> tox , state2 , dispatch );
135+ iterate_one (state3 -> tox , state3 , dispatch );
137136
138137 c_sleep (interval );
139138}
@@ -147,32 +146,32 @@ int main(void)
147146 State state3 = {3 };
148147
149148 // Create toxes.
150- Tox * tox1 = tox_new_log (nullptr , nullptr , & state1 .id );
151- Tox * tox2 = tox_new_log (nullptr , nullptr , & state2 .id );
152- Tox * tox3 = tox_new_log (nullptr , nullptr , & state3 .id );
149+ state1 . tox = tox_new_log (nullptr , nullptr , & state1 .id );
150+ state2 . tox = tox_new_log (nullptr , nullptr , & state2 .id );
151+ state3 . tox = tox_new_log (nullptr , nullptr , & state3 .id );
153152
154- tox_events_init (tox1 );
155- tox_events_init (tox2 );
156- tox_events_init (tox3 );
153+ tox_events_init (state1 . tox );
154+ tox_events_init (state2 . tox );
155+ tox_events_init (state3 . tox );
157156
158157 // tox1 <-> tox2, tox2 <-> tox3
159158 uint8_t key [TOX_PUBLIC_KEY_SIZE ];
160- tox_self_get_public_key (tox2 , key );
161- tox_friend_add_norequest (tox1 , key , nullptr ); // tox1 -> tox2
162- tox_self_get_public_key (tox1 , key );
163- tox_friend_add_norequest (tox2 , key , nullptr ); // tox2 -> tox1
164- tox_self_get_public_key (tox3 , key );
165- tox_friend_add_norequest (tox2 , key , nullptr ); // tox2 -> tox3
166- tox_self_get_public_key (tox2 , key );
167- tox_friend_add_norequest (tox3 , key , nullptr ); // tox3 -> tox2
159+ tox_self_get_public_key (state2 . tox , key );
160+ tox_friend_add_norequest (state1 . tox , key , nullptr ); // tox1 -> tox2
161+ tox_self_get_public_key (state1 . tox , key );
162+ tox_friend_add_norequest (state2 . tox , key , nullptr ); // tox2 -> tox1
163+ tox_self_get_public_key (state3 . tox , key );
164+ tox_friend_add_norequest (state2 . tox , key , nullptr ); // tox2 -> tox3
165+ tox_self_get_public_key (state2 . tox , key );
166+ tox_friend_add_norequest (state3 . tox , key , nullptr ); // tox3 -> tox2
168167
169168 printf ("bootstrapping tox2 and tox3 off tox1\n" );
170169 uint8_t dht_key [TOX_PUBLIC_KEY_SIZE ];
171- tox_self_get_dht_id (tox1 , dht_key );
172- const uint16_t dht_port = tox_self_get_udp_port (tox1 , nullptr );
170+ tox_self_get_dht_id (state1 . tox , dht_key );
171+ const uint16_t dht_port = tox_self_get_udp_port (state1 . tox , nullptr );
173172
174- tox_bootstrap (tox2 , "localhost" , dht_port , dht_key , nullptr );
175- tox_bootstrap (tox3 , "localhost" , dht_port , dht_key , nullptr );
173+ tox_bootstrap (state2 . tox , "localhost" , dht_port , dht_key , nullptr );
174+ tox_bootstrap (state3 . tox , "localhost" , dht_port , dht_key , nullptr );
176175
177176 Tox_Dispatch * dispatch = tox_dispatch_new (nullptr );
178177 ck_assert (dispatch != nullptr );
@@ -191,7 +190,7 @@ int main(void)
191190 fprintf (stderr , "Waiting for toxes to come online\n" );
192191
193192 do {
194- iterate3_wait (tox1 , & state1 , tox2 , & state2 , tox3 , & state3 , dispatch , 100 );
193+ iterate3_wait (& state1 , & state2 , & state3 , dispatch , 100 );
195194 } while (!state1 .self_online || !state2 .self_online || !state3 .self_online );
196195
197196 fprintf (stderr , "Toxes are online\n" );
@@ -200,15 +199,15 @@ int main(void)
200199 fprintf (stderr , "Waiting for friends to connect\n" );
201200
202201 do {
203- iterate3_wait (tox1 , & state1 , tox2 , & state2 , tox3 , & state3 , dispatch , 100 );
202+ iterate3_wait (& state1 , & state2 , & state3 , dispatch , 100 );
204203 } while (!state1 .friend_online || !state2 .friend_online || !state3 .friend_online );
205204
206205 fprintf (stderr , "Friends are connected\n" );
207206
208207 {
209208 // Create new conference, tox1 is the founder.
210209 Tox_Err_Conference_New err ;
211- state1 .conference = tox_conference_new (tox1 , & err );
210+ state1 .conference = tox_conference_new (state1 . tox , & err );
212211 state1 .joined = true;
213212 ck_assert_msg (err == TOX_ERR_CONFERENCE_NEW_OK , "failed to create a conference: err = %d" , err );
214213 fprintf (stderr , "Created conference: id = %u\n" , state1 .conference );
@@ -217,7 +216,7 @@ int main(void)
217216 {
218217 // Invite friend.
219218 Tox_Err_Conference_Invite err ;
220- tox_conference_invite (tox1 , 0 , state1 .conference , & err );
219+ tox_conference_invite (state1 . tox , 0 , state1 .conference , & err );
221220 ck_assert_msg (err == TOX_ERR_CONFERENCE_INVITE_OK , "failed to invite a friend: err = %d" , err );
222221 state1 .invited_next = true;
223222 fprintf (stderr , "tox1 invited tox2\n" );
@@ -226,23 +225,23 @@ int main(void)
226225 fprintf (stderr , "Waiting for invitation to arrive\n" );
227226
228227 do {
229- iterate3_wait (tox1 , & state1 , tox2 , & state2 , tox3 , & state3 , dispatch , 100 );
228+ iterate3_wait (& state1 , & state2 , & state3 , dispatch , 100 );
230229 } while (!state1 .joined || !state2 .joined || !state3 .joined );
231230
232231 fprintf (stderr , "Invitations accepted\n" );
233232
234233 fprintf (stderr , "Waiting for peers to come online\n" );
235234
236235 do {
237- iterate3_wait (tox1 , & state1 , tox2 , & state2 , tox3 , & state3 , dispatch , 100 );
236+ iterate3_wait (& state1 , & state2 , & state3 , dispatch , 100 );
238237 } while (state1 .peers == 0 || state2 .peers == 0 || state3 .peers == 0 );
239238
240239 fprintf (stderr , "All peers are online\n" );
241240
242241 {
243242 fprintf (stderr , "tox1 sends a message to the group: \"hello!\"\n" );
244243 Tox_Err_Conference_Send_Message err ;
245- tox_conference_send_message (tox1 , state1 .conference , TOX_MESSAGE_TYPE_NORMAL ,
244+ tox_conference_send_message (state1 . tox , state1 .conference , TOX_MESSAGE_TYPE_NORMAL ,
246245 (const uint8_t * )"hello!" , 7 , & err );
247246
248247 if (err != TOX_ERR_CONFERENCE_SEND_MESSAGE_OK ) {
@@ -254,16 +253,16 @@ int main(void)
254253 fprintf (stderr , "Waiting for messages to arrive\n" );
255254
256255 do {
257- iterate3_wait (tox1 , & state1 , tox2 , & state2 , tox3 , & state3 , dispatch , 100 );
256+ iterate3_wait (& state1 , & state2 , & state3 , dispatch , 100 );
258257 c_sleep (100 );
259258 } while (!state2 .received || !state3 .received );
260259
261260 fprintf (stderr , "Messages received. Test complete.\n" );
262261
263262 tox_dispatch_free (dispatch );
264- tox_kill (tox3 );
265- tox_kill (tox2 );
266- tox_kill (tox1 );
263+ tox_kill (state3 . tox );
264+ tox_kill (state2 . tox );
265+ tox_kill (state1 . tox );
267266
268267 return 0 ;
269268}
0 commit comments