pipeserviceprotocol.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 412 行 · 第 1/2 页

JAVA
412
字号
                    cause.printStackTrace();                }                throw new IOException("Cannot communicate with pipe service: " + cause);            }        }        if (DEBUG)            debugPrint(" connectServer " + serverName + ' ' + serverVersion + " completed");    }    /**     * Given that given instance of PipeServiceProtocol bound as pipe server this method blocks     * execution of current thread until appropriate pipe client is bound and connected to this     * server connection.     *     * @return newly created PipeServiceProtocol instance for communication with connected client     * @throws java.io.IOException if accept operation failed for some reason or if connection     * was closed     */    public PipeServiceProtocol acceptByServer() throws IOException {        synchronized (acceptLock) {            if (DEBUG)                debugPrint(" acceptByServer");            try {                synchronized (serviceConnLock) {                    SystemServiceDataMessage msg = SystemServiceMessage.newDataMessage();                    DataOutput out = msg.getDataOutput();                    out.writeInt(MAGIC_ACCEPT_PIPE_SERVER);                    out.writeLong(serverInstanceId);                    serviceConn.send(msg);                    SystemServiceLinkMessage linkMsg = (SystemServiceLinkMessage) serviceConn.receive();                    acceptLink = linkMsg.getLink();                }                // now block waiting for client to come                LinkMessage lMsg = acceptLink.receive();                DataInput in = new DataInputStream(new ByteArrayInputStream(lMsg.extractData()));                if (in.readInt() == MAGIC_BIND_PIPE_CLIENT) {                    PipeServiceProtocol service = new PipeServiceProtocol(token);                    service.serverVersionRequested = in.readUTF();                    lMsg = acceptLink.receive();                    service.inboundLink = lMsg.extractLink();                    lMsg = acceptLink.receive();                    service.outboundLink = lMsg.extractLink();                    if (DEBUG)                        debugPrint(" acceptByServer completed");                    return service;                } else {                    throw new IOException();                }            } catch (SystemServiceConnectionClosedException cause) {                if (DEBUG) {                    debugPrint(" ERR:");                    cause.printStackTrace();                }                throw new IOException("Cannot communicate with pipe service: " + cause);            } catch (InterruptedIOException cause) {                if (DEBUG)                    debugPrint(" acceptByServer aborted for " + serverInstanceId);                throw cause;            } catch (IOException cause) {                if (DEBUG) {                    debugPrint(" ERR:");                    cause.printStackTrace();                }                throw new IOException("Cannot communicate with pipe service: " + cause);            } finally {                acceptLink = null;            }        }    }    /**     * Unregisters given server connection from pipe service unblocking accept operation as needed.     *     * @throws java.io.IOException if there is any problem executing request     */    public void closeServer() throws IOException {        if (DEBUG)            debugPrint(" closeServer");                // close accept link if there is one. causes abort of pending acceptByServer        try {            if (acceptLink != null)                acceptLink.close();        } catch (Exception e) {            // ignored        }        // send notification to the pipe service        synchronized (serviceConnLock) {            try {                SystemServiceDataMessage msg = SystemServiceMessage.newDataMessage();                DataOutput out = msg.getDataOutput();                out.writeInt(MAGIC_CLOSE_PIPE_SERVER);                out.writeLong(serverInstanceId);                serviceConn.send(msg);                msg = (SystemServiceDataMessage) serviceConn.receive();                DataInput in = msg.getDataInput();                int result = in.readInt();                if (result != MAGIC_OK) {                    throw new IOException(in.readUTF());                }            } catch (SystemServiceConnectionClosedException cause) {                if (DEBUG) {                    debugPrint(" ERR:");                    cause.printStackTrace();                }                throw new IOException("Cannot communicate with pipe service: " + cause);            }        }    }    /**     * Unregisters given client connection and closes data links.     *     * @throws java.io.IOException if there is any problem executing operation     */    public void closeClient() throws IOException {        if (inboundLink != null)            inboundLink.close();        if (outboundLink != null)            outboundLink.close();    }    private void debugPrint(String msg) {        System.out.println("[pipe " + Isolate.currentIsolate().id() + '/' + debugInstanceId + "] " + msg);    }    static void debugPrintS(String msg) {        System.out.println("[pipe " + Isolate.currentIsolate().id() + "] " + msg);    }    static int parseVersion(String str) {        int dot1 = str.indexOf('.');        int dot2 = str.indexOf('.', dot1 + 1);        if (dot1 < 1 || dot2 == dot1 + 1)            throw new IllegalArgumentException("Malformed server version");        int version = 0;        try {            if (dot2 < 0)                dot2 = str.length();            version = Integer.parseInt(str.substring(0, dot1)) * 10000 +                    Integer.parseInt(str.substring(dot1 + 1, dot2)) * 100;            if (dot2 < str.length())                version += Integer.parseInt(str.substring(dot2));        } catch (NumberFormatException ex) {            throw new IllegalArgumentException("Malformed server version");        }        return version;    }    synchronized static long generateEndpointId() {        return nextEndpointIdToIssue++;    }    /**     * Returns outbound link for given client connection. It's up to caller to ensure this connection     * is client one and that it was already bound and connected to pipe server.     *     * @return outbound link     */    public Link getOutboundLink() {        return outboundLink;    }    /**     * Returns inbound link for given client connection. It's up to caller to ensure this connection     * is client one and that it was already bound and connected to pipe server.     *     * @return outbound link     */    public Link getInboundLink() {        return inboundLink;    }    /**     * Obtains name of the pipe server connection.     *     * @return pipe server name     */    public String getServerName() {        return serverName;    }    /**     * Obtains actual version of pipe server. Applicable to both server and client connections.     *     * @return actual version of pipe server     */    public String getServerVersionActual() {        return serverVersionActual;    }    /**     * Obtains version of the server connection which was requested by client.     *     * @return requested version of pipe server     */    public String getServerVersionRequested() {        return serverVersionRequested;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?