# Path: guild/components.py
# def Backplane(name):
#     plane = Splitter()
#     guild.register(name, plane)
#     return plane
# 
# def PublishTo(name):
#     splitter = guild.lookup(name)
#     return splitter
# 
# class SubscribeTo(Actor):
#     def __init__(self, name):
#         super(SubscribeTo, self).__init__()
#         self.name = name
# 
#     def process_start(self):
#         tries = 0
#         while True:
#             splitter = guild.lookup(self.name)
#             if splitter is None:
#                 tries += 1
#                 time.sleep(0.01)
#                 if tries > 100:
#                     raise Exception("BUST")
#             else:
#                 break
#         splitter.subscribe(self.input)
# 
#     @actor_method
#     def input(self, value):
#         self.output(value)
# 
#     @late_bind_safe
#     def output(self, value):
#         pass
# 
# class Printer(Actor):
#     @actor_method
#     def input(self, line):
#         if isinstance(line, str):
#             sys.stdout.write(line)
#         else:
#             sys.stdout.write(" ".join([str(x) for x in line]))
#         sys.stdout.write("\n")
#         sys.stdout.flush()
# 
#     @late_bind_safe
#     def output(self, line):
#         print("unbound, odd,", line)


import time
from guild.actor import *
from guild.components import Backplane, PublishTo, SubscribeTo, Printer




class Producer(Actor):
    @process_method
    def process(self):
        self.output("hello")

    @late_bind_safe
    def output(self, value):
        pass


Backplane("HELLO").start()
p = Producer()
pr = Printer()

time.sleep(1)

pub = PublishTo("HELLO")
sub = SubscribeTo("HELLO")

print "pub", pub, repr(pub), pub.input

pipeline(p, pub)
pipeline(sub, pr)

start(p, pr, sub)


# Path: guild/components.py
# def Backplane(name):
#     plane = Splitter()
#     guild.register(name, plane)
#     return plane
# 
# def PublishTo(name):
#     splitter = guild.lookup(name)
#     return splitter
# 
# class SubscribeTo(Actor):
#     def __init__(self, name):
#         super(SubscribeTo, self).__init__()
#         self.name = name
# 
#     def process_start(self):
#         tries = 0
#         while True:
#             splitter = guild.lookup(self.name)
#             if splitter is None:
#                 tries += 1
#                 time.sleep(0.01)
#                 if tries > 100:
#                     raise Exception("BUST")
#             else:
#                 break
#         splitter.subscribe(self.input)
# 
#     @actor_method
#     def input(self, value):
#         self.output(value)
# 
#     @late_bind_safe
#     def output(self, value):
#         pass
# 
# class Printer(Actor):
#     @actor_method
#     def input(self, line):
#         if isinstance(line, str):
#             sys.stdout.write(line)
#         else:
#             sys.stdout.write(" ".join([str(x) for x in line]))
#         sys.stdout.write("\n")
#         sys.stdout.flush()
# 
#     @late_bind_safe
#     def output(self, line):
#         print("unbound, odd,", line)


# Path: examples/backplane_demo.py
import time
from guild.actor import *
from guild.components import Backplane, PublishTo, SubscribeTo, Printer





class Producer(Actor):
    @process_method
    def process(self):
        self.output("hello")

    @late_bind_safe
    def output(self, value):
        pass


Backplane("HELLO").start()
p = Producer()
pr = Printer()

time.sleep(1)

pub = PublishTo("HELLO")
sub = SubscribeTo("HELLO")

print "pub", pub, repr(pub), pub.input

pipeline(p, pub)
pipeline(sub, pr)

start(p, pr, sub)


time.sleep(1.0)