-
Book Overview & Buying
-
Table Of Contents
Java Web Internals
By :
Let's assume we are going to implement a remote calculator, initially, with the following six arithmetic operations:
In this case, to simplify things, let's define the structure of our request. Of the six operations, five expect two operands, and only one (square root) expects a single operand. Let's define a standard structure:
Request:operand1: numberoperand2: numberoperator: textAn example of a sum operation can be viewed in the following code snippet. In defining the protocol that will be used, we will transmit serialized Java objects. This notation is a textual notation (in this case, the JSON format); however, the communication will be done by sending and receiving serialized Java objects.
Operation 2 + 3 → Request:
{
"operand1": 2,
"operand2": 3,
"operator": "+"
}
Now, as an answer, you might be thinking that just returning a number with the result of the operation is enough, right? However, let's consider some restrictions:
0+, -, *, /, ^, and sqrt)Therefore, if we send data that violates some of these restrictions, our protocol needs to signal that the operation cannot be performed. Also, if we send a symbol other than the supported operators, we have to signal that the operation is not recognized.
So, our answer is as follows:
Response:
status: textvalue: numberWe will now see a table where our protocol handles valid and invalid requests.
|
Operation |
Request |
Response |
Detail |
|
2 + 3 |
|
|
Successful operation. |
|
3 / 0 |
|
|
Operation failed. One of the operators violates the division constraint. |
|
5 ** 8 |
|
|
Operation failed. Informed that operation is not supported. |
Once we understand this concept, we can translate it into a programming language. But first, we need to understand how to make this communication possible through sockets!