#!/usr/bin/python
"""
Demo implementation of a xesam server. Run it like

	demo/xesam-dummy-server

And launch a search on it via

	./xesam-tool search -Phit.fields="xesam:title,xesam:url" hello

"""


import demo
import xesam
import xesam.query
from xesam.server import *
#!/usr/bin/python
"""
Demo implementation of a xesam server. Run it like

	demo/xesam-dummy-service [-s|--state-messages]

And launch a search on it via

	./xesam-tool search hello

You can use the -s or --state-messages switch to enable StateChanged
signal monitoring in xesam-tool as well as in xesam-dummy-service.
"""

from xesam.dummy import *


import gobject
import urllib
import xml.dom.minidom
import sys
from Queue import Queue
from Queue import Empty
import threading
from time import sleep


# Defalt number of hits before emitting SearchDone
MAX_HITS = 50

# Default number of hits before hits-added is emitted
HIT_CHUNK = 10

# Default time to sleep in between each hit chunk generation
CHUNK_DELAY = 2

class DummyServer (xesam.server.Searcher):
	"""
		
	"""
	
	def __init__ (self):
		h_fact = HandleFactory ()
		fact = ClientFactory (self, h_fact, DummySession, DummySearch)
		xesam.server.Searcher.__init__ (self, h_fact, fact)
		self.set_echo_queries (True)
		
		# The StateChanger manage our state
		self._state_changer = StateChanger(self)
	
	def start (self):
		if "-s" in sys.argv or "--state-messages" in sys.argv:
			xesam.debug ("State changing enabled")
			self._state_changer.start()
		else:
			xesam.debug ("State changing disabled. Enable with -s or --state-messages")
		
		# Export our selves via a SearchServerStub
		SearchServerStub(self).start()
	
	def GetProperty (self, shandle, name):
		prop = xesam.server.Searcher.GetProperty(self, shandle, name)
		xesam.debug ("Got property request for '%s' on session '%s', returning %s" % (name, shandle, prop))
		return prop
	
	def SetProperty (self, shandle, name, value):
		val = xesam.server.Searcher.SetProperty(self, shandle, name, value)
		xesam.debug ("Set property request for '%s=%s', on session '%s', returning %s" % (name, value, shandle,val))
		return val

class DummySession (Session):
	"""
	
	"""
	def __init__ (self, searcher, session_handle):
		Session.__init__ (self, searcher, session_handle)
		self.set_property ("vendor.id", "xesam-dummy-service")
		
class DummySearch (Search, threading.Thread):
	"""
		
	"""
	def __init__ (self, searcher, session, search_handle, query=None, xml=None) :
		Search.__init__ (self, searcher, session, search_handle, query=query, xml=xml)
		threading.Thread.__init__ (self)
		
		self._hit_fields = session.get_property (xesam.SESSION_HIT_FIELDS)
		if self._hit_fields is None:
			xesam.error ("Got property hit.fields as None. Setting default xesam:url")
			self._hit_fields = ["xesam:url"]
				
		xesam.debug ("Created %s with handle %s and query:\n%s" % (self.__class__, self.get_handle(), self.get_query()))
	
	def start (self):
		threading.Thread.start (self)
		xesam.debug ("%s started with handle %s" % (self.__class__, self.get_handle()))
	
	def get_hit_data_by_id (self, hit_ids, field_names):
		"""
		This method overrides the deault impl.
		"""
		
		# Update the cache to return UNSET for unset fields
		cache = self.get_hit_cache ()
		for hit_id in hit_ids:
			for field in field_names:
				if not cache.hit_has_field (hit_id, field):
					cache.put_data (hit_id, [field], ["UNSET"]) 
		
		return Search.get_hit_data_by_id (self, hit_ids, field_names)
	
	def run (self):
		hits = 0
		while hits < MAX_HITS:
			for i in range(HIT_CHUNK):
				data = ["field:" + str(j) for j in range(len(self._hit_fields))]
				self.add_new_hit (self._hit_fields, data)
				hits += 1
			self.emit ("hits-added", HIT_CHUNK)
			xesam.debug ("%s emitted %s hits" % (self.get_handle(), HIT_CHUNK))
		self.emit ("done")
		xesam.debug ("Search '%s' emitted 'done'" % self.get_handle())
		
		xesam.debug ("FIXME: linger around and spawn events if search.live")
			
			
if __name__ == "__main__":
	DummyServer().start()
	
