RequestManager.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. #import "ContentManager.h"
  15. #import "IconCache.h"
  16. #import "JSONKit.h"
  17. #import "RequestManager.h"
  18. #define READ_TAG 2422
  19. static RequestManager* sharedInstance = nil;
  20. @implementation RequestManager
  21. - (id)init
  22. {
  23. if ((self = [super init]))
  24. {
  25. _socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
  26. _filterFolder = nil;
  27. _isRunning = NO;
  28. _isConnected = NO;
  29. [self start];
  30. }
  31. return self;
  32. }
  33. - (void)dealloc
  34. {
  35. [_socket setDelegate:nil delegateQueue:NULL];
  36. [_socket disconnect];
  37. [_socket release];
  38. // dispatch_release(_listenQueue);
  39. // [_callbackMsgs release];
  40. // [_filterFolder release];
  41. sharedInstance = nil;
  42. [super dealloc];
  43. }
  44. + (RequestManager*)sharedInstance
  45. {
  46. @synchronized(self)
  47. {
  48. if (sharedInstance == nil)
  49. {
  50. sharedInstance = [[self alloc] init];
  51. }
  52. }
  53. return sharedInstance;
  54. }
  55. - (void)askOnSocket:(NSString*)path
  56. {
  57. NSLog(@"XX on Socket for %@", path);
  58. NSData* data = [[NSString stringWithFormat:@"RETRIEVE_FILE_STATUS:%@\n", path] dataUsingEncoding:NSUTF8StringEncoding];
  59. [_socket writeData:data withTimeout:5 tag:4711];
  60. NSData* stop = [@"\n" dataUsingEncoding:NSUTF8StringEncoding];
  61. [_socket readDataToData:stop withTimeout:-1 tag:READ_TAG];
  62. }
  63. - (void)askForIcon:(NSString*)path
  64. {
  65. if( _isConnected ) {
  66. [self askOnSocket:path];
  67. } else {
  68. [_requestQueue addObject:path];
  69. }
  70. }
  71. - (void)socket:(GCDAsyncSocket*)socket didConnectToHost:(NSString*)host port:(UInt16)port
  72. {
  73. // [socket readDataToData:[GCDAsyncSocket CRLFData] withTimeout:-1 tag:0];
  74. NSLog( @"Connected to host successfully!");
  75. _isConnected = YES;
  76. if( [_requestQueue count] > 0 ) {
  77. NSLog( @"We have to empty the queue");
  78. for( NSString *path in _requestQueue ) {
  79. [self askOnSocket:path];
  80. }
  81. }
  82. }
  83. - (void)socket:(GCDAsyncSocket*)socket didReadData:(NSData*)data withTag:(long)tag
  84. {
  85. if( tag == READ_TAG) {
  86. NSString* a1 = [NSString stringWithUTF8String:[data bytes]];
  87. NSString *answer = [a1 stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
  88. NSLog(@"READ from socket (%ld): AA%@OO", tag, answer);
  89. if( answer != nil ) {
  90. NSArray *chunks = [answer componentsSeparatedByString: @":"];
  91. // FIXME: Check if chunks[0] equals "STATUS"
  92. if( [chunks count] > 0 && [[chunks objectAtIndex:0] isEqualToString:@"STATUS"] ) {
  93. ContentManager *contentman = [ContentManager sharedInstance];
  94. [contentman setResultForPath:[chunks objectAtIndex:2] result:[chunks objectAtIndex:1]];
  95. }
  96. }
  97. }
  98. }
  99. - (NSTimeInterval)socket:(GCDAsyncSocket*)socket shouldTimeoutReadWithTag:(long)tag elapsed:(NSTimeInterval)elapsed bytesDone:(NSUInteger)length
  100. {
  101. // Called if a read operation has reached its timeout without completing.
  102. return 0.0;
  103. }
  104. - (void)socketDidDisconnect:(GCDAsyncSocket*)socket withError:(NSError*)err
  105. {
  106. if ([_connectedListenSockets containsObject:socket])
  107. {
  108. [_connectedListenSockets removeObject:socket];
  109. [[ContentManager sharedInstance] enableFileIcons:false];
  110. }
  111. if ([_connectedCallbackSockets containsObject:socket])
  112. {
  113. [_connectedCallbackSockets removeObject:socket];
  114. }
  115. }
  116. - (void)start
  117. {
  118. if (!_isRunning)
  119. {
  120. NSLog(@"Connect Socket!");
  121. NSError *err = nil;
  122. if (![_socket connectToHost:@"localhost" onPort:33001 error:&err]) // Asynchronous!
  123. {
  124. // If there was an error, it's likely something like "already connected" or "no delegate set"
  125. NSLog(@"I goofed: %@", err);
  126. }
  127. NSLog(@"Socket Connected!");
  128. _isRunning = YES;
  129. }
  130. }
  131. @end