Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Main
- #import <Cocoa/Cocoa.h>
- #import <Foundation/Foundation.h>
- #import "MainController.h"
- int main (int argc, const char * argv[]) {
- NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
- MainController *consolApp = [[MainController alloc] init];
- [consolApp checkConf];
- [consolApp startServ];
- [consolApp startSock];
- //while (!exit);
- NSLog(@"working");
- getchar();
- NSLog(@"Closing");
- [consolApp stopServ];
- [consolApp stopSock];
- sleep(1);
- [consolApp release];
- NSLog(@"press anykey to close.");
- getchar();
- }
- --------------------------------------------------------
- //MainController
- #import "MainController.h"
- static NSLock *dbServerLock;
- static NSLock *dbAccessLock;
- static thread_t weatherThread;
- static thread_t forecastThread;
- static thread_t resortThread;
- //var used by socket server
- static thread_t sockThread;
- static NSLock *socketThreadLock;
- static BOOL serverstart;
- static NSMutableArray *sockList;
- static Socket *listenSocket;
- //use to make the thread ID
- static NSInteger threadcount;
- static NSLock *threadcountlock;
- @implementation MainController
- - (id)init{
- if(![super init]) return nil;
- proccess = [[Proccess alloc] init];
- sockList = [[NSMutableArray alloc] init];
- //init thread lock
- socketThreadLock = [[NSLock alloc] init];
- NSAssert (socketThreadLock != nil , @"impossible to creat socket lock");
- dbServerLock = [[NSLock alloc] init];
- NSAssert (dbServerLock != nil , @"impossible to creat server lock");
- dbAccessLock = [[NSLock alloc] init];
- NSAssert (dbAccessLock != nil , @"impossible to creat access lock");
- //init the counter lock
- threadcountlock = [[NSLock alloc] init];
- NSAssert (threadcountlock != nil , @"impossible to creat thread counter lock");
- serverstart = FALSE; //init server to stop state
- threadcount = 0;
- return self;
- }
- - (void)checkConf{
- NSDictionary *config = [[NSUserDefaults standardUserDefaults]persistentDomainForName:@"com.UWS_Daemon"];
- if( [[config objectForKey:@"dbHost"] isEqual:@""] || [config objectForKey:@"dbHost"] == nil) {
- NSLog(@"Config empty");
- NSDictionary *data = [[NSDictionary alloc] initWithContentsOfFile:@"config.plist"];
- NSDictionary *subdata = [data objectForKey:@"DB_config"];
- dbHost = [subdata objectForKey:@"dbHost"];
- dbPort = [[subdata objectForKey:@"dbPort"] intValue];
- dbUser = [subdata objectForKey:@"dbUser"];
- dbPassword = [subdata objectForKey:@"dbPassword"];
- dbName = [subdata objectForKey:@"dbName"];
- subdata = [data objectForKey:@"Network"];
- nicInterface = [[subdata objectForKey:@"nicInterface"] intValue];
- nicPort = [[subdata objectForKey:@"nicPort"] intValue];
- subdata = [data objectForKey:@"Refresh_rate"];
- urWeather = [[subdata objectForKey:@"urWeather"] intValue];
- urForecast = [[subdata objectForKey:@"urForecast"] intValue];
- urResort = [[subdata objectForKey:@"urResort"] intValue];
- NSLog(@"config loaded");
- }
- }
- - (void)dealloc{
- [proccess release];
- [socketThreadLock release];
- [threadcountlock release];
- [dbAccessLock release];
- [dbServerLock release];
- [sockList release];
- [super dealloc];
- }
- - (void)startSock {
- if([socketThreadLock tryLock]){
- #ifdef DEBUG
- NSLog(@"new socket server");
- #endif
- serverstart = TRUE;
- //create the first socket thread
- [NSThread detachNewThreadSelector:@selector(threadSocket:) toTarget:self withObject:nil];
- }
- }
- - (void)stopSock {
- if([socketThreadLock tryLock]){
- [socketThreadLock unlock];
- #ifdef DEBUG
- NSLog(@"Server not started");
- #endif
- }
- else {
- serverstart = FALSE;
- //close connected socket
- [listenSocket close];
- //NSLog(@"listenSocket closed");
- for(Socket *sock in sockList) {
- //NSLog(@"%@", sock);
- if(sock != nil) {
- //do not release because counter value = 1, and socket release when socketlist released.
- [sock close];
- }
- }
- if(listenSocket) [listenSocket release];
- listenSocket = NULL;
- [socketThreadLock unlock];
- [sockList release];
- sockList =[[NSMutableArray alloc]init];
- NSLog(@"socket server stoped");
- }
- }
- - (void)startServ {
- //lock var, updates thread can only be launch 1 time
- if([dbServerLock tryLock]) {
- //[proccess printConfig];
- //start Update server threads
- [NSThread detachNewThreadSelector:@selector(updateWeatherThread:) toTarget:self withObject:nil];
- [NSThread detachNewThreadSelector:@selector(updateForecastThread:) toTarget:self withObject:nil];
- [NSThread detachNewThreadSelector:@selector(updateResortThread:) toTarget:self withObject:nil];
- }
- }
- - (void)stopServ {
- if(![dbServerLock tryLock]){
- //stop all threads
- thread_terminate(weatherThread);
- thread_terminate(forecastThread);
- thread_terminate(resortThread);
- //release lock, and allow to restart server
- [dbAccessLock unlock];
- [dbServerLock unlock];
- }
- else {
- [dbServerLock unlock];
- }
- }
- - (void)updateWeatherThread:(id)object {
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- weatherThread = mach_thread_self();
- NSLog(@"update weather thread");
- while(1){
- //[dbAccessLock lock];
- @try {
- [proccess updateDBWeather];
- NSLog(@"update weather done");
- }
- @catch (NSException * e) {
- NSLog(@"%@",e);
- }
- //[dbAccessLock unlock];
- sleep(urWeather);
- }
- [pool drain];
- }
- - (void)updateForecastThread:(id)object {
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- forecastThread = mach_thread_self();
- while(1){
- //[dbAccessLock lock];
- @try {
- NSLog(@"update forecast thread");
- [proccess updateDBForecast];
- NSLog(@"update done");
- }
- @catch (NSException * e) {
- NSLog(@"%@",e);
- }
- //[dbAccessLock unlock];
- sleep(urForecast);
- }
- [pool drain];
- }
- - (void)updateResortThread:(id)object {
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- resortThread = mach_thread_self();
- while(1){
- //[dbAccessLock lock];
- @try {
- NSLog(@"update resort thread");
- [proccess updateResort];
- NSLog(@"update done");
- }
- @catch (NSException * e) {
- NSLog(@"%@",e);
- }
- //[dbAccessLock unlock];
- sleep(urResort);
- }
- [pool drain];
- }
- - (void)threadSocket:(id)object {
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- NSInteger threadID = 0;
- sockThread = mach_thread_self();
- NSLog(@"socket thread port: %d", nicPort);
- @try {
- //create and configure the socket
- listenSocket = [[Socket alloc] init];
- [listenSocket listenOnPort:nicPort];
- //[sockList addObject:socket];
- //wait an incomming connection
- Socket *connectedSock = NULL;
- while(connectedSock = [listenSocket acceptConnectionAndKeepListening]) {
- //increase thread ID
- [threadcountlock lock];
- threadcount +=1;
- threadID = threadcount;
- [threadcountlock unlock];
- NSLog(@"new socket thread: %d\n", threadID);
- [sockList addObject:connectedSock];
- [NSThread detachNewThreadSelector:@selector(proccessThreadSocket:) toTarget:self withObject:(connectedSock)];
- NSLog(@"%@", sockList);
- }
- //free lock for the new socket
- [socketThreadLock unlock];
- //[sockList removeObject:socket];
- if(listenSocket)[listenSocket release];
- }
- @catch (NSException *ex) {
- NSLog(@"ex: %@", ex);
- }
- @catch (NSError *err) {
- NSLog(@"err: %@", err);
- }
- [pool drain];
- }
- - (void)proccessThreadSocket:(Socket*) socket {
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- //thread_t proccSockThread = mach_thread_self();
- //[proccSockThreadArray addObject:proccSockThread];
- NSMutableData *data = [NSMutableData data];
- NSMutableString *repString = [NSMutableString string];
- BOOL conf = FALSE;
- @try {
- //welcome message
- [socket writeString:[NSString stringWithFormat:@"UNiTY Weather Serveur\n"]];
- NSString *tempstr;
- //read all data receive by the socket line by line (\n or \n\r at the end of the line)
- while ([socket readData:data]) {
- tempstr = [[[NSString alloc] initWithData:data encoding:[NSString defaultCStringEncoding]] autorelease]; //convert NSData to NSString
- tempstr = [tempstr stringByReplacingOccurrencesOfString:@"\n" withString:@""];
- tempstr = [tempstr stringByReplacingOccurrencesOfString:@"\r" withString:@""];
- //return socket text value
- NSLog(@"%@",tempstr);
- //if string if 8 char long (Weather code length) with end line character try to add it in the DB
- if((conf == FALSE) && ([tempstr length]>=8 && [tempstr length]<=38)){
- //if GUI send ';;;' all 4 cities fields are emtpy, return EMPTY
- if([tempstr length] == 3)
- repString = @"EMPTY";
- //else use normal proccess function
- else {
- NSArray *citylist = [tempstr componentsSeparatedByString:@";"];
- for(NSString *cdata in citylist){
- if([cdata length] == 8){
- if([proccess newCityDB:cdata dbsock:NULL] == TRUE) {
- //if code is correct, and city correctly added (with weather and forecast)
- repString = [repString stringByAppendingString:@"OK;"];
- }
- else {
- repString = [repString stringByAppendingString:@"ERROR;"];
- }
- }
- else{
- //city empty
- if([cdata length] == 0) {
- repString = [repString stringByAppendingString:@"EMPTY;"];
- }
- //bad weather code
- else
- repString = [repString stringByAppendingString:@"ERROR;"];
- }
- }
- }
- repString = [repString stringByAppendingString:@"\n\r"];
- //NSLog(@"%@", repString);
- [socket writeString:repString]; //send OK to the remote host
- [socket close];
- [sockList removeObject:socket];
- [socket release];
- [pool drain];
- //[proccSockThreadArray removeObject:proccSockThread];
- return;
- }
- if([[tempstr lowercaseString] rangeOfString:@"exit"].location != NSNotFound ){ //use for debugging, close the socket manually from the server
- [socket close];
- [sockList removeObject:socket];
- [socket release];
- [pool drain];
- return;
- }
- [data init];
- repString = @"";
- if(conf == FALSE) {
- [socket close];
- [sockList removeObject:socket];
- [socket release];
- [pool drain];
- return;
- }
- }
- NSLog(@"socket close");
- }
- @catch (NSException *ex) {
- NSLog(@"exeption: %@", ex);
- }
- @catch (NSError *err) {
- NSLog(@"error: %@", err);
- }
- @catch (id oerr) {
- NSLog(@"oe: %@", oerr);
- }
- [pool drain];
- }
- @end
Add Comment
Please, Sign In to add comment