NSURLConnection

Discussion in 'Mac Programming' started by DaveGee, Jul 26, 2008.

  1. DaveGee macrumors 6502a

    Joined:
    Jul 25, 2001
    #1
    Okay how about this one....

    I'm creating an SOAP request to... a SOAP server... (funny how that works) anyway the SOAP request does what it's set out to do (ask for 'status information' by 'subscribing' to the device).

    In fact if I simply telnet to the device (on port 80) and past a properly formated http header/body type message my telnet session will be FILLED with the following... (it just keeps going and going) and that's exactly what it's supposed to do!

    Code:
    HTTP/1.1 200 OK
    Content-Length: 215
    Connection: Keep-Alive
    WWW-Authenticate: Basic realm="/"
    Content-Type: application/soap+xml; charset=utf-8
    Cache-Control: no-cache
    EXT: UCoS, UPnP/1.0, UDI/1.0
    Last-Modified: Sat, 26 Jul 2008 15:33:1 GMT
    
    <?xml version="1.0" encoding="UTF-8"?><s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope"><s:Body><SubscriptionResponse><SID>uuid:20</SID><duration>0</duration></SubscriptionResponse></s:Body></s:Envelope>POST reuse HTTP/1.1
    HOST:255.255.255.255:65535
    CONTENT-TYPE:text/xml
    CONTENT-LENGTH:119
    Connection: Keep-Alive
    
    <?xml version="1.0"?><Event seqnum="0" sid="uuid:20"><control>_5</control><action>0</action><node/><eventInfo/></Event>POST reuse HTTP/1.1
    HOST:255.255.255.255:65535
    CONTENT-TYPE:text/xml
    CONTENT-LENGTH:121
    Connection: Keep-Alive
    
    <?xml version="1.0"?><Event seqnum="1" sid="uuid:20"><control>_0</control><action>120</action><node/><eventInfo/></Event>POST reuse HTTP/1.1
    HOST:255.255.255.255:65535
    CONTENT-TYPE:text/xml
    CONTENT-LENGTH:119
    Connection: Keep-Alive
    
    <?xml version="1.0"?><Event seqnum="2" sid="uuid:20"><control>_8</control><action>0</action><node/><eventInfo/></Event>POST reuse HTTP/1.1
    HOST:255.255.255.255:65535
    CONTENT-TYPE:text/xml
    CONTENT-LENGTH:136
    Connection: Keep-Alive
    
    So I get my NSURLConnection all setup with all the proper settings -- and set it for async since it's a 'never ending connection' (till I choose to close it down) but instead of it 'never ending here is what I get...

    First here is some of my code...

    Code:
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    NSLog(@"Yep... I did receive a response!"); 
    } 
    
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    NSString *sdata = [[NSString alloc] initWithData: data encoding: 
    NSLog(@"The data is: %@",sdata); 
    } 
    
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    NSLog(@"got error %@", [error description]); 
    } 
    
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    NSLog(@"connection closed done!"); 
    } 
    
    ---------------

    and now here is my log file...

    Code:
    7/26/08 3:55:30 PM ISY-26[5943] Yep... I did receive a response!
    7/26/08 3:55:30 PM ISY-26[5943] The data is: <?xml version="1.0" encoding="UTF-8"?><s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope"><s:Body><SubscriptionResponse><SID>uuid:25</SID><duration>0</duration></SubscriptionResponse></s:Body></s:Envelope> 
    7/26/08 3:55:30 PM ISY-26[5943] connection closed done! 
    
    My big question is why did the connection close after only that very first initial bit of xml?? Especially when a there are bunch of other xml messages that I need to read and act on -- but instead the connect simply closed... I'm almost positive that it's NSURLConnection that's doing the closing cause when I telnet in and past the soap request it wont quit till I kill the telnet process!!

    This is basically a keep-alive connection I guess... I send out an properly formatted body that contains a soap request that it understands and it spews back xml data.

    Under normal conditions, like when I request a 'device list' I just to a sync connection and send the soap body and back comes a single xml file will all the devices in it... but this is different... it's status update messages that will be never-ending (unless I turn off the device) or close the connection... it needs to be constantly read and sometimes reacted to....

    Can anyone help?

    Dave
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    You could check the response object and see if its properly reading the keep-alive header (see NSHTTPURLResponse).
     
  3. DaveGee thread starter macrumors 6502a

    Joined:
    Jul 25, 2001
    #3
    Thanks, I will check that right after I post this... (and thanks for responding!) here is a followup question that I think *might* be the sticking point to to all of this... In my zest to resolve this issue I readopted my app my app to make use of "AsyncSocket" v4.3 (iirc) a 3rd part api of sorts that makes working with socket communications more flexible.. and it worked GREAT till the very end.. when the initial BLAST of 80-100 'status update' xml packets ends and then I only get new ones when a device actually changes its status...

    You see I have a network of 40 or so devices that on first subscription send up 'status updates' on 3 items/variables -- ST their actual status - RR a current 'ramp-rate' for those devices that provide dimming/brightening and OL the current 'on-level' again for those devices that support 'dimming/brightening'.

    So lets take a table-lamp...

    The ST might by 255 (100% full on) the OL might be 192 (75% of full) and the RR might be 28 (.2 seconds to get from 0 (off) to 192 (the preset OL) and finally the node ID for the device.

    I would get that from 3 separate XML packets:

    Code:
    POST reuse HTTP/1.1
    HOST:255.255.255.255:65535
    CONTENT-TYPE:text/xml
    CONTENT-LENGTH:119
    Connection: Keep-Alive
    
    <?xml version="1.0"?><Event seqnum="0" sid="uuid:20"><control>RR</control><action>192</action><node>x.y.z</node><eventInfo/></Event>POST reuse HTTP/1.1
    HOST:255.255.255.255:65535
    CONTENT-TYPE:text/xml
    CONTENT-LENGTH:121
    Connection: Keep-Alive
    
    <?xml version="1.0"?><Event seqnum="1" sid="uuid:20"><control>OL</control><action>192</action><node>x.y.z</node><eventInfo/></Event>POST reuse HTTP/1.1
    HOST:255.255.255.255:65535
    CONTENT-TYPE:text/xml
    CONTENT-LENGTH:119
    Connection: Keep-Alive
    
    <?xml version="1.0"?><Event seqnum="2" sid="uuid:20"><control>RR</control><action>28</action><node>x.y.z</node><eventInfo/></Event>POST reuse HTTP/1.1
    
    So if I have 20 devices I should (for the most part) see 60 xml packets in the initial RUSH of the subscribing to the device. Plus a few other random update packets thrown in.

    That all happens in the first second or os of subscribing...

    After that I will then only get new messages when one of those 20 devices actually changes (or gets changes)...

    Lamp goes from off to on... I would then get the folloing (stripping out the headers)...

    Code:
    <?xml version="1.0"?><Event seqnum="0" sid="uuid:20"><control>ST</control><action>255</action><node>x.y.z</node><eventInfo/></Event>
    
    Finally to what I think is the real issue at hand...

    Look more closely at the packets that are sent... but scroll all the way to the RIGHT hand side so you can see the end lines (I've bolded to make things easier)

    Code:
    POST reuse HTTP/1.1
    HOST:255.255.255.255:65535
    CONTENT-TYPE:text/xml
    CONTENT-LENGTH:119
    Connection: Keep-Alive
    
    <?xml version="1.0"?><Event seqnum="0" sid="uuid:20"><control>RR</control><action>192</action><node>x.y.z</node><eventInfo/>[B]</Event>POST reuse HTTP/1.1[/B]
    HOST:255.255.255.255:65535
    CONTENT-TYPE:text/xml
    CONTENT-LENGTH:121
    Connection: Keep-Alive
    
    <?xml version="1.0"?><Event seqnum="1" sid="uuid:20"><control>OL</control><action>192</action><node>x.y.z</node><eventInfo/>[B]</Event>POST reuse HTTP/1.1[/B]
    HOST:255.255.255.255:65535
    CONTENT-TYPE:text/xml
    CONTENT-LENGTH:119
    Connection: Keep-Alive
    
    <?xml version="1.0"?><Event seqnum="2" sid="uuid:20"><control>RR</control><action>28</action><node>x.y.z</node><eventInfo/>[B]</Event>POST reuse HTTP/1.1[/B]
    
    Notice that "POST reuse HTTP/1.1" at the end of each xml packet that isn't a part of the END of that XML status message BUT instead the **START** of the next status packet that is on its way -- the XML packet was not terminated with either a \n or \r but instead kinda just sits there... until another packet comes in that *starts with* POST reuse HTTP/1.1\n"! -- See the new-line thats what triggers the async lib to tell me the new packet has arrived... Anway I think this whole time it the fact that the XML status message is not being (properly?) terminated with some kind of end of line \n \r character that is the heart of the problem...

    Now even when using the async lib I mentioned above things go SUPER GREAT till it gets to the very end of the big initial rush of data... at that time I never get to actually see the last message until 'another' packet comes over the connection and since that packet is starting with POST reuse HTTP/1.1\n" that \n trigger tells my async lib that a new packet came in and gives it to me.

    Ugh.. looks like I might need to speak to the hardware developer of the box that is generating the status packets... Why do I have the feeling that they are going to say... Windows developers don't have any... and I'm sure they don't...

    Oh well such is the life of a Mac developer I guess... (removed an enormous rant against the less than helpful Apple hosted developer forums since it would only do more harm than good)

    Dave
     

Share This Page