Purpose of Exec function and Handler

Purpose of Exec function and Handler

by Mathieu Caboche -
Number of replies: 8
Hello,

I do not completely understand the way we are supposed to use handlers and the Exec function.

My understanding is that the handlers are registered in the NewGossiper function (given). The handlers use the Exec function to handle different types of received messages.

However I don't get if we are ever supposed to call Exec ourselves (either directly or via ExecuteHandler). Additionally, it doesn't seem clear from the handout or the code what functionnality should be in Exec, i.e. should Exec be sending any messages? Or is Run responsible for this? Or maybe AddMessage?

I feel the purpose and functionnality of Exec should be explained more precisely.

Thanks!
Mathieu
In reply to Mathieu Caboche

Purpose of Exec function and Handler

by Mathieu Caboche -
Also, I wonder if (and where) we are supposed to be listening for client connections in the gossiper since there is no mention of this either in the code or in the handout.
In reply to Mathieu Caboche

Purpose of Exec function and Handler

by Adrian Matthijs Christian Hamelink -
The controller.go handles client messages. If you look at the code, you will see that it calls Gossiper.AddSimpleMessage when a POST is called on /message
In reply to Adrian Matthijs Christian Hamelink

Purpose of Exec function and Handler

by Mathieu Caboche -
Thanks !

I understand that this means that Exec should not be sending anything, rather just processing the messages, is that right?
In reply to Mathieu Caboche

Purpose of Exec function and Handler

by Mathieu Caboche -
Also, I wonder if (and where) we are supposed to be listening for client connections in the gossiper since there is no mention of this either in the code or in the handout.
In reply to Mathieu Caboche

Purpose of Exec function and Handler

by Adrian Matthijs Christian Hamelink -
The code for handlers is a bit complicated to understand but rather elegant. The gossiper stores the types of messages it can handle via the RegisterHandler method.
Using these, the ExecuteHandler method, when called with a pointer to a strict with the same type (here *SimpleMessage), will find the Exec method defined for this type.
In future homework, if we need to handle other message types, you can continue calling ExecuteHandler on the new message type and just define a new Exec method.
Therefore, the (*SimpleMessage) Exec method is called by ExecuteHandler when the gossiper receives a new message from the network, and you need to only call ExecuteHandler

This is just from my understanding of the code. There may be other reasons why it was designed as such.
In reply to Mathieu Caboche

Re: Purpose of Exec function and Handler

by Kirill Nikitin -

Thanks to Adrian for the great explanation!

Mathieu, we indeed suggest in the skeleton to define the Exec method that handles a given message type (in the case of this homework, it's SimpleMessage). Adrian explains well the logic of how it can be executed. Note that we, however, do not require you to strictly follow the proposed structure in your implementation. In fact, designing the logic of your program is a part of this assignment (and of the learning process). Hence, I suggest that you consider possible designs and decide yourself where certain logic, e.g., of message sending, should be placed.

Best,

Kirill