📄 baseuac.java
字号:
/* * * Copyright (c) 2007, Sun Microsystems, Inc. * * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of Sun Microsystems nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */package example.sip;import java.io.IOException;import java.io.InputStream;import java.io.InterruptedIOException;import java.io.OutputStream;import javax.microedition.io.Connector;import javax.microedition.io.ServerSocketConnection;import javax.microedition.io.SocketConnection;import javax.microedition.lcdui.*;import javax.microedition.midlet.*;import javax.microedition.sip.SipClientConnection;import javax.microedition.sip.SipClientConnectionListener;import javax.microedition.sip.SipConnectionNotifier;import javax.microedition.sip.SipDialog;import javax.microedition.sip.SipException;import javax.microedition.sip.SipServerConnection;import javax.microedition.sip.SipServerConnectionListener;/** * Application demonstrates sip clients talking via sip server and registrar. * Both clients register to sip registrar. Send connection informations * in INVITE message to each other and start to communicate via sockets. * * Application demonstrates REGISTER, INVITE, BYE requests and * OK, RINGING responses. * * @version 1.2 */public abstract class BaseUAC extends MIDlet implements CommandListener, SipClientConnectionListener, SipServerConnectionListener { /** client states */ private static final int DISCONNECTED = 0; private static final int REGISTERING = 1; private static final int REGISTERED = 2; private static final int INVITING = 3; private static final int TALKING = 4; private static final int RINGING = 5; private static final int BYE = 6; /** user name if the client */ public String myName = "a"; /** display name of the client */ public String myDisplayName = "A"; /** socket used by this client */ public int mySocket = 1111; /** sip port the client is listening on */ public int mySipPort = 9000; /** user name of second client */ public String friendName = "B"; /** socket used by second client */ public int friendSocket = 2222; /** sip port of second client * we need this because running both * clients on the same machine. That isn't * typical use case. */ public int friendSipPort = 9090; /** second client's domain */ public String friendDomain = "localhost"; /** forms used in ui */ protected Form proxyFrm = null; protected Form registerFrm = null; protected Form waitScreen = null; protected Form inviteFrm = null; protected Form talkFrm = null; protected Form sendFrm = null; protected Form failFrm = null; protected Form ringingFrm = null; protected Form byeFrm = null; private Form backupForm = null; private Gauge progressGauge = null; /** commands used in ui */ private Command exitCmd = new Command("Exit", Command.EXIT, 1); private Command registerCmd = new Command("Register", Command.OK, 1); private Command backCmd = new Command("Back", Command.BACK, 1); private Command nextCmd = new Command("Next", Command.SCREEN, 1); private Command inviteCmd = new Command("Invite", Command.OK, 1); private Command sendCmd = new Command("Send", Command.OK, 1); private Command okCmd = new Command("Ok", Command.OK, 1); private Command failedCmd = new Command("Failed", Command.BACK, 1); private Command denyCmd = new Command("Deny", Command.EXIT, 1); private Command answerCmd = new Command("Answer", Command.OK, 1); private Command byeCmd = new Command("Bye", Command.BACK, 1); private Displayable currentDisplay; private Displayable backDisplay; private Display display; /** receive thread runs forever */ private Thread receiveThread = null; /** gauge */ private Thread gaugeThread = null; private boolean progressGaugeFinished = true; /** socket streams */ private InputStream socketIStream; private OutputStream socketOStream; /** server socket */ private ServerSocketConnection serverSocket = null; /** client socket */ private SocketConnection sc = null; /** sip variables */ private String proxyAddress = ""; private SipConnectionNotifier scn; private String failMessage; private SipClientConnection scc; private SipServerConnection ssc; private SipDialog dialog; private String clientSockParams; private Sender sender; /** application status */ private Status uaStatus = new Status(); /** how long the gauge should run (simplified)*/ private int finishGauge; public void start() { display = Display.getDisplay(this); if ((proxyAddress == null) || (proxyAddress.length() == 0)) { setDisplay(getProxyFrm()); } else { setDisplay(getRegisterForm()); } } public void pauseApp() { } public void destroyApp(boolean unconditional) { System.out.println("Closing app ..."); tearDown(); notifyDestroyed(); } public void setProxyAddress(String address) { proxyAddress = address; } public void setSocket(int socket) { mySocket = socket; } /** Setup all params of the client. * * @param name username of the client. * @param displayName the display name of the user. * @param port sip listener port */ public void setUserIdentity(String name, String displayName, int port) { myName = name; myDisplayName = displayName; mySipPort = port; } /** Setup all params of 2nd client. * * @param name the username of 2nd client * @param domain the domain of 2nd client * @param port the listener sip port of 2nd client */ public void setFriendIdentity(String name, String domain, int port) { friendName = name; friendDomain = domain; friendSipPort = port; } /** * Display another screen */ private void setDisplay(Displayable d) { if (currentDisplay != waitScreen) { backDisplay = currentDisplay; } display.setCurrent(d); currentDisplay = d; } private Form getProxyFrm() { if (proxyFrm == null) { if (proxyAddress.length() == 0) { proxyAddress = System.getProperty("microedition.hostname"); if (proxyAddress == null) { proxyAddress = ""; } } proxyFrm = new Form("Proxy setup", new Item[] { new TextField("Proxy host:", proxyAddress, 20, TextField.ANY) }); proxyFrm.addCommand(nextCmd); proxyFrm.addCommand(exitCmd); proxyFrm.setCommandListener(this); } return proxyFrm; } private Form getRingingFrm(String message) { if (ringingFrm == null) { ringingFrm = new Form("Ringing ..."); ringingFrm.append(new StringItem("Message:", message)); ringingFrm.addCommand(denyCmd); ringingFrm.addCommand(answerCmd); ringingFrm.setCommandListener(this); } StringItem si = (StringItem)ringingFrm.get(0); si.setText(message); return ringingFrm; } private Form getRegisterForm() { if (registerFrm == null) { registerFrm = new Form("Welcome", new Item[] { new StringItem(null, "You need to register to the registrar server.\n" + "Please press register and wait for response.") }); registerFrm.addCommand(exitCmd); registerFrm.addCommand(registerCmd); registerFrm.setCommandListener(this); } return registerFrm; } private Form getWaitScreen(String title, int finishAfter, Form bForm) { if (waitScreen == null) { progressGauge = new Gauge(title, false, 10, 0); progressGauge.setLayout(Item.LAYOUT_CENTER | Item.LAYOUT_EXPAND | Item.LAYOUT_VCENTER); waitScreen = new Form(""); waitScreen.append(progressGauge); waitScreen.addCommand(backCmd); waitScreen.setCommandListener(this); } finishGauge = (finishAfter == 0) ? Integer.MAX_VALUE : finishAfter; backupForm = bForm; progressGauge.setLabel(title); progressGauge.setValue(0); return waitScreen; } private Form getInviteForm() { if (inviteFrm == null) { inviteFrm = new Form("Invite", new Item[] { new StringItem(null, "Invite your sip friend " + friendName + " to talk.") }); inviteFrm.addCommand(exitCmd); inviteFrm.addCommand(inviteCmd); inviteFrm.setCommandListener(this); } return inviteFrm; } private Form getTalkForm() { if (talkFrm == null) { talkFrm = new Form("Talking", new Item[] { }); talkFrm.addCommand(sendCmd); talkFrm.addCommand(byeCmd); talkFrm.setCommandListener(this); } return talkFrm; } private Form getSendForm() { if (sendFrm == null) { sendFrm = new Form("Send", new Item[] { new TextField("Enter message:", "", 255, TextField.ANY) }); sendFrm.addCommand(okCmd); sendFrm.addCommand(byeCmd); sendFrm.setCommandListener(this); } TextField tfield = (TextField)sendFrm.get(0); if (tfield != null) { tfield.setString("");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -