⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connector.java

📁 基于SKYPE API 控件的开发示例 JSkype is an JNI implementation which enables Java clients to use the Skyp API
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            isInitialized = true;
        }
        Status tmpStatus = connect(timeout);
        if (tmpStatus == Status.ATTACHED) {
            try {
                sendApplicationName(getApplicationName());
                execute("PROTOCOL 9999", new String[] {"PROTOCOL "}, false);
            } catch (TimeOutException e) {
                tmpStatus = Status.NOT_RUNNING;
            }
        }
        return tmpStatus;
    }
    
    /**
     * Platform specific connector needs to implement it's own initialize method.
     * @param timeout Timeout in milliseconds to use while initializing.
     * @throws ConnectorException thrown when the connection to the Skype client has gone bad.
     */
    protected abstract void initialize(int timeout) throws ConnectorException;
    
    /**
     * Platform specific connector needs to implement it's own connect method. 
     * @param timeout Timeout is milliseconds to use while connecting.
     * @return Status after connecting.
     * @throws ConnectorException thrown when the connection to the Skype client has gone bad.
     */
    protected abstract Status connect(int timeout) throws ConnectorException;
    
    /**
     * Clean-up and disconnect from Skype client.
     */
    protected abstract void disposeImpl();
    
    /**
     * Send the application name to the Skype client.
     * todo: should this be abstract?
     * @param newApplicationName new application name.
     * @throws ConnectorException thrown when the connection to the Skype client has gone bad.
     */
    protected void sendApplicationName(String newApplicationName) throws ConnectorException {
    }

    /**
     * Clean up this connection instance and the platform specific one.
     * IMPORTANT!
     * This allows all native code to clean up and disconnect in a nice way.
     * @throws ConnectorException thrown when the connection to the Skype client has gone bad.
     */
    public final synchronized void dispose() throws ConnectorException {
        if (!isInitialized) {
            return;
        }
        disposeImpl();
        isInitialized = false;
    }

    
    /**
     * Check if connector is connected to the Skype Client.
     * @return true if connector is connected.
     * @throws ConnectorException thrown when the connection to the Skype client has gone bad.
     */
    public final boolean isRunning() throws ConnectorException {
        try {
            assureAttached();
            return true;
        } catch (TimeOutException e) {
            return false;
        } catch (NotAttachedException e) {
            return false;
        }
    }

    /**
     * Send a Skype command to the Skype client and handle responses by a message processor.
     * This method is not event-save. another reply could be picked-up.
     * Please use executeWithID or execute with responseheader instead.
     * @param command the command to send.
     * @param processor the message processor
     * @throws ConnectorException thrown when the connection to the Skype client has gone bad.
     */
    @Deprecated
    public final void execute(final String command, final MessageProcessor processor) throws ConnectorException {
        ConnectorUtils.checkNotNull("command", command);
        ConnectorUtils.checkNotNull("processor", processor);
        assureAttached();
        final Object lock = new Object();
        ConnectorListener listener = new AbstractConnectorListener() {
            public void messageReceived(ConnectorMessageEvent event) {
                processor.messageReceived(event.getMessage());
            }
        };
        processor.init(lock, listener);
        addConnectorListener(listener, false);
        fireMessageSent(command);
        synchronized (lock) {
            try {
                sendCommand(command);
                long start = System.currentTimeMillis();
                long commandResponseTime = getCommandTimeout();
                lock.wait(commandResponseTime);
                if (commandResponseTime <= System.currentTimeMillis() - start) {
                    setStatus(Status.NOT_RUNNING);
                    throw new TimeOutException("The '" + command + "' command failed by timeout.");
                }
            } catch (InterruptedException e) {
                throw new ConnectorException("The '" + command + "' command was interrupted.", e);
            } finally {
                removeConnectorListener(listener);
            }
        }
    }


    /**
     * Send a Skype command to the Skype client and wait for the reply.
     * This method is not event-save. another reply could be picked-up.
     * Please use executeWithID or execute with responseheader instead.
     * @param command the command to send.
     * @return the reply message.
     * @throws ConnectorException thrown when the connection to the Skype client has gone bad.
     */
    public final String execute(final String command) throws ConnectorException {
        ConnectorUtils.checkNotNull("command", command);
        return execute(command, command);
    }

    /**
     * Send a Skype command to the Skype client and wait for the reply, using an ID.
     * @param command The command to send.
     * @param responseHeader The expected reply header.
     * @return The reply.
     * @throws ConnectorException thrown when the connection to the Skype client has gone bad.
     */
    public final String executeWithId(final String command, final String responseHeader) throws ConnectorException {
        ConnectorUtils.checkNotNull("command", command);
        ConnectorUtils.checkNotNull("responseHeader", responseHeader);
        String header = "#" + (commandCount++) + " ";
        String response = execute(header + command, new String[] { header + responseHeader, header + "ERROR " }, true);
        return response.substring(header.length());
    }

    /**
     * Send a Skype command to the Skype client and wait for the reply based on the responseheader.
     * @param command the command to send.
     * @param responseHeader the expected reply header.
     * @return the reply.
     * @throws ConnectorException thrown when the connection to the Skype client has gone bad.
     */
    public final String execute(final String command, final String responseHeader) throws ConnectorException {
        ConnectorUtils.checkNotNull("responseHeader", responseHeader);
        return execute(command, new String[] { responseHeader, "ERROR " }, true);
    }

    /**
     * Send a Skype command to Skype client and allow for several reply headers.
     * @param command the command to send.
     * @param responseHeaders the expected response headers.
     * @return the reply.
     * @throws ConnectorException thrown when the connection to the Skype client has gone bad.
     */
    public final String execute(final String command, final String[] responseHeaders) throws ConnectorException {
        ConnectorUtils.checkNotNull("command", command);
        ConnectorUtils.checkNotNull("responseHeaders", responseHeaders);
        return execute(command, responseHeaders, true);
    }

    /**
     * Send a Skype command to Skype (actual implementation method) and wait for response.
     * @param command the command to send.
     * @param responseHeaders The expected response headers.
     * @param checkAttached if true the connector will first check if it is connected.
     * @return the response.
     * @throws ConnectorException thrown when the connection to the Skype client has gone bad.
     */
    protected final String execute(final String command, final String[] responseHeaders, final boolean checkAttached) throws ConnectorException {
        ConnectorUtils.checkNotNull("command", command);
        ConnectorUtils.checkNotNull("responseHeaders", responseHeaders);
        if (checkAttached) {
            assureAttached();
        }
        final Object lock = new Object();
        final String[] response = new String[1];
        ConnectorListener listener = new AbstractConnectorListener() {
            public void messageReceived(ConnectorMessageEvent event) {
                String message = event.getMessage();
                for (String responseHeader : responseHeaders) {
                    if (message.startsWith(responseHeader)) {
                        response[0] = message;
                        synchronized (lock) {
                            lock.notify();
                        }
                        return;
                    }
                }
            }
        };
        addConnectorListener(listener, false);
        fireMessageSent(command);
        synchronized (lock) {
            try {
                sendCommand(command);
                long start = System.currentTimeMillis();
                long commandResponseTime = getCommandTimeout();
                lock.wait(commandResponseTime);
                if (commandResponseTime <= System.currentTimeMillis() - start) {
                    setStatus(Status.NOT_RUNNING);
                    throw new TimeOutException("The '" + command + "' command failed by timeout.");
                }
            } catch (InterruptedException e) {
                throw new ConnectorException("The '" + command + "' command was interrupted.");
            } finally {
                removeConnectorListener(listener);
            }
        }
        return response[0];
    }

    /**
     * Event trigger called when a command is send to the Skype client.
     * @param message the message that has been send.
     */
    private void fireMessageSent(final String message) {
        assert message != null;
        ConnectorListener[] fireListeners = Connector.this.listeners.getListeners(ConnectorListener.class);
        if (fireListeners.length == 0) {
            return;
        }
        ConnectorMessageEvent event = new ConnectorMessageEvent(this, message);
        for (ConnectorListener listener : fireListeners) {
            listener.messageSent(event);
        }
    }

    /**
     * Send a command message to the Skype client.
     * @param command the command message to send.
     */
    protected abstract void sendCommand(String command);

    /**
     * Method to check the attached status of the connector to the Skype Client.
     * If it isn't connected it will connect.
     * @throws ConnectorException thrown when the connection to the Skype client has gone bad.
     */
    private void assureAttached() throws ConnectorException {
        Status attachedStatus = getStatus();
        if (attachedStatus != Status.ATTACHED) {
            attachedStatus = connect();
            if (attachedStatus != Status.ATTACHED) {
                throw new NotAttachedException(attachedStatus);
            }
        }
    }

    /**
     * Add a listener to this connector instance.
     * @param listener the listener to add.
     * @throws ConnectorException thrown when the connection to the Skype client has gone bad.
     */
    public final void addConnectorListener(final ConnectorListener listener) throws ConnectorException {
        addConnectorListener(listener, true);
    }

    /**
     * Add a listener to this connector if the connector is attached.
     * @param listener The listener to add.
     * @param checkAttached if true check if connector is attached.
     * @throws ConnectorException thrown when the connection to the Skype client has gone bad.
     */
    public final void addConnectorListener(final ConnectorListener listener, boolean checkAttached) throws ConnectorException {
        ConnectorUtils.checkNotNull("listener", listener);
        listeners.add(ConnectorListener.class, listener);
        if (checkAttached) {
            assureAttached();
        }
    }

    /**
     * Remove a listener from the collection of listeners. The listener will no longer be triggered when a event happens.
     * @param listener The listener to remove.
     */
    public final void removeConnectorListener(final ConnectorListener listener) {
        ConnectorUtils.checkNotNull("listener", listener);
        listeners.remove(ConnectorListener.class, listener);
    }

    /**
     * Fire a message received event.
     * @param message the message that triggered the event.
     */
    protected final void fireMessageReceived(final String message) {
        ConnectorUtils.checkNotNull("message", message);
        executor.execute(new Runnable() {
            public void run() {
                ConnectorListener[] fireListeners = Connector.this.listeners.getListeners(ConnectorListener.class);
                if (fireListeners.length == 0) {
                    return;
                }
                ConnectorMessageEvent event = new ConnectorMessageEvent(this, message);
                for (int i = fireListeners.length - 1; 0 <= i; i--) {
                    fireListeners[i].messageReceived(event);
                }
            }
        });
    }

    /**
     * Fire a status change event.
     * @param newStatus the new status that triggered this event.
     */
    protected final void fireStatusChanged(final Status newStatus) {
        ConnectorUtils.checkNotNull("status", newStatus);
        executor.execute(new Runnable() {
            public void run() {
                ConnectorListener[] fireListeners = Connector.this.listeners.getListeners(ConnectorListener.class);
                if (fireListeners.length == 0) {
                    return;
                }
                ConnectorStatusEvent event = new ConnectorStatusEvent(this, newStatus);
                for (int i = fireListeners.length - 1; 0 <= i; i--) {
                    fireListeners[i].statusChanged(event);
                }
            };
        });
    }
}

⌨️ 快捷键说明

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