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

📄 application.java

📁 基于SKYPE API 控件的开发示例 JSkype is an JNI implementation which enables Java clients to use the Skyp API
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            fireStreamEvents(streamIds);
            if("".equals(streamIds)) {
                return new Stream[0];
            }
            String[] ids = streamIds.split(" ");
            Stream[] results = new Stream[ids.length];
            for(int i = 0; i < ids.length; i++) {
                results[i] = streams.get(ids[i]);
            }
            return results;
        }
    }

    /**
     * @TODO Fill in this javadoc.
     * @param newStreamIdList idunno.
     */
    private void fireStreamEvents(final String newStreamIdList) {
        synchronized(streams) {
            String[] newStreamIds = "".equals(newStreamIdList)? new String[0]: newStreamIdList.split(" ");
            for(String streamId: newStreamIds) {
                if(!streams.containsKey(streamId)) {
                    Stream stream = new Stream(this, streamId);
                    streams.put(streamId, stream);
                    fireConnected(stream);
                }
            }
            String[] oldStreamIds = streams.keySet().toArray(new String[0]);
            NEXT: for(String oldStreamId: oldStreamIds) {
                for(String newStreamId: newStreamIds) {
                    if(oldStreamId.equals(newStreamId)) {
                        continue NEXT;
                    }
                }
                Stream stream = streams.remove(oldStreamId);
                fireDisconnected(stream);
            }
        }
    }

    /**
     * Fire an event if a AP2AP connection is connected.
     * 
     * @param stream The connected stream.
     */
    private void fireConnected(final Stream stream) {
        assert stream != null;
        // to prevent ConcurrentModificationException
        ApplicationListener[] myListeners = this.listeners.toArray(new ApplicationListener[0]);
        for(ApplicationListener listener: myListeners) {
            try {
                listener.connected(stream);
            } catch(Throwable e) {
                Utils.handleUncaughtException(e, exceptionHandler);
            }
        }
    }

    /**
     * Fire an event if an AP2AP connection is closed.
     * 
     * @param stream the closed AP2AP stream.
     */
    private void fireDisconnected(final Stream stream) {
        assert stream != null;
        // to prevent ConcurrentModificationException
        ApplicationListener[] myListeners = this.listeners.toArray(new ApplicationListener[0]);
        for(ApplicationListener listener: myListeners) {
            try {
                listener.disconnected(stream);
            } catch(Throwable e) {
                Utils.handleUncaughtException(e, exceptionHandler);
            }
        }
    }

    /**
     * Add a listener for events to this AP2AP implementation.
     * 
     * @param listener the listener which will be triggered.
     */
    public void addApplicationListener(final ApplicationListener listener) {
        Utils.checkNotNull("listener", listener);
        listeners.add(listener);
    }

    /**
     * Remove a listener for this AP2AP implementation. If listener is already removed nothing happens.
     * 
     * @param listener The listener that has to be removed.
     */
    public void removeApplicationListener(final ApplicationListener listener) {
        Utils.checkNotNull("listener", listener);
        listeners.remove(listener);
    }

    /**
     * This class implements a listener for data packets.
     * 
     * @author Koji Hisano
     */
    private class DataListener extends AbstractConnectorListener {
        /**
         * Message received event method. It checks the name of the AP2AP application name and strips the SKYPE protocol data from the message. Then it will call handleData(String) to process the inner data.
         * 
         * @param event The event and message that triggered this listener.
         */
        public void messageReceived(ConnectorMessageEvent event) {
            String message = event.getMessage();
            String streamsHeader = "APPLICATION " + getName() + " STREAMS ";
            if(message.startsWith(streamsHeader)) {
                String streamIds = message.substring(streamsHeader.length());
                fireStreamEvents(streamIds);
            }
            final String dataHeader = "APPLICATION " + getName() + " ";
            if(message.startsWith(dataHeader)) {
                handleData(message.substring(dataHeader.length()));
            }
        }

        /**
         * This method will process the inner data of a data message.
         * 
         * @param dataResponse the received data.
         */
        private void handleData(final String dataResponse) {
            try {
                if(isReceivedText(dataResponse)) {
                    String data = dataResponse.substring("RECEIVED ".length());
                    String streamId = data.substring(0, data.indexOf('='));
                    String dataHeader = "ALTER APPLICATION " + getName() + " READ " + streamId;
                    String response = Connector.getInstance().executeWithId(dataHeader, dataHeader);
                    Utils.checkError(response);
                    String text = response.substring(dataHeader.length() + 1);
                    synchronized(streams) {
                        if(streams.containsKey(streamId)) {
                            streams.get(streamId).fireTextReceived(text);
                        }
                    }
                } else if(isReceivedDatagram(dataResponse)) {
                    String data = dataResponse.substring("DATAGRAM ".length());
                    String streamId = data.substring(0, data.indexOf(' '));
                    String datagram = data.substring(data.indexOf(' ') + 1);
                    synchronized(streams) {
                        if(streams.containsKey(streamId)) {
                            streams.get(streamId).fireDatagramReceived(datagram);
                        }
                    }
                }
            } catch(Exception e) {
                Utils.handleUncaughtException(e, exceptionHandler);
            }
        }

        /**
         * Check if received data is text instead of DATAGRAM.
         * 
         * @param dataResponse the data to check.
         * @return true if the data is text.
         */
        private boolean isReceivedText(final String dataResponse) {
            return dataResponse.startsWith("RECEIVED ") && ("RECEIVED ".length() < dataResponse.length());
        }

        /**
         * Check if received data is DATAGRAM instead of text.
         * 
         * @param dataResponse the data to check.
         * @return true if the data is DATAGRAM.
         */
        private boolean isReceivedDatagram(final String dataResponse) {
            return dataResponse.startsWith("DATAGRAM ");
        }
    }

    /**
     * Find user to whom Skype can connect using AP2AP.
     * 
     * @return Array of connectable users.
     * @throws SkypeException when connection is gone bad.
     */
    public Friend[] getAllConnectableFriends() throws SkypeException {
        return getAllFriends("CONNECTABLE");
    }

    /**
     * Find all users to whom SKype is connecting a AP2AP connection.
     * 
     * @return Array of user to whom a connecting AP2AP is in progress.
     * @throws SkypeException when connection is gone bad.
     */
    public Friend[] getAllConnectingFriends() throws SkypeException {
        return getAllFriends("CONNECTING");
    }

    /**
     * Find all user with whom we have a established AP2AP connection.
     * 
     * @return all AP2AP connected users.
     * @throws SkypeException when connection is gone bad.
     */
    public Friend[] getAllConnectedFriends() throws SkypeException {
        return getAllFriends("STREAMS");
    }

    /**
     * Find all user to whom we are sending data using a AP2AP connection.
     * 
     * @return an array of users that we are sending to.
     * @throws SkypeException when connection is gone bad.
     */
    public Friend[] getAllSendingFriends() throws SkypeException {
        return getAllFriends("SENDING");
    }

    /**
     * Find all users which we have received data from using an AP2AP connection.
     * 
     * @return array of found users.
     * @throws SkypeException when connection is gone bad.
     */
    public Friend[] getAllReceivedFriends() throws SkypeException {
        return getAllFriends("RECEIVED");
    }

    /**
     * Search method to find friend with a parameter.
     * 
     * @param type The searchstring.
     * @return array of found friends.
     * @throws SkypeException when connection is gone bad.
     */
    private Friend[] getAllFriends(final String type) throws SkypeException {
        try {
            String command = "GET APPLICATION " + getName() + " " + type;
            String responseHeader = "APPLICATION " + getName() + " " + type + " ";
            String response = Connector.getInstance().executeWithId(command, responseHeader);
            Utils.checkError(response);
            return extractFriends(response.substring(responseHeader.length()));
        } catch(ConnectorException e) {
            Utils.convertToSkypeException(e);
            return null;
        }
    }

    /**
     * Parse the results of a search action for names.
     * 
     * @param list with search results.
     * @return array of parsed users.
     * @throws SkypeException when connection is gone bad.
     */
    private Friend[] extractFriends(final String list) throws SkypeException {
        assert list != null;
        if("".equals(list)) {
            return new Friend[0];
        }
        String[] ids = list.split(" ");
        for(int i = 0; i < ids.length; i++) {
            String id = ids[i];
            if(id.contains(":")) {
                ids[i] = id.substring(0, id.indexOf(':'));
            }
        }
        Friend[] allFriends = Skype.getContactList().getAllFriends();
        List<Friend> friends = new ArrayList<Friend>();
        for(String id: ids) {
            for(Friend friend: allFriends) {
                if(friend.getId().equals(id)) {
                    friends.add(friend);
                }
            }
        }
        return friends.toArray(new Friend[0]);
    }
}

⌨️ 快捷键说明

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