Guest User

Untitled

a guest
Aug 22nd, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.50 KB | None | 0 0
  1. //Main
  2. #import <Cocoa/Cocoa.h>
  3. #import <Foundation/Foundation.h>
  4. #import "MainController.h"
  5.  
  6. int main (int argc, const char * argv[]) {
  7. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  8.  
  9. MainController *consolApp = [[MainController alloc] init];
  10.  
  11. [consolApp checkConf];
  12.  
  13. [consolApp startServ];
  14. [consolApp startSock];
  15.  
  16. //while (!exit);
  17. NSLog(@"working");
  18. getchar();
  19. NSLog(@"Closing");
  20.  
  21. [consolApp stopServ];
  22. [consolApp stopSock];
  23. sleep(1);
  24.  
  25. [consolApp release];
  26.  
  27. NSLog(@"press anykey to close.");
  28. getchar();
  29. }
  30.  
  31. --------------------------------------------------------
  32.  
  33. //MainController
  34. #import "MainController.h"
  35.  
  36. static NSLock *dbServerLock;
  37. static NSLock *dbAccessLock;
  38. static thread_t weatherThread;
  39. static thread_t forecastThread;
  40. static thread_t resortThread;
  41.  
  42. //var used by socket server
  43. static thread_t sockThread;
  44. static NSLock *socketThreadLock;
  45. static BOOL serverstart;
  46. static NSMutableArray *sockList;
  47. static Socket *listenSocket;
  48.  
  49. //use to make the thread ID
  50. static NSInteger threadcount;
  51. static NSLock *threadcountlock;
  52.  
  53. @implementation MainController
  54.  
  55. - (id)init{
  56. if(![super init]) return nil;
  57.  
  58. proccess = [[Proccess alloc] init];
  59. sockList = [[NSMutableArray alloc] init];
  60.  
  61. //init thread lock
  62. socketThreadLock = [[NSLock alloc] init];
  63. NSAssert (socketThreadLock != nil , @"impossible to creat socket lock");
  64.  
  65. dbServerLock = [[NSLock alloc] init];
  66. NSAssert (dbServerLock != nil , @"impossible to creat server lock");
  67.  
  68. dbAccessLock = [[NSLock alloc] init];
  69. NSAssert (dbAccessLock != nil , @"impossible to creat access lock");
  70.  
  71. //init the counter lock
  72. threadcountlock = [[NSLock alloc] init];
  73. NSAssert (threadcountlock != nil , @"impossible to creat thread counter lock");
  74.  
  75. serverstart = FALSE; //init server to stop state
  76. threadcount = 0;
  77.  
  78. return self;
  79. }
  80.  
  81. - (void)checkConf{
  82. NSDictionary *config = [[NSUserDefaults standardUserDefaults]persistentDomainForName:@"com.UWS_Daemon"];
  83.  
  84. if( [[config objectForKey:@"dbHost"] isEqual:@""] || [config objectForKey:@"dbHost"] == nil) {
  85.  
  86. NSLog(@"Config empty");
  87.  
  88. NSDictionary *data = [[NSDictionary alloc] initWithContentsOfFile:@"config.plist"];
  89.  
  90. NSDictionary *subdata = [data objectForKey:@"DB_config"];
  91. dbHost = [subdata objectForKey:@"dbHost"];
  92. dbPort = [[subdata objectForKey:@"dbPort"] intValue];
  93. dbUser = [subdata objectForKey:@"dbUser"];
  94. dbPassword = [subdata objectForKey:@"dbPassword"];
  95. dbName = [subdata objectForKey:@"dbName"];
  96.  
  97. subdata = [data objectForKey:@"Network"];
  98. nicInterface = [[subdata objectForKey:@"nicInterface"] intValue];
  99. nicPort = [[subdata objectForKey:@"nicPort"] intValue];
  100.  
  101. subdata = [data objectForKey:@"Refresh_rate"];
  102. urWeather = [[subdata objectForKey:@"urWeather"] intValue];
  103. urForecast = [[subdata objectForKey:@"urForecast"] intValue];
  104. urResort = [[subdata objectForKey:@"urResort"] intValue];
  105.  
  106. NSLog(@"config loaded");
  107. }
  108. }
  109.  
  110. - (void)dealloc{
  111.  
  112. [proccess release];
  113. [socketThreadLock release];
  114. [threadcountlock release];
  115. [dbAccessLock release];
  116. [dbServerLock release];
  117.  
  118. [sockList release];
  119.  
  120. [super dealloc];
  121. }
  122.  
  123. - (void)startSock {
  124.  
  125. if([socketThreadLock tryLock]){
  126. #ifdef DEBUG
  127. NSLog(@"new socket server");
  128. #endif
  129. serverstart = TRUE;
  130.  
  131. //create the first socket thread
  132. [NSThread detachNewThreadSelector:@selector(threadSocket:) toTarget:self withObject:nil];
  133. }
  134. }
  135.  
  136. - (void)stopSock {
  137.  
  138. if([socketThreadLock tryLock]){
  139. [socketThreadLock unlock];
  140. #ifdef DEBUG
  141. NSLog(@"Server not started");
  142. #endif
  143. }
  144. else {
  145.  
  146. serverstart = FALSE;
  147.  
  148. //close connected socket
  149. [listenSocket close];
  150. //NSLog(@"listenSocket closed");
  151.  
  152. for(Socket *sock in sockList) {
  153.  
  154. //NSLog(@"%@", sock);
  155. if(sock != nil) {
  156. //do not release because counter value = 1, and socket release when socketlist released.
  157. [sock close];
  158. }
  159. }
  160.  
  161. if(listenSocket) [listenSocket release];
  162. listenSocket = NULL;
  163.  
  164. [socketThreadLock unlock];
  165. [sockList release];
  166. sockList =[[NSMutableArray alloc]init];
  167.  
  168. NSLog(@"socket server stoped");
  169. }
  170. }
  171.  
  172. - (void)startServ {
  173. //lock var, updates thread can only be launch 1 time
  174. if([dbServerLock tryLock]) {
  175. //[proccess printConfig];
  176.  
  177. //start Update server threads
  178. [NSThread detachNewThreadSelector:@selector(updateWeatherThread:) toTarget:self withObject:nil];
  179. [NSThread detachNewThreadSelector:@selector(updateForecastThread:) toTarget:self withObject:nil];
  180. [NSThread detachNewThreadSelector:@selector(updateResortThread:) toTarget:self withObject:nil];
  181. }
  182. }
  183.  
  184. - (void)stopServ {
  185.  
  186. if(![dbServerLock tryLock]){
  187. //stop all threads
  188. thread_terminate(weatherThread);
  189. thread_terminate(forecastThread);
  190. thread_terminate(resortThread);
  191.  
  192. //release lock, and allow to restart server
  193. [dbAccessLock unlock];
  194.  
  195. [dbServerLock unlock];
  196. }
  197. else {
  198.  
  199. [dbServerLock unlock];
  200. }
  201. }
  202.  
  203. - (void)updateWeatherThread:(id)object {
  204. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  205.  
  206. weatherThread = mach_thread_self();
  207.  
  208. NSLog(@"update weather thread");
  209.  
  210. while(1){
  211. //[dbAccessLock lock];
  212. @try {
  213. [proccess updateDBWeather];
  214. NSLog(@"update weather done");
  215. }
  216. @catch (NSException * e) {
  217. NSLog(@"%@",e);
  218. }
  219. //[dbAccessLock unlock];
  220. sleep(urWeather);
  221. }
  222.  
  223. [pool drain];
  224. }
  225.  
  226. - (void)updateForecastThread:(id)object {
  227. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  228.  
  229. forecastThread = mach_thread_self();
  230.  
  231. while(1){
  232. //[dbAccessLock lock];
  233. @try {
  234. NSLog(@"update forecast thread");
  235. [proccess updateDBForecast];
  236. NSLog(@"update done");
  237. }
  238. @catch (NSException * e) {
  239. NSLog(@"%@",e);
  240. }
  241. //[dbAccessLock unlock];
  242. sleep(urForecast);
  243. }
  244.  
  245. [pool drain];
  246. }
  247.  
  248. - (void)updateResortThread:(id)object {
  249. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  250.  
  251. resortThread = mach_thread_self();
  252.  
  253. while(1){
  254. //[dbAccessLock lock];
  255. @try {
  256. NSLog(@"update resort thread");
  257. [proccess updateResort];
  258. NSLog(@"update done");
  259. }
  260. @catch (NSException * e) {
  261. NSLog(@"%@",e);
  262. }
  263. //[dbAccessLock unlock];
  264. sleep(urResort);
  265. }
  266.  
  267. [pool drain];
  268. }
  269.  
  270. - (void)threadSocket:(id)object {
  271. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  272.  
  273. NSInteger threadID = 0;
  274.  
  275. sockThread = mach_thread_self();
  276. NSLog(@"socket thread port: %d", nicPort);
  277.  
  278. @try {
  279. //create and configure the socket
  280. listenSocket = [[Socket alloc] init];
  281. [listenSocket listenOnPort:nicPort];
  282. //[sockList addObject:socket];
  283.  
  284. //wait an incomming connection
  285. Socket *connectedSock = NULL;
  286. while(connectedSock = [listenSocket acceptConnectionAndKeepListening]) {
  287.  
  288. //increase thread ID
  289. [threadcountlock lock];
  290. threadcount +=1;
  291. threadID = threadcount;
  292. [threadcountlock unlock];
  293.  
  294. NSLog(@"new socket thread: %d\n", threadID);
  295.  
  296. [sockList addObject:connectedSock];
  297. [NSThread detachNewThreadSelector:@selector(proccessThreadSocket:) toTarget:self withObject:(connectedSock)];
  298. NSLog(@"%@", sockList);
  299. }
  300.  
  301. //free lock for the new socket
  302. [socketThreadLock unlock];
  303.  
  304. //[sockList removeObject:socket];
  305. if(listenSocket)[listenSocket release];
  306.  
  307. }
  308. @catch (NSException *ex) {
  309. NSLog(@"ex: %@", ex);
  310. }
  311. @catch (NSError *err) {
  312. NSLog(@"err: %@", err);
  313. }
  314. [pool drain];
  315. }
  316.  
  317. - (void)proccessThreadSocket:(Socket*) socket {
  318.  
  319. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  320. //thread_t proccSockThread = mach_thread_self();
  321. //[proccSockThreadArray addObject:proccSockThread];
  322.  
  323. NSMutableData *data = [NSMutableData data];
  324. NSMutableString *repString = [NSMutableString string];
  325. BOOL conf = FALSE;
  326.  
  327. @try {
  328. //welcome message
  329. [socket writeString:[NSString stringWithFormat:@"UNiTY Weather Serveur\n"]];
  330.  
  331. NSString *tempstr;
  332. //read all data receive by the socket line by line (\n or \n\r at the end of the line)
  333. while ([socket readData:data]) {
  334. tempstr = [[[NSString alloc] initWithData:data encoding:[NSString defaultCStringEncoding]] autorelease]; //convert NSData to NSString
  335. tempstr = [tempstr stringByReplacingOccurrencesOfString:@"\n" withString:@""];
  336. tempstr = [tempstr stringByReplacingOccurrencesOfString:@"\r" withString:@""];
  337.  
  338. //return socket text value
  339. NSLog(@"%@",tempstr);
  340.  
  341. //if string if 8 char long (Weather code length) with end line character try to add it in the DB
  342. if((conf == FALSE) && ([tempstr length]>=8 && [tempstr length]<=38)){
  343.  
  344. //if GUI send ';;;' all 4 cities fields are emtpy, return EMPTY
  345. if([tempstr length] == 3)
  346. repString = @"EMPTY";
  347. //else use normal proccess function
  348. else {
  349. NSArray *citylist = [tempstr componentsSeparatedByString:@";"];
  350. for(NSString *cdata in citylist){
  351. if([cdata length] == 8){
  352. if([proccess newCityDB:cdata dbsock:NULL] == TRUE) {
  353. //if code is correct, and city correctly added (with weather and forecast)
  354. repString = [repString stringByAppendingString:@"OK;"];
  355. }
  356. else {
  357. repString = [repString stringByAppendingString:@"ERROR;"];
  358. }
  359. }
  360. else{
  361. //city empty
  362. if([cdata length] == 0) {
  363. repString = [repString stringByAppendingString:@"EMPTY;"];
  364. }
  365. //bad weather code
  366. else
  367. repString = [repString stringByAppendingString:@"ERROR;"];
  368. }
  369. }
  370. }
  371. repString = [repString stringByAppendingString:@"\n\r"];
  372. //NSLog(@"%@", repString);
  373. [socket writeString:repString]; //send OK to the remote host
  374.  
  375. [socket close];
  376. [sockList removeObject:socket];
  377. [socket release];
  378.  
  379. [pool drain];
  380.  
  381. //[proccSockThreadArray removeObject:proccSockThread];
  382. return;
  383. }
  384. if([[tempstr lowercaseString] rangeOfString:@"exit"].location != NSNotFound ){ //use for debugging, close the socket manually from the server
  385.  
  386. [socket close];
  387. [sockList removeObject:socket];
  388. [socket release];
  389.  
  390. [pool drain];
  391.  
  392. return;
  393. }
  394.  
  395.  
  396. [data init];
  397. repString = @"";
  398.  
  399. if(conf == FALSE) {
  400.  
  401. [socket close];
  402. [sockList removeObject:socket];
  403. [socket release];
  404.  
  405. [pool drain];
  406.  
  407. return;
  408. }
  409. }
  410.  
  411. NSLog(@"socket close");
  412.  
  413. }
  414. @catch (NSException *ex) {
  415. NSLog(@"exeption: %@", ex);
  416. }
  417. @catch (NSError *err) {
  418. NSLog(@"error: %@", err);
  419. }
  420. @catch (id oerr) {
  421. NSLog(@"oe: %@", oerr);
  422. }
  423.  
  424. [pool drain];
  425. }
  426.  
  427. @end
Add Comment
Please, Sign In to add comment