Today we are going to talk about creating a tcp server to parse iso messages using erlang .
This tutorial explains the very basic of creating tcp servers in erlang . there are better specialized libraries out there (ranch) for creating tcp servers and pools of them but this is very basic and we need just a simple tcp server to show how to process incoming iso messages .
We will be using the 1993 iso8583 ascii protocol format. I covered the basics about the iso8583 messag structure in a previous post so you can go there to get a basic summary of iso8583 . knowledge of this protocol and also erlang would help
to install erlang go here.
the files for the example is contained in this git Lets hit the road !!
lets look at the iso_process.erl file and the portions where the tcp server is created
%% @doc this part is for starting the iso server
<br>-spec start_iso_server()->[pid()] | {error,term()}. <br>start_iso_server()-> ....<br></p><p>
%% @doc this part is for listener socket
<br>-spec loop_listen(port())->[port()] | {error,term()} | fun(). <br>loop_listen(Listen_socket)->....</p>
the start_iso_server function and the loop_listen function are responsible for creating the tcp server and erlang processes for handling client connection .
the
{ok,Listen} = gen_tcp:listen(...)
in start_iso_server function creates the tcp listener socket and sets up pattern matching so if the listener socket is created successfully the code goes on succesfully else it stops if there is an error(eg..if port is being used by another program).
note that because the messages will be coming as strings and also because we want to process only one message at a time we use
[list, {packet, 0},{active, once}]
options . strings are represented as lists in erlang so list is in there .
the {packet,0}
option also tells us to allow messages to flow without breaking it up .
the loop_listen function
%% @doc this part is for listener socket
-spec loop_listen(port())->[port()] | {error,term()} | fun(). <br>loop_listen(Listen_socket)
the size of the message in the header is then used to calculate
how many more bits needs to be streamed to get the whole message in .
the iso string is streamed until the whole message is obtained after which it is processed with this function
Response_message = process_message(Rest)
To run the program do a git clone of the repository
git clone https://github.com/nayibor/iso_process_tut_repo.git
cd to the iso_process_tut_repo folder. after that log into the erlang shell(erl or werl in windows) and run
c(iso_process).
this compiles iso_process.erl module. then run
iso_process.start_iso_server()
whch starts the tcp server.
to send messages to server run
iso_process:send_message(Message).
eg
iso_process:send_message(”</p><p>012912007230040102B000001012312313122</p><p>0123400001000000011072218000000011612</p><p>12081731FABCDE123ABD06414243000termi</p><p>d1210Community106A5DFGR”).
you should see a print out of the isomessage showing the various fields.
we will go into how the message is processed and the mti,bitmap,data elements are obtained from the message in the next post as well as how to use jpos to send the messages so we learn a little about both jpos and message processing . neat huh . see u later for that !!!