Question

I am writing a method that searches for clients in a simple peer to peer network. the method I wrote searchForResponsibleClient takes a point in this network and checks if the client which was calling the searchForResponsibleClient method is responsible for this point.

If it is responsible it returns itself.

If it is not responsible it looks at its client neighbours (which are saved in the object) and checks if any of the neighbours is responsible and if so it returns the neighbour.

These two scenarios work fine.

If the neighbours are not responsible, howewer, the first neighbour of the calling client is taken and the searchForResponsibleClient method is called again recursively.

When I call it recursively I get the right output at the console but the wrong return value.

Here is my code:

public ClientInterface searchForResponsibleClient(Position p) {
    System.out.println("calling searchForResponsibleClient with " + this.uniqueID);


    boolean contains =  this.clientArea.contains(p);
    System.out.println("calling client: "+ this.uniqueID);
    System.out.println("The current client contains the element:"+ contains);

    // the current client contains the document
    if (contains){
        System.out.println("current element is responsible" +this.uniqueID);
        return this;
    }

    // apparently the current client is not responsible lets check the clients neighbours.
    System.out.println("++++++++++++++++++++++++++++++++++++++++++");
    System.out.println("calling element: "+ this.uniqueID + " has this neighbours:");
    for(ClientInterface neighbour: this.neighbours){
        System.out.println(neighbour.getUniqueID());
        System.out.println("contains the position : "+neighbour.getArea().contains(p));
        if(neighbour.getArea().contains(p)){
            System.out.println("found golden neighbour; "+neighbour.getUniqueID());
            return neighbour;
        }
    }

   System.out.println("+++++++++++++++++++++++++++++++++++++++++++");


   // if the neighbours are not responsible lets get the first neighbour of the neighbourlist and restart the search
   ClientInterface temporalClient = this.neighbours.get(0);
   System.out.println("the first neighbour element is responsible: "+ temporalClient.getArea().contains(p));

   if (!temporalClient.getArea().contains(p)){
        System.out.println("Performing another search this client is callling it: "+ this.uniqueID +" with the client that it found but was not the right one: "+ temporalClient.getUniqueID());
        temporalClient.searchForResponsibleClient(p);
   }
   else {
       return temporalClient;
    }
  System.out.println("!!!!!! reached the position that i should never reach! !!!!!");
  return null;
}

And here is the output from my console:

calling searchForResponsibleClient with client0
calling client: client0
The current client contains the element:false
++++++++++++++++++++++++++++++++++++++++++
calling element: client0 has this neighbours:
client3
contains the position : false
+++++++++++++++++++++++++++++++++++++++++++
the first neighbour element is responsible: false
Performing another search this client is callling it: client0 with the client that it found but was not the right one: client3
calling searchForResponsibleClient with client3
calling client: client3
The current client contains the element:false
++++++++++++++++++++++++++++++++++++++++++ calling element: client3 has this neighbours:
client4
contains the position : true
found golden neighbour; client4
!!!!!! reached the position that i should never reach! !!!!!

In this case client4 should contain the position(which is actually the case) but instead of client4 null gets returned which causes a NullpointerException. I must have made a mistake somewhere with my return statement but somehow I just do not see where the mistake could be.

Was it helpful?

Solution

You need to return the final value that you found. It looks like modifying this line:

temporalClient.searchForResponsibleClient(p);

to

return temporalClient.searchForResponsibleClient(p);

should do the trick. This will explain why you're reaching the code you otherwise think you shouldn't get to

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top