Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import bottle
- from bottle import route, run, Bottle, request
- import sys, os, re, urllib
- fcpHost = "127.0.0.1"
- import fcp
- jobs={}
- app = Bottle()
- class FreeWorkerFactory:
- job = None #A freenet job
- g = None
- body = None
- OutputType =None
- def __init__(job_or_uri,message=None,afile=None,fcpHost = "127.0.0.1",fcpPort= 9481,aBody=None,OutputType="HTML"):
- if type(job_or_uri) is str:
- if job_or_uri in jobs:
- self.job=jobs[job_or_uri]
- else:
- node = fcp.FCPNode(fcpHost,verbosity='fcp.DETAIL',port=fcpPort)
- val = raw_input(message)
- ksk = raw_input(job_or_uri)
- uri = "KSK@" + ksk
- node.put("KSK@"+ksk, data=val, mimetype="text/plain")
- self.job = node.get(uri, async=True)
- else:
- self.job=job_or_uri
- self.OutputType=OutputType
- if OutputType == "HTML":
- self.Brk="<br>"
- elif OutputType == "UNIX":
- self.Brk="" #We won't use "\n" since print automatically generates the \n. Write can be used instead if one wants to explicitly use the /n
- self.setBody(aBody)
- def freenetWorker(self):
- # Empty Callback: http://echochamber.me/viewtopic.php?t=64825
- #stop_me = lambda *_, **_: None
- def on_data(self):
- # we can poll the job
- while True:
- if job.isComplete():
- print "Yay! job complete"
- body.put(job.wait()) #Returns the completion message.
- break
- else:
- print "Waiting" + datetime.datetime.now()+self.Brk
- body.put("Waiting" + datetime.datetime.now()+self.Brk)
- gevent.sleep(0) #This yields control to another greenlet
- self.on_finish() #Maybe we can sequence the greenlets instead of putting this here.
- def on_finish():
- body.put('</body></html>') #There should be a way to seperate these last two lines from this method.
- body.put(StopIteration)
- def header():
- if OutputType == "HTML":
- if start_respone is not None:
- start_response('200 OK', [('Content-Type', 'text/html')])
- self.body.put(' ' * 1000)
- if OutputType == "HTML":
- self.body.put("<html><body><h1>Current Time:</h1>")
- def handle(self,environ=None, start_response=None,aBody=None):
- lambda a: self.start(self,enviorn,start_response)
- #g = Greenlet.spawn(on_data, body)
- def setBody(aBody=None):
- if aBody is None:
- if self.body is None:
- self.body = queue.Queue()
- self.g = Greenlet(self.on_data)
- else:
- self.body = aBody
- def start(self,environ=None, start_response=None,aBody=None): # Not sure
- #https://stackoverflow.com/questions/20824218/how-to-implement-someasyncworker-from-bottle-asynchronous-primer
- #g = Greenlet.spawn(current_time, lambda a: on_data(body))
- self.setBody(aBody)
- self.header()
- self.g.start()
- return self.freenetWorker
- @app.post('/KSK/insert')
- def rest_test():
- node = fcp.FCPNode(request.json.get('ip'),request.json.get('verbosity=fcp.DETAIL'))
- val = raw_input(request.json.get('message'))
- ksk = raw_input(request.json.get('ksk'))
- uri = "KSK@" + ksk
- print "Inserting %s, containing '%s'" % (uri, val)
- # do the put - note that 'data=' inserts a string directly
- # note too that mimetype is optional, defaulting to text/plain
- node.put("KSK@"+ksk, data=val, mimetype="text/plain")
- # ------------------------------------------
- # now, demonstrate asynchronous requests
- print "Launching asynchronous request"
- job = node.get(uri, async=True)
- jobs["KSK@"+ksk]=job
- worker = FreeWorkerFactory(job).freenetWorker()
- worker.start()
- return body
- app.run(host='localhost', port=8082, debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement