def __init__(self, host, portnumber, delay = 0, workers = 5, debug = 1): JsonRPCBase.__init__(self, workers = workers, debug = debug) self.host = host self.portnumber = portnumber self.delay = delay self.client = Graphline( TCPCLIENT = TCPClient(self.host, self.portnumber, self.delay), PROTOCOL = self.jsonprotocol(), linkages = { ('TCPCLIENT', 'outbox') : ('PROTOCOL', 'inbox'), ('PROTOCOL', 'outbox') : ('TCPCLIENT', 'inbox'), ('TCPCLIENT', 'signal') : ('PROTOCOL', 'control'), ('PROTOCOL', 'signal') : ('TCPCLIENT', 'control'), } ) self.handle = Handle(self.client)
# limitations under the License. from Axon.background import background from Axon.Handle import Handle from Kamaelia.Codec.Vorbis import VorbisDecode, AOAudioPlaybackAdaptor from Kamaelia.Chassis.Pipeline import Pipeline from Kamaelia.File.ReadFileAdaptor import ReadFileAdaptor from Kamaelia.Internet.TCPClient import TCPClient import time import Queue import ao background(slowmo=0.001).start() filename = "./snail.ogg" playStream = Handle(Pipeline(VorbisDecode(), AOAudioPlaybackAdaptor())).activate() # set of components for playing the stream back. host = "bbc.kamaelia.org" port = 1500 client = Handle(TCPClient(host = host, port = port)).activate() # component to grab a stream from the internet filedump = open("streamdump.ogg", "w+b") def get_item(handle): while 1: try: X = handle.get("outbox") return X
# you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ------------------------------------------------------------------------- from Axon.background import background from Axon.Handle import Handle from Kamaelia.Codec.Vorbis import VorbisDecode, AOAudioPlaybackAdaptor from Kamaelia.Chassis.Pipeline import Pipeline from Kamaelia.File.ReadFileAdaptor import ReadFileAdaptor import time import ao background(slowmo=0.001).start() filename = "../SupportingMediaFiles/KDE_Startup_2.ogg" playStream = Handle(Pipeline(VorbisDecode(), AOAudioPlaybackAdaptor())).activate() # Play the ogg data in the background oggdata = open(filename, "r+b").read() playStream.put(oggdata,"inbox") while True: time.sleep(0.1)
# requests on connect ##req = RequestOrNotification('long_calc', params = (100, 'progress_callback'), response_callback = ResponseCallback(callback_func = request_result)) ##client.add_request_on_connect(req, wait=False) ##req = RequestOrNotification('double', params = (50,), response_callback = ResponseCallback(callback_func = request_result)) ##client.add_request_on_connect(req, wait = True) ##req = RequestOrNotification('foo', params = {'callback': 'progress_callback' }, response_callback = ResponseCallback(callback_func = request_result)) ##client.add_request_on_connect(req, wait = False) ##req = RequestOrNotification('bar', params = (3, ), response_callback = ResponseCallback(callback_func = request_result)) ##client.add_request_on_connect(req) client.start() connection = None counter = 0 while 1: counter += 1 if len(client.jsonprotocol.connections) > 0 and not connection: print 'Got connection' connection = client.jsonprotocol.connections[0] repr(connection) sys.stdout.write('.' + str(counter)) sys.stdout.flush() sleep(1) if counter == 2: print 'Sending request' req = RequestOrNotification('double', params = (3.142,), response_callback = ResponseCallback(callback_func = request_result)) h = Handle(connection) #h.activate() h.put(req, 'request')
# ------------------------------------------------------------------------- # a slightly more complicated example of a TCP client, where we define an echo. from Kamaelia.Chassis.ConnectedServer import SimpleServer from Kamaelia.Protocol.EchoProtocol import EchoProtocol from Kamaelia.Internet.TCPClient import TCPClient from Axon.background import background from Axon.Handle import Handle import Queue import time background(slowmo=0.01).start() PORT = 1900 # This starts an echo server in the background. SimpleServer(protocol=EchoProtocol, port=PORT).activate() # give the component time to commence listening on a port. time.sleep(0.5) echoClient = Handle(TCPClient(host="localhost", port=PORT)).activate() while True: echoClient.put(raw_input(">>> "), "inbox") while 1: try: print echoClient.get("outbox") break except Queue.Empty: time.sleep(0.01)
yield 1 self.send(self.recv("control"), "signal") # pass on the shutdown from Kamaelia.Util.Backplane import * from Kamaelia.Chassis.Pipeline import Pipeline from Kamaelia.Util.Console import * # Test 1 if 0: # This works background().start() Backplane("TEST").activate() reverser = Handle(Pipeline(Reverser(), PublishTo("TEST"))).activate() Pipeline(SubscribeTo("TEST"), ConsoleEchoer()).activate() while True: line = sys.stdin.readline() if line == "": break line = line.rstrip() # get rid of newline - looks odd otherwise :) reverser.put(line, "inbox") # Test 2 if 0: # This works background().start()
def setUp(self): # print "begin setUp..." r = Reverser() h = Handle(r) self.reverser = h.activate()
# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ------------------------------------------------------------------------- from Axon.background import background from Axon.Handle import Handle from Kamaelia.Protocol.HTTP.HTTPClient import SimpleHTTPClient background = background().start() import time import Queue p = Handle(SimpleHTTPClient()).activate() p.put("http://google.com","inbox") p.put("http://slashdot.org","inbox") p.put("http://whatismyip.org","inbox") def get_item(handle): while 1: try: item = handle.get("outbox") break except Queue.Empty: time.sleep(0.05) return item google = get_item(p) slashdot = get_item(p)
background().start() class Reverser(Axon.Component.component): def main(self): while True: if self.dataReady('inbox'): item = self.recv('inbox') self.send(item[::-1], 'outbox') # strings have no "reverse" method, hence this indexing 'hack'. else: self.pause() yield 1 sys.stderr.write("""_Similar_ to Unix's "rev" tool, implemented using likefile., type stuff, it reverses it\n""") reverser = Handle(Reverser()).activate() while True: line = sys.stdin.readline() if line == "": break line = line.rstrip() # get rid of the newline (Doesn't just strip newline, so rev(rev()) would not work 'correctly') reverser.put(line, "inbox") while 1: try: enil = reverser.get("outbox") break except queue.Empty: time.sleep(0.1)
# # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ------------------------------------------------------------------------- from Axon.background import background from Axon.Handle import Handle from Kamaelia.Codec.Vorbis import VorbisDecode, AOAudioPlaybackAdaptor from Kamaelia.Chassis.Pipeline import Pipeline from Kamaelia.File.ReadFileAdaptor import ReadFileAdaptor import time import ao background(slowmo=0.001).start() filename = "../SupportingMediaFiles/KDE_Startup_2.ogg" playStream = Handle(Pipeline(VorbisDecode(), AOAudioPlaybackAdaptor())).activate() # Play the ogg data in the background oggdata = open(filename, "r+b").read() playStream.put(oggdata, "inbox") while True: time.sleep(0.1)
import Axon from Axon.background import background from Axon.Handle import Handle from Kamaelia.UI.Pygame.Ticker import Ticker from Kamaelia.Chassis.Pipeline import Pipeline from Kamaelia.File.ReadFileAdaptor import ReadFileAdaptor import time bg = background(slowmo=0.01).start() ticker1 = Handle(Pipeline( Ticker(background_colour=(128,48,128), render_left = 1, render_top = 1, render_right = 600, render_bottom = 200, position = (100, 250), ) ) ).activate() ticker2 = Handle(Pipeline( Ticker(background_colour=(128,48,128), render_left = 1, render_top = 1, render_right = 600, render_bottom = 200, position = (100, 0), ) ) ).activate()
import Axon from Axon.background import background from Axon.Handle import Handle from Kamaelia.UI.Pygame.Ticker import Ticker from Kamaelia.Chassis.Pipeline import Pipeline from Kamaelia.File.ReadFileAdaptor import ReadFileAdaptor import time bg = background(slowmo=0.01).start() ticker1 = Handle( Pipeline( Ticker( background_colour=(128, 48, 128), render_left=1, render_top=1, render_right=600, render_bottom=200, position=(100, 250), ) ) ).activate() ticker2 = Handle( Pipeline( Ticker( background_colour=(128, 48, 128), render_left=1, render_top=1, render_right=600, render_bottom=200, position=(100, 0), )
while True: if self.dataReady('inbox'): item = self.recv('inbox') self.send( item[::-1], 'outbox' ) # strings have no "reverse" method, hence this indexing 'hack'. else: self.pause() yield 1 sys.stderr.write( """_Similar_ to Unix's "rev" tool, implemented using likefile., type stuff, it reverses it\n""" ) reverser = Handle(Reverser()).activate() while True: line = sys.stdin.readline() if line == "": break line = line.rstrip( ) # get rid of the newline (Doesn't just strip newline, so rev(rev()) would not work 'correctly') reverser.put(line, "inbox") while 1: try: enil = reverser.get("outbox") break except queue.Empty: time.sleep(0.1)
# a slightly more complicated example of a TCP client, where we define an echo. from Kamaelia.Chassis.ConnectedServer import SimpleServer from Kamaelia.Protocol.EchoProtocol import EchoProtocol from Kamaelia.Internet.TCPClient import TCPClient from Axon.background import background from Axon.Handle import Handle import Queue import time background(slowmo=0.01).start() PORT = 1900 # This starts an echo server in the background. SimpleServer(protocol = EchoProtocol, port = PORT).activate() # give the component time to commence listening on a port. time.sleep(0.5) echoClient = Handle(TCPClient(host = "localhost", port = PORT)).activate() while True: echoClient.put(raw_input(">>> "),"inbox") while 1: try: print echoClient.get("outbox") break except Queue.Empty: time.sleep(0.01)
import time from Axon.background import background from Kamaelia.UI.Pygame.Text import Textbox, TextDisplayer from Axon.Handle import Handle background().start() try: import Queue queue = Queue # Python 3 compatibility change except ImportError: # Python 3 compatibility change import queue TD = Handle( TextDisplayer(position=(20, 90), text_height=36, screen_width=900, screen_height=200, background_color=(130, 0, 70), text_color=(255, 255, 255))).activate() TB = Handle( Textbox(position=(20, 340), text_height=36, screen_width=900, screen_height=400, background_color=(130, 0, 70), text_color=(255, 255, 255))).activate() message = "hello\n" while 1: time.sleep(1)
from Axon.background import background from Kamaelia.UI.Pygame.Text import Textbox, TextDisplayer from Axon.Handle import Handle background().start() try: import Queue queue = Queue # Python 3 compatibility change except ImportError: # Python 3 compatibility change import queue TD = Handle( TextDisplayer(position=(20, 90), text_height=36, screen_width=900, screen_height=200, background_color=(130,0,70), text_color=(255,255,255) ) ).activate() TB = Handle( Textbox(position=(20, 340), text_height=36, screen_width=900, screen_height=400, background_color=(130,0,70), text_color=(255,255,255) ) ).activate()
##req = RequestOrNotification('long_calc', params = (100, 'progress_callback'), response_callback = ResponseCallback(callback_func = request_result)) ##client.add_request_on_connect(req, wait=False) ##req = RequestOrNotification('double', params = (50,), response_callback = ResponseCallback(callback_func = request_result)) ##client.add_request_on_connect(req, wait = True) ##req = RequestOrNotification('foo', params = {'callback': 'progress_callback' }, response_callback = ResponseCallback(callback_func = request_result)) ##client.add_request_on_connect(req, wait = False) ##req = RequestOrNotification('bar', params = (3, ), response_callback = ResponseCallback(callback_func = request_result)) ##client.add_request_on_connect(req) client.start() connection = None counter = 0 while 1: counter += 1 if len(client.jsonprotocol.connections) > 0 and not connection: print 'Got connection' connection = client.jsonprotocol.connections[0] repr(connection) sys.stdout.write('.' + str(counter)) sys.stdout.flush() sleep(1) if counter == 2: print 'Sending request' req = RequestOrNotification('double', params=(3.142, ), response_callback=ResponseCallback( callback_func=request_result)) h = Handle(connection) #h.activate() h.put(req, 'request')
item = self.recv('inbox') self.send(item[::-1], 'outbox') else: self.pause() yield 1 # If using zap=True, number of breaks is increased # background(zap=True).start() background(zap=True).start() # Uncomment this -> ~40% fail, it doesn't seem matter how long we sleep time.sleep(1) # time.sleep(5) reverser = Handle(Reverser()).activate() # There seems that there is not a big difference uncommenting this # time.sleep(1) reverser.put("hello world", "inbox") # There seems that there is not a big difference uncommenting this # time.sleep(1) n = 0 print '*' * 30 initial = time.time() while True: try:
while True: if self.dataReady('inbox'): item = self.recv('inbox') self.send(item[::-1], 'outbox') else: self.pause() yield 1 # If using zap=True, number of breaks is increased # background(zap=True).start() background(zap=True).start() # Uncomment this -> ~40% fail, it doesn't seem matter how long we sleep time.sleep(1) # time.sleep(5) reverser = Handle(Reverser()).activate() # There seems that there is not a big difference uncommenting this # time.sleep(1) reverser.put("hello world", "inbox") # There seems that there is not a big difference uncommenting this # time.sleep(1) n = 0 print '*' * 30 initial = time.time() while True: try: