advertisement

Kamaelia Protocol Walkthrough

67 %
33 %
advertisement
Information about Kamaelia Protocol Walkthrough
Technology

Published on October 31, 2008

Author: kamaelian

Source: slideshare.net

Description

A walkthrough of what's going on in this example: http://tinyurl.com/4k3df2
advertisement

A walk through building & stacking protocols using Kamaelia Michael Sparks October 2008

The aim of this document is to guide you through building a system where: ● A user connects to a server and ● Over a secure connection, ● Receives a sequence of JSON encode-able objects

The aim of this document is to guide you through building a system where: ● A user connects to a server and ● Over a secure connection, This doc also uses speech ● Receives a sequence of JSON make “aside” bubbles like this to encode-able comments regarding particlar objects panels. You can skip these safely.

ServerCore First of all, we start off with the ServerCore component. The hole is for fitting a protocol handler factory. eg a class, or function.

ServerCore As an aside, this hole is pretty crucial. Without filling this hole, this component is pretty useless. So we call this sort of component a CHASSIS First of all, we start off with the ServerCore component. The hole is for fitting a protocol handler factory. eg a class, or function.

ServerCore «factory» protocol So, we provide a protocol factory. ServerCore(protocol=protocol, port=2345)

ServerCore «factory» protocol In retrospect, “protocol” as the name of an example protocol handler is a bad idea, so let's change this slightly. So, we provide a protocol factory. ServerCore(protocol=protocol, port=2345)

ServerCore «factory» stackedjson So, we provide a protocol factory. ServerCore(protocol=stackedjson, port=2345)

ServerCore «factory» stackedjson We then activate it, which causes it to start a subcomponent. ServerCore( .... )

ServerCore TCPServer «factory» stackedjson We then activate it. It starts TCPServer subcomponent, which listens for connections. ServerCore( .... ).activate()

ServerCore Connected TCPServer SocketAdapter «factory» stackedjson A client then connects to the TCPServer which creates a ConnectedSocketAdapter to handle the mechanics of the connection.

ServerCore Connected TCPServer SocketAdapter «factory» stackedjson stackedjson TCPServer tells ServerCore it has done this. ServerCore in response calls the protocol factory to create a handler component...

ServerCore Connected TCPServer SocketAdapter «factory» stackedjson stackedjson ...which is wired up. The protocol handler just receives bytes and sends bytes, which are sent to the client via the ConnectedSocketAdapter

ServerCore Connected TCPServer SocketAdapter «factory» stackedjson stackedjson There, the key thing in creating a server, is to create an appropriate protocol handler component.

ServerCore Connected TCPServer SocketAdapter «factory» stackedjson stackedjson There, the key thing in creating a server, is to create an appropriate protocol handler component.

stackedjson So, let's look inside this component

stackedjson def protocol(*args,**argd): return Pipeline( PeriodicWakeup(message=quot;NEXTquot;, interval=1), Chooser(messages), MarshallJSON(), Encrypter(), DataChunker(), ) So, let's look inside this component

stackedjson def protocol(*args,**argd): return Pipeline( PeriodicWakeup(message=quot;NEXTquot;, interval=1), Chooser(messages), MarshallJSON(), Encrypter(), DataChunker(), ) So, let's look inside this component. Clearly this is what actually handles the connection.

PeriodicWakeup Chooser Pipeline( PeriodicWakeup(message=quot;NEXTquot;, MarshallJSON interval=1), Chooser(messages), Encrypter MarshallJSON(), Encrypter(), DataChunker DataChunker(), ) Let's expand that...

PeriodicWakeup Chooser MarshallJSON Direction of Encrypter data flow DataChunker We see this is a uni-directional protocol – as soon as this pipeline starts up. Encrypted JSON data chunks get sent, ignoring client data.

PeriodicWakeup Chooser ConsoleEchoer MarshallJSON DeMarshallJSON Direction of Encrypter data flow Decrypter DataChunker DataDeChunker TCPClient So, given the ServerCore infrastructure means we can more or less ignore the network, what does the client side pipeline look like?

PeriodicWakeup Chooser ConsoleEchoer MarshallJSON DeMarshallJSON Encrypter Decrypter DataChunker DataDeChunker TCPClient Each component in the server, that is part of the user's protocol, has a matching component in the client.

PeriodicWakeup Chooser ConsoleEchoer MarshallJSON DeMarshallJSON Encrypter Decrypter DataChunker DataDeChunker TCPClient Fundamentally, this protocol allows one side of it to securely throw dictionary objects to clients which can then do something with them

PeriodicWakeup Chooser ConsoleEchoer MarshallJSON DeMarshallJSON Encrypter Decrypter DataChunker DataDeChunker TCPClient To understand what a client sees, we just look at these three components. The 2 highlighted server side components are the core behaviour.

PeriodicWakeup Chooser ConsoleEchoer Let's rearrange these, showing the logical pipeline that drives what the user sees.

PeriodicWakeup( PeriodicWakeup message=quot;NEXTquot;, interval=1) Chooser ConsoleEchoer First of all the PeriodicWakeupComponent sends out the message “NEXT” once per second.

Messages = [ PeriodicWakeup {quot;helloquot;: quot;worldquot; }, {quot;helloquot;: [1,2,3] }, {quot;worldquot;: [1,2,3] }, {quot;worldquot;: {quot;gamequot;:quot;overquot;} }, Chooser ]*10 Chooser(messages) ConsoleEchoer These “NEXT” messages control the Chooser. The chooser steps through a list of messages, and when told “NEXT”, sends the next message.

Messages = [ PeriodicWakeup {quot;helloquot;: quot;worldquot; }, {quot;helloquot;: [1,2,3] }, {quot;worldquot;: [1,2,3] }, It's worth noting that the Chooser Chooser can back backwards{quot;gamequot;:quot;overquot;} }, {quot;worldquot;: as well, just to ]*10 the start and end etc. For more Chooser(messages)at the docs :) details look ConsoleEchoer These “NEXT” messages control the Chooser. The chooser steps through a list of messages, and when told “NEXT”, sends the next message.

  • PeriodicWakeup Chooser ConsoleEchoer(use_repr=True) ConsoleEchoer These messages arrive intact at the client - in this case a ConsoleEchoer. However it could be something more interesting – eg a game system.
  • PeriodicWakeup Chooser ConsoleEchoer MarshallJSON DeMarshallJSON Encrypter Decrypter DataChunker DataDeChunker TCPClient Going back to the overview slide, this diagram shows the various protocols which are stacked on top of each other.
  • PeriodicWakeup Chooser ConsoleEchoer MarshallJSON DeMarshallJSON Encrypter Decrypter DataChunker DataDeChunker TCPClient We have our core application protocol, stacked on top of a secure serialising communications channel. This truly is at the application level.
  • PeriodicWakeup Chooser ConsoleEchoer MarshallJSON DeMarshallJSON Encrypter Decrypter DataChunker DataDeChunker TCPClient We have our serialisation protocol. Clearly this is a message oriented protocol – it requires the lower layer to preserve boundaries.
  • PeriodicWakeup Chooser ConsoleEchoer MarshallJSON DeMarshallJSON Encrypter Decrypter DataChunker DataDeChunker TCPClient We have a secure transmission protocol. This is admittedly simplistic – it assumes predistributed keys. However it gives a flavour
  • PeriodicWakeup Chooser ConsoleEchoer MarshallJSON DeMarshallJSON Encrypter Decrypter DataChunker DataDeChunker TCPClient We then have our chunking protocol, which preserves message boundaries – necessary because TCP does not guarantee to do so.
  • PeriodicWakeup Chooser ConsoleEchoer MarshallJSON DeMarshallJSON Encrypter Decrypter DataChunker DataDeChunker TCPClient So let's go back and see where these things fit back into the system.
  • ServerCore Connected TCPServer SocketAdapter «factory» stackedjson stackedjson So this is what we had ...
  • ServerCore Connected TCPServer SocketAdapter «factory» stackedjson stackedjson The client really looks like this... Also, this is a one way protocol
  • ServerCore Connected TCPServer SocketAdapter «factory» stackedjson The client really looks like this & this is a one way protocol. The protocol itself looks like this. And that's how those parts fit together.
  • ServerCore Connected TCPServer SocketAdapter «factory» stackedjson So, finally, for completeness, we'll include the code.
  • So, finally, for completeness, we'll include the code. Though, for code, this is a better location: http://tinyurl.com/4k3df2
  • Bunch of imports... import cjson import Axon from Kamaelia.Chassis.Pipeline import Pipeline from Kamaelia.Util.Chooser import Chooser from Kamaelia.Util.Console import ConsoleEchoer from Kamaelia.Internet.TCPClient import TCPClient from Kamaelia.Chassis.ConnectedServer import ServerCore from Kamaelia.Protocol.Framing import DataChunker, DataDeChunker from Kamaelia.Apps.Grey.PeriodicWakeup import PeriodicWakeup from Crypto.Cipher import DES
  • Define the messages being slung to clients messages = [ {quot;helloquot;: quot;worldquot; }, {quot;helloquot;: [1,2,3] }, {quot;worldquot;: [1,2,3] }, {quot;worldquot;: {quot;gamequot;:quot;overquot;} }, ]*10

    Then the JSON De/Marshalling components class MarshallJSON(Axon.Component.component): def main(self): while not self.dataReady(quot;controlquot;): for j in self.Inbox(quot;inboxquot;): j_encoded = cjson.encode(j) self.send(j_encoded, quot;outboxquot;) if not self.anyReady(): self.pause() yield 1 class DeMarshallJSON(Axon.Component.component): def main(self): while not self.dataReady(quot;controlquot;): for j in self.Inbox(quot;inboxquot;): j_decoded = cjson.decode(j) self.send(j_decoded, quot;outboxquot;) if not self.anyReady(): self.pause() yield 1

    Define our encoding layer using normal code...1 class Encoder(object): quot;quot;quot;Null encoder/base encoder - returns the same string for encode/decodequot;quot;quot; def __init__(self, key, **argd): super(Encoder, self).__init__(**argd) self.__dict__.update(argd) self.key = key def encode(self, some_string): return some_string def decode(self, some_string): return some_string

    Define our encoding layer using normal code...2 class DES_CRYPT(Encoder): def __init__(self, key, **argd): super(DES_CRYPT, self).__init__(key, **argd) self.key = self.pad_eight(key)[:8] self.obj = obj=DES.new(self.key, DES.MODE_ECB) def encode(self, some_string): padded = self.pad_eight(some_string) encrypted = self.obj.encrypt(padded) return encrypted def decode(self, some_string): padded = self.obj.decrypt(some_string) decoded = self.unpad(padded) return decoded # ... continued

    Define our encoding layer using normal code...3 # class DES_CRYPT(Encoder): # ... continued from before def pad_eight(self, some_string): X = len(some_string) if X % 8 != 0: pad_needed = 8-X % 8 else: pad_needed = 8 pad_needed = 8-(X % 8) PAD = pad_needed * chr(pad_needed) return some_string+PAD def unpad(self, some_string): x = ord(some_string[-1]) return some_string[:-x]

    Create Encryption components using this class Encrypter(Axon.Component.component): key = quot;ABCDquot; def main(self): crypter = DES_CRYPT(self.key) while not self.dataReady(quot;controlquot;): for j in self.Inbox(quot;inboxquot;): j_encoded = crypter.encode(j) self.send(j_encoded, quot;outboxquot;) if not self.anyReady(): self.pause() yield 1 Note, this is not a serious protocol, it is an illustration. This form of pre-shared key would be a bad idea, but the pattern of usage will be useful.

    Create Decryption components using this class Decrypter(Axon.Component.component): key = quot;ABCDquot; def main(self): crypter = DES_CRYPT(self.key) while not self.dataReady(quot;controlquot;): for j in self.Inbox(quot;inboxquot;): j_decoded = crypter.decode(j) self.send(j_decoded, quot;outboxquot;) if not self.anyReady(): self.pause() yield 1 Note, this is not a serious protocol, it is an illustration. This form of pre-shared key would be a bad idea, but the pattern of usage will be useful.

    Create the protocol handler factory & Client def stackedjson(*args,**argd): return Pipeline( PeriodicWakeup(message=quot;NEXTquot;, interval=1), Chooser(messages), MarshallJSON(), Encrypter(), # Encrypt on the way out DataChunker(), ) def json_client_prefab(ip, port): return Pipeline( TCPClient(ip, port=port), DataDeChunker(), Decrypter(), # Decrypt on the way in DeMarshallJSON(), ConsoleEchoer(use_repr=True) )

    And finally start the server and point a client at the server. ServerCore(protocol=protocol, port=2345).activate() json_client_prefab(quot;127.0.0.1quot;, 2345).run() ServerCore Connected TCPServer SocketAdapter «factory» stackedjson

  • Add a comment

    Related presentations

    Presentación que realice en el Evento Nacional de Gobierno Abierto, realizado los ...

    In this presentation we will describe our experience developing with a highly dyna...

    Presentation to the LITA Forum 7th November 2014 Albuquerque, NM

    Un recorrido por los cambios que nos generará el wearabletech en el futuro

    Um paralelo entre as novidades & mercado em Wearable Computing e Tecnologias Assis...

    Microsoft finally joins the smartwatch and fitness tracker game by introducing the...

    Related pages

    Stealth Sniper 2 - Full Game Walkthrough (All 1-4 Missions ...

    Stealth Sniper 2 - Full Game Walkthrough (All 1-4 Missions) 108GAME. Subscribe Subscribed Unsubscribe 351 351. Loading... Loading... Working...
    Read more

    Block cipher - Wikipedia, the free encyclopedia

    Block ciphers operate as important elementary components in the design of many cryptographic protocols, ... Most block cipher algorithms are ... Camellia ...
    Read more

    Cheatbook - Cheat Codes, Cheats, Games Trainer, Cheatsbook ...

    Cheatbook your source for Cheats, Video game Cheat Codes and Game Hints, Walkthroughs, FAQ, Games Trainer, Games Guides, Secrets, cheatsbook
    Read more

    strongSwan - IPsec for Linux

    The strongSwan 5.x branch supports both the IKEv1 and IKEv2 key exchange protocols in conjunction with the native NETKEY IPsec stack of the Linux kernel.
    Read more

    Kotor – Wikipedia

    Kotor (serbisch-kyrillisch Котор; italienisch Cattaro; lateinisch Acruvium) ist eine alte mediterrane Handels- und Hafenstadt und überregionales ...
    Read more

    A post per day in November - sparkslabs.com

    A post per day in November November 02, 2008 at 02:38 PM | categories: ... Kamaelia Protocol Walkthrough. View SlideShare presentation or Upload your own.
    Read more

    Sparks

    Now the reason I'd been thinking about this, is because I'd like to retain the hierarchy of components in Kamaelia that we have at the moment ...
    Read more

    Open Graph Stories - Sharing - Documentation - Facebook ...

    Connect real world actions and objects in your apps with Open Graph. It allows apps to tell stories on Facebook through a structured, strongly typed API.
    Read more