# Path: pygowave_rpc/amqp_client.py
# class AmqpMessageProcessor(object):
# 	purge_every = datetime.timedelta(minutes=10)
# 	conn_lifetime = datetime.timedelta(minutes=getattr(settings, "ACCESS_KEY_TIMEOUT_MINUTES", 2))
# 	
# 	def __init__(self, connection):
# 		self.pygo_mp = PyGoWaveClientMessageProcessor()
# 		self.consumer = Consumer(
# 			connection,
# 			queue="wavelet_rpc_singlethread",
# 			exchange="wavelet.topic",
# 			routing_key="#.#.clientop",
# 			exchange_type="topic",
# 			serializer="json",
# 			auto_ack=True,
# 		)
# 		self.consumer.register_callback(self.receive)
# 		self.publisher = Publisher(
# 			connection,
# 			exchange="wavelet.direct",
# 			exchange_type="direct",
# 			delivery_mode=1,
# 			serializer="json",
# 		)
# 		
# 		self.pygo_mp.purge_connections()
# 		self.next_purge = datetime.datetime.now() + self.purge_every
# 	
# 	def wait(self, limit=None):
# 		self.consumer.wait(limit)
# 	
# 	def send(self, routing_key, message_data):
# 		self.publisher.send(message_data, routing_key=routing_key, delivery_mode=1)
# 	
# 	def receive(self, message_data, message):
# 		routing_key = message.amqp_message.routing_key
# 		
# 		msg_dict = self.pygo_mp.process(routing_key, message_data)
# 		
# 		for out_rkey, messages in msg_dict.iteritems():
# 			self.send(out_rkey, messages)
# 		
# 		# Cleanup time?
# 		if datetime.datetime.now() > self.next_purge:
# 			self.pygo_mp.purge_connections()
# 			self.next_purge = datetime.datetime.now() + self.purge_every


import sys, os, logging
import pygowave_rpc.logger
import signal
	import traceback
from carrot.connection import AMQPConnection
from pygowave_rpc.amqp_client import AmqpMessageProcessor
from django.conf import settings
# through RabbitMQ. This may be slightly faster than it's twisted-based cousin.


sys.path.append(os.path.dirname(__file__))
if not os.environ.has_key("DJANGO_SETTINGS_MODULE"):
	os.environ["DJANGO_SETTINGS_MODULE"] = "settings"




logger = pygowave_rpc.logger.setupLogging()

if pygowave_rpc.logger.logMode() == "verbose":
	amqplogger = logging.getLogger("amqplib")
	amqplogger.setLevel(logging.DEBUG)
	amqplogger.addHandler(logging.StreamHandler())

logger.info("=> PyGoWave RPC Server starting <=")

# Python Ctrl-C handler
signal.signal(signal.SIGINT, signal.SIG_DFL)

try:
	amqpconn = AMQPConnection(
		hostname=getattr(settings, "RPC_SERVER", "localhost"),
		userid=getattr(settings, "RPC_USER", "pygowave_server"),
		password=getattr(settings, "RPC_PASSWORD", "pygowave_server"),
		virtual_host=getattr(settings, "AMQP_VHOST", "/"),
		port=getattr(settings, "AMQP_PORT", 5672)
	)

# Path: pygowave_rpc/amqp_client.py
# class AmqpMessageProcessor(object):
# 	purge_every = datetime.timedelta(minutes=10)
# 	conn_lifetime = datetime.timedelta(minutes=getattr(settings, "ACCESS_KEY_TIMEOUT_MINUTES", 2))
# 	
# 	def __init__(self, connection):
# 		self.pygo_mp = PyGoWaveClientMessageProcessor()
# 		self.consumer = Consumer(
# 			connection,
# 			queue="wavelet_rpc_singlethread",
# 			exchange="wavelet.topic",
# 			routing_key="#.#.clientop",
# 			exchange_type="topic",
# 			serializer="json",
# 			auto_ack=True,
# 		)
# 		self.consumer.register_callback(self.receive)
# 		self.publisher = Publisher(
# 			connection,
# 			exchange="wavelet.direct",
# 			exchange_type="direct",
# 			delivery_mode=1,
# 			serializer="json",
# 		)
# 		
# 		self.pygo_mp.purge_connections()
# 		self.next_purge = datetime.datetime.now() + self.purge_every
# 	
# 	def wait(self, limit=None):
# 		self.consumer.wait(limit)
# 	
# 	def send(self, routing_key, message_data):
# 		self.publisher.send(message_data, routing_key=routing_key, delivery_mode=1)
# 	
# 	def receive(self, message_data, message):
# 		routing_key = message.amqp_message.routing_key
# 		
# 		msg_dict = self.pygo_mp.process(routing_key, message_data)
# 		
# 		for out_rkey, messages in msg_dict.iteritems():
# 			self.send(out_rkey, messages)
# 		
# 		# Cleanup time?
# 		if datetime.datetime.now() > self.next_purge:
# 			self.pygo_mp.purge_connections()
# 			self.next_purge = datetime.datetime.now() + self.purge_every


# Path: amqp_rpc_server.py
import sys, os, logging
import pygowave_rpc.logger
import signal
	import traceback
from carrot.connection import AMQPConnection
from pygowave_rpc.amqp_client import AmqpMessageProcessor
from django.conf import settings

# through RabbitMQ. This may be slightly faster than it's twisted-based cousin.


sys.path.append(os.path.dirname(__file__))
if not os.environ.has_key("DJANGO_SETTINGS_MODULE"):
	os.environ["DJANGO_SETTINGS_MODULE"] = "settings"




logger = pygowave_rpc.logger.setupLogging()

if pygowave_rpc.logger.logMode() == "verbose":
	amqplogger = logging.getLogger("amqplib")
	amqplogger.setLevel(logging.DEBUG)
	amqplogger.addHandler(logging.StreamHandler())

logger.info("=> PyGoWave RPC Server starting <=")

# Python Ctrl-C handler
signal.signal(signal.SIGINT, signal.SIG_DFL)

try:
	amqpconn = AMQPConnection(
		hostname=getattr(settings, "RPC_SERVER", "localhost"),
		userid=getattr(settings, "RPC_USER", "pygowave_server"),
		password=getattr(settings, "RPC_PASSWORD", "pygowave_server"),
		virtual_host=getattr(settings, "AMQP_VHOST", "/"),
		port=getattr(settings, "AMQP_PORT", 5672)
	)

	omc = AmqpMessageProcessor(amqpconn)