Code generation
for enterprise integration
           Max Lapshin
        max@maxidoors.ru
FIX protocol


β€’   Automated stock exchange trading

β€’   Binary protocol for receiving quotes and sending orders

β€’   No opensource erlang implementation
FIX protocol

β€’   Packet is a Key,Value list.

β€’   Keys are ASCII-coded integers

β€’   Values may vary

β€’   Separated with 0x01 byte
FIX protocol
β€’   Binary protocol with three levels: transport (TCP), packet and
    business

β€’   All business logic is described in protocol spec

β€’   Different message types with many fields and groups of fields

β€’   Very large protocol specification (about 100 different messages)

β€’   10 us limit for all parsing
FIX protocol


β€’   Received data is decoded to proplists

β€’   But we use records in code

β€’   How to translate? And do it fast!
FIX protocol


β€’   Large XML description how to compose objects from proplists

β€’   XML describes syntax and semantic levels
Problem

β€’   Every FIX user needs only small subset of whole spec

β€’   Impossible to work with full FIX records (more than 100 fields)

β€’   My code works with my records and business logic

β€’   How to fight impedance between our logic and FIX business logic?
Ways to go


β€’   Enterprise

β€’   Ad-hoc

β€’   Controlled code generation
Enterprise way

β€’   Object with 100 fields and 300 methods is ok

β€’   It is Java: abstract singleton factory will make you happy

β€’   Copy-pasted glue code will translate full spec to required subset

β€’   Impossible to use because large size of records

β€’   Not my way
Ad-hoc

β€’   Write custom code for each message type

β€’   Workaround for broker’s bugs

β€’   Ok for some situations

β€’   Not my way: I’m too lazy to copy-paste code by hands
Right way

β€’   Autogenerate proplists-to-record translator

β€’   Make code reuseable for others

β€’   Design system, that can fit into other businesses

β€’   Don’t want to write glue code

β€’   I chose configurable code generation
Code generation

β€’   Parse XML

β€’   Reduce messages according to config file

β€’   Generate headers

β€’   Generate parsers

β€’   Profit!
Headers

β€’   Translate only required FIX messages to records

β€’   Include only required fields in these records

β€’   Always include β€œο¬elds” field to each record for remaining data

β€’   Dialyzer-compatible: types are known at generate time
Example


NewOrderSingle message with 60 fields is translated to

#new_order_single{} with 6 fields
Syntax parser

β€’   Translate binary fields to erlang types and back

β€’   Don’t forget sugar: translate 0,1,2 to atoms buy, sell, make_gift

β€’   Doesn’t depend on config

β€’   Generate C extension (blazing fast)

β€’   Produces readable proplist (not hash!): ordered key-value list
Semantic parser
β€’   Translates proplists to records

β€’   Need to produce records, directly used in business logic

β€’   Can loose some not important information (checksum)

β€’   Need to handle groups of fields (most cryptic part)

β€’   It is generated with config

β€’   Yet have some limited ad-hoc code to keep flexibility
Structure of semantic parser

1. Determine type of message and use compile time record info

2. Process the rest of proplist

3. If key is a known record field, write it to record

4. Else append this {Key,Value} to β€œο¬elds” field

5. Use β€œsetelement” and generated β€œο¬eld_index(MsgType,KeyName)”
Results

β€’   Simple and useable code

β€’   FIX datatypes are used in business logic directly

β€’   Configurable autogeneration of parser is really helpful

β€’   Working code
Questions?


    Max Lapshin

 max@maxidoors.ru

Code generation in Erlang

  • 1.
    Code generation for enterpriseintegration Max Lapshin max@maxidoors.ru
  • 2.
    FIX protocol β€’ Automated stock exchange trading β€’ Binary protocol for receiving quotes and sending orders β€’ No opensource erlang implementation
  • 3.
    FIX protocol β€’ Packet is a Key,Value list. β€’ Keys are ASCII-coded integers β€’ Values may vary β€’ Separated with 0x01 byte
  • 4.
    FIX protocol β€’ Binary protocol with three levels: transport (TCP), packet and business β€’ All business logic is described in protocol spec β€’ Different message types with many fields and groups of fields β€’ Very large protocol specification (about 100 different messages) β€’ 10 us limit for all parsing
  • 5.
    FIX protocol β€’ Received data is decoded to proplists β€’ But we use records in code β€’ How to translate? And do it fast!
  • 6.
    FIX protocol β€’ Large XML description how to compose objects from proplists β€’ XML describes syntax and semantic levels
  • 7.
    Problem β€’ Every FIX user needs only small subset of whole spec β€’ Impossible to work with full FIX records (more than 100 fields) β€’ My code works with my records and business logic β€’ How to fight impedance between our logic and FIX business logic?
  • 8.
    Ways to go β€’ Enterprise β€’ Ad-hoc β€’ Controlled code generation
  • 9.
    Enterprise way β€’ Object with 100 fields and 300 methods is ok β€’ It is Java: abstract singleton factory will make you happy β€’ Copy-pasted glue code will translate full spec to required subset β€’ Impossible to use because large size of records β€’ Not my way
  • 10.
    Ad-hoc β€’ Write custom code for each message type β€’ Workaround for broker’s bugs β€’ Ok for some situations β€’ Not my way: I’m too lazy to copy-paste code by hands
  • 11.
    Right way β€’ Autogenerate proplists-to-record translator β€’ Make code reuseable for others β€’ Design system, that can fit into other businesses β€’ Don’t want to write glue code β€’ I chose configurable code generation
  • 12.
    Code generation β€’ Parse XML β€’ Reduce messages according to config file β€’ Generate headers β€’ Generate parsers β€’ Profit!
  • 13.
    Headers β€’ Translate only required FIX messages to records β€’ Include only required fields in these records β€’ Always include β€œο¬elds” field to each record for remaining data β€’ Dialyzer-compatible: types are known at generate time
  • 14.
    Example NewOrderSingle message with60 fields is translated to #new_order_single{} with 6 fields
  • 15.
    Syntax parser β€’ Translate binary fields to erlang types and back β€’ Don’t forget sugar: translate 0,1,2 to atoms buy, sell, make_gift β€’ Doesn’t depend on config β€’ Generate C extension (blazing fast) β€’ Produces readable proplist (not hash!): ordered key-value list
  • 16.
    Semantic parser β€’ Translates proplists to records β€’ Need to produce records, directly used in business logic β€’ Can loose some not important information (checksum) β€’ Need to handle groups of fields (most cryptic part) β€’ It is generated with config β€’ Yet have some limited ad-hoc code to keep flexibility
  • 17.
    Structure of semanticparser 1. Determine type of message and use compile time record info 2. Process the rest of proplist 3. If key is a known record field, write it to record 4. Else append this {Key,Value} to β€œο¬elds” field 5. Use β€œsetelement” and generated β€œο¬eld_index(MsgType,KeyName)”
  • 18.
    Results β€’ Simple and useable code β€’ FIX datatypes are used in business logic directly β€’ Configurable autogeneration of parser is really helpful β€’ Working code
  • 19.
    Questions? Max Lapshin max@maxidoors.ru