commit 9f46aaa458398ddf1953a4a7be38609c0deb7e06
parent 91d06cc07ea8b950554c43d6efc3a2cf12717abc
Author: Brian C. Lane <bcl@brianlane.com>
Date: Wed, 3 Nov 2010 22:40:32 -0700
Handle invalid server entries and bad directories
At startup the server and top level directory listing are checked before
allowing HMS to run. I will loop until a valid server is entered and
it serves up a parsable directory listing.
Diffstat:
5 files changed, 75 insertions(+), 23 deletions(-)
diff --git a/HMS/source/appDisplayDirectory.brs b/HMS/source/appDisplayDirectory.brs
@@ -23,7 +23,6 @@ Function displayDirectory( url As String ) As Object
' Get the directory listing
dir = getDirectoryListing(url)
- print "got listing"
if dir = invalid then
print "Failed to get directory listing for";url
return invalid
diff --git a/HMS/source/appMain.brs b/HMS/source/appMain.brs
@@ -12,11 +12,17 @@ Sub Main()
return
end if
- if not checkServerUrl(false) then
- ' Need to show an error to the user here and exit when they click done
- print "Server URL is invalid"
- return
- endif
+ ' Get server url and make sure it is valid
+ valid_dir = false
+ force_edit = false
+ while not valid_dir
+ valid_dir = checkServerUrl(force_edit)
+ dir = getDirectoryListing( "http://"+RegRead("ServerURL") )
+ if dir = invalid then
+ force_edit = true
+ valid_dir = false
+ end if
+ end while
path = []
diff --git a/HMS/source/checkServerUrl.brs b/HMS/source/checkServerUrl.brs
@@ -10,19 +10,19 @@
' ** found and write it to the registry.
'************************************************************
Function checkServerUrl(forceEdit As Boolean) As Boolean
- serverURL = RegRead("ServerURL")
- if (serverURL = invalid) then
- print "ServerURL not found in the registry"
- serverURL = "video.local"
- else if not forceEdit then
- print "Server set to "; serverURL
+ serverUrl = RegRead("ServerURL")
+ if (serverUrl = invalid) then
+ print "ServerUrl not found in the registry"
+ serverUrl = "video.local"
+ else if not forceEdit and isUrlValid(serverUrl) then
+ print "Server set to "; serverUrl
return true
- endif
+ end if
screen = CreateObject("roKeyboardScreen")
port = CreateObject("roMessagePort")
screen.SetMessagePort(port)
- screen.SetTitle("Video Server URL")
+ screen.SetTitle("HMS Video Server URL")
screen.SetText(serverURL)
screen.SetDisplayText("Enter Host Name or IP Address")
screen.SetMaxLength(25)
@@ -38,13 +38,24 @@ Function checkServerUrl(forceEdit As Boolean) As Boolean
else if msg.isButtonPressed() then
print "Evt: ";msg.GetMessage();" idx:"; msg.GetIndex()
if msg.GetIndex() = 1 then
- searchText = screen.GetText()
- print "Server set to "; searchText
- RegWrite("ServerURL", searchText)
- return true
+ serverText = screen.GetText()
+ print "Server set to "; serverText
+
+ if isUrlValid(serverText) then
+ RegWrite("ServerURL", serverText)
+ return true
+ end if
endif
endif
endif
end while
End Function
+'************************************************************
+'** Check a URL to see if it is valid
+'************************************************************
+Function isUrlValid( url As String ) As Boolean
+ result = getHTMLWithTimeout(url, 60)
+ return not result.error
+End Function
+
diff --git a/HMS/source/getDirectoryListing.brs b/HMS/source/getDirectoryListing.brs
@@ -3,14 +3,13 @@
' ** Copyright (c) 2010 Brian C. Lane All Rights Reserved.
' ********************************************************************
Function getDirectoryListing(url As String) As Object
- http = CreateObject("roUrlTransfer")
- http.SetUrl(url)
- dir = http.GetToString()
+ result = getHTMLWithTimeout(url, 60)
- if dir = invalid then
- print "Could not get directory listing"
+ if result.error then
+ print "ERROR: Could not get directory listing"
return invalid
end if
+ dir = result.str
' Try parsing the html as if it is XML
xml=CreateObject("roXMLElement")
diff --git a/HMS/source/urlUtils.brs b/HMS/source/urlUtils.brs
@@ -213,3 +213,40 @@ Function http_post_from_string_with_timeout(val As String, seconds as Integer) a
return str
End Function
+
+' ********************************************************************
+' ** Get a HTML page with timeout. Gather up error info and return it
+' ********************************************************************
+Function getHTMLWithTimeout(url As String, seconds As Integer) as Object
+ timeout% = 1000 * seconds
+
+ result = { str : "", error : false, response : 0, reason : "" }
+
+ http = CreateObject("roUrlTransfer")
+ http.SetUrl(url)
+ http.SetPort(CreateObject("roMessagePort"))
+ http.EnableFreshConnection(true) 'Don't reuse existing connections
+ if (http.AsyncGetToString())
+ event = wait(timeout%, http.GetPort())
+ if type(event) = "roUrlEvent"
+ if event.GetResponseCode() = 200 then
+ result.str = event.GetString()
+ else
+ result.error = true
+ result.response = event.GetResponseCode()
+ result.reason = event.GetFailureReason()
+ end if
+ elseif event = invalid
+ Dbg("AsyncGetToString timeout")
+ htp.AsyncCancel()
+ else
+ Dbg("AsyncGetToString unknown event", event)
+ endif
+ endif
+
+ print "HTTP result: "
+ print result
+
+ return result
+End Function
+