Skip to content

Commit

Permalink
Some bugs and memeory leak fixed.
Browse files Browse the repository at this point in the history
  • Loading branch information
horsson committed Aug 31, 2011
1 parent 7625364 commit bda587e
Show file tree
Hide file tree
Showing 10 changed files with 4,813 additions and 2,892 deletions.
18 changes: 11 additions & 7 deletions UPnPControlPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,25 @@

@protocol UPnPControlPointDelegate <NSObject>

-(void) errorDidReceived:(NSError*) error;

-(void) errorDidReceive: (NSError*) error;
-(void) upnpDeviceDidAdd: (UPnPDevice*) upnpDevice;
-(void) upnpDeviceDidLeave: (UPnPDevice*) upnpDevice;

@end

//One UPnPControlPoint is a cooresponding UPnP Client in libupnp.
@interface UPnPControlPoint : NSObject {
@interface UPnPControlPoint : NSObject<UPnPDDeviceDelegate> {
@private
//UPnP client handle, which is used in the entire Control Point life.
UpnpClient_Handle _clientHandle;

dispatch_queue_t _controlPointQueue;
NSLock* _globalLock;
NSLock* _devicesLock;

}

@property(nonatomic,retain) id<UPnPControlPointDelegate> delegate;
@property(nonatomic,assign) id<UPnPControlPointDelegate> delegate;


//All the devices found in the LAN, key is the Device_id.
@property(retain) NSMutableDictionary* devices;
Expand All @@ -42,8 +45,9 @@

-(void) stop;

-(NSLock*) globalLock;
-(NSLock*) devicesLock;

-(NSMutableDictionary*) getDevices;
-(NSLock*) getGlobalLock;
-(dispatch_queue_t) controlPointQueue;

@end
102 changes: 65 additions & 37 deletions UPnPControlPoint.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@

@implementation UPnPControlPoint
@synthesize delegate;
@synthesize devices = _device;
@synthesize devices = _devices;

typedef void (^upnpXmlParserBlock)(void);

//Callback function for the client.
int upnp_callback_func(Upnp_EventType, void *, void *);
Expand All @@ -27,27 +26,27 @@ - (id) init
{
[self initWithHostAddress:nil andPort:0];
}

return self;
}

-(dispatch_queue_t) controlPointQueue
{
return _controlPointQueue;
}

-(NSLock*) devicesLock
{
return _devicesLock;
}

-(void) fireErrorEvent:(int) upnpError
{
NSError* error = [[[NSError alloc] initWithUPnPError:upnpError] autorelease];
if (delegate)
[delegate errorDidReceived:error];
[delegate errorDidReceive:error];
}

-(NSMutableDictionary*) getDevices
{
@synchronized(self)
{
[[_device retain] autorelease];
}
return _device;
}



-(id) initWithHostAddress:(NSString *)address andPort:(UInt16)port
Expand All @@ -72,8 +71,12 @@ -(id) initWithHostAddress:(NSString *)address andPort:(UInt16)port
}
}

_device = [[NSMutableDictionary alloc] init];
//==========================Init some iVars=====================================
_devices = [[NSMutableDictionary alloc] init];
_globalLock =[[NSLock alloc] init];
_devicesLock = [[NSLock alloc] init];
_controlPointQueue = dispatch_queue_create("de.haohu.upnp.controlpoint", NULL);

}
return self;
}
Expand All @@ -83,8 +86,9 @@ -(id) initWithHostAddress:(NSString *)address andPort:(UInt16)port
//Callback function for the client.
int upnp_callback_func(Upnp_EventType eventType, void *event, void *cookie)
{
NSLock* lock = [[refToSelf getGlobalLock] retain];
NSLock* lock = [refToSelf globalLock];
[lock lock];

switch(eventType)
{
case UPNP_DISCOVERY_SEARCH_RESULT:
Expand Down Expand Up @@ -115,44 +119,46 @@ int upnp_callback_func(Upnp_EventType eventType, void *event, void *cookie)
}
}
[lock unlock];
[lock release];
return UPNP_E_SUCCESS;
}


#pragma Callback function different handle
void handle_discovery_message(void* event)
{
/*
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init];

NSLock* nslock = [refToSelf devicesLock];
[nslock lock];
struct Upnp_Discovery *discovery = (struct Upnp_Discovery*) event;
UPnPDevice *device = [[UPnPDevice alloc] init];
NSString* locationURL = [[NSString stringWithCString:discovery->Location encoding:NSUTF8StringEncoding] autorelease];
NSString *deviceID = [[NSString stringWithCString:discovery->DeviceId encoding:NSUTF8StringEncoding] autorelease];
device.UDN = deviceID;
//dispatch_queue_t upnpXmlParserQueue;
NSString *deviceID = [NSString stringWithCString:discovery->DeviceId encoding:NSUTF8StringEncoding];
/*
if ([[refToSelf devices] objectForKey:deviceID])
{
NSLog(@"Device is in, ignore.");
return;
}
*/
NSString* locationURL = [NSString stringWithCString:discovery->Location encoding:NSUTF8StringEncoding];

//upnpXmlParserQueue = dispatch_queue_create("de.haohu.upnp.xml.queue", NULL);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSMutableDictionary* devicesDict = [refToSelf getDevices];
[devicesDict retain];
[devicesDict setObject:device forKey:deviceID];
[devicesDict release];


UPnPDevice *device = [[UPnPDevice alloc] initWithLocationURL:locationURL timeout:4.0];


device.UDN = deviceID;
device.delegate = refToSelf;
[nslock unlock];
dispatch_async([refToSelf controlPointQueue], ^{
[device startParsing];
});
//dispatch_release(upnpXmlParserQueue);

[device release];
[pool drain];
*/

}




-(void) searchTarget:(NSString*) target withMx:(NSUInteger) mx
{
UpnpSearchAsync(_clientHandle, mx, [target cStringUsingEncoding:NSUTF8StringEncoding], NULL);
Expand All @@ -168,14 +174,36 @@ -(void) stop
}
}

-(NSLock*) getGlobalLock
-(NSLock*) globalLock
{
return _globalLock;
}


#pragma UPnPDevice callback
-(void) upnpDeviceDidFinishParsing:(UPnPDevice*) upnpDevice
{
NSLog(@"Finish parsing.");
[_devices setObject:upnpDevice forKey:upnpDevice.UDN];
[delegate upnpDeviceDidAdd:upnpDevice];
[upnpDevice release];


}

-(void) upnpDeviceDidReceiveError:(UPnPDevice*) withError:(NSError*) error;
{

}



-(void) dealloc
{
dispatch_release(_controlPointQueue);
[_devicesLock release];
[_globalLock release];
[_device release];
[_devices release];
[delegate release];
[super dealloc];
}
Expand Down
1 change: 1 addition & 0 deletions UPnPDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
NSTimeInterval _timeout;

}

@property(nonatomic,retain) NSString* deviceType;
@property(nonatomic,retain) NSString* friendlyName;
@property(nonatomic,retain) NSString* manufacturer;
Expand Down
14 changes: 11 additions & 3 deletions UPnPDevice.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ -(id) initWithLocationURL:(NSString*) locationURL timeout:(NSTimeInterval) timeo
self = [super init];
if (self)
{
if (_locationURL)
{
[_locationURL release];
}
_locationURL = locationURL;
_baseURL = [self getBaseUrlFrom:locationURL];
[_locationURL copy];
_baseURL = [[self getBaseUrlFrom:locationURL] retain];
_upnpServiceLock = [[NSLock alloc] init];
_timeout = timeout;
}
Expand All @@ -39,7 +44,7 @@ -(void) startParsing
NSURL* nsurl = [[NSURL alloc] initWithString:_locationURL];
NSURLRequest* urlRequest = [[NSURLRequest alloc] initWithURL:nsurl cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:_timeout];
[nsurl release];
NSURLResponse* resp;
NSURLResponse* resp = NULL;
_xmlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&resp error:NULL];
[urlRequest release];
NSHTTPURLResponse* httpResp = (NSHTTPURLResponse*) resp;
Expand Down Expand Up @@ -101,6 +106,7 @@ - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName nam
if ([elementName isEqualToString:@"service"])
{
_service = [[UPnPService alloc] init];
_service.controlPointHandle = self.controlPointHandle;
_service.delegate = self;
}
else if ([elementName isEqualToString:@"deviceType"] || [elementName isEqualToString:@"UDN"] ||
Expand Down Expand Up @@ -349,6 +355,8 @@ -(void) parseDidFinish:(UPnPService*) upnpService
#pragma dealloc and clean code.
- (void)dealloc {

[_baseURL release];
[_locationURL release];
[_upnpServiceLock release];
[deviceType release];
[friendlyName release];
Expand All @@ -363,7 +371,7 @@ - (void)dealloc {
[presentationURL release];
[iconList release];
[serviceList release];

[super dealloc];
}

Expand Down
1 change: 1 addition & 0 deletions UPnPService.m
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName nam
if ([elementName isEqualToString:@"action"])
{
_action = [[UPnPAction alloc] init];
_action.controlPointHandle = self.controlPointHandle;
}

if ([elementName isEqualToString:@"argumentList"])
Expand Down
Loading

0 comments on commit bda587e

Please sign in to comment.