# Path: conpot/protocols/IEC104/errors.py
# class Timeout_t3(BaseException):
#     """Base class for exceptions in this module."""
# 
# Path: conpot/core/protocol_wrapper.py
# def conpot_protocol(cls):
#     class Wrapper(object):
#         def __init__(self, *args, **kwargs):
#             self.wrapped = cls(*args, **kwargs)
#             self.cls = cls
#             if self.cls.__name__ not in "Proxy":
#                 if self.cls not in core_interface.protocols:
#                     core_interface.protocols[self.cls] = []
# 
#                 core_interface.protocols[self.cls].append(self.wrapped)
# 
#             self.__class__.__name__ = self.cls.__name__
# 
#         def __getattr__(self, name):
#             if name == "handle":
#                 # assuming that handle function from a class is only called when a client tries to connect with an
#                 # enabled protocol, update the last_active (last_attacked attribute)
#                 # FIXME: No handle function in HTTPServer
#                 core_interface.last_active = datetime.now().strftime(
#                     "%b %d %Y - %H:%M:%S"
#                 )
#             return self.wrapped.__getattribute__(name)
# 
#         def __repr__(self):
#             return self.wrapped.__repr__()
# 
#         __doc__ = cls.__doc__
#         __module__ = cls.__module__
# 
#     return Wrapper


from conpot.protocols.IEC104.DeviceDataController import DeviceDataController
from conpot.protocols.IEC104.IEC104 import IEC104
from .frames import struct, TESTFR_act, socket, errno
from gevent.server import StreamServer
from .errors import Timeout_t3
from conpot.core.protocol_wrapper import conpot_protocol
import logging
import conpot.core as conpot_core
import gevent
@conpot_protocol
class IEC104Server(object):
    def __init__(self, template, template_directory, args):
        self.timeout = conpot_core.get_databus().get_value("T_0")
        self.device_data_controller = DeviceDataController(template)
        self.server_active = True
        self.server = None
        logger.info("IEC 104 Server up")
        self.template = template

    def handle(self, sock, address):
        sock.settimeout(self.timeout)
        session = conpot_core.get_session(
            "IEC104",
            address[0],
            address[1],
            sock.getsockname()[0],
            sock.getsockname()[1],
        )
        logger.info(
            "New IEC 104 connection from %s:%s. (%s)",
            address[0],
            address[1],
            session.id,
        )
        session.add_event({"type": "NEW_CONNECTION"})
        iec104_handler = IEC104(self.device_data_controller, sock, address, session.id)
        try:
            while True:
                timeout_t3 = gevent.Timeout(

# Path: conpot/protocols/IEC104/errors.py
# class Timeout_t3(BaseException):
#     """Base class for exceptions in this module."""
# 
# Path: conpot/core/protocol_wrapper.py
# def conpot_protocol(cls):
#     class Wrapper(object):
#         def __init__(self, *args, **kwargs):
#             self.wrapped = cls(*args, **kwargs)
#             self.cls = cls
#             if self.cls.__name__ not in "Proxy":
#                 if self.cls not in core_interface.protocols:
#                     core_interface.protocols[self.cls] = []
# 
#                 core_interface.protocols[self.cls].append(self.wrapped)
# 
#             self.__class__.__name__ = self.cls.__name__
# 
#         def __getattr__(self, name):
#             if name == "handle":
#                 # assuming that handle function from a class is only called when a client tries to connect with an
#                 # enabled protocol, update the last_active (last_attacked attribute)
#                 # FIXME: No handle function in HTTPServer
#                 core_interface.last_active = datetime.now().strftime(
#                     "%b %d %Y - %H:%M:%S"
#                 )
#             return self.wrapped.__getattribute__(name)
# 
#         def __repr__(self):
#             return self.wrapped.__repr__()
# 
#         __doc__ = cls.__doc__
#         __module__ = cls.__module__
# 
#     return Wrapper


# Path: conpot/protocols/IEC104/IEC104_server.py
from conpot.protocols.IEC104.DeviceDataController import DeviceDataController
from conpot.protocols.IEC104.IEC104 import IEC104
from .frames import struct, TESTFR_act, socket, errno
from gevent.server import StreamServer
from .errors import Timeout_t3
from conpot.core.protocol_wrapper import conpot_protocol
import logging
import conpot.core as conpot_core
import gevent

@conpot_protocol
class IEC104Server(object):
    def __init__(self, template, template_directory, args):
        self.timeout = conpot_core.get_databus().get_value("T_0")
        self.device_data_controller = DeviceDataController(template)
        self.server_active = True
        self.server = None
        logger.info("IEC 104 Server up")
        self.template = template

    def handle(self, sock, address):
        sock.settimeout(self.timeout)
        session = conpot_core.get_session(
            "IEC104",
            address[0],
            address[1],
            sock.getsockname()[0],
            sock.getsockname()[1],
        )
        logger.info(
            "New IEC 104 connection from %s:%s. (%s)",
            address[0],
            address[1],
            session.id,
        )
        session.add_event({"type": "NEW_CONNECTION"})
        iec104_handler = IEC104(self.device_data_controller, sock, address, session.id)
        try:
            while True:
                timeout_t3 = gevent.Timeout(

                    conpot_core.get_databus().get_value("T_3"), Timeout_t3