Parsing iso8583 messages Part 1
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 this in a previous post(here) 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 repository .
Lets hit the road !!
Creating a basic tcp server in erlang
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
-spec start_iso_server()->[pid()] | {error,term()}.
start_iso_server()-> ....%% @doc this part is for listener socket
-spec loop_listen(port())->[port()] | {error,term()} | fun().
loop_listen(Listen_socket)->....
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().
loop_listen(Listen_socket)->...
is what waits for connections from clients and independently deal with each client connection while the loop_receive is what is responsible for sending/receiving data from clients .
the loop_receive function
%% @doc this part is for the acceptor socket
-spec loop_receive(port(),[])->[pos_integer()] | {error,term()} | fun().
loop_receive(Socket,Isom)->...
is the function which receives communicates with clients . i.e. sends and receives data in this case iso messages from clients .
for every iso string(string = header+iso message) sent a check is first done to get the header which contains the size of the iso message messsage itself . 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 repo .
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(”
012912007230040102B000001012312313122
0123400001000000011072218000000011612
12081731FABCDE123ABD06414243000termi
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 !!!