从服务器返回响应

时间:2015-03-04 00:06:41

标签: java sockets minecraft bukkit

我正在开发一个允许多个Minecraft服务器进行通信的程序,但是我需要一种方法来从一个基本上等待'等待的方法中返回一个字符串。作出回应。当插件尝试检索我正在调用服务器属性的内容时,会向该目标服务器发送该属性的请求。由于这使用了一种事件系统,因此在接收信息时会运行由插件定义的另一种方法。我将在下面的代码中解释更多内容。

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

当从目标服务器收到String时,CSC(我的插件)根据语法和内容处理信息,并相应地处理它。插件如何访问此信息? ServerResponse课程如下。

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;
    }

}  

我创建的示例如下所示,但我非常怀疑它是最有效的方法。基本上,我怎么能告诉程序等到它有一个返回字符串的响应?

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;
}

1 个答案:

答案 0 :(得分:0)

您可以使用名为Observer的设计模式,并且您将为属性更改事件注册一个侦听器,当属性更改时,您将为所有侦听器触发一个事件,在此列表器中您可以拥有一个Lock对象你得到这个锁并等待它。

这样的事情:

这只是一个例子,不会工作,你需要创建你的。

public ResponseListener implements ServerResponseListener {

    // Object use only for locking and waiting
    private Object LOCK = new Object();

    private ServerResponse serverResponse;

    // Method triggered by the observable who has its state changed and notfies its observers (this one)
    public void onChange(ServerResponse serverResponse) {
        this.serverResponse = serverResponse;

        synchronzed(LOCK) {
            // Notifies the threads waiting this object monitor
            LOCK.notifyAll();
        }
    }

    // Gets the object for locking
    public Object getLock() {
        return this.LOCK();
    }

    // Gets the response
    public ServerResponse getResponse() {
        return this.serverResponse;
    }
}

public String property(String property) {
    // Instantiate the listener
    ServerResponseListener listener = new ServerResponseListener();

    // Pass the listener as a parameter to be triggered when the response is received by the server
    ServerResponse response = super.retrieveProperty(property, listener);

    // Check if the response isn't there already
    if(listener.getResponse() == null) {
        Object lock = listener.getLock();

        // Get its monitor
        synchronzed(lock) {
            // The time is the limit for wait, use like a timeout
            try {lock.wait(1000 * 30);}catch(Excepion ex){}
        }

        ServerResponse resp = listener.getResponse();

        if(resp == null) throw new Exception("Timeout while waiting for the response");

        String aResponse = response.getResponse();

        return aResponse;
    }

    return listener.getResponse();
}