/ping
commandFor this lab we will add the ability to ping other clients. This command is used to measure how long it takes for a message to reach a specific user and be returned.
When connected to the server, the user frodo
can type /ping bilbo
to send a ping request to user bilbo
.
The client will receive a message from the GUI with the form: {ping, "bilbo"}
.
The client should check if user bilbo
is connected to the server, returning error atom user_not_found
if not.
If he is connected, the ping request is passed on to bilbo
,
who should then send a response (pong) directly to the original user frodo
.
When frodo
receives the pong, he should forward this to the GUI using the following line of code:
gen_server:call(list_to_atom(GUIName), {msg_to_SYSTEM, io_lib:format("Pong ~s: ~pms", [UserNick,Diff])}),
where:
GUIName
contains the GUI name as given in client:initial_state/2
UserNick
is a string containing the nick (in this case "bilbo"
)Diff
is the time difference in milliseconds since the ping request was first sentYou cannot ping when not connected to the server.
The client sending the ping shouldn't wait synchronously for a pong response. In other words, if the client being pinged takes many seconds to respond, this shouldn't cause a timeout.
Note that you should be able to ping yourself! The ping/pongs must still make a round trip to the server and back, which can be useful to test.
Use the Erlang BIF now/0
to get the current timestamp.
You can use the function helper:timeSince/1
to calculate the time since an old timestamp (in ms).
Since all proceseses will be running on the same machine, your pings will return very quickly.
To be sure that things work, try introducing an artificial delay before sending a pong using timer:sleep/1
(but remove this when running the test suite!)
The test suite from the skeleton code already contains a test for the ping
command.
To run it, simply type the following in the system console:
$ make -s run_ping_tests
For more information about running the tests, see here.