# Path: jmbase/jmbase/support.py
# def get_log():
#     """
#     provides joinmarket logging instance
#     :return: log instance
#     """
#     return log
# 
# Path: jmclient/jmclient/configure.py
# def load_test_config(**kwargs):
#     if "config_path" not in kwargs:
#         load_program_config(config_path=".", **kwargs)
#     else:
#         load_program_config(**kwargs)
# 
# def jm_single():
#     return global_singleton
# 
# def get_irc_mchannels():
#     SECTION_NAME = 'MESSAGING'
#     # FIXME: remove in future release
#     if jm_single().config.has_section(SECTION_NAME):
#         log.warning("Old IRC configuration detected. Please adopt your "
#                     "joinmarket.cfg as documented in 'docs/config-irc-"
#                     "update.md'. Support for the old setting will be removed "
#                     "in a future version.")
#         return _get_irc_mchannels_old()
# 
#     SECTION_NAME += ':'
#     irc_sections = []
#     for s in jm_single().config.sections():
#         if s.startswith(SECTION_NAME):
#             irc_sections.append(s)
#     assert irc_sections
# 
#     req_fields = [("host", str), ("port", int), ("channel", str), ("usessl", str)]
# 
#     configs = []
#     for section in irc_sections:
#         server_data = {}
# 
#         # check if socks5 is enabled for tor and load relevant config if so
#         try:
#             server_data["socks5"] = jm_single().config.get(section, "socks5")
#         except NoOptionError:
#             server_data["socks5"] = "false"
#         if server_data["socks5"].lower() == 'true':
#             server_data["socks5_host"] = jm_single().config.get(section, "socks5_host")
#             server_data["socks5_port"] = jm_single().config.get(section, "socks5_port")
# 
#         for option, otype in req_fields:
#             val = jm_single().config.get(section, option)
#             server_data[option] = otype(val)
#         server_data['btcnet'] = get_network()
#         configs.append(server_data)
#     return configs


from jmdaemon import MessageChannelCollection
from jmdaemon.orderbookwatch import OrderbookWatch
from jmdaemon.daemon_protocol import JMDaemonServerProtocol
from jmdaemon.protocol import NICK_HASH_LENGTH, NICK_MAX_ENCODED, JM_VERSION,\
    JOINMARKET_NICK_HEADER
from jmbase import get_log
from jmclient import (load_test_config, jm_single, get_irc_mchannels)
from twisted.python.log import msg as tmsg
from twisted.python.log import startLogging
from twisted.internet import protocol, reactor, task
from twisted.internet.protocol import ServerFactory
from twisted.internet.error import (ConnectionLost, ConnectionAborted,
                                    ConnectionClosed, ConnectionDone)
from twisted.protocols.amp import UnknownRemoteError
from twisted.protocols import amp
from twisted.trial import unittest
from jmbase.commands import *
from msgdata import *
from dummy_mc import DummyMessageChannel
import base64
import sys

class JMDaemonTest2ServerProtocolFactory(ServerFactory):
    protocol = JMDaemonTest2ServerProtocol
    def buildProtocol(self, addr):
        return JMDaemonTest2ServerProtocol(self)

class TrialTestJMDaemonProto(unittest.TestCase):

    def setUp(self):
        startLogging(sys.stdout)
        load_test_config()
        jm_single().maker_timeout_sec = 1
        self.port = reactor.listenTCP(28184, JMDaemonTestServerProtocolFactory())
        self.addCleanup(self.port.stopListening)
        clientconn = reactor.connectTCP("localhost", 28184,
                                        JMTestClientProtocolFactory())
        self.addCleanup(clientconn.disconnect)

    def test_waiter(self):
        return task.deferLater(reactor, 12, self._called_by_deffered)

    def _called_by_deffered(self):
        pass


class TestJMDaemonProtoInit(unittest.TestCase):

    def setUp(self):
        global end_early
        end_early = True

# Path: jmbase/jmbase/support.py
# def get_log():
#     """
#     provides joinmarket logging instance
#     :return: log instance
#     """
#     return log
# 
# Path: jmclient/jmclient/configure.py
# def load_test_config(**kwargs):
#     if "config_path" not in kwargs:
#         load_program_config(config_path=".", **kwargs)
#     else:
#         load_program_config(**kwargs)
# 
# def jm_single():
#     return global_singleton
# 
# def get_irc_mchannels():
#     SECTION_NAME = 'MESSAGING'
#     # FIXME: remove in future release
#     if jm_single().config.has_section(SECTION_NAME):
#         log.warning("Old IRC configuration detected. Please adopt your "
#                     "joinmarket.cfg as documented in 'docs/config-irc-"
#                     "update.md'. Support for the old setting will be removed "
#                     "in a future version.")
#         return _get_irc_mchannels_old()
# 
#     SECTION_NAME += ':'
#     irc_sections = []
#     for s in jm_single().config.sections():
#         if s.startswith(SECTION_NAME):
#             irc_sections.append(s)
#     assert irc_sections
# 
#     req_fields = [("host", str), ("port", int), ("channel", str), ("usessl", str)]
# 
#     configs = []
#     for section in irc_sections:
#         server_data = {}
# 
#         # check if socks5 is enabled for tor and load relevant config if so
#         try:
#             server_data["socks5"] = jm_single().config.get(section, "socks5")
#         except NoOptionError:
#             server_data["socks5"] = "false"
#         if server_data["socks5"].lower() == 'true':
#             server_data["socks5_host"] = jm_single().config.get(section, "socks5_host")
#             server_data["socks5_port"] = jm_single().config.get(section, "socks5_port")
# 
#         for option, otype in req_fields:
#             val = jm_single().config.get(section, option)
#             server_data[option] = otype(val)
#         server_data['btcnet'] = get_network()
#         configs.append(server_data)
#     return configs


# Path: jmdaemon/test/test_daemon_protocol.py
from jmdaemon import MessageChannelCollection
from jmdaemon.orderbookwatch import OrderbookWatch
from jmdaemon.daemon_protocol import JMDaemonServerProtocol
from jmdaemon.protocol import NICK_HASH_LENGTH, NICK_MAX_ENCODED, JM_VERSION,\
    JOINMARKET_NICK_HEADER
from jmbase import get_log
from jmclient import (load_test_config, jm_single, get_irc_mchannels)
from twisted.python.log import msg as tmsg
from twisted.python.log import startLogging
from twisted.internet import protocol, reactor, task
from twisted.internet.protocol import ServerFactory
from twisted.internet.error import (ConnectionLost, ConnectionAborted,
                                    ConnectionClosed, ConnectionDone)
from twisted.protocols.amp import UnknownRemoteError
from twisted.protocols import amp
from twisted.trial import unittest
from jmbase.commands import *
from msgdata import *
from dummy_mc import DummyMessageChannel
import base64
import sys


class JMDaemonTest2ServerProtocolFactory(ServerFactory):
    protocol = JMDaemonTest2ServerProtocol
    def buildProtocol(self, addr):
        return JMDaemonTest2ServerProtocol(self)

class TrialTestJMDaemonProto(unittest.TestCase):

    def setUp(self):
        startLogging(sys.stdout)
        load_test_config()
        jm_single().maker_timeout_sec = 1
        self.port = reactor.listenTCP(28184, JMDaemonTestServerProtocolFactory())
        self.addCleanup(self.port.stopListening)
        clientconn = reactor.connectTCP("localhost", 28184,
                                        JMTestClientProtocolFactory())
        self.addCleanup(clientconn.disconnect)

    def test_waiter(self):
        return task.deferLater(reactor, 12, self._called_by_deffered)

    def _called_by_deffered(self):
        pass


class TestJMDaemonProtoInit(unittest.TestCase):

    def setUp(self):
        global end_early
        end_early = True

        load_test_config()