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

📄 baseuac.java

📁 即时下一代互联网的3G通信技术
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                            sc = (SocketConnection)Connector.open(params);                            System.out.println("Client opened connection " + params);                            socketIStream = sc.openInputStream();                            socketOStream = sc.openOutputStream();                            sender = new Sender(getSocketOStream());                            //Loop forever, receiving data                            while (true) {                                StringBuffer sb = new StringBuffer();                                int c = 0;                                while (((c = getSocketIStream().read()) != '\n') && (c != -1)) {                                    sb.append((char)c);                                }                                if (c == -1) {                                    break;                                }                                // Display message to user                                getTalkForm().append(new StringItem(friendName, sb.toString()));                            }                        } catch (IOException ex) {                            ex.printStackTrace();                        } finally {                            tearDown();                        }                    }                };        receiveThread.start();    }    private void send(final String message) {        getTalkForm().append(new StringItem(myName + ":", message));        if (sender != null) {            sender.send(message);        }    }    private void sendBye() {        Thread t =            new Thread() {                public void run() {                    runGauge();                    if ((dialog != null) && (dialog.getState() == SipDialog.CONFIRMED)) {                        try {                            SipClientConnection sc = dialog.getNewClientConnection("BYE");                            sc.send();                            System.out.println("Sending BYE ...");                            uaStatus.setStatus(BYE);                        } catch (Exception e) {                            e.printStackTrace();                        }                    } else {                        System.out.println("Dialog isn't initialized. Cannot send BYE.");                    }                }            };        t.start();    }    private void runGauge() {        if ((gaugeThread == null) || !gaugeThread.isAlive()) {            gaugeThread =                new Thread() {                        public void run() {                            progressGaugeFinished = false;                            progressGauge.setValue(0);                            boolean up = true;                            int i = 0;                            int c = 0;                            while (!progressGaugeFinished && (c < finishGauge)) {                                progressGauge.setValue((up) ? i++ : i--);                                up = ((i == 0) || (i == progressGauge.getMaxValue())) ? (up = !up)                                                                                      : up;                                try {                                    Thread.currentThread().sleep(100);                                } catch (InterruptedException e) {                                }                                c++;                            } //while                            if (backupForm != null) {                                setDisplay(backupForm);                            }                            progressGaugeFinished = true;                        }                    };            gaugeThread.start();        }    }    private void stopGauge() {        progressGaugeFinished = true;    }    public void notifyResponse(SipClientConnection sipClientConnection) {        try {            boolean ok = scc.receive(1000);            if (!ok) {                System.out.println("Response not received");                return;            }            int code = scc.getStatusCode();            System.out.println("Received status code: " + code);            if (uaStatus.getStatus() == REGISTERING) {                if (code == 200) {                    System.out.println("Received OK after REGISTER");                    uaStatus.setStatus(REGISTERED);                    stopGauge();                    commandAction(nextCmd, currentDisplay);                } else {                    stopGauge();                    failMessage = "Unknown response: " + code;                    commandAction(failedCmd, currentDisplay);                }            } else if (uaStatus.getStatus() == INVITING) {                if ((code >= 100) && (code < 200)) {                    System.out.println("Provisioning response: " + code);                } else if (code == 200) {                    System.out.println("Received OK after INVITE");                    scc.initAck(); // initialize and send ACK                    scc.send();                    dialog = scc.getDialog();                    uaStatus.setStatus(TALKING);                    openServerConnection(mySocket);                    stopGauge();                    commandAction(nextCmd, currentDisplay);                } else if (code == 486) { //BUSY HERE                    stopGauge();                    failMessage = "2nd client is busy.";                    commandAction(failedCmd, currentDisplay);                } else {                    stopGauge();                    failMessage = "Unknown response: " + code;                    commandAction(failedCmd, currentDisplay);                }            } else if (uaStatus.getStatus() == BYE) {                if (code == 200) {                    System.out.println("Received OK after BYE");                    uaStatus.setStatus(REGISTERED);                    ssc.close();                    stopGauge();                    commandAction(byeCmd, currentDisplay);                }            }        } catch (Exception e) {            e.printStackTrace();        }    }    public void notifyRequest(SipConnectionNotifier sipConnectionNotifier) {        try {            ssc = sipConnectionNotifier.acceptAndOpen(); // blocking            System.out.println("Received request: " + ssc.getMethod().toString());            if (ssc.getMethod().equals("INVITE")) {                String contentType = ssc.getHeader("Content-Type");                String contentLength = ssc.getHeader("Content-Length");                int length = Integer.parseInt(contentLength);                if (contentType.equals("text/plain")) {                    InputStream is = ssc.openContentInputStream();                    byte[] content = new byte[length];                    is.read(content);                    clientSockParams = new String(content);                    //parse socket connection params                    uaStatus.setStatus(RINGING);                    setDisplay(getRingingFrm(clientSockParams));                    ssc.initResponse(180); //RINGING                    ssc.send();                    dialog = ssc.getDialog();                }            } else if (ssc.getMethod().equals("ACK")) {                if (uaStatus.getStatus() == RINGING) {                    openClientConnection(clientSockParams);                    uaStatus.setStatus(TALKING);                    stopGauge();                    commandAction(nextCmd, currentDisplay);                }            } else if (ssc.getMethod().equals("BYE")) {                if (dialog.isSameDialog(ssc)) {                    setDisplay(getByeFrm());                    if (uaStatus.getStatus() == TALKING) {                        ssc.initResponse(200);                        ssc.send();                        getTalkForm().setTitle("Client disconnected !");                        try {                            if (serverSocket != null) {                                serverSocket.close();                            } else if (sc != null) {                                sc.close();                            }                        } catch (Exception e) {                        }                        ;                        uaStatus.setStatus(REGISTERED);                    }                } else {                    System.out.println("Not a same dialog");                }            }        } catch (Exception e) {            e.printStackTrace();        }    }    private void sendCancel() {        Thread t =            new Thread() {                public void run() {                    try {                        ssc.initResponse(486); //BUSY HERE                        ssc.send();                    } catch (Exception e) {                        System.out.println("Exception when sending cancel");                        e.printStackTrace();                    }                }            };        t.start();    }    private void sendAccepted() {        Thread t =            new Thread() {                public void run() {                    try {                        ssc.initResponse(200); //OK                        ssc.send();                        // save Dialog                        dialog = ssc.getDialog();                        ssc.close();                    } catch (Exception e) {                        System.out.println("Exception while sending OK");                        e.printStackTrace();                    }                }            };        t.start();    }    private boolean isIPAddress(String address) {        String addrex = address + ".";        char c;        int digcount = 0;        int numcount = 0;        for (int i = 0; i < addrex.length(); i++) {            c = addrex.charAt(i);            if (c == '.') {                digcount = 0;                numcount++;                if (numcount > 4) {                    return false;                }            } else {                if (('0' <= c) && (c <= '9')) {                    digcount++;                    if (digcount > 3) {                        return false;                    }                } else {                    return false;                }            }        }        return (numcount == 4);    }    /**     * Help to keep status of application.     *     */    class Status {        private boolean changed = false;        private int status = 0;        public int getStatus() {            return status;        }        public synchronized int waitUntilChanged() {            while (!changed) {                try {                    wait();                } catch (InterruptedException e) {                }            }            changed = false;            return status;        }        public synchronized void setStatus(int status) {            this.status = status;            changed = true;            notify();        }    }    public InputStream getSocketIStream() {        return socketIStream;    }    public OutputStream getSocketOStream() {        return socketOStream;    }}

⌨️ 快捷键说明

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