As it says.. I'm trying to develop a protocol, but I guess I am having
some trouble regarding C++ programming. This is what happens:

I have an agent class, with several variables inside it, and when I
receive a specific kind of packet I need to change some of these
variables' values (inside the recv function). I change two of them when
one of these messages arrives, but when I change it, just the value of one
of them is kept, the other value is lost when I return from the fuction.

Here is the problematic piece of the code (description of what happens is
below it):
 45 void ProtClusterAgent::recv(Packet* pkt, Handler*)
 46 {
 47    hdr_protcluster* hdr = (hdr_protcluster*)
hdr_protcluster::access(pkt);
 48    hdr_cmn *cmh = HDR_CMN(pkt);
 49
 50    if (cmh->prev_hop_ == myaddr && hdr->type != 0){ /* if hdr->type=0
then I musn't send anything yet */
 51       //printf("{[%d]}~>type: %d\n",myaddr,hdr->type);
 52       cmh->direction()= hdr_cmn::DOWN; /* in order to send efectively */
 53       send(pkt,0);
 54       return;
 55    }
 56
 57    if (hdr->type==10 && clusterAllow==true) { /* there is already
someone near me wanting to be a cluster-head..lets cance    l my
ellection then*/
 58       printf("[%d quitting election and entering node %d
cluster]\n",myaddr,cmh->prev_hop_);
 59
 60       head = cmh->prev_hop_; /* he is my masteeeeeeeer */
 61       clusterAllow = false;
 62       hdr->type=11; /* answering my cluster-head */
 63       cmh->next_hop() = cmh->prev_hop_;
 64       cmh->prev_hop_ = myaddr;
 65       cmh->addr_type()= NS_AF_INET;
 66       cmh->direction()= hdr_cmn::UP; /* send to transport layer,
probably */
 67       send(pkt,0);
 68       return;
 69    }

On the lines 60 and 61 I change the values of clusterAllow and head, but
the variable head loses its value: when I try to send a message to the
node which id should be stored on head, it tries to send the message to
the old value of head. Although, custerAllow have her value changed.

Here is part of my header file, describing my agent:
 48 class ProtClusterAgent : public Agent {
 49  public:
 50    ProtClusterAgent();
 51    //int myaddr,aim;
 52    int command(int argc, const char*const* argv);
 53    void recv(Packet*, Handler*);
 54  protected:
 55    MobileNode *node;
 56    lista sent,friends;
 57    int myaddr,p,head/*who is my cluster head? head==-1 ~> i'm a
cluster-head*/,delay_,tam/*max no of members per cluster*/;
 58    bool clusterAllow/*can i really be a cluster-head?*/;
 59    Trace *tracetarget;
 60 };


Any ideas of what I should do in order to have the value of my head
variable correctly changed?




Thanks,
Fernando.

Reply via email to