1 #include <cstdio>
2 #include <cstring>
3 #include <stdlib.h>
4 #include "GetTime.h"
5 #include "Rand.h"
6 #include "RakPeerInterface.h"
7 #include "MessageIdentifiers.h"
8 #include "RakNetworkFactory.h"
9 #include "ReadyEvent.h"
10 #include <assert.h>
11 #include "Kbhit.h"
12 #include "RakSleep.h"
13 #include "ConnectionGraph.h"
14 #include "FullyConnectedMesh.h"
15 
16 void PrintConnections();
17 
18 static const int NUM_PEERS=3;
19 RakPeerInterface *rakPeer[NUM_PEERS];
20 ReadyEvent readyEventPlugin[NUM_PEERS];
21 ConnectionGraph connectionGraphPlugin[NUM_PEERS];
22 FullyConnectedMesh fullyConnectedMeshPlugin[NUM_PEERS];
23 
main(void)24 int main(void)
25 {
26 	int i;
27 
28 	for (i=0; i < NUM_PEERS; i++)
29 		rakPeer[i]=RakNetworkFactory::GetRakPeerInterface();
30 
31 	printf("This project tests and demonstrates the ready event plugin.\n");
32 	printf("It is used in a peer to peer environment to have a group of\nsystems signal an event.\n");
33 	printf("It is useful for changing turns in a turn based game,\nor for lobby systems where everyone has to set ready before the game starts\n");
34 	printf("Difficulty: Beginner\n\n");
35 
36 	int peerIndex;
37 
38 	// Initialize the message handlers
39 	for (peerIndex=0; peerIndex < NUM_PEERS; peerIndex++)
40 	{
41 		fullyConnectedMeshPlugin[peerIndex].Startup(0,0);
42 		rakPeer[peerIndex]->AttachPlugin(&connectionGraphPlugin[peerIndex]);
43 		rakPeer[peerIndex]->AttachPlugin(&fullyConnectedMeshPlugin[peerIndex]);
44 		rakPeer[peerIndex]->AttachPlugin(&readyEventPlugin[peerIndex]);
45 		rakPeer[peerIndex]->SetMaximumIncomingConnections(NUM_PEERS);
46 	}
47 
48 
49 	// Initialize the peers
50 	for (peerIndex=0; peerIndex < NUM_PEERS; peerIndex++)
51 	{
52 		SocketDescriptor socketDescriptor(60000+peerIndex,0);
53 		rakPeer[peerIndex]->Startup(NUM_PEERS, 0, &socketDescriptor, 1);
54 	}
55 
56 	// Give the threads time to properly start
57 	RakSleep(200);
58 
59 	printf("Peers initialized.\n");
60 
61 	printf("'C' to connect a system\n");
62 	printf("'D' to disconnect a system\n");
63 	printf("'S' to signal a system\n");
64 	printf("'U' to unsignal a system\n");
65 	printf("'F' to force all systems to be completed (cannot be unset)\n");
66 	printf("'Q' to quit\n");
67 	printf("' ' to print wait statuses\n");
68 
69 	char str[128];
70 	char ch=0;
71 	while (1)
72 	{
73 		if (kbhit())
74 			ch=getch();
75 
76 		if (ch=='s' || ch=='S')
77 		{
78 			ch=0;
79 			printf("Which system? 0 to %i\n", NUM_PEERS-1);
80 			gets(str);
81 			int sysIndex = atoi(str);
82 			if (sysIndex>=0 && sysIndex<NUM_PEERS)
83 			{
84 				if (readyEventPlugin[sysIndex].SetEvent(0,true))
85 					printf("Set system %i to signaled\n", sysIndex);
86 				else
87 					printf("Set system %i to signaled FAILED\n", sysIndex);
88 			}
89 			else
90 			{
91 				printf("Invalid range\n");
92 			}
93 		}
94 		if (ch=='u' || ch=='U')
95 		{
96 			ch=0;
97 			printf("Which system? 0 to %i\n", NUM_PEERS-1);
98 			gets(str);
99 			int sysIndex = atoi(str);
100 			if (sysIndex>=0 && sysIndex<NUM_PEERS)
101 			{
102 				if (readyEventPlugin[sysIndex].SetEvent(0,false))
103 					printf("Set index %i to unsignaled\n", sysIndex);
104 				else
105 					printf("Set index %i to unsignaled FAILED\n", sysIndex);
106 			}
107 			else
108 			{
109 				printf("Invalid range\n");
110 			}
111 		}
112 		if (ch=='c' || ch=='C')
113 		{
114 			ch=0;
115 			printf("Which system? 0 to %i\n", NUM_PEERS-1);
116 			gets(str);
117 			int sysIndex = atoi(str);
118 			if (sysIndex>=0 && sysIndex<NUM_PEERS)
119 			{
120 				rakPeer[sysIndex]->Connect("127.0.0.1", 60000, 0, 0, 0);
121 				printf("Connecting system %i.\n", sysIndex);
122 			}
123 			else
124 			{
125 				printf("Invalid range\n");
126 			}
127 		}
128 		if (ch=='d' || ch=='D')
129 		{
130 			ch=0;
131 			printf("Which system? 0 to %i\n", NUM_PEERS-1);
132 			gets(str);
133 			int sysIndex = atoi(str);
134 			if (sysIndex>=0 && sysIndex<NUM_PEERS)
135 			{
136 				rakPeer[sysIndex]->Shutdown(100,0);
137 				SocketDescriptor socketDescriptor(60000+sysIndex,0);
138 				rakPeer[sysIndex]->Startup(NUM_PEERS, 0, &socketDescriptor, 1);
139 				printf("Restarting system %i.\n", sysIndex);
140 			}
141 			else
142 			{
143 				printf("Invalid range\n");
144 			}
145 		}
146 		if (ch=='f' || ch=='F')
147 		{
148 			ch=0;
149 			printf("Which system? 0 to %i\n", NUM_PEERS-1);
150 			gets(str);
151 			int sysIndex = atoi(str);
152 			if (sysIndex>=0 && sysIndex<NUM_PEERS)
153 			{
154 				if (readyEventPlugin[sysIndex].ForceCompletion(0))
155 					printf("Set system %i to force complete\n", sysIndex);
156 				else
157 					printf("Set system %i to force complete FAILED\n", sysIndex);
158 			}
159 			else
160 			{
161 				printf("Invalid range\n");
162 			}
163 		}
164 		if (ch==' ')
165 		{
166 			SystemAddress sysAddr;
167 			sysAddr.SetBinaryAddress("127.0.0.1");
168 			unsigned j;
169 			printf("\n");
170 			PrintConnections();
171 			for (i=0; i < NUM_PEERS; i++)
172 			{
173 				printf("System %i, ", i);
174 				if (readyEventPlugin[i].IsEventSet(0))
175 					printf("Set=True, ");
176 				else
177 					printf("Set=False, ");
178 
179 				if (readyEventPlugin[i].IsEventCompleted(0))
180 					printf("Completed=True\n");
181 				else if (readyEventPlugin[i].IsEventCompletionProcessing(0))
182 					printf("Completed=InProgress\n");
183 				else
184 					printf("Completed=False\n");
185 
186 				for (j=0; j < NUM_PEERS; j++)
187 				{
188 					if (i!=j)
189 					{
190 
191 						ReadyEventSystemStatus ress;
192 						sysAddr.port=60000+j;
193 						ress = readyEventPlugin[i].GetReadyStatus(0, sysAddr);
194 						printf("  Remote system %i, status = ", j);
195 
196 						switch (ress)
197 						{
198 						case RES_NOT_WAITING:
199 							printf("RES_NOT_WAITING\n");
200 							break;
201 						case RES_WAITING:
202 							printf("RES_WAITING\n");
203 							break;
204 						case RES_READY:
205 							printf("RES_READY\n");
206 							break;
207 						case RES_ALL_READY:
208 							printf("RES_ALL_READY\n");
209 							break;
210 						case RES_UNKNOWN_EVENT:
211 							printf("RES_UNKNOWN_EVENT\n");
212 							break;
213 
214 						}
215 					}
216 				}
217 			}
218 			ch=0;
219 		}
220 		if (ch=='Q')
221 		{
222 			break;
223 		}
224 
225 		for (i=0; i < NUM_PEERS; i++)
226 		{
227 			Packet *p = rakPeer[i]->Receive();
228 			if (p)
229 			{
230 				switch (p->data[0])
231 				{
232 				case ID_NEW_INCOMING_CONNECTION:
233 					readyEventPlugin[i].AddToWaitList(0, p->systemAddress);
234 					break;
235 				case ID_CONNECTION_REQUEST_ACCEPTED:
236 					readyEventPlugin[i].AddToWaitList(0, p->systemAddress);
237 					break;
238 				case ID_READY_EVENT_ALL_SET:
239 					printf("Got ID_READY_EVENT_ALL_SET from %s\n", p->systemAddress.ToString(true));
240 					break;
241 				}
242 
243 				rakPeer[i]->DeallocatePacket(p);
244 			}
245 		}
246 
247 		// Keep raknet threads responsive
248 		RakSleep(30);
249 	}
250 
251 	for (i=0; i < NUM_PEERS; i++)
252 		RakNetworkFactory::DestroyRakPeerInterface(rakPeer[i]);
253 
254 	return 1;
255 }
256 
PrintConnections()257 void PrintConnections()
258 {
259 	int i,j;
260 	char ch=0;
261 	SystemAddress systemAddress;
262 
263 	printf("--------------------------------\n");
264 	for (i=0; i < NUM_PEERS; i++)
265 	{
266 		if (NUM_PEERS<=10)
267 		{
268 			/*
269 			printf("%i (Mesh): ", 60000+i);
270 			for (j=0; j < (int)fullyConnectedMeshPlugin[i].GetMeshPeerListSize(); j++)
271 			{
272 			systemAddress=fullyConnectedMeshPlugin[i].GetPeerIDAtIndex(j);
273 			if (systemAddress!=UNASSIGNED_SYSTEM_ADDRESS)
274 			printf("%i ", systemAddress.port);
275 			}
276 
277 			printf("\n");
278 			*/
279 
280 			printf("%i (Conn): ", 60000+i);
281 			for (j=0; j < NUM_PEERS; j++)
282 			{
283 				systemAddress=rakPeer[i]->GetSystemAddressFromIndex(j);
284 				if (systemAddress!=UNASSIGNED_SYSTEM_ADDRESS)
285 					printf("%i ", systemAddress.port);
286 			}
287 
288 			printf("\n");
289 		}
290 		else
291 		{
292 			int connCount;
293 			//int meshCount;
294 			for (connCount=0, j=0; j < NUM_PEERS; j++)
295 			{
296 				systemAddress=rakPeer[i]->GetSystemAddressFromIndex(j);
297 				if (systemAddress!=UNASSIGNED_SYSTEM_ADDRESS)
298 					connCount++;
299 			}
300 			/*
301 			for (meshCount=0, j=0; j < (int)fullyConnectedMeshPlugin[i].GetMeshPeerListSize(); j++)
302 			{
303 			systemAddress=fullyConnectedMeshPlugin[i].GetPeerIDAtIndex(j);
304 			if (systemAddress!=UNASSIGNED_SYSTEM_ADDRESS)
305 			meshCount++;
306 			}
307 			*/
308 
309 			//printf("%i (Mesh): %i peers should be connected\n", 60000+i, meshCount);
310 			printf("%i (Conn): %i peers are connected\n", 60000+i, connCount);
311 		}
312 	}
313 	printf("\n");
314 	ch=0;
315 
316 	printf("--------------------------------\n");
317 
318 }