main.cpp 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. // How to patch
  11. // 1. Create a new server instance using the most recent image (optional)
  12. // 2. Connect to the new (or any existing) instance through remote desktop. Immediately set max concurrent users to 0. Wait for users to finish patching, if any started.
  13. // 3. Update the patch
  14. // 4. Image the server
  15. // 5. Wait for the image to complete (press l to check, or use Rackspace control panel. Takes like half an hour).
  16. // 6. When the image completes, set the image spawn from the 'l' option to the image that was created in step 4
  17. // 6. Send terminate command. Servers running the old patch will shutdown automatically.
  18. // 9. Set max concurrent users back above 0.
  19. // 10. Optional: Manually call SpawnServers() if load is immediately anticipated
  20. // Common includes
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include "Kbhit.h"
  24. #include "GetTime.h"
  25. #include "RakPeerInterface.h"
  26. #include "MessageIdentifiers.h"
  27. #include "BitStream.h"
  28. #include "StringCompressor.h"
  29. #include "FileListTransfer.h"
  30. #include "FileList.h" // FLP_Printf
  31. #include "PacketizedTCP.h"
  32. #include "Gets.h"
  33. #include "CloudServerHelper.h"
  34. #include "FullyConnectedMesh2.h"
  35. #include "TwoWayAuthentication.h"
  36. #include "CloudClient.h"
  37. #include "DynDNS.h"
  38. #include "RakPeerInterface.h"
  39. #include "RakSleep.h"
  40. #include "ConnectionGraph2.h"
  41. #include "CloudServerHelper.h"
  42. #include "HTTPConnection2.h"
  43. #include "Rackspace2.h"
  44. #include "GetTime.h"
  45. // See http://www.digip.org/jansson/doc/2.4/
  46. // This is used to make it easier to parse the JSON returned from the master server
  47. #include "jansson.h"
  48. // Server only includes
  49. #include "AutopatcherServer.h"
  50. // Replace this repository with your own implementation if you don't want to use PostgreSQL
  51. #include "AutopatcherPostgreRepository.h"
  52. #ifdef _WIN32
  53. #include "WindowsIncludes.h" // Sleep
  54. #else
  55. #include <unistd.h> // usleep
  56. #endif
  57. #define LISTEN_PORT_TCP_PATCHER 60000
  58. using namespace RakNet;
  59. static const RakNet::Time LOAD_CHECK_INTERVAL=1000*60*5;
  60. static const RakNet::Time LOAD_CHECK_INTERVAL_AFTER_SPAWN=1000*60*60;
  61. char databasePassword[128];
  62. int workerThreadCount;
  63. int sqlConnectionObjectCount;
  64. int allowDownloadingUnmodifiedFiles;
  65. unsigned short autopatcherLoad=0;
  66. RakNet::RakPeerInterface *rakPeer;
  67. RakNet::AutopatcherServer *autopatcherServer;
  68. enum AppState
  69. {
  70. AP_RUNNING,
  71. AP_TERMINATE_WHEN_ZERO_USERS_REACHED,
  72. AP_TERMINATE_IF_NOT_DNS_HOST,
  73. AP_TERMINATING,
  74. AP_TERMINATED,
  75. } appState;
  76. RakNet::Time timeSinceZeroUsers;
  77. struct CloudServerHelper_RackspaceCloudDNS : public CloudServerHelper, public Rackspace2EventCallback
  78. {
  79. public:
  80. enum CSHState
  81. {
  82. AUTHENTICATING,
  83. AUTHENTICATED,
  84. CANNOT_FIND_PATCHER_DOMAIN,
  85. CANNOT_FIND_PATCHER_RECORD,
  86. GETTING_SERVERS,
  87. GOT_MY_SERVER,
  88. CANNOT_FIND_MY_SERVER,
  89. GETTING_DOMAINS,
  90. GOT_DOMAIN,
  91. GETTING_RECORDS,
  92. GOT_RECORDS,
  93. } cshState;
  94. CloudServerHelper_RackspaceCloudDNS() {
  95. rackspace2= RakNet::OP_NEW<Rackspace2>(_FILE_AND_LINE_);
  96. memset(&thisServerDetail, 0, sizeof(ServerDetail));
  97. }
  98. ~CloudServerHelper_RackspaceCloudDNS() {
  99. RakNet::OP_DELETE(rackspace2,_FILE_AND_LINE_);
  100. }
  101. virtual bool Update(void) {
  102. UpdateTCP();
  103. return true;
  104. }
  105. void CopyServerDetails(json_t *arrayElement, const char *publicIPV4)
  106. {
  107. strcpy(thisServerDetail.publicIPV4, publicIPV4);
  108. strcpy(thisServerDetail.id, json_string_value(json_object_get(arrayElement, "id")));
  109. json_t *privateAddressesArray = json_object_get(json_object_get(arrayElement, "addresses"),"private");
  110. size_t privateAddressesArraySize = json_array_size(privateAddressesArray);
  111. for (size_t l=0; l < privateAddressesArraySize; l++)
  112. {
  113. json_t *privateAddressElement = json_array_get(privateAddressesArray, l);
  114. if (json_integer_value(json_object_get(privateAddressElement, "version"))==4)
  115. {
  116. strcpy(thisServerDetail.privateIPV4,json_string_value(json_object_get(privateAddressElement, "addr")));
  117. break;
  118. }
  119. }
  120. strcpy(thisServerDetail.flavorId,json_string_value(json_object_get(json_object_get(arrayElement, "flavor"),"id")));
  121. strcpy(thisServerDetail.hostId,json_string_value(json_object_get(arrayElement, "hostId")));
  122. strcpy(thisServerDetail.id,json_string_value(json_object_get(arrayElement, "id")));
  123. strcpy(thisServerDetail.imageId,json_string_value(json_object_get(json_object_get(arrayElement, "image"),"id")));
  124. strcpy(thisServerDetail.name,json_string_value(json_object_get(arrayElement, "name")));
  125. strcpy(spawningImageId, thisServerDetail.imageId);
  126. strcpy(spawningFlavorId, thisServerDetail.flavorId);
  127. printf("Using image id %s for new servers\n", spawningImageId);
  128. }
  129. virtual void OnTCPFailure(void){ printf("--ERROR--: OnTCPFailure()\n"); }
  130. virtual void OnTransmissionFailed(HTTPConnection2 *httpConnection2, RakString postStr, RakString authURLDomain)
  131. {
  132. printf("--ERROR--: OnTransmissionFailed().\npostStr=%s\nauthURLDomain=%s\n", postStr.C_String(), authURLDomain.C_String());
  133. RakSleep(30000);
  134. printf("Retrying...\n");
  135. httpConnection2->TransmitRequest(postStr,authURLDomain, 443, true);
  136. }
  137. virtual void OnEmptyResponse(RakString stringTransmitted) {
  138. printf("--ERROR--: OnEmptyResponse(). stringTransmitted=%s", stringTransmitted.C_String());
  139. }
  140. virtual void OnMessage(const char *message, RakString responseReceived, RakString stringTransmitted, int contentOffset)
  141. {
  142. printf("--WARNING--: OnMessage(). message=%s\nstringTransmitted=%s", message, stringTransmitted.C_String());
  143. }
  144. virtual void OnResponse(Rackspace2ResponseCode r2rc, RakString responseReceived, int contentOffset){
  145. if (r2rc==R2RC_AUTHENTICATED)
  146. {
  147. printf("Authenticated with Rackspace\nX-Auth-Token: %s\n", rackspace2->GetAuthToken());
  148. cshState=AUTHENTICATED;
  149. json_error_t error;
  150. json_t *root = json_loads(strstr(responseReceived.C_String() + contentOffset, "{"), JSON_REJECT_DUPLICATES | JSON_DISABLE_EOF_CHECK, &error);
  151. json_t *accessJson = json_object_get(root, "access");
  152. json_t *userJson = json_object_get(accessJson, "user");
  153. json_t *regionJson = json_object_get(userJson, "RAX-AUTH:defaultRegion");
  154. const char *userRegionStr = json_string_value(regionJson);
  155. json_t *serviceCatalogArray = json_object_get(accessJson, "serviceCatalog");
  156. size_t arraySize = json_array_size(serviceCatalogArray);
  157. size_t i;
  158. printf("Services:\n");
  159. for (i=0; i < arraySize; i++)
  160. {
  161. json_t *arrayElement = json_array_get(serviceCatalogArray, i);
  162. json_t *elementNameJson = json_object_get(arrayElement, "name");
  163. const char *elementNameStr = json_string_value(elementNameJson);
  164. json_t *elementTypeJson = json_object_get(arrayElement, "type");
  165. const char *elementTypeStr = json_string_value(elementTypeJson);
  166. printf("%i. name=%s type=%s ", i+1, elementNameStr, elementTypeStr);
  167. json_t *endpointArray = json_object_get(arrayElement, "endpoints");
  168. size_t endpointArraySize = json_array_size(endpointArray);
  169. size_t j;
  170. char *myPublicURLStr=0;
  171. for (j=0; j < endpointArraySize; j++)
  172. {
  173. json_t *endpointArrayElement = json_array_get(endpointArray, j);
  174. json_t *publicURLJson = json_object_get(endpointArrayElement, "publicURL");
  175. const char *publicURLStr = json_string_value(publicURLJson);
  176. json_t *regionJson = json_object_get(endpointArrayElement, "region");
  177. if (regionJson)
  178. {
  179. const char *regionStr = json_string_value(regionJson);
  180. if (strcmp(regionStr, userRegionStr)==0)
  181. {
  182. printf("My URL=%s", publicURLStr);
  183. myPublicURLStr=(char*) publicURLStr;
  184. break;
  185. }
  186. }
  187. else
  188. {
  189. printf("My URL=%s", publicURLStr);
  190. myPublicURLStr=(char*) publicURLStr;
  191. break;
  192. }
  193. }
  194. if (j==endpointArraySize)
  195. printf("My URL=<unknown>");
  196. if (myPublicURLStr)
  197. {
  198. if (strcmp(elementNameStr, "cloudDNS")==0)
  199. strcpy(rackspaceDNSURL, myPublicURLStr);
  200. else if (strcmp(elementNameStr, "cloudServersOpenStack")==0)
  201. strcpy(rackspaceServersURL, myPublicURLStr);
  202. }
  203. printf("\n");
  204. }
  205. if (appState==AP_TERMINATING || appState==AP_TERMINATED ||
  206. appState==AP_TERMINATE_WHEN_ZERO_USERS_REACHED || appState==AP_TERMINATE_IF_NOT_DNS_HOST)
  207. Terminate();
  208. json_decref(root);
  209. }
  210. else if (r2rc==R2RC_GOT_SERVERS)
  211. {
  212. json_error_t error;
  213. json_t *root = json_loads(strstr(responseReceived.C_String() + contentOffset, "{"), JSON_REJECT_DUPLICATES | JSON_DISABLE_EOF_CHECK, &error);
  214. json_t *serversArray = json_object_get(root, "servers");
  215. size_t arraySize = json_array_size(serversArray);
  216. size_t i;
  217. for (i=0; i < arraySize && cshState==GETTING_SERVERS; i++)
  218. {
  219. json_t *arrayElement = json_array_get(serversArray, i);
  220. //json_t *publicAddressesArray = json_object_get(json_object_get(arrayElement, "addresses"),"public");
  221. //size_t publicAddressesArraySize = json_array_size(publicAddressesArray);
  222. //for (size_t j=0; j < publicAddressesArraySize; j++)
  223. //{
  224. for (int k=0; k < rakPeer->GetNumberOfAddresses(); k++)
  225. {
  226. if (strcmp(rakPeer->GetLocalIP(k), json_string_value(json_object_get(arrayElement, "accessIPv4")))==0)
  227. {
  228. CopyServerDetails(arrayElement, rakPeer->GetLocalIP(k));
  229. cshState=GOT_MY_SERVER;
  230. break;
  231. }
  232. }
  233. //}
  234. }
  235. if (cshState==GETTING_SERVERS)
  236. {
  237. // printf("Could not find a server with this IP address\n");
  238. // printf("Continue anyway (y/n)?\n");
  239. // if (getch()=='y')
  240. // {
  241. for (int k=0; k < rakPeer->GetNumberOfAddresses(); k++)
  242. {
  243. SystemAddress sa = rakPeer->GetInternalID(UNASSIGNED_SYSTEM_ADDRESS,k);
  244. if (sa.IsLANAddress()==false)
  245. {
  246. json_t *arrayElement = json_array_get(serversArray, 0);
  247. CopyServerDetails(arrayElement, rakPeer->GetLocalIP(0));
  248. cshState=GOT_MY_SERVER;;
  249. break;
  250. }
  251. }
  252. // }
  253. // else
  254. // cshState=CANNOT_FIND_MY_SERVER;
  255. }
  256. json_decref(root);
  257. }
  258. else if (r2rc==R2RC_GOT_IMAGES)
  259. {
  260. json_error_t error;
  261. json_t *root = json_loads(strstr(responseReceived.C_String() + contentOffset, "{"), JSON_REJECT_DUPLICATES | JSON_DISABLE_EOF_CHECK, &error);
  262. json_t *domainsArray = json_object_get(root, "images");
  263. size_t arraySize = json_array_size(domainsArray);
  264. size_t i;
  265. printf("Got %i images:\n", arraySize);
  266. for (i=0; i < arraySize; i++)
  267. {
  268. json_t *arrayElement = json_array_get(domainsArray, i);
  269. printf("%i. name=%s ", i+1, json_string_value(json_object_get(arrayElement, "name")));
  270. printf("progress=%i ", json_integer_value(json_object_get(arrayElement, "progress")));
  271. printf("status=%s ", json_string_value(json_object_get(arrayElement, "status")));
  272. printf("created=%s ", json_string_value(json_object_get(arrayElement, "created")));
  273. printf("id=%s\n", json_string_value(json_object_get(arrayElement, "id")));
  274. }
  275. printf("\n");
  276. if (arraySize>0)
  277. {
  278. printf("Select image to spawn for new servers?: ");
  279. char str[32];
  280. Gets(str, sizeof(str));
  281. if (str[0])
  282. {
  283. int idx = atoi(str);
  284. if (idx>=1 && idx <= arraySize)
  285. {
  286. json_t *arrayElement = json_array_get(domainsArray, idx-1);
  287. strcpy(spawningImageId, json_string_value(json_object_get(arrayElement, "id")));
  288. // Use spawningFlavorId of whatever it was when the server was started, I don't care about flavor
  289. printf("Using image id %s for new servers\n", spawningImageId);
  290. }
  291. }
  292. else
  293. printf("Aborted.\n");
  294. }
  295. json_decref(root);
  296. }
  297. else if (r2rc==R2RC_GOT_RECORDS)
  298. {
  299. json_error_t error;
  300. json_t *root = json_loads(strstr(responseReceived.C_String() + contentOffset, "{"), JSON_REJECT_DUPLICATES | JSON_DISABLE_EOF_CHECK, &error);
  301. json_t *domainsArray = json_object_get(root, "records");
  302. size_t arraySize = json_array_size(domainsArray);
  303. size_t i;
  304. for (i=0; i < arraySize; i++)
  305. {
  306. json_t *arrayElement = json_array_get(domainsArray, i);
  307. json_t *elementNameJson = json_object_get(arrayElement, "name");
  308. const char *elementNameStr = json_string_value(elementNameJson);
  309. if (strcmp(elementNameStr, patcherHostSubdomainURL)==0)
  310. {
  311. json_t *arrayElement = json_array_get(domainsArray, i);
  312. json_t *elementIPJson = json_object_get(arrayElement, "data");
  313. strcpy(dnsHostIP, json_string_value(elementIPJson));
  314. json_t *elementIDJson = json_object_get(arrayElement, "id");
  315. strcpy(patchHostRecordID,json_string_value(elementIDJson));
  316. timeOfLastDNSHostCheck=RakNet::GetTime();
  317. if (appState==AP_TERMINATE_IF_NOT_DNS_HOST)
  318. {
  319. if (strcmp(dnsHostIP, rakPeer->GetLocalIP(0))==0)
  320. {
  321. // We are dns host. Just keep running
  322. appState=AP_RUNNING;
  323. }
  324. else
  325. {
  326. if (timeSinceZeroUsers!=0)
  327. Terminate();
  328. else
  329. appState=AP_TERMINATE_WHEN_ZERO_USERS_REACHED;
  330. }
  331. }
  332. cshState=GOT_RECORDS;
  333. break;
  334. }
  335. }
  336. if (i==arraySize)
  337. {
  338. printf("Can't find %s in record list\n", patcherHostSubdomainURL);
  339. cshState=CANNOT_FIND_PATCHER_RECORD;
  340. }
  341. json_decref(root);
  342. }
  343. else if (r2rc==R2RC_GOT_DOMAINS)
  344. {
  345. json_error_t error;
  346. json_t *root = json_loads(strstr(responseReceived.C_String() + contentOffset, "{"), JSON_REJECT_DUPLICATES | JSON_DISABLE_EOF_CHECK, &error);
  347. json_t *domainsArray = json_object_get(root, "domains");
  348. size_t arraySize = json_array_size(domainsArray);
  349. size_t i;
  350. for (i=0; i < arraySize; i++)
  351. {
  352. json_t *arrayElement = json_array_get(domainsArray, i);
  353. json_t *elementNameJson = json_object_get(arrayElement, "name");
  354. const char *elementNameStr = json_string_value(elementNameJson);
  355. if (strcmp(elementNameStr, "raknetpatcher.com")==0)
  356. {
  357. // Primary domain
  358. json_t *idNameJson = json_object_get(arrayElement, "id");
  359. raknetPatcherDomainId = (int) json_integer_value(idNameJson);
  360. RakString url("%s/domains/%i/records", rackspaceDNSURL, raknetPatcherDomainId );
  361. rackspace2->AddOperation(url,Rackspace2::OT_GET,0, true);
  362. cshState=GETTING_RECORDS;
  363. break;
  364. }
  365. }
  366. json_decref(root);
  367. if (i==arraySize)
  368. {
  369. printf("Can't find raknetpatcher.com in domain list\n");
  370. cshState=CANNOT_FIND_PATCHER_DOMAIN;
  371. }
  372. }
  373. else
  374. {
  375. if (r2rc==R2RC_UNAUTHORIZED)
  376. {
  377. printf("Unauthorized\n");
  378. // Try to reauthenticate
  379. rackspace2->Authenticate(rackspaceAuthenticationURL, rackspaceCloudUsername, apiAccessKey);
  380. }
  381. else
  382. {
  383. printf("General response failure: %s\n", responseReceived.C_String());
  384. }
  385. }
  386. }
  387. virtual bool UpdateTCP() {
  388. rackspace2->Update();
  389. return false;
  390. }
  391. void Terminate(void)
  392. {
  393. appState=AP_TERMINATING;
  394. // http://docs.rackspace.com/servers/api/v2/cs-devguide/content/Delete_Server-d1e2883.html
  395. RakString url("%s/servers/%s", rackspaceServersURL, thisServerDetail.id);
  396. rackspace2->AddOperation(url,Rackspace2::OT_DELETE,0, true);
  397. }
  398. void GetCustomImages(void)
  399. {
  400. // curl -k https://ord.servers.api.rackspacecloud.com/v2/570016/images?type=SNAPSHOT --header "X-Auth-Token: 97d8335b-7b9e-4a74-96a1-2a3a043c1000" --header "Content-Type: application/json"
  401. // List images
  402. // http://docs.rackspace.com/servers/api/v2/cs-devguide/content/List_Images-d1e4435.html
  403. RakString url("%s/images/detail?type=SNAPSHOT", rackspaceServersURL);
  404. rackspace2->AddOperation(url,Rackspace2::OT_GET,0, true);
  405. }
  406. void ImageThisServer(void)
  407. {
  408. // Create image
  409. // date
  410. // http://docs.rackspace.com/servers/api/v2/cs-devguide/content/Create_Image-d1e4655.html
  411. time_t aclock;
  412. time( &aclock ); // Get time in seconds
  413. tm *newtime = localtime( &aclock ); // Convert time to struct tm form
  414. char text[1024];
  415. sprintf(text, "%s_%s", patcherHostSubdomainURL, asctime( newtime ));
  416. RakString url("%s/servers/%s/action", rackspaceServersURL, thisServerDetail.id);
  417. json_t *jsonObjectRoot = json_object();
  418. json_t *jsonObjectCreateImage = json_object();
  419. json_t *jsonMetaData = json_object();
  420. json_object_set(jsonObjectCreateImage, "name", json_string(text));
  421. json_object_set(jsonObjectRoot, "createImage", jsonObjectCreateImage);
  422. json_object_set(jsonObjectCreateImage, "metadata", jsonMetaData);
  423. json_object_set(jsonMetaData, "ImageType", json_string("snapshot"));
  424. printf("Imaging server with name %s\n", text);
  425. rackspace2->AddOperation(url,Rackspace2::OT_POST,jsonObjectRoot, true);
  426. }
  427. void SpawnServers(int count)
  428. {
  429. if (count==0)
  430. return;
  431. RakAssert(count <= 4);
  432. // http://docs.rackspace.com/servers/api/v2/cs-devguide/content/CreateServers.html
  433. RakString url("%s/servers", rackspaceServersURL);
  434. time_t aclock;
  435. time( &aclock ); // Get time in seconds
  436. tm *newtime = localtime( &aclock ); // Convert time to struct tm form
  437. char text[1024];
  438. for (unsigned int i=0; i < count; i++)
  439. {
  440. json_t *jsonObjectRoot = json_object();
  441. json_t *jsonObjectServer = json_object();
  442. json_object_set(jsonObjectRoot, "server", jsonObjectServer);
  443. sprintf(text, "%s_%s_%i", patcherHostSubdomainURL, asctime( newtime ), i);
  444. json_object_set(jsonObjectServer, "name", json_string(text));
  445. json_object_set(jsonObjectServer, "imageRef", json_string(spawningImageId));
  446. json_object_set(jsonObjectServer, "flavorRef", json_string(spawningFlavorId));
  447. rackspace2->AddOperation(url,Rackspace2::OT_POST,jsonObjectRoot, true);
  448. }
  449. }
  450. void GetThisServer(void)
  451. {
  452. // http://docs.rackspace.com/servers/api/v2/cs-devguide/content/List_Servers-d1e2078.html
  453. cshState=GETTING_SERVERS;
  454. RakString url("%s/servers/detail?status=ACTIVE", rackspaceServersURL);
  455. //RakString url("%s/servers/detail", rackspaceServersURL);
  456. rackspace2->AddOperation(url,Rackspace2::OT_GET,0, true);
  457. }
  458. void GetDomainRecords(void) {
  459. cshState=GETTING_DOMAINS;
  460. RakString url("%s/domains/?name=%s", rackspaceDNSURL, "raknetpatcher.com");
  461. rackspace2->AddOperation(url,Rackspace2::OT_GET,0, true);
  462. }
  463. bool AuthenticateWithRackspaceBlocking(void) {
  464. rackspace2->SetEventCallback(this);
  465. rackspace2->Authenticate(rackspaceAuthenticationURL, rackspaceCloudUsername, apiAccessKey);
  466. cshState=AUTHENTICATING;
  467. while (cshState==AUTHENTICATING)
  468. {
  469. UpdateTCP();
  470. RakSleep(30);
  471. }
  472. if (cshState!=AUTHENTICATED)
  473. return false;
  474. GetThisServer();
  475. while (cshState==GETTING_SERVERS)
  476. {
  477. UpdateTCP();
  478. RakSleep(30);
  479. }
  480. if (cshState!=GOT_MY_SERVER)
  481. {
  482. return false;
  483. }
  484. // http://docs.rackspace.com/cdns/api/v1.0/cdns-devguide/content/list_domains.html
  485. GetDomainRecords();
  486. while (cshState==GETTING_DOMAINS || cshState==GETTING_RECORDS)
  487. {
  488. UpdateTCP();
  489. RakSleep(30);
  490. }
  491. if (cshState!=GOT_RECORDS)
  492. return false;
  493. return true;
  494. }
  495. void UpdateHostIPAsynch(SystemAddress sa) {
  496. if (appState!=AP_RUNNING)
  497. return;
  498. // RakString url("%s/domains/domainId/records/recordId");
  499. // rackspace2->AddOperation()
  500. // http://docs.rackspace.com/cdns/api/v1.0/cdns-devguide/content/Modify_Records-d1e5033.html
  501. //rackspace2;
  502. RakString url("%s/domains/%i/records/%s", rackspaceDNSURL, raknetPatcherDomainId, patchHostRecordID);
  503. json_t *jsonObject = json_object();
  504. json_object_set(jsonObject, "data", json_string(sa.ToString(false)));
  505. printf("Setting DNS to %s\n", sa.ToString(false));
  506. // TESTING HACK
  507. // json_object_set(jsonObject, "data", json_string("198.61.202.20"));
  508. json_object_set(jsonObject, "name", json_string(patcherHostSubdomainURL));
  509. rackspace2->AddOperation(url,Rackspace2::OT_PUT,jsonObject, true);
  510. }
  511. virtual void PrintHelp(void)
  512. {
  513. printf("Distributed authenticated CloudServer using DNS based host migration.\n");
  514. printf("Parameter1: serverToServerPassword\n");
  515. printf("Parameter2: serverPort\n");
  516. printf("Parameter3: allowedIncomingConnections\n");
  517. printf("Parameter4: allowedOutgoingConnections\n");
  518. printf("Parameter5: patcherHostRecordURL\n");
  519. printf("Parameter6: patcherHostDomainURL\n");
  520. printf("Parameter7: rackspaceAuthenticationURL\n");
  521. printf("Parameter8: rackspaceCloudUsername\n");
  522. printf("Parameter9: apiAccessKey\n");
  523. printf("Parameter10: databasePassword\n");
  524. printf("Parameter11: workerThreadCount\n");
  525. printf("Parameter12: sqlConnectionObjectCount\n");
  526. printf("Parameter13: allowDownloadingUnmodifiedFiles\n");
  527. printf("Example:\n");
  528. printf("AutopatcherServer_SelfScaling s2sPassword 60000 1024 128 game1.mycompanypatcher.com mycompanypatcher.com https://identity.api.rackspacecloud.com/v2.0 rackspaceUsername rackspacePw dbPassword 3 6 1");
  529. }
  530. virtual bool ParseCommandLineParameters(int argc, char **argv) {
  531. if (argc!=14)
  532. PrintHelp();
  533. serverToServerPassword=argv[1];
  534. rakPeerPort=atoi(argv[2]);
  535. allowedIncomingConnections=atoi(argv[3]);
  536. allowedOutgoingConnections=atoi(argv[4]);
  537. strcpy(patcherHostSubdomainURL, argv[5]);
  538. strcpy(patcherHostDomainURL, argv[6]);
  539. strcpy(rackspaceAuthenticationURL, argv[7]);
  540. strcpy(rackspaceCloudUsername, argv[8]);
  541. strcpy(apiAccessKey, argv[9]);
  542. strcpy(databasePassword, argv[10]);
  543. workerThreadCount=atoi(argv[11]);
  544. sqlConnectionObjectCount=atoi(argv[12]);
  545. allowDownloadingUnmodifiedFiles=atoi(argv[13]);
  546. /*
  547. 2 worker threads, 4 mb read: 5 minutes, 55 seconds
  548. 4 worker threads, 4 mb read: 5 minutes, 7 seconds
  549. 8 worker threads, 4 mb read: 4 minutes, 48 seconds
  550. 2 worker threads, 2 mb read: 8 minutes, 31 seconds
  551. 8 worker threads, 2 mb read: 4 minutes, 30 seconds, but one failed to start
  552. 4 worker threads, 8 mb read: 4 minutes, 55 seconds
  553. 4 worker threads, 16 mb read: 4 minutes, 48 seconds
  554. 3 worker threads, 16 mb read: 4 minutes, 19 seconds
  555. 2 worker threads, 16 mb read: 5 minutes, 18 seconds
  556. 3 worker threads, 32 mb read: 4 minutes, 18 seconds
  557. */
  558. return true;
  559. }
  560. // Call when you get ID_FCM2_NEW_HOST
  561. virtual void OnFCMNewHost(Packet *packet, RakPeerInterface *rakPeer) {
  562. RakAssert(packet->data[0]==ID_FCM2_NEW_HOST);
  563. RakNet::BitStream bsIn(packet->data, packet->length, false);
  564. bsIn.IgnoreBytes(sizeof(MessageID));
  565. RakNetGUID oldHost;
  566. bsIn.Read(oldHost);
  567. RakNetGUID newHost = packet->guid;
  568. if (newHost==rakPeer->GetMyGUID() && oldHost!=newHost)
  569. {
  570. for (unsigned int i=0; i < rakPeer->GetNumberOfAddresses(); i++)
  571. {
  572. SystemAddress sa = rakPeer->GetInternalID(UNASSIGNED_SYSTEM_ADDRESS,i);
  573. if (sa.IsLANAddress()==false)
  574. {
  575. printf("Assuming host. Updating DNS\n");
  576. UpdateHostIPAsynch(sa);
  577. break;
  578. }
  579. }
  580. }
  581. }
  582. const char *GetCloudHostAddress(void) const {return dnsHostIP;}
  583. RakNet::Time GetTimeOfLastDNSHostCheck(void) {return timeOfLastDNSHostCheck;}
  584. void SetTimeOfLastDNSHostCheck(RakNet::Time t) {timeOfLastDNSHostCheck=t;}
  585. virtual int OnJoinCloudResult(
  586. Packet *packet,
  587. RakNet::RakPeerInterface *rakPeer,
  588. RakNet::CloudServer *cloudServer,
  589. RakNet::CloudClient *cloudClient,
  590. RakNet::FullyConnectedMesh2 *fullyConnectedMesh2,
  591. RakNet::TwoWayAuthentication *twoWayAuthentication,
  592. RakNet::ConnectionGraph2 *connectionGraph2,
  593. const char *rakPeerIpOrDomain,
  594. char myPublicIP[32]
  595. ) {
  596. if (packet->data[0]==ID_CONNECTION_ATTEMPT_FAILED)
  597. {
  598. for (unsigned int i=0; i < rakPeer->GetNumberOfAddresses(); i++)
  599. {
  600. SystemAddress sa = rakPeer->GetInternalID(UNASSIGNED_SYSTEM_ADDRESS,i);
  601. if (sa.IsLANAddress()==false)
  602. {
  603. printf("Failed connection. Changing DNS to point to this system.\n");
  604. UpdateHostIPAsynch(sa);
  605. break;
  606. }
  607. }
  608. }
  609. else if (packet->data[0]==ID_ALREADY_CONNECTED && packet->systemAddress.IsLANAddress())
  610. {
  611. for (unsigned int i=0; i < rakPeer->GetNumberOfAddresses(); i++)
  612. {
  613. SystemAddress sa = rakPeer->GetInternalID(UNASSIGNED_SYSTEM_ADDRESS,i);
  614. if (sa.IsLANAddress()==false)
  615. {
  616. printf("Connected to self-LAN address. Changing DNS to point to this system.\n");
  617. UpdateHostIPAsynch(sa);
  618. return CloudServerHelper::OnJoinCloudResult(packet, rakPeer, cloudServer, cloudClient, fullyConnectedMesh2, twoWayAuthentication, connectionGraph2, sa.ToString(false), myPublicIP);
  619. }
  620. }
  621. }
  622. return CloudServerHelper::OnJoinCloudResult(packet, rakPeer, cloudServer, cloudClient, fullyConnectedMesh2, twoWayAuthentication, connectionGraph2, rakPeerIpOrDomain, myPublicIP);
  623. }
  624. virtual void OnConnectionCountChange(RakPeerInterface *rakPeer, CloudClient *cloudClient)
  625. {
  626. RakNet::BitStream bs;
  627. CloudKey cloudKey("CloudConnCount",0);
  628. bs.Write(autopatcherLoad);
  629. cloudClient->Post(&cloudKey, bs.GetData(), bs.GetNumberOfBytesUsed(), rakPeer->GetMyGUID());
  630. }
  631. char patcherHostSubdomainURL[128];
  632. char patcherHostDomainURL[128];
  633. char dnsHostIP[128];
  634. char rackspaceAuthenticationURL[128];
  635. char rackspaceDNSURL[128];
  636. char rackspaceServersURL[128];
  637. char rackspaceCloudUsername[128];
  638. char apiAccessKey[128];
  639. Rackspace2 *rackspace2;
  640. char patchHostRecordID[128];
  641. protected:
  642. int raknetPatcherDomainId;
  643. RakNet::Time timeOfLastDNSHostCheck;
  644. struct ServerDetail
  645. {
  646. char publicIPV4[32];
  647. char privateIPV4[32];
  648. char flavorId[128];
  649. char hostId[128];
  650. char id[128];
  651. char imageId[128];
  652. char name[128];
  653. } thisServerDetail;
  654. char spawningImageId[128];
  655. char spawningFlavorId[128];
  656. } *cloudServerHelper;
  657. class AutopatcherLoadNotifier : public RakNet::AutopatcherServerLoadNotifier
  658. {
  659. void AutopatcherLoadNotifier::OnQueueUpdate(SystemAddress remoteSystem,
  660. AutopatcherServerLoadNotifier::RequestType requestType,
  661. AutopatcherServerLoadNotifier::QueueOperation queueOperation,
  662. AutopatcherServerLoadNotifier::AutopatcherState *autopatcherState)
  663. {
  664. autopatcherLoad=autopatcherState->requestsQueued+autopatcherState->requestsWorking;
  665. //AutopatcherServerLoadNotifier_Printf::OnQueueUpdate(remoteSystem, requestType, queueOperation, autopatcherState);
  666. }
  667. void AutopatcherLoadNotifier::OnGetChangelistCompleted(
  668. SystemAddress remoteSystem,
  669. AutopatcherServerLoadNotifier::GetChangelistResult getChangelistResult,
  670. AutopatcherServerLoadNotifier::AutopatcherState *autopatcherState)
  671. {
  672. autopatcherLoad=autopatcherState->requestsQueued+autopatcherState->requestsWorking;
  673. //AutopatcherServerLoadNotifier_Printf::OnGetChangelistCompleted(remoteSystem, getChangelistResult, autopatcherState);
  674. }
  675. void AutopatcherLoadNotifier::OnGetPatchCompleted(SystemAddress remoteSystem,
  676. AutopatcherServerLoadNotifier::PatchResult patchResult,
  677. AutopatcherServerLoadNotifier::AutopatcherState *autopatcherState)
  678. {
  679. autopatcherLoad=autopatcherState->requestsQueued+autopatcherState->requestsWorking;
  680. //AutopatcherServerLoadNotifier_Printf::OnGetPatchCompleted(remoteSystem, patchResult, autopatcherState);
  681. }
  682. };
  683. void SetMaxConcurrentUsers(int i)
  684. {
  685. autopatcherServer->SetMaxConurrentUsers(i);
  686. }
  687. //char WORKING_DIRECTORY[MAX_PATH];
  688. //char PATH_TO_XDELTA_EXE[MAX_PATH];
  689. // The default AutopatcherPostgreRepository2 uses bsdiff which takes too much memory for large files.
  690. // I override MakePatch to use XDelta in this case
  691. class AutopatcherPostgreRepository2_WithXDelta : public RakNet::AutopatcherPostgreRepository2
  692. {
  693. bool MakePatch(const char *oldFile, const char *newFile, char **patch, unsigned int *patchLength, int *patchAlgorithm)
  694. {
  695. FILE *fpOld = fopen(oldFile, "rb");
  696. fseek(fpOld, 0, SEEK_END);
  697. int contentLengthOld = ftell(fpOld);
  698. FILE *fpNew = fopen(newFile, "rb");
  699. fseek(fpNew, 0, SEEK_END);
  700. int contentLengthNew = ftell(fpNew);
  701. char WORKING_DIRECTORY[MAX_PATH];
  702. GetTempPath(MAX_PATH, WORKING_DIRECTORY);
  703. if (WORKING_DIRECTORY[strlen(WORKING_DIRECTORY)-1]=='\\' || WORKING_DIRECTORY[strlen(WORKING_DIRECTORY)-1]=='/')
  704. WORKING_DIRECTORY[strlen(WORKING_DIRECTORY)-1]=0;
  705. const char *PATH_TO_XDELTA_EXE = "c:/xdelta3-3.0.6-win32.exe";
  706. bool shortContent = contentLengthOld < 33554432 && contentLengthNew < 33554432;
  707. if (shortContent || PATH_TO_XDELTA_EXE[0]==0)
  708. {
  709. if (shortContent==false)
  710. {
  711. printf("Warning: PATH_TO_XDELTA_EXE needed but not set.\ncontentLengthOld=%i\ncontentLengthNew=%i\n", contentLengthOld, contentLengthNew );
  712. }
  713. // Use bsdiff, which does a good job but takes a lot of memory based on the size of the file
  714. *patchAlgorithm=0;
  715. bool b = MakePatchBSDiff(fpOld, contentLengthOld, fpNew, contentLengthNew, patch, patchLength);
  716. fclose(fpOld);
  717. fclose(fpNew);
  718. return b;
  719. }
  720. else
  721. {
  722. *patchAlgorithm=1;
  723. fclose(fpOld);
  724. fclose(fpNew);
  725. char buff[128];
  726. RakNet::TimeUS time = RakNet::GetTimeUS();
  727. #if defined(_WIN32)
  728. sprintf(buff, "%I64u", time);
  729. #else
  730. sprintf(buff, "%llu", (long long unsigned int) time);
  731. #endif
  732. // Invoke xdelta
  733. // See https://code.google.com/p/xdelta/wiki/CommandLineSyntax
  734. char commandLine[512];
  735. _snprintf(commandLine, sizeof(commandLine)-1, "-f -s %s %s patchServer_%s.tmp", oldFile, newFile, buff);
  736. commandLine[511]=0;
  737. SHELLEXECUTEINFO shellExecuteInfo;
  738. shellExecuteInfo.cbSize = sizeof(SHELLEXECUTEINFO);
  739. shellExecuteInfo.fMask = SEE_MASK_NOASYNC | SEE_MASK_NO_CONSOLE;
  740. shellExecuteInfo.hwnd = NULL;
  741. shellExecuteInfo.lpVerb = "open";
  742. shellExecuteInfo.lpFile = PATH_TO_XDELTA_EXE;
  743. shellExecuteInfo.lpParameters = commandLine;
  744. shellExecuteInfo.lpDirectory = WORKING_DIRECTORY;
  745. shellExecuteInfo.nShow = SW_SHOWNORMAL;
  746. shellExecuteInfo.hInstApp = NULL;
  747. ShellExecuteEx(&shellExecuteInfo);
  748. // ShellExecute is blocking, but if it writes a file to disk that file is not always immediately accessible after it returns. And this only happens in release, and only when not running in the debugger
  749. //ShellExecute(NULL, "open", PATH_TO_XDELTA_EXE, commandLine, WORKING_DIRECTORY, SW_SHOWNORMAL);
  750. char pathToPatch[MAX_PATH];
  751. sprintf(pathToPatch, "%s/patchServer_%s.tmp", WORKING_DIRECTORY, buff);
  752. FILE *fpPatch = fopen(pathToPatch, "r+b");
  753. RakNet::TimeUS stopWaiting = time + 60000000 * 5;
  754. while (fpPatch==0 && RakNet::GetTimeUS() < stopWaiting)
  755. {
  756. RakSleep(1000);
  757. fpPatch = fopen(pathToPatch, "r+b");
  758. }
  759. if (fpPatch==0)
  760. {
  761. printf("\nERROR: Could not open %s.\nerr=%i (%s)\narguments=%s\n", pathToPatch, errno, strerror(errno), commandLine);
  762. return false;
  763. }
  764. fseek(fpPatch, 0, SEEK_END);
  765. *patchLength = ftell(fpPatch);
  766. fseek(fpPatch, 0, SEEK_SET);
  767. *patch = (char*) rakMalloc_Ex(*patchLength, _FILE_AND_LINE_);
  768. fread(*patch, 1, *patchLength, fpPatch);
  769. fclose(fpPatch);
  770. int unlinkRes = _unlink(pathToPatch);
  771. while (unlinkRes!=0 && RakNet::GetTimeUS() < stopWaiting)
  772. {
  773. RakSleep(1000);
  774. unlinkRes = _unlink(pathToPatch);
  775. }
  776. if (unlinkRes!=0)
  777. printf("\nWARNING: unlink %s failed.\nerr=%i (%s)\n", pathToPatch, errno, strerror(errno));
  778. return true;
  779. }
  780. }
  781. };
  782. int main(int argc, char **argv)
  783. {
  784. #if OPEN_SSL_CLIENT_SUPPORT==0
  785. #error RakNet must be compiled with OPEN_SSL_CLIENT_SUPPORT=1 to connect to Rackspace
  786. return 0;
  787. #endif
  788. cloudServerHelper = RakNet::OP_NEW<CloudServerHelper_RackspaceCloudDNS>(_FILE_AND_LINE_);
  789. if (!cloudServerHelper->ParseCommandLineParameters(argc, argv))
  790. {
  791. return 1;
  792. }
  793. rakPeer = RakNet::RakPeerInterface::GetInstance();
  794. RakNet::Time timeForNextLoadCheck=RakNet::GetTime()+LOAD_CHECK_INTERVAL;
  795. if (!cloudServerHelper->AuthenticateWithRackspaceBlocking())
  796. {
  797. printf("Exiting due to failed startup with Rackspace\n");
  798. return 1;
  799. }
  800. // This does not work unless it comes after AuthenticateWithRackspaceBlocking
  801. RakNet::PacketizedTCP packetizedTCP;
  802. if (packetizedTCP.Start(LISTEN_PORT_TCP_PATCHER,cloudServerHelper->allowedIncomingConnections)==false)
  803. {
  804. printf("Failed to start TCP. Is the port already in use?");
  805. return 1;
  806. }
  807. appState=AP_RUNNING;
  808. timeSinceZeroUsers=RakNet::GetTime();
  809. printf("Server starting... ");
  810. autopatcherServer = RakNet::OP_NEW<AutopatcherServer>(_FILE_AND_LINE_);
  811. // RakNet::FLP_Printf progressIndicator;
  812. RakNet::FileListTransfer fileListTransfer;
  813. AutopatcherPostgreRepository2_WithXDelta *connectionObject;
  814. connectionObject = RakNet::OP_NEW_ARRAY<AutopatcherPostgreRepository2_WithXDelta>(sqlConnectionObjectCount, _FILE_AND_LINE_);
  815. // RakNet::AutopatcherRepositoryInterface **connectionObjectAddresses[sqlConnectionObjectCount];
  816. AutopatcherRepositoryInterface **connectionObjectAddresses = RakNet::OP_NEW_ARRAY<AutopatcherRepositoryInterface *>(sqlConnectionObjectCount, _FILE_AND_LINE_);
  817. for (int i=0; i < sqlConnectionObjectCount; i++)
  818. connectionObjectAddresses[i]=&connectionObject[i];
  819. autopatcherServer->SetFileListTransferPlugin(&fileListTransfer);
  820. // PostgreSQL is fast, so this may not be necessary, or could use fewer threads
  821. // This is used to read increments of large files concurrently, thereby serving users downloads as other users read from the DB
  822. fileListTransfer.StartIncrementalReadThreads(sqlConnectionObjectCount);
  823. autopatcherServer->SetMaxConurrentUsers(workerThreadCount); // More users than this get queued up
  824. AutopatcherLoadNotifier autopatcherLoadNotifier;
  825. autopatcherServer->SetLoadManagementCallback(&autopatcherLoadNotifier);
  826. packetizedTCP.AttachPlugin(autopatcherServer);
  827. packetizedTCP.AttachPlugin(&fileListTransfer);
  828. autopatcherServer->SetAllowDownloadOfOriginalUnmodifiedFiles(allowDownloadingUnmodifiedFiles!=0);
  829. // ---- PLUGINS -----
  830. // Used to load balance clients, allow for client to client discovery
  831. RakNet::CloudServer cloudServer;
  832. // Used to update the local cloudServer
  833. RakNet::CloudClient cloudClient;
  834. // Used to determine the host of the server fully connected mesh, as well as to connect servers automatically
  835. RakNet::FullyConnectedMesh2 fullyConnectedMesh2;
  836. // Used for servers to verify each other - otherwise any system could pose as a server
  837. // Could also be used to verify and restrict clients if paired with the MessageFilter plugin
  838. RakNet::TwoWayAuthentication twoWayAuthentication;
  839. // Used to tell servers about each other
  840. RakNet::ConnectionGraph2 connectionGraph2;
  841. rakPeer->AttachPlugin(&cloudServer);
  842. rakPeer->AttachPlugin(&cloudClient);
  843. rakPeer->AttachPlugin(&fullyConnectedMesh2);
  844. rakPeer->AttachPlugin(&twoWayAuthentication);
  845. rakPeer->AttachPlugin(&connectionGraph2);
  846. if (!cloudServerHelper->StartRakPeer(rakPeer))
  847. return 1;
  848. RakNet::CloudServerHelperFilter sampleFilter; // Keeps clients from updating stuff to the server they are not supposed to
  849. sampleFilter.serverGuid=rakPeer->GetMyGUID();
  850. cloudServerHelper->SetupPlugins(&cloudServer, &sampleFilter, &cloudClient, &fullyConnectedMesh2, &twoWayAuthentication,&connectionGraph2, cloudServerHelper->serverToServerPassword);
  851. int ret;
  852. do
  853. {
  854. ret = cloudServerHelper->JoinCloud(rakPeer, &cloudServer, &cloudClient, &fullyConnectedMesh2, &twoWayAuthentication, &connectionGraph2, cloudServerHelper->GetCloudHostAddress());
  855. } while (ret==2);
  856. if (ret==1)
  857. return 1;
  858. printf("started.\n");
  859. printf("Enter database password:\n");
  860. char connectionString[256];
  861. char username[256];
  862. bool connectionSuccess=false;
  863. while (connectionSuccess==false)
  864. {
  865. strcpy(username, "postgres");
  866. strcpy(connectionString, "user=");
  867. strcat(connectionString, username);
  868. strcat(connectionString, " password=");
  869. strcat(connectionString, databasePassword);
  870. int conIdx;
  871. for (conIdx=0; conIdx < sqlConnectionObjectCount; conIdx++)
  872. {
  873. if (connectionObject[conIdx].Connect(connectionString)==false)
  874. {
  875. printf("Database connection failed.\n");
  876. printf("Try a different password? (y/n)\n");
  877. if (getch()!='y')
  878. {
  879. return 1;
  880. }
  881. else
  882. {
  883. connectionSuccess=false;
  884. printf("Enter password: ");
  885. Gets(databasePassword, sizeof(databasePassword));
  886. break;
  887. }
  888. }
  889. }
  890. if (conIdx==sqlConnectionObjectCount)
  891. connectionSuccess=true;
  892. }
  893. printf("Database connection suceeded.\n");
  894. printf("Starting threads\n");
  895. // 4 Worker threads, which is CPU intensive
  896. // A greater number of SQL connections, which read files incrementally for large downloads
  897. autopatcherServer->StartThreads(workerThreadCount,sqlConnectionObjectCount, connectionObjectAddresses);
  898. autopatcherServer->CacheMostRecentPatch(0);
  899. printf("System ready for connections\n");
  900. printf("(D)rop database\n(C)reate database.\n(U)pdate revision.\n(S)pawn a clone of this server\n(M)ax concurrent users (this server only)\n(I)mage the current state of this server\n(L)ist images\n(T)erminate cloud\n(Q)uit\n");
  901. char ch;
  902. RakNet::Packet *p;
  903. while (appState!=AP_TERMINATED)
  904. {
  905. RakNet::SystemAddress notificationAddress;
  906. notificationAddress=packetizedTCP.HasCompletedConnectionAttempt();
  907. // if (notificationAddress!=RakNet::UNASSIGNED_SYSTEM_ADDRESS)
  908. // printf("ID_CONNECTION_REQUEST_ACCEPTED\n");
  909. notificationAddress=packetizedTCP.HasNewIncomingConnection();
  910. // if (notificationAddress!=RakNet::UNASSIGNED_SYSTEM_ADDRESS)
  911. // printf("ID_NEW_INCOMING_CONNECTION\n");
  912. notificationAddress=packetizedTCP.HasLostConnection();
  913. // if (notificationAddress!=RakNet::UNASSIGNED_SYSTEM_ADDRESS)
  914. // printf("ID_CONNECTION_LOST\n");
  915. p=packetizedTCP.Receive();
  916. while (p)
  917. {
  918. packetizedTCP.DeallocatePacket(p);
  919. p=packetizedTCP.Receive();
  920. }
  921. if (autopatcherLoad==0)
  922. {
  923. if (appState==AP_TERMINATE_WHEN_ZERO_USERS_REACHED)
  924. {
  925. cloudServerHelper->Terminate();
  926. }
  927. if (timeSinceZeroUsers==0)
  928. timeSinceZeroUsers=RakNet::GetTime();
  929. }
  930. else
  931. {
  932. timeSinceZeroUsers=0;
  933. }
  934. p=rakPeer->Receive();
  935. while (p)
  936. {
  937. /*
  938. if (p->data[0]==ID_NEW_INCOMING_CONNECTION)
  939. printf("ID_NEW_INCOMING_CONNECTION (TCP) from %s\n", p->systemAddress.ToString(true));
  940. else if (p->data[0]==ID_DISCONNECTION_NOTIFICATION)
  941. printf("ID_DISCONNECTION_NOTIFICATION (TCP) from %s\n", p->systemAddress.ToString(true));
  942. else if (p->data[0]==ID_CONNECTION_LOST)
  943. printf("ID_CONNECTION_LOST (TCP) from %s\n", p->systemAddress.ToString(true));
  944. else
  945. */
  946. if (p->data[0]==ID_FCM2_NEW_HOST)
  947. {
  948. if (appState==AP_RUNNING)
  949. {
  950. RakNet::BitStream bs(p->data,p->length,false);
  951. bs.IgnoreBytes(1);
  952. RakNetGUID oldHost;
  953. bs.Read(oldHost);
  954. if (p->guid==rakPeer->GetMyGUID())
  955. {
  956. if (oldHost!=UNASSIGNED_RAKNET_GUID)
  957. {
  958. printf("ID_FCM2_NEW_HOST: Taking over as host from the old host.\n");
  959. }
  960. else
  961. {
  962. // Room not hosted if we become host the first time since this was done in CreateRoom() already
  963. printf("ID_FCM2_NEW_HOST: We have become host for the first time.\n");
  964. }
  965. }
  966. timeForNextLoadCheck=RakNet::GetTime()+LOAD_CHECK_INTERVAL;
  967. }
  968. }
  969. else if (p->data[0]==ID_USER_PACKET_ENUM)
  970. {
  971. RakNet::BitStream bsIn(p->data, p->length, false);
  972. bsIn.IgnoreBytes((sizeof(MessageID)));
  973. RakString incomingPw;
  974. bsIn.Read(incomingPw);
  975. if (incomingPw==cloudServerHelper->serverToServerPassword)
  976. {
  977. SetMaxConcurrentUsers(0);
  978. if (appState==AP_RUNNING)
  979. {
  980. if (timeSinceZeroUsers!=0)
  981. cloudServerHelper->Terminate();
  982. else
  983. appState=AP_TERMINATE_WHEN_ZERO_USERS_REACHED;
  984. }
  985. // Disconnect from the cloud, and do not accept new connections from users
  986. // The autopatcher will still run since it is on TCP
  987. rakPeer->Shutdown(1000);
  988. }
  989. }
  990. else if (p->data[0]==ID_CLOUD_GET_RESPONSE)
  991. {
  992. RakNet::CloudQueryResult cloudQueryResult;
  993. cloudClient.OnGetReponse(&cloudQueryResult, p);
  994. unsigned int rowIndex;
  995. const bool wasCallToGetServers=cloudQueryResult.cloudQuery.keys[0].primaryKey=="CloudConnCount";
  996. RakAssert(wasCallToGetServers);
  997. /*
  998. if (wasCallToGetServers)
  999. printf("Downloaded server list. %i servers.\n", cloudQueryResult.rowsReturned.Size());
  1000. */
  1001. unsigned short connectionsOnOurServer=65535;
  1002. unsigned short lowestConnectionsServer=65535;
  1003. RakNet::SystemAddress lowestConnectionAddress;
  1004. int totalConnections=0;
  1005. for (rowIndex=0; rowIndex < cloudQueryResult.rowsReturned.Size(); rowIndex++)
  1006. {
  1007. RakNet::CloudQueryRow *row = cloudQueryResult.rowsReturned[rowIndex];
  1008. unsigned short connCount;
  1009. RakNet::BitStream bsIn(row->data, row->length, false);
  1010. bsIn.Read(connCount);
  1011. printf("%i. Server found at %s with %i connections\n", rowIndex+1, row->serverSystemAddress.ToString(true), connCount);
  1012. totalConnections+=connCount;
  1013. }
  1014. const int MAX_SERVERS_EVER=32;
  1015. int available = cloudQueryResult.rowsReturned.Size() * sqlConnectionObjectCount - totalConnections;
  1016. if (available < 0 &&
  1017. cloudQueryResult.rowsReturned.Size() < MAX_SERVERS_EVER // no more than MAX_SERVERS_EVER servers ever, to control costs
  1018. )
  1019. {
  1020. int newServersNeeded = -available / sqlConnectionObjectCount;
  1021. if (newServersNeeded > 4)
  1022. newServersNeeded = 4; // Do not start more than 4 servers at a time
  1023. if (cloudQueryResult.rowsReturned.Size() + newServersNeeded > MAX_SERVERS_EVER)
  1024. {
  1025. newServersNeeded = MAX_SERVERS_EVER - cloudQueryResult.rowsReturned.Size();
  1026. }
  1027. if (newServersNeeded>0)
  1028. cloudServerHelper->SpawnServers(newServersNeeded);
  1029. }
  1030. timeForNextLoadCheck = RakNet::GetTime() + LOAD_CHECK_INTERVAL_AFTER_SPAWN;
  1031. cloudClient.DeallocateWithDefaultAllocator(&cloudQueryResult);
  1032. }
  1033. cloudServerHelper->OnPacket(p, rakPeer, &cloudClient, &cloudServer, &fullyConnectedMesh2, &twoWayAuthentication, &connectionGraph2);
  1034. rakPeer->DeallocatePacket(p);
  1035. p=rakPeer->Receive();
  1036. }
  1037. if (timeSinceZeroUsers!=0 && appState==AP_RUNNING && autopatcherServer->GetMaxConurrentUsers()>0)
  1038. {
  1039. // No users currently connected
  1040. RakNet::Time curTime = RakNet::GetTime();
  1041. RakNet::Time diff = curTime - timeSinceZeroUsers;
  1042. if (diff > 0)
  1043. {
  1044. if (fullyConnectedMesh2.IsHostSystem()==false)
  1045. {
  1046. // 2. Shutdown automatically if no users for 12 hours, except if we are the host
  1047. if (diff > 1000 * 60 * 60 * 12)
  1048. cloudServerHelper->Terminate();
  1049. }
  1050. else
  1051. {
  1052. // We are host according to RakPeer. But no users for 24 hours
  1053. // Verify we are the host according to Rackspace DNS records. If not, shutdown
  1054. if (diff > 1000 * 60 * 60 * 24)
  1055. {
  1056. // No users for 24 hours, verify if we are the host according to Rackspace DNS records. If not, shutdown
  1057. RakNet::Time diff2;
  1058. diff2 = curTime - cloudServerHelper->GetTimeOfLastDNSHostCheck();
  1059. if (diff2 > 1000 * 60 * 60 * 24)
  1060. {
  1061. appState=AP_TERMINATE_IF_NOT_DNS_HOST;
  1062. cloudServerHelper->SetTimeOfLastDNSHostCheck(curTime);
  1063. cloudServerHelper->GetDomainRecords();
  1064. }
  1065. }
  1066. }
  1067. }
  1068. }
  1069. cloudServerHelper->Update();
  1070. // 4. If we are the host, every 1 hour check load among all servers (including self). It takes like 30 minutes to build a new server.
  1071. if (fullyConnectedMesh2.IsHostSystem() && autopatcherServer->GetMaxConurrentUsers()>0)
  1072. {
  1073. RakNet::Time curTime = RakNet::GetTime();
  1074. if (curTime > timeForNextLoadCheck)
  1075. {
  1076. timeForNextLoadCheck = curTime + LOAD_CHECK_INTERVAL;
  1077. // If the load exceeds such that >=1 new fully loaded server is needed, add that many servers
  1078. // See ID_CLOUD_GET_RESPONSE
  1079. RakNet::CloudQuery cloudQuery;
  1080. cloudQuery.keys.Push(RakNet::CloudKey("CloudConnCount",0),_FILE_AND_LINE_); // CloudConnCount is defined at the top of CloudServerHelper.cpp
  1081. cloudQuery.subscribeToResults=false;
  1082. cloudClient.Get(&cloudQuery, rakPeer->GetMyGUID());
  1083. }
  1084. }
  1085. if (kbhit())
  1086. {
  1087. ch=getch();
  1088. if (ch=='q')
  1089. break;
  1090. else if (ch=='c')
  1091. {
  1092. if (connectionObject[0].CreateAutopatcherTables()==false)
  1093. printf("%s", connectionObject[0].GetLastError());
  1094. else
  1095. printf("Created tables.\n");
  1096. if (connectionObject[0].AddApplication(cloudServerHelper->patcherHostSubdomainURL, username)==false)
  1097. printf("%s", connectionObject[0].GetLastError());
  1098. else
  1099. printf("Added application\n");
  1100. }
  1101. else if (ch=='d')
  1102. {
  1103. if (connectionObject[0].DestroyAutopatcherTables()==false)
  1104. printf("%s", connectionObject[0].GetLastError());
  1105. }
  1106. else if (ch=='i')
  1107. {
  1108. printf("Image this server?\nWill clone this server so that future servers made from this image will use its current state.\nPress 'y' to image.\n");
  1109. if (getch()=='y')
  1110. {
  1111. cloudServerHelper->ImageThisServer();
  1112. }
  1113. }
  1114. else if (ch=='s')
  1115. {
  1116. printf("How many servers to spawn? (0-4) ");
  1117. char str[32];
  1118. Gets(str, sizeof(str));
  1119. int num = atoi(str);
  1120. if (num>0)
  1121. cloudServerHelper->SpawnServers(num);
  1122. }
  1123. else if (ch=='m')
  1124. {
  1125. char maxConcurrent[64];
  1126. printf("Enter new allowed max concurrent users: ");
  1127. Gets(maxConcurrent, sizeof(maxConcurrent));
  1128. if (maxConcurrent[0])
  1129. SetMaxConcurrentUsers(atoi(maxConcurrent));
  1130. }
  1131. else if (ch=='t')
  1132. {
  1133. printf("Setting max concurrent users to 0 on all servers\n");
  1134. SetMaxConcurrentUsers(0);
  1135. BitStream bsOut;
  1136. bsOut.Write((MessageID)ID_USER_PACKET_ENUM);
  1137. bsOut.Write(cloudServerHelper->serverToServerPassword);
  1138. DataStructures::List<RakNetGUID> remoteServersOut;
  1139. cloudServer.GetRemoteServers(remoteServersOut);
  1140. for (unsigned int i=0; i < remoteServersOut.Size(); i++)
  1141. {
  1142. rakPeer->Send(&bsOut, HIGH_PRIORITY, RELIABLE_ORDERED, 0, remoteServersOut[i], false);
  1143. rakPeer->CloseConnection(remoteServersOut[i], true);
  1144. }
  1145. printf("Remote servers will shutdown when all users are done\n");
  1146. printf("Disconnected from remote servers\n");
  1147. for (unsigned int i=0; i < rakPeer->GetNumberOfAddresses(); i++)
  1148. {
  1149. SystemAddress sa = rakPeer->GetInternalID(UNASSIGNED_SYSTEM_ADDRESS,i);
  1150. if (sa.IsLANAddress()==false)
  1151. {
  1152. printf("Updating host DNS entry to this server\n");
  1153. cloudServerHelper->UpdateHostIPAsynch(sa);
  1154. break;
  1155. }
  1156. }
  1157. }
  1158. else if (ch=='l')
  1159. {
  1160. cloudServerHelper->GetCustomImages();
  1161. }
  1162. else if (ch=='u')
  1163. {
  1164. // printf("Enter application name: ");
  1165. char appName[512];
  1166. strcpy(appName, cloudServerHelper->patcherHostSubdomainURL);
  1167. // Gets(appName,sizeof(appName));
  1168. // if (appName[0]==0)
  1169. // strcpy(appName, "TestApp");
  1170. printf("Enter application directory: ");
  1171. char appDir[512];
  1172. Gets(appDir,sizeof(appDir));
  1173. if (appDir[0]==0)
  1174. strcpy(appDir, "D:/temp");
  1175. if (connectionObject[0].UpdateApplicationFiles(appName, appDir, username, 0)==false)
  1176. {
  1177. printf("%s", connectionObject[0].GetLastError());
  1178. }
  1179. else
  1180. {
  1181. printf("Update success.\n");
  1182. autopatcherServer->CacheMostRecentPatch(appName);
  1183. }
  1184. }
  1185. }
  1186. RakSleep(30);
  1187. }
  1188. RakNet::OP_DELETE_ARRAY(connectionObject, _FILE_AND_LINE_);
  1189. RakNet::OP_DELETE_ARRAY(connectionObjectAddresses, _FILE_AND_LINE_);
  1190. packetizedTCP.Stop();
  1191. RakNet::RakPeerInterface::DestroyInstance(rakPeer);
  1192. RakNet::OP_DELETE(cloudServerHelper, _FILE_AND_LINE_);
  1193. return 0;
  1194. }
粤ICP备19079148号