# Path: servicebus/parser.py
# class XmlRequestGenerator(object):
#     def __init__(self, configuration, category, service, message):
#         self.message = message
#         self.configuration = configuration
#         self.category = category
#         self.service = service
#         self.message = message
# 
#     def generate_id(self):
#         global ID_SEED
#         ID_SEED = ID_SEED + 1
#         return ID_SEED
# 
#     def generate_token(self):
#         return do_generate_token(self.configuration)
# 
#     def encode_params(self):
#         return json.dumps(self.message)
# 
#     def to_xml(self):
#         return MESSAGE_TEMPLATE % (
#             self.generate_id(),
#             self.generate_token(),
#             self.category, self.service, self.encode_params())
# 
# class XmlResponseParser(XmlParserHelper):
#     def parse(self, xml_doc):
#         try:
#             doc = parseString(xml_doc)
#             root = doc.childNodes[0]
#             rid = self.get_request_id(root)
#             message = self.get_message(root)
#             return (rid, message)
#         except Exception:
#             # if got any exception in parse return None
#             return None
# 
#     def get_request_id(self, root):
#         node = root.getElementsByTagName('id')[0]
#         return self.get_text(node)
# 
#     def get_message(self, root):
#         node = root.getElementsByTagName('message')[0]
#         return self.get_text(node)


import logging
from servicebus.parser import XmlRequestGenerator, XmlResponseParser

    def ping_all(self, target, timeout=3):
        callers = self.get_callers()
        total = len(callers)
        success = 0
        for caller in callers:
            try:
                caller.set_exchange(self.exchange_name)
                ret = caller.call(target, "PING", timeout)
                if ret == "PONG":
                    success += 1
            except Exception:
                pass
        return success, total

    def call(self, target, params, timeout=300, reverse=False):
        target, category, service = self.parse_target(target)
        caller = self.choose_caller(target, reverse)
        if caller is None:
            raise Exception("Cannot connect to %s" % target)
        req_msg = XmlRequestGenerator(self.configuration, category, service, params)
        ret = caller.call(target, req_msg.to_xml(), timeout)
        resp_parser = XmlResponseParser()
        return resp_parser.parse(ret)

    def send(self, target, params):
        target, category, service = self.parse_target(target)
        caller = self.choose_caller(target)
        if caller is None:
            raise Exception("Cannot connect to %s" % target)

# Path: servicebus/parser.py
# class XmlRequestGenerator(object):
#     def __init__(self, configuration, category, service, message):
#         self.message = message
#         self.configuration = configuration
#         self.category = category
#         self.service = service
#         self.message = message
# 
#     def generate_id(self):
#         global ID_SEED
#         ID_SEED = ID_SEED + 1
#         return ID_SEED
# 
#     def generate_token(self):
#         return do_generate_token(self.configuration)
# 
#     def encode_params(self):
#         return json.dumps(self.message)
# 
#     def to_xml(self):
#         return MESSAGE_TEMPLATE % (
#             self.generate_id(),
#             self.generate_token(),
#             self.category, self.service, self.encode_params())
# 
# class XmlResponseParser(XmlParserHelper):
#     def parse(self, xml_doc):
#         try:
#             doc = parseString(xml_doc)
#             root = doc.childNodes[0]
#             rid = self.get_request_id(root)
#             message = self.get_message(root)
#             return (rid, message)
#         except Exception:
#             # if got any exception in parse return None
#             return None
# 
#     def get_request_id(self, root):
#         node = root.getElementsByTagName('id')[0]
#         return self.get_text(node)
# 
#     def get_message(self, root):
#         node = root.getElementsByTagName('message')[0]
#         return self.get_text(node)


# Path: servicebus/sender.py
import logging
from servicebus.parser import XmlRequestGenerator, XmlResponseParser


    def ping_all(self, target, timeout=3):
        callers = self.get_callers()
        total = len(callers)
        success = 0
        for caller in callers:
            try:
                caller.set_exchange(self.exchange_name)
                ret = caller.call(target, "PING", timeout)
                if ret == "PONG":
                    success += 1
            except Exception:
                pass
        return success, total

    def call(self, target, params, timeout=300, reverse=False):
        target, category, service = self.parse_target(target)
        caller = self.choose_caller(target, reverse)
        if caller is None:
            raise Exception("Cannot connect to %s" % target)
        req_msg = XmlRequestGenerator(self.configuration, category, service, params)
        ret = caller.call(target, req_msg.to_xml(), timeout)
        resp_parser = XmlResponseParser()
        return resp_parser.parse(ret)

    def send(self, target, params):
        target, category, service = self.parse_target(target)
        caller = self.choose_caller(target)
        if caller is None:
            raise Exception("Cannot connect to %s" % target)

        req_msg = XmlRequestGenerator(self.configuration, category, service, params)