📄 baseuac.java
字号:
} return sendFrm; } private Form getFailFrm(String msg) { if (failFrm == null) { failFrm = new Form("Error"); failFrm.addCommand(exitCmd); failFrm.setCommandListener(this); failFrm.append(new StringItem("", "")); } StringItem si = (StringItem)failFrm.get(0); si.setText(msg); return failFrm; } private Form getByeFrm() { if (byeFrm == null) { byeFrm = new Form("BYE"); byeFrm.append("Received BYE from 2nd terminal.\nTerminating GoSIP session ..."); byeFrm.addCommand(okCmd); byeFrm.setCommandListener(this); } return byeFrm; } public void commandAction(Command command, Displayable displayable) { if ((command == exitCmd) && (displayable == registerFrm)) { destroyApp(false); } else if ((command == exitCmd) && (displayable == failFrm)) { destroyApp(false); } else if ((command == registerCmd) && (displayable == registerFrm)) { setDisplay(getWaitScreen("Registration pending ...", 0, null)); Thread t = listen(this); register(this, t); } else if ((command == backCmd) && (displayable == waitScreen)) { setDisplay(backDisplay); } else if ((command == nextCmd) && (displayable == waitScreen) && (backDisplay == registerFrm)) { setDisplay(getInviteForm()); } else if ((command == nextCmd) && (displayable == waitScreen) && (backDisplay == inviteFrm)) { setDisplay(getTalkForm()); } else if ((command == backCmd) && (displayable == waitScreen) && (backDisplay == registerFrm)) { setDisplay(getRegisterForm()); } else if ((command == backCmd) && (displayable == waitScreen) && (backDisplay == inviteFrm)) { setDisplay(getInviteForm()); } else if ((command == failedCmd) && (displayable == waitScreen) && (backDisplay == registerFrm)) { setDisplay(getFailFrm("Failed to register:\n Cause: " + failMessage)); } else if ((command == failedCmd) && (displayable == waitScreen) && (backDisplay == inviteFrm)) { setDisplay(getFailFrm("Failed to invite:\n Cause: " + failMessage)); } else if ((command == byeCmd) && (displayable == waitScreen)) { setDisplay(getInviteForm()); } else if ((command == denyCmd) && (displayable == ringingFrm)) { sendCancel(); } else if ((command == exitCmd) && (displayable == proxyFrm)) { destroyApp(false); } else if ((command == nextCmd) && (displayable == proxyFrm)) { TextField tfield = (TextField)proxyFrm.get(0); String proxy = tfield.getString(); if ((proxy.length() == 0) || isIPAddress(proxy)) { if (proxyFrm.size() == 1) { proxyFrm.append(new StringItem(null, "Proxy name can't be empty or plain ip address. Use valid hostname.")); setDisplay(getProxyFrm()); } } else { if (proxyFrm.size() > 1) { proxyFrm.delete(1); } setProxyAddress(proxy); setDisplay(getRegisterForm()); } } else if ((command == answerCmd) && (displayable == ringingFrm)) { setDisplay(getWaitScreen("Accepting ...", 0, null)); sendAccepted(); try { Thread.currentThread().sleep(1500); } catch (Exception e) { } if (uaStatus.getStatus() == RINGING) { openClientConnection(clientSockParams); uaStatus.setStatus(TALKING); stopGauge(); commandAction(nextCmd, currentDisplay); } } else if ((command == inviteCmd) && (displayable == inviteFrm)) { setDisplay(getWaitScreen("Invite pending ...", 0, null)); invite(this); } else if ((command == byeCmd) && (displayable == talkFrm)) { if (uaStatus.getStatus() == TALKING) { setDisplay(getWaitScreen("Bye ...", 10, getInviteForm())); sendBye(); } else if (uaStatus.getStatus() == REGISTERED) { //do nothing setDisplay(getInviteForm()); } } else if ((command == sendCmd) && (displayable == talkFrm)) { setDisplay(getSendForm()); } else if ((command == nextCmd) && (displayable == waitScreen) && (backDisplay == ringingFrm)) { setDisplay(getTalkForm()); } else if ((command == okCmd) && (displayable == byeFrm)) { setDisplay(getInviteForm()); } else if ((command == okCmd) && (displayable == sendFrm)) { TextField txtField = (TextField)sendFrm.get(0); if ((txtField != null) && (txtField.getString().length() > 0)) { send(txtField.getString()); } setDisplay(getTalkForm()); } else if ((command == backCmd) && (displayable == sendFrm)) { setDisplay(backDisplay); } else if ((command == exitCmd) && (displayable == inviteFrm)) { destroyApp(false); } } private Thread listen(final SipServerConnectionListener listener) { Thread t = new Thread() { public void run() { try { if (scn != null) { scn.close(); } scn = (SipConnectionNotifier)Connector.open("sip:" + mySipPort); scn.setListener(listener); try { Thread.currentThread().sleep((1000)); } catch (Exception e) { } } catch (IOException ex) { ex.printStackTrace(); } } }; t.start(); return t; } private void register(final SipClientConnectionListener listener, final Thread waitFor) { Thread t = new Thread() { public void run() { runGauge(); try { try { if (waitFor != null) { waitFor.join(); } else { } } catch (InterruptedException ie) { } scc = (SipClientConnection)Connector.open("sip:" + proxyAddress + ":5060;transport=udp"); scc.setListener(listener); scc.initRequest("REGISTER", scn); String adr = myDisplayName + " <sip:" + myName + "@" + scn.getLocalAddress() + ":" + scn.getLocalPort() + ">"; scc.setHeader("To", adr); scc.setHeader("From", adr); scc.setHeader("Content-Length", "0"); scc.setHeader("Max-Forwards", "6"); uaStatus.setStatus(REGISTERING); scc.send(); uaStatus.waitUntilChanged(); progressGaugeFinished = true; } catch (Exception e) { e.printStackTrace(); failMessage = e.getMessage(); commandAction(failedCmd, currentDisplay); return; } } }; t.start(); } private void invite(final SipClientConnectionListener listener) { Thread t = new Thread() { public void run() { runGauge(); try { String host = scn.getLocalAddress(); String adr = "sip:" + myName + "@" + host + ":" + scn.getLocalPort(); String toAdr = "sip:" + friendName + "@" + friendDomain + ":" + friendSipPort; scc = (SipClientConnection)Connector.open(toAdr); scc.setListener(listener); scc.initRequest("INVITE", scn); String message = "socket://" + host + ":" + mySocket; scc.setHeader("To", toAdr); scc.setHeader("From", adr); scc.setHeader("Content-Type", "text/plain"); scc.setHeader("Content-Length", Integer.toString(message.length())); scc.setHeader("Max-Forwards", "6"); OutputStream os = scc.openContentOutputStream(); os.write(message.getBytes()); uaStatus.setStatus(INVITING); os.close(); // close and send uaStatus.waitUntilChanged(); //Thread.currentThread().sleep(4000); progressGaugeFinished = true; } catch (Exception e) { e.printStackTrace(); failMessage = e.getMessage(); stopGauge(); commandAction(failedCmd, currentDisplay); return; } } }; t.start(); } private synchronized void tearDown() { try { if (getSocketIStream() != null) { getSocketIStream().close(); } if (getSocketOStream() != null) { getSocketOStream().close(); } if (sender != null) { sender.stop(); } if (scc != null) { scc.close(); } if (ssc != null) { ssc.close(); } if (scn != null) { scn.close(); } if (sc != null) { sc.close(); } if (serverSocket != null) { serverSocket.close(); } } catch (Exception e) { } } private void openServerConnection(int socket) { Thread t = new Thread() { public void run() { try { serverSocket = (ServerSocketConnection)Connector.open("socket://:" + mySocket); System.out.println("waiting for connection socket://:" + mySocket); SocketConnection sc = (SocketConnection)serverSocket.acceptAndOpen(); System.out.println("connection accepted from: " + sc.getAddress()); socketIStream = sc.openInputStream(); socketOStream = sc.openOutputStream(); sender = new Sender(getSocketOStream()); while (true) { StringBuffer sb = new StringBuffer(); int c = 0; while (((c = getSocketIStream().read()) != '\n') && (c != -1)) { sb.append((char)c); } if (c == -1) { break; } getTalkForm().append(new StringItem(friendName + ":", sb.toString())); } //while } catch (IOException ioe) { failMessage = "Cannot open server socket"; } finally { tearDown(); } } }; t.start(); } private void openClientConnection(final String params) { receiveThread = new Thread() { public void run() { try { //open socket
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -