SlideShare a Scribd company logo
1 of 50
Download to read offline
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

More Related Content

What's hot

è§ŁèŻ»server.xml文件
è§ŁèŻ»server.xmlæ–‡ä»¶è§ŁèŻ»server.xml文件
è§ŁèŻ»server.xml文件
wensheng wei
 
Nodejs í”„ëĄœê·žëž˜ë° ch.3
Nodejs í”„ëĄœê·žëž˜ë° ch.3Nodejs í”„ëĄœê·žëž˜ë° ch.3
Nodejs í”„ëĄœê·žëž˜ë° ch.3
HyeonSeok Choi
 
ikh331-06-distributed-programming
ikh331-06-distributed-programmingikh331-06-distributed-programming
ikh331-06-distributed-programming
Anung Ariwibowo
 
Basic socket programming
Basic socket programmingBasic socket programming
Basic socket programming
Kristian Arjianto
 
Java- Datagram Socket class & Datagram Packet class
Java- Datagram Socket class  & Datagram Packet classJava- Datagram Socket class  & Datagram Packet class
Java- Datagram Socket class & Datagram Packet class
Ruchi Maurya
 

What's hot (20)

Chat Room System using Java Swing
Chat Room System using Java SwingChat Room System using Java Swing
Chat Room System using Java Swing
 
OSGi Puzzlers
OSGi PuzzlersOSGi Puzzlers
OSGi Puzzlers
 
Complex Event Processing with Esper and WSO2 ESB
Complex Event Processing with Esper and WSO2 ESBComplex Event Processing with Esper and WSO2 ESB
Complex Event Processing with Esper and WSO2 ESB
 
Advance Java Programs skeleton
Advance Java Programs skeletonAdvance Java Programs skeleton
Advance Java Programs skeleton
 
Ad java prac sol set
Ad java prac sol setAd java prac sol set
Ad java prac sol set
 
è§ŁèŻ»server.xml文件
è§ŁèŻ»server.xmlæ–‡ä»¶è§ŁèŻ»server.xml文件
è§ŁèŻ»server.xml文件
 
Java sockets
Java socketsJava sockets
Java sockets
 
Nodejs í”„ëĄœê·žëž˜ë° ch.3
Nodejs í”„ëĄœê·žëž˜ë° ch.3Nodejs í”„ëĄœê·žëž˜ë° ch.3
Nodejs í”„ëĄœê·žëž˜ë° ch.3
 
Networking & Socket Programming In Java
Networking & Socket Programming In JavaNetworking & Socket Programming In Java
Networking & Socket Programming In Java
 
ikh331-06-distributed-programming
ikh331-06-distributed-programmingikh331-06-distributed-programming
ikh331-06-distributed-programming
 
Dynamo: Not Just For Datastores
Dynamo: Not Just For DatastoresDynamo: Not Just For Datastores
Dynamo: Not Just For Datastores
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Lecture10
Lecture10Lecture10
Lecture10
 
Java NIO.2
Java NIO.2Java NIO.2
Java NIO.2
 
Socket programming using java
Socket programming using javaSocket programming using java
Socket programming using java
 
From A to Z | WireShark Tutorial
From A to Z | WireShark TutorialFrom A to Z | WireShark Tutorial
From A to Z | WireShark Tutorial
 
Basic socket programming
Basic socket programmingBasic socket programming
Basic socket programming
 
ĐĐŸĐČыĐč InterSystems: open-source, ĐŒĐžŃ‚Đ°ĐżŃ‹, хаĐșĐ°Ń‚ĐŸĐœŃ‹
ĐĐŸĐČыĐč InterSystems: open-source, ĐŒĐžŃ‚Đ°ĐżŃ‹, хаĐșĐ°Ń‚ĐŸĐœŃ‹ĐĐŸĐČыĐč InterSystems: open-source, ĐŒĐžŃ‚Đ°ĐżŃ‹, хаĐșĐ°Ń‚ĐŸĐœŃ‹
ĐĐŸĐČыĐč InterSystems: open-source, ĐŒĐžŃ‚Đ°ĐżŃ‹, хаĐșĐ°Ń‚ĐŸĐœŃ‹
 
Java- Datagram Socket class & Datagram Packet class
Java- Datagram Socket class  & Datagram Packet classJava- Datagram Socket class  & Datagram Packet class
Java- Datagram Socket class & Datagram Packet class
 
Behind modern concurrency primitives
Behind modern concurrency primitivesBehind modern concurrency primitives
Behind modern concurrency primitives
 

Viewers also liked

Photoshop Club
Photoshop ClubPhotoshop Club
Photoshop Club
Kitara
 
RabbitMQ with python and ruby RuPy 2009
RabbitMQ with python and ruby RuPy 2009RabbitMQ with python and ruby RuPy 2009
RabbitMQ with python and ruby RuPy 2009
Paolo Negri
 
Data Analysis and Statistics in Python using pandas and statsmodels
Data Analysis and Statistics in Python using pandas and statsmodelsData Analysis and Statistics in Python using pandas and statsmodels
Data Analysis and Statistics in Python using pandas and statsmodels
Wes McKinney
 
Python for Financial Data Analysis with pandas
Python for Financial Data Analysis with pandasPython for Financial Data Analysis with pandas
Python for Financial Data Analysis with pandas
Wes McKinney
 

Viewers also liked (20)

Embracing concurrency for fun utility and simpler code
Embracing concurrency for fun utility and simpler codeEmbracing concurrency for fun utility and simpler code
Embracing concurrency for fun utility and simpler code
 
Kamaelia Europython Tutorial
Kamaelia Europython TutorialKamaelia Europython Tutorial
Kamaelia Europython Tutorial
 
Kamaelia lightning2010opensource
Kamaelia lightning2010opensourceKamaelia lightning2010opensource
Kamaelia lightning2010opensource
 
Practical concurrent systems made simple using Kamaelia
Practical concurrent systems made simple using KamaeliaPractical concurrent systems made simple using Kamaelia
Practical concurrent systems made simple using Kamaelia
 
Bleach
BleachBleach
Bleach
 
Photoshop Club
Photoshop ClubPhotoshop Club
Photoshop Club
 
Kamaelia Grey
Kamaelia GreyKamaelia Grey
Kamaelia Grey
 
Managing Creativity
Managing CreativityManaging Creativity
Managing Creativity
 
Timeshift Everything, Miss Nothing - Mashup your PVR with Kamaelia
Timeshift Everything, Miss Nothing - Mashup your PVR with KamaeliaTimeshift Everything, Miss Nothing - Mashup your PVR with Kamaelia
Timeshift Everything, Miss Nothing - Mashup your PVR with Kamaelia
 
The Selfish Programmer
The Selfish ProgrammerThe Selfish Programmer
The Selfish Programmer
 
RabbitMQ with python and ruby RuPy 2009
RabbitMQ with python and ruby RuPy 2009RabbitMQ with python and ruby RuPy 2009
RabbitMQ with python and ruby RuPy 2009
 
Descriptors In Python
Descriptors In PythonDescriptors In Python
Descriptors In Python
 
Messaging with RabbitMQ and AMQP
Messaging with RabbitMQ and AMQPMessaging with RabbitMQ and AMQP
Messaging with RabbitMQ and AMQP
 
Django introduction
Django introductionDjango introduction
Django introduction
 
Data Analysis and Statistics in Python using pandas and statsmodels
Data Analysis and Statistics in Python using pandas and statsmodelsData Analysis and Statistics in Python using pandas and statsmodels
Data Analysis and Statistics in Python using pandas and statsmodels
 
Django for Beginners
Django for BeginnersDjango for Beginners
Django for Beginners
 
Introduction To Django
Introduction To DjangoIntroduction To Django
Introduction To Django
 
Python for Financial Data Analysis with pandas
Python for Financial Data Analysis with pandasPython for Financial Data Analysis with pandas
Python for Financial Data Analysis with pandas
 
Advance OOP concepts in Python
Advance OOP concepts in PythonAdvance OOP concepts in Python
Advance OOP concepts in Python
 
Python Advanced – Building on the foundation
Python Advanced – Building on the foundationPython Advanced – Building on the foundation
Python Advanced – Building on the foundation
 

Similar to Kamaelia Protocol Walkthrough

Distribute Key Value Store
Distribute Key Value StoreDistribute Key Value Store
Distribute Key Value Store
Santal Li
 
Distribute key value_store
Distribute key value_storeDistribute key value_store
Distribute key value_store
drewz lin
 
Use perl creating web services with xml rpc
Use perl creating web services with xml rpcUse perl creating web services with xml rpc
Use perl creating web services with xml rpc
Johnny Pork
 
Introduction to Thrift
Introduction to ThriftIntroduction to Thrift
Introduction to Thrift
Dvir Volk
 

Similar to Kamaelia Protocol Walkthrough (20)

Cassandra 2.1 boot camp, Overview
Cassandra 2.1 boot camp, OverviewCassandra 2.1 boot camp, Overview
Cassandra 2.1 boot camp, Overview
 
weblogic perfomence tuning
weblogic perfomence tuningweblogic perfomence tuning
weblogic perfomence tuning
 
The post release technologies of Crysis 3 (Slides Only) - Stewart Needham
The post release technologies of Crysis 3 (Slides Only) - Stewart NeedhamThe post release technologies of Crysis 3 (Slides Only) - Stewart Needham
The post release technologies of Crysis 3 (Slides Only) - Stewart Needham
 
Distribute Key Value Store
Distribute Key Value StoreDistribute Key Value Store
Distribute Key Value Store
 
Distribute key value_store
Distribute key value_storeDistribute key value_store
Distribute key value_store
 
Use perl creating web services with xml rpc
Use perl creating web services with xml rpcUse perl creating web services with xml rpc
Use perl creating web services with xml rpc
 
Apache Con NA 2013 - Cassandra Internals
Apache Con NA 2013 - Cassandra InternalsApache Con NA 2013 - Cassandra Internals
Apache Con NA 2013 - Cassandra Internals
 
WCF and WF in Framework 3.5
WCF and WF in Framework 3.5WCF and WF in Framework 3.5
WCF and WF in Framework 3.5
 
Porting a Streaming Pipeline from Scala to Rust
Porting a Streaming Pipeline from Scala to RustPorting a Streaming Pipeline from Scala to Rust
Porting a Streaming Pipeline from Scala to Rust
 
You need Event Mesh, not Service Mesh - Chris Suszynski [WJUG 301]
You need Event Mesh, not Service Mesh - Chris Suszynski [WJUG 301]You need Event Mesh, not Service Mesh - Chris Suszynski [WJUG 301]
You need Event Mesh, not Service Mesh - Chris Suszynski [WJUG 301]
 
Porting is a Delicate Matter: Checking Far Manager under Linux
Porting is a Delicate Matter: Checking Far Manager under LinuxPorting is a Delicate Matter: Checking Far Manager under Linux
Porting is a Delicate Matter: Checking Far Manager under Linux
 
Kafka monitoring and metrics
Kafka monitoring and metricsKafka monitoring and metrics
Kafka monitoring and metrics
 
Low latency microservices in java QCon New York 2016
Low latency microservices in java   QCon New York 2016Low latency microservices in java   QCon New York 2016
Low latency microservices in java QCon New York 2016
 
Learning spark ch10 - Spark Streaming
Learning spark ch10 - Spark StreamingLearning spark ch10 - Spark Streaming
Learning spark ch10 - Spark Streaming
 
XML-RPC and SOAP (April 2003)
XML-RPC and SOAP (April 2003)XML-RPC and SOAP (April 2003)
XML-RPC and SOAP (April 2003)
 
Troubleshooting common oslo.messaging and RabbitMQ issues
Troubleshooting common oslo.messaging and RabbitMQ issuesTroubleshooting common oslo.messaging and RabbitMQ issues
Troubleshooting common oslo.messaging and RabbitMQ issues
 
Open Source XMPP for Cloud Services
Open Source XMPP for Cloud ServicesOpen Source XMPP for Cloud Services
Open Source XMPP for Cloud Services
 
Runtime Bytecode Transformation for Smalltalk
Runtime Bytecode Transformation for SmalltalkRuntime Bytecode Transformation for Smalltalk
Runtime Bytecode Transformation for Smalltalk
 
Oracle Drivers configuration for High Availability
Oracle Drivers configuration for High AvailabilityOracle Drivers configuration for High Availability
Oracle Drivers configuration for High Availability
 
Introduction to Thrift
Introduction to ThriftIntroduction to Thrift
Introduction to Thrift
 

More from kamaelian

More from kamaelian (11)

Sharing Data and Services Safely in Concurrent Systems using Kamaelia
Sharing Data and Services Safely in Concurrent Systems using KamaeliaSharing Data and Services Safely in Concurrent Systems using Kamaelia
Sharing Data and Services Safely in Concurrent Systems using Kamaelia
 
Sociable Software
Sociable SoftwareSociable Software
Sociable Software
 
Open Source at the BBC: When, Why, Why not & How
Open Source at the BBC: When, Why, Why not & HowOpen Source at the BBC: When, Why, Why not & How
Open Source at the BBC: When, Why, Why not & How
 
Open Source at the BBC
Open Source at the BBCOpen Source at the BBC
Open Source at the BBC
 
Kamaelia - Fave 2005
Kamaelia - Fave 2005Kamaelia - Fave 2005
Kamaelia - Fave 2005
 
SWP - A Generic Language Parser
SWP - A Generic Language ParserSWP - A Generic Language Parser
SWP - A Generic Language Parser
 
Kamaelia - Networking Using Generators
Kamaelia - Networking Using GeneratorsKamaelia - Networking Using Generators
Kamaelia - Networking Using Generators
 
Scaling Streaming - Concepts, Research, Goals
Scaling Streaming - Concepts, Research, GoalsScaling Streaming - Concepts, Research, Goals
Scaling Streaming - Concepts, Research, Goals
 
Kamaelia Internals
Kamaelia InternalsKamaelia Internals
Kamaelia Internals
 
Building systems with Kamaelia
Building systems with KamaeliaBuilding systems with Kamaelia
Building systems with Kamaelia
 
Free software: How does it work?
Free software: How does it work?Free software: How does it work?
Free software: How does it work?
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Recently uploaded (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 

Kamaelia Protocol Walkthrough

  • 1. A walk through building & stacking protocols using Kamaelia Michael Sparks October 2008
  • 2. 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
  • 3. 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.
  • 4. 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.
  • 5. 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.
  • 6. ServerCore «factory» protocol So, we provide a protocol factory. ServerCore(protocol=protocol, port=2345)
  • 7. 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)
  • 8. ServerCore «factory» stackedjson So, we provide a protocol factory. ServerCore(protocol=stackedjson, port=2345)
  • 9. ServerCore «factory» stackedjson We then activate it, which causes it to start a subcomponent. ServerCore( .... )
  • 10. ServerCore TCPServer «factory» stackedjson We then activate it. It starts TCPServer subcomponent, which listens for connections. ServerCore( .... ).activate()
  • 11. ServerCore Connected TCPServer SocketAdapter «factory» stackedjson A client then connects to the TCPServer which creates a ConnectedSocketAdapter to handle the mechanics of the connection.
  • 12. 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...
  • 13. 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
  • 14. ServerCore Connected TCPServer SocketAdapter «factory» stackedjson stackedjson There, the key thing in creating a server, is to create an appropriate protocol handler component.
  • 15. ServerCore Connected TCPServer SocketAdapter «factory» stackedjson stackedjson There, the key thing in creating a server, is to create an appropriate protocol handler component.
  • 16. stackedjson So, let's look inside this component
  • 17. 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
  • 18. 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.
  • 19. PeriodicWakeup Chooser Pipeline( PeriodicWakeup(message=quot;NEXTquot;, MarshallJSON interval=1), Chooser(messages), Encrypter MarshallJSON(), Encrypter(), DataChunker DataChunker(), ) Let's expand that...
  • 20. 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.
  • 21. 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?
  • 22. 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.
  • 23. 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
  • 24. 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.
  • 25. PeriodicWakeup Chooser ConsoleEchoer Let's rearrange these, showing the logical pipeline that drives what the user sees.
  • 26. PeriodicWakeup( PeriodicWakeup message=quot;NEXTquot;, interval=1) Chooser ConsoleEchoer First of all the PeriodicWakeupComponent sends out the message “NEXT” once per second.
  • 27. 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.
  • 28. 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.
  • 29. 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.
  • 30. 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.
  • 31. 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.
  • 32. 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.
  • 33. 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
  • 34. 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.
  • 35. 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.
  • 36. ServerCore Connected TCPServer SocketAdapter «factory» stackedjson stackedjson So this is what we had ...
  • 37. ServerCore Connected TCPServer SocketAdapter «factory» stackedjson stackedjson The client really looks like this... Also, this is a one way protocol
  • 38. 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.
  • 39. ServerCore Connected TCPServer SocketAdapter «factory» stackedjson So, finally, for completeness, we'll include the code.
  • 40. So, finally, for completeness, we'll include the code. Though, for code, this is a better location: http://tinyurl.com/4k3df2
  • 41. 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
  • 42. 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
  • 43. 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
  • 44. 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
  • 45. 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
  • 46. 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]
  • 47. 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.
  • 48. 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.
  • 49. 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) )
  • 50. 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