# Path: ejtp/util/compat.py

# 
# Path: ejtp/applications/motd.py
# class MOTDServer(Client):
#     def __init__(self, router, interface, filename, message="", encryptor_cache=None, make_jack=True):
#         Client.__init__(self, router, interface, encryptor_cache, make_jack)
#         self.filename = filename
#         self.message  = message
# 
#     def rcv_callback(self, msg, client_obj):
#         sender = msg.sender
#         response = None
#         try:
#             with open(self.filename) as rfile:
#                 response = rfile.read()
#         except:
#             response = self.message
#         self.write_json(sender, {
#             'type':'motd-response',
#             'content': response,
#         })
# 
# class MOTDClient(Client):
#     def __init__(self, *args, **kwargs):
#         Client.__init__(self, *args, **kwargs)
#         self.callbacks = {}
# 
#     def request(self, remote, callback):
#         '''
#         Callbacks get called with arguments (frame msg, Client client)
#         '''
#         self.callbacks[str_address(remote)] = callback
#         self.write_json(py_address(remote), {
#             'type':'motd-request'
#         })
# 
#     def rcv_callback(self, msg, client_obj):
#         sender = msg.sender
#         strsender = str_address(sender)
#         if strsender not in self.callbacks:
#             logger.info("Message from stranger: %r" % sender)
#         self.callbacks[strsender](msg, self)


from ejtp.util.compat import unittest
from ejtp.applications.motd import MOTDServer, MOTDClient
        from ejtp.router import Router


class TestMotd(unittest.TestCase):

    def setUp(self):
        router = Router()
        server_interface = ['local', None, 'motdserver']
        client_interface = ['local', None, 'motdclient']
        self.server = MOTDServer(router, server_interface, "", "Example message")
        self.client = MOTDClient(router, client_interface)
        self.client.encryptor_cache = self.server.encryptor_cache
        self.client.encryptor_set(server_interface, ['rotate', 143])
        self.client.encryptor_set(client_interface, ['rotate', 222])

    def test_response(self):
        def assert_response(msg, c):
            self.assertEqual(msg.unpack()['content'], 'Example message')

# Path: ejtp/util/compat.py

# 
# Path: ejtp/applications/motd.py
# class MOTDServer(Client):
#     def __init__(self, router, interface, filename, message="", encryptor_cache=None, make_jack=True):
#         Client.__init__(self, router, interface, encryptor_cache, make_jack)
#         self.filename = filename
#         self.message  = message
# 
#     def rcv_callback(self, msg, client_obj):
#         sender = msg.sender
#         response = None
#         try:
#             with open(self.filename) as rfile:
#                 response = rfile.read()
#         except:
#             response = self.message
#         self.write_json(sender, {
#             'type':'motd-response',
#             'content': response,
#         })
# 
# class MOTDClient(Client):
#     def __init__(self, *args, **kwargs):
#         Client.__init__(self, *args, **kwargs)
#         self.callbacks = {}
# 
#     def request(self, remote, callback):
#         '''
#         Callbacks get called with arguments (frame msg, Client client)
#         '''
#         self.callbacks[str_address(remote)] = callback
#         self.write_json(py_address(remote), {
#             'type':'motd-request'
#         })
# 
#     def rcv_callback(self, msg, client_obj):
#         sender = msg.sender
#         strsender = str_address(sender)
#         if strsender not in self.callbacks:
#             logger.info("Message from stranger: %r" % sender)
#         self.callbacks[strsender](msg, self)


# Path: ejtp/tests/applications/test_motd.py
from ejtp.util.compat import unittest
from ejtp.applications.motd import MOTDServer, MOTDClient
        from ejtp.router import Router



class TestMotd(unittest.TestCase):

    def setUp(self):
        router = Router()
        server_interface = ['local', None, 'motdserver']
        client_interface = ['local', None, 'motdclient']
        self.server = MOTDServer(router, server_interface, "", "Example message")
        self.client = MOTDClient(router, client_interface)
        self.client.encryptor_cache = self.server.encryptor_cache
        self.client.encryptor_set(server_interface, ['rotate', 143])
        self.client.encryptor_set(client_interface, ['rotate', 222])

    def test_response(self):
        def assert_response(msg, c):
            self.assertEqual(msg.unpack()['content'], 'Example message')

        self.client.request(self.server.interface, assert_response)