mardi 3 mars 2015

Return Response from Server



I'm working on a program to allow several Minecraft servers to communicate, but I need to have a way to return a string from a method that essentially 'waits' for a response. When a plugin attempts to retrieve what I'm calling Server Properties, a request is sent to the target server for that property. Since this uses a sort of event system, where another method, defined by the plugin, is run when information is received. I'll explain more in the code below.



public final ServerResponse retrieveProperty(String property) {
if (!PropertyHandler.isValidProperty()) {
return null;
}
try {
return Handler.retrieveProperty(property,
CONNECTIONS.get(entryPoint).getKey().getName());
} catch (Exception e) {
return null;
}
}


When a String is received from the target server, CSC (my plugin), handles the information based on the syntax and contents, and deals with it accordingly. How would a plugin the access this information? The ServerResponse class is below.



public final class ServerResponse {

private String plugin;
private String identifier;

private boolean hasResponse = false;
private String response = null;

protected ServerResponse(String plugin) {
this.plugin = plugin;
this.identifier = UUID.randomUUID().toString().replaceAll("-", "")
.substring(0, 7);
}

public String getPlugin() {
return new String(this.plugin);
}

public String getIdentifier() {
return this.identifier;
}

public boolean hasResponse() {
return this.hasResponse;
}

protected void respond(String response) {
this.hasResponse = true;
this.response = response;
}

public String getResponse() {
return this.response;
}

}


The example I have created is shown below, but I highly doubt it is the most efficient method. Basically, how can I tell the program to wait until it has a response to return a string?



public String property(String property) {
ServerResponse response = super.retrieveProperty(property);
String aResponse = response.getResponse();
if (!response.hasResponse()) {
while (true) {
if (!response.hasResponse())
continue;
else {
aResponse = response.getResponse();
break;
}
}
}
return aResponse;
}



Aucun commentaire:

Enregistrer un commentaire