--****************************************************************************
-- Software: NET CLASS
-- Version: 1.1
-- Date: 2011-09-14
-- Author: Valentin Schmidt
-- Contact: fluxus@freenet.de
-- License: Freeware
--
-- Usage:
--
-- props = [:]
-- props["url"] = member("URL").text
-- props["method"] = "post"
-- props["cb_handler"] = #netCallback
-- props["cb_target"] = me
-- props["vars"] = ["foo":"bar"]
-- pNet = script("NET").new(props)
--
--****************************************************************************
property pNetID
property pProps
property pTimeout
-- progressbar related
property pPbProps
----------------------------------------
-- props: url, vars, method, timeout, serverosstring, cb_handler, cb_target, file
-- progbar_props (optional): rate, ...
----------------------------------------
on new (me, props, progbar_props)
if voidP(props["vars"]) then props["vars"]=[:]
if voidP(props["method"]) then props["method"] ="get"
if voidP(props["serverosstring"]) then props["serverosstring"]="UNIX" -- "Win", "Mac"
if not voidP(props["timeout"]) then me.pTimeout = the milliseconds+props["timeout"]
if voidP(props["cb_target"]) then props["cb_target"]=_movie
if not voidP(progbar_props) then
if voidP(progbar_props["rate"]) then progbar_props["rate"]=200 -- in ms
pPbProps = progbar_props
pPbProps["timer"] = the milliseconds
pPbProps["n"] = 0
end if
me.pProps = props
case (props["method"]) of
"post":
me.pNetID = postNetText(props["url"], props["vars"], props["serverosstring"])
"get":
me.pNetID = getNetText(props["url"], props["vars"], props["serverosstring"])
"preload":
me.pNetID = preloadNetThing(props["url"])
"download":
me.pNetID = downloadNetThing(props["url"], props["file"])
-- D11.5+ only
"post_bytes":
me.pNetID = postNetBytearray(props["url"], props["vars"])
"get_bytes":
me.pNetID = getNetBytearray(props["url"], props["vars"], props["serverosstring"])
end case
(the actorList).add(me)
return me
end
----------------------------------------
-- HOOKS FOR PROGRESS BAR (overwrite, either directly or in child script!)
----------------------------------------
on updateProgBar (me, prog)
--
end
on stopProgBar (me)
--
end
----------------------------------------
--
----------------------------------------
on stepFrame me
progbar_flag = not voidP(pPbProps)
if progbar_flag then
if (the milliseconds-pPbProps["timer"]>pPbProps["rate"]) then
if ["preload","download"].getpos(pProps["method"]) then
info = getStreamStatus(me.pNetID)
if info.bytesTotal>0 then
me.updateProgBar(float(info.bytesSoFar)/info.bytesTotal)
end if
else
pPbProps["n"]=pPbProps["n"]+10
if pPbProps["n"]>100 then pPbProps["n"]=0
me.updateProgBar(pPbProps["n"]/100.0)
end if
pPbProps["timer"] = the milliseconds
end if
end if
if netDone(me.pNetID) then
res = EMPTY
if ["post", "get"].getPos(me.pProps["method"]) then
res = netTextResult(me.pNetID)
else if ["post_bytes", "get_bytes"].getPos(me.pProps["method"]) then
res = netBytearrayResult(me.pNetID)
end if
err = netError(me.pNetID)
errStr = me.returnErrorString(err)
(the actorList).deleteOne(me)
if progbar_flag then me.stopProgBar()
if not voidP(me.pProps["cb_handler"]) then call(me.pProps["cb_handler"], me.pProps["cb_target"], res, err, errStr, me.pProps)
else if not voidP(me.pTimeout) then
if the milliseconds>pTimeout then
(the actorList).deleteOne(me)
if progbar_flag then me.stopProgBar()
if not voidP(me.pProps["cb_handler"]) then call(me.pProps["cb_handler"], me.pProps["cb_target"], EMPTY, -1, "Timeout", me.pProps)
end if
end if
end
----------------------------------------
--Error codes http post
----------------------------------------
on returnErrorString me, errorCode
if errorCode = "OK" then return("")
case errorCode of
"OK": return "OK"
-128 : return "User cancelled."
0 : return "Everything is OK, peachy, and generally acceptable."
1 : return "Memory error."
4 : return "Bad MOA Class. Network Xtras may be improperly installed."
5 : return "Bad MOA Interface. Network Xtras may be improperly installed."
6 : return "Bad URL."
20 : return "Internal error. Browser detected a network or internal error."
42 : return "Response Header Field too long to be handled by NetLingo."
900 : return "File to be written to is read only."
903 : return "Disk is full."
905 : return "Bad URL."
2018 : return "postNetText fail."
4146 : return "Connection could not be established with the remote host."
4149 : return "Data supplied by the server was in an unexpected format."
4150 : return "Unexpected early closing of connection."
4152 : return "Data returned is truncated."
4154 : return "Operation could not be completed due to timeout."
4155 : return "Not enough memory available to complete the transaction."
4156 : return "Protocol reply to request indicates an error in the reply."
4157 : return "Transaction failed to be authenticated."
4159 : return "Invalid URL."
4164 : return "Could not create a socket."
4165 : return "Requested Object could not be found (URL may be incorrect)."
4166 : return "Generic proxy failure."
4167 : return "Transfer was intentionally interrupted by client."
4168 : return "Failed network operation."
4240 : return "The network xtras weren't initialized properly."
4242 : return "Download stopped by netAbort(url)."
4836 : return "Download stopped for an unknown reason."
otherwise :
if errorCode > 4143 AND errorCode < 4164 then
return "Failed network operation:" && errorCode
else
return "Unknown network error:" && errorCode
end if
end case
end