
__author__ = 'Miroslav Bauer'

import time
import simplejson as j
from job           import Job, JobParam
from radical.utils import signatures as sig


class RequestAPI:
	''' Base class containing methods for handling
	    requests to application.
	'''

	@sig.takes('RequestAPI', str)
	@sig.returns(str)
	def ping(self, caller):
		time.sleep(10)
		return 'PONG!'

	@sig.takes('RequestAPI', str, str)
	@sig.returns(str)
	def echo(self, caller, message):
		return message

	@sig.takes('RequestAPI', str, int, Job)
	@sig.returns(sig.nothing)
	def compute(self, caller, jobId, job):
		pass

	@sig.takes('RequestAPI', str, int, sig.anything)
	@sig.returns(list)
	def results(self, caller, jobId, result):
		return ['RESULTS', j.dumps({'jobID':jobId, 'results':result})]

	@sig.takes('RequestAPI', str, int, str)
	@sig.returns(list)
	def notify(self, caller, jid, state):
		return ['NOTIFY', j.dumps({'jobID':str(jid), 'state':state})]
	
	@sig.takes('RequestAPI', str, str, str)
	@sig.returns(list)
	def error(self, caller, eType, eMsg, jid=''):
		return ['ERROR', j.dumps({'errType':eType, 'errMsg':eMsg, 'jobID':str(jid)})]
	
	@sig.takes('RequestAPI', str, list)
	def reloadagents(self, caller, resources):
		pass
	
	@sig.takes('RequestAPI', str)
	@sig.returns(list)
	def getagents(self, caller):
		return ['AGENTS', '[]']
