1 // Common includes
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include "Kbhit.h"
5 #include "RakNetworkFactory.h"
6 #include "GetTime.h"
7 #include "RakPeerInterface.h"
8 #include "MessageIdentifiers.h"
9 #include "BitStream.h"
10 #include "StringCompressor.h"
11 #include "FileListTransfer.h"
12 #include "FileList.h" // FLP_Printf
13 #include "AutopatcherServer.h"
14 #include "AutopatcherMySQLRepository.h"
15 #include "PacketizedTCP.h"
16
17 #ifdef _WIN32
18 #include "WindowsIncludes.h" // Sleep
19 #else
20 #include <unistd.h> // usleep
21 #endif
22
23 #define USE_TCP
24 #define LISTEN_PORT 60000
25 #define MAX_INCOMING_CONNECTIONS 8
26
main(int argc,char ** argv)27 int main(int argc, char **argv)
28 {
29 // Avoids the Error: Got a packet bigger than 'max_allowed_packet' bytes
30 printf("Important: Requires that you first set the DB schema and the max packet size on the server.\n");
31 printf("See DependentExtensions/AutopatcherMySQLRepository/readme.txt\n");
32
33 printf("Server starting... ");
34 AutopatcherServer autopatcherServer;
35 FLP_Printf progressIndicator;
36 FileListTransfer fileListTransfer;
37 // So only one thread runs per connection, we create an array of connection objects, and tell the autopatcher server to use one thread per item
38 static const int sqlConnectionObjectCount=4;
39 AutopatcherMySQLRepository connectionObject[sqlConnectionObjectCount];
40 AutopatcherRepositoryInterface *connectionObjectAddresses[sqlConnectionObjectCount];
41 for (int i=0; i < sqlConnectionObjectCount; i++)
42 connectionObjectAddresses[i]=&connectionObject[i];
43 fileListTransfer.SetCallback(&progressIndicator);
44 autopatcherServer.SetFileListTransferPlugin(&fileListTransfer);
45 #ifdef USE_TCP
46 PacketizedTCP packetizedTCP;
47 if (packetizedTCP.Start(LISTEN_PORT,MAX_INCOMING_CONNECTIONS)==false)
48 {
49 printf("Failed to start TCP. Is the port already in use?");
50 return 1;
51 }
52 packetizedTCP.AttachPlugin(&autopatcherServer);
53 packetizedTCP.AttachPlugin(&fileListTransfer);
54 #else
55 RakPeerInterface *rakPeer;
56 rakPeer = RakNetworkFactory::GetRakPeerInterface();
57 SocketDescriptor socketDescriptor(LISTEN_PORT,0);
58 rakPeer->Startup(8,0,&socketDescriptor, 1);
59 rakPeer->SetMaximumIncomingConnections(MAX_INCOMING_CONNECTIONS);
60 rakPeer->AttachPlugin(&autopatcherServer);
61 rakPeer->AttachPlugin(&fileListTransfer);
62 #endif
63 printf("started.\n");
64
65 printf("Enter database password:\n");
66 char password[128];
67 char username[256];
68 strcpy(username, "root");
69 gets(password);
70 if (password[0]==0)
71 strcpy(password,"aaaa");
72 char db[256];
73 printf("Enter DB schema: ");
74 // To create the schema, go to the command line client and type create schema autopatcher;
75 // You also have to add
76 // max_allowed_packet=128M
77 // Where 128 is the maximum size file in megabytes you'll ever add
78 // to MySQL\MySQL Server 5.1\my.ini in the [mysqld] section
79 // Be sure to restart the service after doing so
80 gets(db);
81 if (db[0]==0)
82 strcpy(db,"autopatcher");
83 for (int conIdx=0; conIdx < sqlConnectionObjectCount; conIdx++)
84 {
85 if (!connectionObject[conIdx].Connect("localhost", username, password, db, 0, NULL, 0))
86 {
87 printf("Database connection failed.\n");
88 return 1;
89 }
90 }
91 printf("Database connection suceeded.\n");
92
93
94 printf("Starting threads\n");
95 autopatcherServer.StartThreads(sqlConnectionObjectCount, connectionObjectAddresses);
96 printf("System ready for connections\n");
97
98 printf("(D)rop database\n(C)reate database.\n(A)dd application\n(U)pdate revision.\n(R)emove application\n(Q)uit\n");
99
100 char ch;
101 Packet *p;
102 while (1)
103 {
104 #ifdef USE_TCP
105 SystemAddress notificationAddress;
106 notificationAddress=packetizedTCP.HasCompletedConnectionAttempt();
107 if (notificationAddress!=UNASSIGNED_SYSTEM_ADDRESS)
108 printf("ID_CONNECTION_REQUEST_ACCEPTED\n");
109 notificationAddress=packetizedTCP.HasNewIncomingConnection();
110 if (notificationAddress!=UNASSIGNED_SYSTEM_ADDRESS)
111 printf("ID_NEW_INCOMING_CONNECTION\n");
112 notificationAddress=packetizedTCP.HasLostConnection();
113 if (notificationAddress!=UNASSIGNED_SYSTEM_ADDRESS)
114 printf("ID_CONNECTION_LOST\n");
115
116 p=packetizedTCP.Receive();
117 while (p)
118 {
119 packetizedTCP.DeallocatePacket(p);
120 p=packetizedTCP.Receive();
121 }
122 #else
123 p=rakPeer->Receive();
124 while (p)
125 {
126 if (p->data[0]==ID_NEW_INCOMING_CONNECTION)
127 printf("ID_NEW_INCOMING_CONNECTION\n");
128 else if (p->data[0]==ID_DISCONNECTION_NOTIFICATION)
129 printf("ID_DISCONNECTION_NOTIFICATION\n");
130 else if (p->data[0]==ID_CONNECTION_LOST)
131 printf("ID_CONNECTION_LOST\n");
132
133 rakPeer->DeallocatePacket(p);
134 p=rakPeer->Receive();
135 }
136 #endif
137 if (kbhit())
138 {
139 ch=getch();
140 if (ch=='q')
141 break;
142 else if (ch=='c')
143 {
144 if (connectionObject[0].CreateAutopatcherTables()==false)
145 printf("Error: %s\n", connectionObject[0].GetLastError());
146 else
147 printf("Created\n");
148 }
149 else if (ch=='d')
150 {
151 if (connectionObject[0].DestroyAutopatcherTables()==false)
152 printf("Error: %s\n", connectionObject[0].GetLastError());
153 else
154 printf("Destroyed\n");
155 }
156 else if (ch=='a')
157 {
158 printf("Enter application name to add: ");
159 char appName[512];
160 gets(appName);
161 if (appName[0]==0)
162 strcpy(appName, "TestApp");
163
164 if (connectionObject[0].AddApplication(appName, username)==false)
165 printf("Error: %s\n", connectionObject[0].GetLastError());
166 else
167 printf("Done\n");
168 }
169 else if (ch=='r')
170 {
171 printf("Enter application name to remove: ");
172 char appName[512];
173 gets(appName);
174 if (appName[0]==0)
175 strcpy(appName, "TestApp");
176
177 if (connectionObject[0].RemoveApplication(appName)==false)
178 printf("Error: %s\n", connectionObject[0].GetLastError());
179 else
180 printf("Done\n");
181 }
182 else if (ch=='u')
183 {
184 printf("Enter application name: ");
185 char appName[512];
186 gets(appName);
187 if (appName[0]==0)
188 strcpy(appName, "TestApp");
189
190 printf("Enter application directory: ");
191 char appDir[512];
192 gets(appDir);
193 if (appDir[0]==0)
194 strcpy(appDir, "C:/temp");
195
196 if (connectionObject[0].UpdateApplicationFiles(appName, appDir, username, &progressIndicator)==false)
197 {
198 printf("Error: %s\n", connectionObject[0].GetLastError());
199 }
200 else
201 {
202 printf("Update success.\n");
203 }
204 }
205 }
206
207 RakSleep(30);
208
209
210 }
211
212 #ifdef USE_TCP
213 packetizedTCP.Stop();
214 #else
215 RakNetworkFactory::DestroyRakPeerInterface(rakPeer);
216 #endif
217 }
218