Wednesday, August 31, 2011

RPN main server code

Previous to this I have presented the RPN calculator code Now here is the server code to work with that. Below I am also presenting how to work with the RPN Server also. First let us present the server code, if you feel any difficulty in understanding the code post me I will give you the explanations step by step


Trying out the RPN Server

Here are all the files

rpneng.c
mkaddr.c

To compile all the related source modules for the RPN server, you can perform the following make command:

Output
gcc -c -D_GNU_SOURCE -Wall -Wreturn-type rpnsrv.c
gcc -c -D_GNU_SOURCE -Wall -Wreturn-type rpneng.c
gcc -c -D_GNU_SOURCE -Wall -Wreturn-type mkaddr.c
gcc rpnsrv.o rpneng.o mkaddr.o -o rpnsrv -lgmp

After the executable rpnsrv for the server has been created, you can start the server as follows:

Output
$ ./rpnsrv &
[1] 13321
$

In the output shown, the server was started with a process ID of 13321, and run in the background. To keep things simple at this point, you'll just use the telnet command to try out the server. The
next chapter will fully outline this server's functions. For now, just try some simple tests.

CAUTION
The server presented is not a production-grade server. Some forms of incorrect input can provoke the server to abort. The RPN calculator computes based upon numbers that are pushed onto the stack. To perform the add operation, for example, requires at least two numbers to exist on the stack. To push a number onto the stack, you will enter a line as follows:

#:970976453

After you press Enter, the server will respond with something like this:
0:

This tells you that the number has been stacked at the bottom of the stack (entry number zero). To stack another number, simply do the same, as follows:

#:2636364
The server will respond with
1:

This indicates that the number 2636364 was stacked at position 1, although the original number 970976453 still sits at the bottom of the stack at position 0. You can list the current contents of
the stack by entering the following:

dump

The following example shows what the session and its output might look like this:
Output

$ telnet localhost 9090
Trying 127.0.0.1 . . .
Connected to localhost.
Escape character is '^]'.
#:970976453
0:
#:2636364
1:
dump
1:2636364
0:970976453
E:end of stack dump

To perform a binary operation, you simply enter the name of the operation or its symbol. For example, to add these numbers, you would just enter the + character and press return. The session
repeated without entering the dump command would appear as follows if the + operation was performed, and then followed by the = operation:

Output
$ telnet localhost 9090
Trying 127.0.0.1 . . .
Connected to localhost.
Escape character is '^]'.
#:970976453
0:
#:2636364
1:
+
0:
=
0:973612817
^]
telnet> c
Connection closed.
$

The + operation caused the two stacked numbers to be added together, and the result replaced the two original values. The = operator here pops the result off the stack and displays it for you. To exit the server, type CTRL+] and you will be prompted with the prompt:

telnet>

From there, enter a c to indicate that you want the session closed, and press Enter. To terminate the server, just use the kill command.
Take a few minutes now to have some fun with the new RPN calculating server program. Restart the server, and see whether you can figure out how to compute the equation (3 + 2) * (2 + 4) using the
calculating server just presented.

No comments:

Post a Comment