from ceilometer import publisher
from ceilometer.openstack.common.gettextutils import _
from ceilometer.openstack.common import log
from oslo.config import cfg
from time import sleep
import json
import zmq

LOG = log.getLogger(__name__)

OPTS = [
    cfg.StrOpt('publisher_zeromq_host',
               default='*'),
    cfg.IntOpt('publisher_zeromq_port',
               default=8282),
]

cfg.CONF.register_opts(OPTS)

class QueuePublisher(publisher.PublisherBase):
    """Republishes all received samples to a collector via ZeroMQ"""

    def __init__(self, parsed_url):
        super(QueuePublisher, self).__init__(parsed_url)
        self.context = None
        self.socket  = None
        self.reconnect()

    def reconnect(self):
        """(re)connects to the collector"""
        self.context = zmq.Context()
        self.socket  = self.context.socket(zmq.PUB)
        self.socket.bind("tcp://%s:%d" % (cfg.CONF.publisher_zeromq_host, cfg.CONF.publisher_zeromq_port))

    def publish_sample(self, message):
        """Attempt to publish a single sample"""
        try:
            self.socket.send("* " + message)
        except Exception as e:
            LOG.error("Error publishing sample: " + str(e))
            return False
        return True

    def publish_samples(self, context, samples):
        """
        Converts each sample into a string of JSON and publishes it the setup rabbit queue
        """
        for sample in samples:
            LOG.debug("Queue Publisher got sample")
            message = json.dumps(sample.as_dict())
            while not self.publish_sample(message):
                LOG.warning("Failed to publish message, sleeping 3 seconds then reconnecting")
                sleep(3)
                self.reconnect()
            LOG.debug(_("Queue Publisher published %s") % message)

@@ -157,31 +157,8 @@
 cfg%0A
-from time import sleep%0A
 impo
@@ -281,17 +281,25 @@
 efault='
-*
+127.0.0.1
 '),%0A    
@@ -550,16 +550,64 @@
 d_url):%0A
+        LOG.info(%22Queue Publisher starting up%22)%0A
         
@@ -894,11 +894,11 @@
 zmq.
-PUB
+REQ
 )%0A  
@@ -919,12 +919,15 @@
 ket.
-bind
+connect
 (%22tc
@@ -1098,25 +1098,8 @@
 %22%22%22%0A
-        try:%0A    
     
@@ -1123,15 +1123,8 @@
 end(
-%22* %22 + 
 mess
@@ -1141,115 +1141,137 @@
    e
-xcept Exception as e:%0A            LOG.error(%22Error publishing sample: %22 + str(e))%0A            return False%0A
+vents = self.socket.poll(1000)%0A        if events == 0:%0A            return False%0A        else:%0A            self.socket.recv()%0A    
     
@@ -1695,72 +1695,22 @@
 sage
-, sleeping 3 seconds then reconnecting%22)%0A                sleep(3
+ reconnecting%22
 )%0A  

use REQ instead of PUB