【发布时间】:2014-06-22 21:56:29
【问题描述】:
我正在编写一种在简单的对等网络中搜索客户端的方法。 我编写的 searchForResponsibleClient 方法在该网络中获取一个点,并检查调用 searchForResponsibleClient 方法的客户端是否对该点负责。
如果它负责,它会返回自己。
如果它不负责,它会查看其客户端邻居(保存在对象中)并检查是否有任何邻居负责,如果是,则返回邻居。
这两种情况都可以正常工作。
如果邻居不负责,但是,调用客户端的第一个邻居被取走,并再次递归调用 searchForResponsibleClient 方法。
当我递归调用它时,我在控制台得到正确的输出,但返回值错误。
这是我的代码:
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;
}
这是我控制台的输出:
使用 client0 调用 searchForResponsibleClient
调用客户端:client0
当前客户端包含元素:false
++++++++++++++++++++++++++++++++++++++++++++++
调用元素:client0 有这个邻居:
客户3
包含位置:假
+++++++++++++++++++++++++++++++++++++++++++++++
第一个邻居元素负责:false
执行另一个搜索,此客户端正在调用它:client0 与它找到但不是正确的客户端:client3
使用 client3 调用 searchForResponsibleClient
调用客户端:client3
当前客户端包含元素:false
++++++++++++++++++++++++++++++++++++++++++++++
调用元素:client3 有这个邻居:
客户端4
包含位置:true
找到黄金邻居;客户端4
!!!!!!达到了我不应该达到的位置! !!!!!!
在这种情况下,client4 应该包含位置(实际上是这种情况),但不是 client4,而是返回 null,这会导致 NullpointerException。 我的退货声明一定是在某个地方犯了错误,但不知何故,我只是看不出错误可能出在哪里。
【问题讨论】: