# Path: guild/network.py
# class ServerCore(Actor):
#     server_options = { "host" : None,
#                        "port" : None,
#                        "socketOptions" : None,
#                        "maxlisten" : None }
#     base_server = TCPServer
#     protocol_class = None
#     def __init__(self, server_options=None, protocol_class=None, base_server=None):
#         super(ServerCore, self).__init__()
#         self.active_connections = {}
#         self.tcpserver = None
#         if server_options is not None:
#             options = {}
#             options.update(self.server_options)
#             options.update(server_options)
#             self.server_options = options
# 
#         if protocol_class is not None:
#             self.protocol_class = protocol_class
#         if base_server is not None:
#             self.base_server = base_server
# 
#     @actor_method
#     def new_connection(self, raw_connection_handler):
# 
#         rch = raw_connection_handler
#         protocol_handler = (self.protocol_class)()
# 
#         rch.bind("from_connection", protocol_handler , "input")
#         protocol_handler.bind("output", rch, "to_connection")
# 
#         self.active_connections[rch] = protocol_handler
# 
#         protocol_handler.start()
#         rch.start()
# 
#     @actor_method
#     def disconnection(self, raw_connection_handler):
# 
#         rch = raw_connection_handler
# 
#         rch.stop()
#         protocol_handler = self.active_connections[rch]
#         del self.active_connections[rch]
# 
#         protocol_handler.stop()
# 
#     def process_start(self):
#         self.tcpserver = (self.base_server)(**self.server_options)
# 
#         self.tcpserver.bind("wire_up_new_connection",               self, "new_connection")
#         self.tcpserver.bind("notify_protocol_on_client_disconnect", self, "disconnection")
# 
#         start(self.tcpserver)
# 
#     def onStop(self):
#         Print("Next: STOP TCP SERVER", self.tcpserver)
#         stop(self.tcpserver)
# 
#         Print("Next: WAITFOR TCP SERVER", self.tcpserver)
#         wait_for(self.tcpserver)
# 
# def stop_selector():
#     stop(selector)
# 
# def waitfor_selector():
#     wait_for(selector)


from guild.actor import *
from guild.network import ServerCore, stop_selector, waitfor_selector
#!/usr/bin/python



class EchoServer(ServerCore):
    server_options = { "port" : 1602 }
    class protocol_class(Actor):
        @late_bind_safe
        def output(self, chunk):
            pass

        @actor_method
        def input(self, chunk):
            self.output(chunk)


echo_server = EchoServer()

echo_server.start()


# Path: guild/network.py
# class ServerCore(Actor):
#     server_options = { "host" : None,
#                        "port" : None,
#                        "socketOptions" : None,
#                        "maxlisten" : None }
#     base_server = TCPServer
#     protocol_class = None
#     def __init__(self, server_options=None, protocol_class=None, base_server=None):
#         super(ServerCore, self).__init__()
#         self.active_connections = {}
#         self.tcpserver = None
#         if server_options is not None:
#             options = {}
#             options.update(self.server_options)
#             options.update(server_options)
#             self.server_options = options
# 
#         if protocol_class is not None:
#             self.protocol_class = protocol_class
#         if base_server is not None:
#             self.base_server = base_server
# 
#     @actor_method
#     def new_connection(self, raw_connection_handler):
# 
#         rch = raw_connection_handler
#         protocol_handler = (self.protocol_class)()
# 
#         rch.bind("from_connection", protocol_handler , "input")
#         protocol_handler.bind("output", rch, "to_connection")
# 
#         self.active_connections[rch] = protocol_handler
# 
#         protocol_handler.start()
#         rch.start()
# 
#     @actor_method
#     def disconnection(self, raw_connection_handler):
# 
#         rch = raw_connection_handler
# 
#         rch.stop()
#         protocol_handler = self.active_connections[rch]
#         del self.active_connections[rch]
# 
#         protocol_handler.stop()
# 
#     def process_start(self):
#         self.tcpserver = (self.base_server)(**self.server_options)
# 
#         self.tcpserver.bind("wire_up_new_connection",               self, "new_connection")
#         self.tcpserver.bind("notify_protocol_on_client_disconnect", self, "disconnection")
# 
#         start(self.tcpserver)
# 
#     def onStop(self):
#         Print("Next: STOP TCP SERVER", self.tcpserver)
#         stop(self.tcpserver)
# 
#         Print("Next: WAITFOR TCP SERVER", self.tcpserver)
#         wait_for(self.tcpserver)
# 
# def stop_selector():
#     stop(selector)
# 
# def waitfor_selector():
#     wait_for(selector)


# Path: examples/tcp_servers/echo_server_declarative.py
from guild.actor import *
from guild.network import ServerCore, stop_selector, waitfor_selector

#!/usr/bin/python



class EchoServer(ServerCore):
    server_options = { "port" : 1602 }
    class protocol_class(Actor):
        @late_bind_safe
        def output(self, chunk):
            pass

        @actor_method
        def input(self, chunk):
            self.output(chunk)


echo_server = EchoServer()

echo_server.start()


wait_KeyboardInterrupt()