Verifying: Better way to do this timeout function?
xanatos
Posts: 1,120
I noticed that when it comes time for HTTPServer to send my email - if the network is down, or really slow (Netflix... :-) ) - the spinneret will hang forever, apparently because of this code:
So I made a sort of timeout function as follows:
It seems to work... but it's a lotta lines of code. I'm just curious if there's a trick I don't know about that might reduce that to like 1 line of code or something... or if the way I've done it is OK. Mainly looking for code efficiency here since the function works and it only waits for a few seconds to connect before giving up and moving on to other functions.
Thanks,
Dave
'ORIGINAL SOCKET CONNECTION WAITS FOREVER Socket.Connect(id) pause(wait) repeat while !Socket.Connected(id) ' Set up timeout here in case network down/unavailable.
So I made a sort of timeout function as follows:
'NEW SOCKET CONNECTION TIMEOUT Socket.Connect(id) pause(wait) emD := 0 repeat while !Socket.Connected(id) ' Set up timeout here in case network down/unavailable. pause(wait) emD := emD + 1 if Socket.Connected(id) emD := 0 quit else if emD == 50 outa[24] := 0 '' Set Email Request line back to 0 for the next sendFreq minutes. Socket.Disconnect(id) pause(delay) tcpMask := tempMask InitializeSocket(id) pause(delay) PinState(23, 0) ' Turn off the LED and the dataline for the email request thing waitcnt(clkfreq/5+cnt) return
It seems to work... but it's a lotta lines of code. I'm just curious if there's a trick I don't know about that might reduce that to like 1 line of code or something... or if the way I've done it is OK. Mainly looking for code efficiency here since the function works and it only waits for a few seconds to connect before giving up and moving on to other functions.
Thanks,
Dave
Comments
One extra line and one line and one line extended a bit.
Would this do what you want?
In looking everything over, and considering that if the loop times out that it needs to skip the whole emailing thing - which means housecleaning with resetting sockets, flags, and RETURNing to the program place from whence it came as-if it had done the emailing, etc., I'm guessing that it's all going to be about the same. I'm just at that point where I can write the code, it apparently works, but I don't necessarily trust that it's really the best option - despite it working :-)
Thanks,
Dave
There's a lot to be said for writing code that works. There is usually some way of improving code but those ways become more obvious with practice and experience.
Looking at your code again, I'd probably move the reset sequence outside of the loop. I'd also only call "Socket.Connect" from one location.
BTW, I don't think I've mentioned how pleased I am to see you've taken up the Propeller. I've always been impressed with your projects and repeatedly restrained myself from suggesting the Propeller to truly unleash your creativity. I'm looking forward to seeing what you can do now you have the Propeller as a tool.
I've wanted to figure out the Propeller for a long time, but the learning curve was much steeper than the Stamp - and until this project, I could get a stamp to do just about anything - those BS2px's with their flat eeprom and the STORE command opened up the world for me. But this project required emailing and coordination beyond the capabilities of a Stamp and an SB-70 (PINK server), and I committed to a deadline without ever really knowing exactly what was involved. It's been terrifying! :-) I'm nearing installation, and everything is working, and for a while I thought I wasn't going to make it. I think I'll be taking a breather after this project, and hopefully in that time I can broaden my focus on the propeller from the narrow focus on making it do the specific tasks at hand, to more of a "what CAN I do with it" type of perspective. I'm particularly interested in making an immensely upgraded version of my speaking, voice-activated home automation system, and in playing with video by making a flight-controller that has artificial horizon, altimeter, air-speed, and outputs to control servos and throttle... sort of an auto-pilot plus.
Of course, I also need to stop making my PC Board designs in PhotoShop... I want to be able to take advantage of the small form-factor of the SM versions of the Propeller, so there has to be some serious upgrading to my fabrication process. :-)
Thanks again,
Dave