Skip to content

Instantly share code, notes, and snippets.

@braoru
Created September 30, 2016 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save braoru/691b1abb72b03ab1731f7b50aa49162c to your computer and use it in GitHub Desktop.
Save braoru/691b1abb72b03ab1731f7b50aa49162c to your computer and use it in GitHub Desktop.
#!/usr/bin/python -i
import logging
import time
from os import getpid
from circuits import Component, Event, Debugger
from circuits.node import Node, remote
# Logging
logging.basicConfig(
format='%(asctime)s %(name)s %(levelname)s %(process)d %(funcName)s() %(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p'
)
logger = logging.getLogger("test")
logger.setLevel(logging.DEBUG)
class hello(Event):
"""hello Event"""
class App(Component):
connection_status = False
def init(self):
Debugger().register(self)
def ready(self, client):
logger.info("App : Ready!")
def connected(self, host, port):
logger.info("App : Connected to {}:{}".format(host, port))
self.connection_status = True
# Work
##
hello_value = yield self.call(remote(hello(), "test"))
logger.info("App -> X : {hello_value}".format(hello_value=hello_value))
def hello(self):
return "App : Hello World from a node! ({0:d})".format(getpid())
class NodeServer(Component):
def init(self):
"""Initialize our ``ChatServer`` Component.
This uses the convenience ``init`` method which is called after the
component is proeprly constructed and initialized and passed the
same args and kwargs that were passed during construction.
"""
self.clients = {}
Debugger().register(self)
port = 9999
address = "0.0.0.0"
Node(port=port, server_ip=address).register(self)
logger.info("NodeServer : init done")
def connect(self, sock, host, port):
"""Connect Event -- Triggered for new connecting clients"""
logger.info("NodeServer client connect")
self.clients[sock] = {
"host": sock,
"port": port,
}
def disconnect(self, sock):
"""Disconnect Event -- Triggered for disconnecting clients"""
if sock not in self.clients:
return
del self.clients[sock]
def ready(self, server, bind):
logger.info("NodeServer : Ready! Listening on {}:{}".format(*bind))
logger.info("NodeServer : Waiting for remote events...")
def hello(self):
logger.info("NodeServer hello")
return "NodeServer : Hello World from server! ({0:d})".format(getpid())
if __name__ == "__main__":
# Server
##
serv = NodeServer()
serv.start(process=True)
time.sleep(2)
# Client
##
app = App()
node = Node().register(app)
bind = ("127.0.0.1", 9999)
node.add("test", *bind)
app.start(process=False)
while not app.connection_status:
time.sleep(1)
logger.info("waiting to start...")
logger.info("started")
#Don't work
##
x = app.fire(remote(hello(),"test"))
logger.info("Main -> X : {x}".format(x=x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment