Created
March 5, 2014 06:44
-
-
Save runvnc/9362338 to your computer and use it in GitHub Desktop.
when it was a ttable[string, tsocket] didnt seem to find my ids
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import httpserver, asyncio, sockets, strutils, tables, oids, cgi, msgqredis, marshal, hashes | |
var myrespq = "" | |
type TQueuedRequest = object | |
responseQueue*:string | |
requestId*:string | |
path*: string | |
query*: string | |
type | |
TReqSockets = TTable[int, TSocket] | |
var reqSockets: TReqSockets = initTable[int, TSocket]() | |
proc uniqidstr():string = | |
var sid:string = $(genOid()) | |
sid.delete(sid.len-2, sid.len-1) | |
return sid | |
proc uniqid():int = | |
return hash(uniqidstr()) | |
proc newMessage(queue:string, msg:seq[string]) = | |
echo "got a new response message in asynchttp!" | |
echo("length of msg seq is " & $(msg.len)) | |
echo("first: " & msg[0]) | |
echo("second: " & msg[1]) | |
var sock = reqSockets[ParseInt(msg[0])] #msg.requestId] | |
if sock != nil: | |
sock.send(msg[1]) | |
sock.close() | |
else: | |
echo "Can't find socket with id " & msg[0] # & msg.requestId | |
echo "reqSockets is:" | |
for k, v in reqSockets.pairs: | |
echo("key is [" & $k & "] and val is " & $(v.getSockName())) | |
myrespq = uniqid() | |
msgqredis.init(newMessage, myrespq) | |
proc queueRequest(client:TSocket, path:string, query:string) = | |
# get unique id for this request | |
var reqid = uniqid() | |
echo("reqid is ----- " & $reqid) | |
var reqidstr: string = $reqid | |
var req = TQueuedRequest(responseQueue: myrespq, requestId: reqidstr, | |
path:path, query: query) | |
# push request data with unique id and list I am listening on | |
reqSockets[reqid] = client | |
echo("reqSockets[" & $reqid & "] sockname is " & $(client.getSockName())) | |
echo "queueing request" | |
var len = pushMessage("httpq", $$req) | |
proc handleRequest(server: PAsyncHTTPServer; client: TSocket; | |
path, query: string): bool = | |
try: | |
var query2 = URLdecode(query) | |
if path.startsWith("/static"): | |
serveFile(client, "." & path) | |
client.close() | |
else: | |
queueRequest(client, path, query2) | |
except: | |
echo("Exception in handler.") | |
finally: | |
try: | |
#client.close() | |
except: | |
echo("Error in close") | |
var disp = newDispatcher() | |
var http = asyncHTTPServer(handleRequest, TPort(3011)) | |
disp.register(http) | |
while true: | |
if not disp.poll(): | |
echo("HTTP server has been closed.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment