Questions About LiveCode Apps That Connect to the Internet

Mark Talluto userev at canelasoftware.com
Wed Aug 10 12:12:41 EDT 2011


On Aug 10, 2011, at 8:33 AM, Gregory Lypny wrote:

> Hello everyone,
> 
> I’m building a standalone for my students.  It will allow them to upload quizzes and small assignments to my Mac via FTP as well as participate in various experiments and surveys.  I have a few Internet-related questions that I hoping someone can answer.
> 
> Is there a handler I can include in the stack that would allow it to detect whether the user is connected to the Internet?  That would allow me to give them the option of working offline.
> 
> How can I get the current date and time from either a time server or my remote Mac?  I need to timestamp student submissions and want the time coming from one source, not their own machines.
> 
> What is the best way to catch common FTP errors, such as a file not existing, so that I can relay a message back to the users?
> 
> 
> 
> Much obliged,
> 
> Gregory
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode


Hi Gregory,


> What is the best way to catch common FTP errors, such as a file not existing, so that I can relay a message back to the users?
After doing an FTP transaction, put the result into a variable.  If the variable is empty then the transaction was successful.  If there was an error, the variable will contain the exact error that occurred.  You can then return that error to the user.


> How can I get the current date and time from either a time server or my remote Mac?  I need to timestamp student submissions and want the time coming from one source, not their own machines.
Timestamping from the perspective the client is very easy.  Doing it from a central clock is a bit more complex.  Two solutions I can think of would be to pull the time from the internet or running a CGI on the server and pulling it from the server. If the Macs are set to keep their timing in sync with the internet then using the local time on each system should be accurate enough though.  


Here is how you can check for the internet.  The csDebugOutput lines can be removed.  They are used to log the function.  I have included that handler in the event you want to log it as well.  This function scales nicely for users that have higher latency connections.  Users that have lower latency connections will not be penalized and will normally return a true or false value within 20 milliseconds.  I have seen affordable dsl connections take as much as 1500 milliseconds.  Some international users may need as much as 3000 milliseconds on dial up connections.  The function allows for up to 6000 milliseconds.  This value can be modified as needed.  The tSiteToCheck variable can be set to any website.  We used to use Google, but found testing against our own sever served us in two ways.  We could see if the client would have issues resolving a connection to our server as well as testing for the ability to communicate with the Internet.


local lInternetFirstCheck, lInternetFirstCheckTiming


function checkInternetStatus
      --INITIALIZE VARIABLES
      put 6000 into tFallbackTime
      put "www.canelasoftware.com" into tSiteToCheck
      put false into lInternetFirstCheck
       
      csDebugOutput ("Starting checkInternetStatus...") 
      put hostNameToAddress(tSiteToCheck,"checkInternetStatus_Callback") into tThrowAway
       
      --GIVE SYSTEM TIME TO RUN THE hostNameToAddress FUNCTION
      put the milliseconds into lInternetFirstCheckTiming --GATHER TIMING FOR ACCURATE LOGGING
      repeat until lInternetFirstCheck is true
            wait 1 milliseconds with messages
            if the milliseconds - lInternetFirstCheckTiming > tFallbackTime then exit repeat
      end repeat
       
      --lInternetFirstCheck: true = DEFINITELY NOT CONNECTED TO ROUTER WITH NO WAN
      --lInternetFirstCheck: false = PROBABLY CONNECTED TO ROUTER WITH NO WAN (OR WE HAVE POOR LATENCY)
       
      --hostNameToAddress DID NOT RUN AND THUS RETURN NO INTERNET
      --PROBABLY CONNECTED TO ROUTER WITH NO WAN (OR WE HAVE POOR LATENCY)
      if lInternetFirstCheck is false then
            csDebugOutput ("First internet check failed after:" && the milliseconds - lInternetFirstCheckTiming & "ms")
            csDebugOutput ("Probably connected to a router with no WAN (or we have poor latency).")
            return false
      end if
       
      --CHECK IF THE ROUTER HAS A VALID INTERNET CONNECTION
      put hostNameToAddress(tSiteToCheck) into tInternetSecondCheck
       
      --WE HAVE A VALID INTERNET CONNECTION
      set the itemdel to "."
      if item 2 of tInternetSecondCheck is a number then
            csDebugOutput ("Second internet check passed after:" && the milliseconds - lInternetFirstCheckTiming & "ms")
            return true
      end if
       
      --WE ARE NOT CONNECTED TO A VALID INTERNET CONNECTION
      csDebugOutput ("Second check failed after:" && the milliseconds - lInternetFirstCheckTiming & "ms")
      return false
end checkInternetStatus


on checkInternetStatus_Callback pParam1
     csDebugOutput ("First internet check passed after:" && the milliseconds - lInternetFirstCheckTiming & "ms")
     put true into lInternetFirstCheck
end checkInternetStatus_Callback


command csDebugOutput pMessage
     global gDebugModeOn
     
     --BUILD LOG URL
     if the platform is "Win32" then put ("file:" & (specialFolderPath(26) & slash & kDebugLog)) into tLogURL
     if the platform is "MacOS" then put ("file:" & (specialFolderPath("preferences") & slash & kDebugLog)) into tLogURL
     if tLogURL is empty then exit csDebugOutput
     
     --CREATE TIMESTAMP
     put (the long date && the long time && ": ") into tTimestamp
     
     if gDebugModeOn is true then
          if char 5 of pMessage = "#" then put (lf & lf & lf) before pMessage --PREPEND MAJOR SECTIONS WITH LF*3
          put 0 into x
          repeat (the num of chars of pMessage)
               add 1 to x
               put char x of pMessage into tChar
               if tChar is not lf and tChar is not cr and tChar is not crlf then 
                    put tTimestamp before char x of pMessage --PUT THE TIMESTAMP AFTER THE SPACES, AND BEFORE THE CONTENT
                    exit repeat
               end if
          end repeat
           
          put (pMessage & cr) after URL tLogURL --WRITE TO THE LOG
     end if
end csDebugOutput



Best regards,

Mark Talluto
http://www.canelasoftware.com







More information about the use-livecode mailing list