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

📄 siptester.java

📁 Java SIP
💻 JAVA
字号:
/* * This file is part of the Java SIP library, an open source SIP library. Please see * http://collaboration.mitre.org/sip for more information *  * Copyright (c) 2001 The MITRE Corporation  *  * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Library General Public License as published by * the Free Software Foundation; either version 2 of the License, or (at your * option) any later version. *  * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public * License for more details. *  * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB.  If not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. * *//** * Test class for the Sip library * * @author S.R.Jones * @version 1 */package org.mitre.jsip;import java.io.*;import javax.swing.*;import java.awt.*;import java.awt.event.*;import org.mitre.jsip.event.*;public class SipTester extends JFrame implements Runnable {    private SipClient client;    private SipUser  localUser;        private SipRegister regServer;        private static final boolean DEBUG = true;    // various testing gui variables    private JRadioButton regButton;    private JRadioButton invButton;    private JTextArea scrollback;    private JTextField entry;        // used in test messages    private final String remoteUri = "sip:srjones@boobala.mitre.org";    private final String regServerUri = "sip:stars.mitre.org";    private final int [] payloadTypes = {0, 3};    private final String mcastAddr = "225.5.5.5";    private final int mcastPort = 23322;    // MESSAGE testing     private static String imUri;    private SipCall call;    private SipCallMember call_leg;    public SipTester (String contact) {	// try initializing the same way kphone does	SipUri uri = new SipUri( contact );	client = SipClient.getSipClient();	localUser = null;		client.setDefaultUserMode( true );	client.setExplicitProxyMode( false );		String laddr = Sip.getLocalAddress();	client.setCallForward( false );	client.setCallForwardUri( null );	client.setCallForwardMessage( null );	client.setMaxForwards( 0 );	client.setBusyMessage( null );	client.setHideViaMode( SipClient.HideHop );    	localUser = new SipUser( client, uri );	SipUri remoteuri = new SipUri( imUri );	call = new SipCall( localUser, null );	call_leg = new SipCallMember( call, remoteuri );	initGui();	// add the listeners for incoming messages	CallListener cl =  new CallListener() {		public void responseReceived(CallEvent ce) {		    // do more with this later		    System.out.println("Received response message event");		}				public void requestReceived(CallEvent ce) {		    // same with this		    System.out.println("ST: Received request message event");		    SipMessage requestMessage = ce.getMessage();		    handleRequestReceived( requestMessage );		}	    };	client.addCallListener(cl);		if ( DEBUG ) System.out.println( "SipClient: initialization complete" );      }    protected void initGui() {	Container cp = this.getContentPane();	cp.setLayout( new BorderLayout() );	scrollback = new JTextArea(10, 40); // 10 rows and 40 columns	cp.add( new JScrollPane( scrollback ), BorderLayout.CENTER );	JPanel entryPanel = new JPanel();	entry = new JTextField( 40 );	JButton sendButton = new JButton( "Send" );	sendButton.addActionListener( new ActionListener() {		public void actionPerformed( ActionEvent ae ) {		    // get the text in the entry field, then clear out entry field		    String message = entry.getText();		    entry.setText("");		    // put the message in Text Area, then send out		    scrollback.append( localUser.getUri().user() + ": " + message + "\n" );		    sendMessage( message );		}	    });	entryPanel.add( entry );	entryPanel.add( sendButton );	cp.add( entryPanel, BorderLayout.SOUTH );    }    protected void initSelectGui() {	Container cp = this.getContentPane();	cp.setLayout( new BorderLayout() );	JPanel centerPanel = new JPanel( new BorderLayout() );	ButtonGroup bg = new ButtonGroup();	regButton = new JRadioButton();	regButton.setText( "Send REGISTER" );	regButton.setSelected( true );        invButton = new JRadioButton();	invButton.setText( "Send INVITE" );	bg.add(regButton);	bg.add(invButton);	centerPanel.add(regButton, BorderLayout.NORTH);	centerPanel.add(invButton, BorderLayout.CENTER);	JPanel buttonPanel = new JPanel();	JButton okButton = new JButton();	JButton cancelButton = new JButton();		okButton.setLabel( "Execute" );	okButton.addActionListener( new ActionListener() {		public void actionPerformed(ActionEvent evt) {		    executeCommand();		}	    });	cancelButton.setLabel( "Exit" );	cancelButton.addActionListener( new ActionListener() {		public void actionPerformed( ActionEvent evt ) {		    exitCommand();		}	    });	buttonPanel.add(okButton);	buttonPanel.add(cancelButton);	cp.add( centerPanel, BorderLayout.CENTER );	cp.add( buttonPanel, BorderLayout.SOUTH );	setSize( 240, 150 );    }    protected void handleRequestReceived( SipMessage requestMessage ) {	SipUri sender = new SipUri( requestMessage.getHeaderData( SipHeader.From ) );	if ( sender == null ) {	    System.err.println( "Don't know where this came from... tossing" );	    return;	}	// real system would handle different messages correctly	// however, since this is a test, just doing IM for now	if ( requestMessage.getMethod() == Sip.MESSAGE ) {	    // put it up on the text area	    String message = requestMessage.messageBody();	    System.err.println("Message received is: [" + message + "]");	    String newText = sender.user() + ": " + message + "\n";	    scrollback.append( newText );	}	        }    public void run() {	// run a loop in here to handle messages	for (;;) {	    while (!client.haveMessages() ) {		try {		    Thread.currentThread().sleep(3000);		    //		    System.out.print(".");		} catch (InterruptedException ie) {break;}	    }	    	    System.out.println("Message in queue, forwarding...");	    client.incomingMessage();	}    }        private static int regTries = 0;    protected void sendRegister(String username, String password) {	// create a listener for REGISTER responses	CallListener regCallListener = new CallListener() {		public void responseReceived(CallEvent ce) {		    System.out.println( "REGCALLLISTENER activated" );		    // since we don't have a dialog here, just try to register twice		    if ( regTries >= 2 )			return;		    // see if this is a response message		    SipMessage respMessage = ce.getMessage();		    if ( respMessage.getStatus().getCode() == 401 ) {			System.out.println("REGCALLLISTENER - updating status");			try {			    regServer.setAuthentication(respMessage);			} catch ( RegisterException re ) {			    System.err.println( re.toString() );			    return;			}			regServer.requestRegister();   // try to register again			regTries++;		    }		}				public void requestReceived(CallEvent ce) {		    // ignore this		}	    };	SipClient.getSipClient().addCallListener( regCallListener );	// create a REGISTER message to a server	regServer = localUser.addServer( regServerUri );	regServer.requestRegister( username, password );	regTries++;    }        protected void sendMessage( String message ) {	call_leg.requestMessage( message );    }    protected void sendInvite() {	// create an INVITE message and send it out	SdpMessage sdpMessage = new SdpMessage();	sdpMessage.setName("jSIP");	sdpMessage.setUsername("steve");	sdpMessage.setPayloadTypes(payloadTypes);	sdpMessage.setIpAddress(mcastAddr);	sdpMessage.setPort(mcastPort);	SipUri remoteuri = new SipUri(remoteUri);	SipCall call = new SipCall( localUser, null );    // this is for creating a brand new call	SipCallMember member = new SipCallMember( call, remoteuri );		member.requestInvite( sdpMessage.message(), new MimeContentType( "application/sdp" ) );    }    protected void executeCommand() {	if ( regButton.isSelected() ) {	    sendRegister("user1", "none"); // junk	} else if ( invButton.isSelected() ) {	    sendInvite();	} else {	    System.out.println("Don't know which command to execute");	}    }    protected void exitCommand() {	System.exit( 0 );    }    public static void main(String[] args) {	// testing IM stuff	String reginfo = args[0];   // this consists of username:password	imUri = args[1];	int colon = reginfo.indexOf( ':' );	if ( colon < 0 ) {	    System.err.println( "First argument should be in form username:password" );	    System.exit( 1 );	}	String contact = reginfo.substring( 0, colon );	String password = reginfo.substring( colon + 1 );	SipTester tester = new SipTester( contact );	tester.setTitle( "Chat with " + imUri );	// start the message queue thread	Thread messageThread = new Thread(tester);	messageThread.start();	tester.pack();	tester.setVisible( true );	// exit the app when the window closes	tester.addWindowListener( new WindowAdapter() {		public void windowClosed( WindowEvent e ) {	    //		    messageThread.stop();		}	    });	// try to register now	// Comment this out if you don't have a Register server	tester.sendRegister( contact, password );		//	tester.sendMessage( "Hi, how are ya?" );	// loop for grabbing input and generating messages	//  	for (;;) {//  	    System.out.println("******* SipTester options *******");//  	    System.out.println("1) Send Register message");//  	    System.out.println("2) Send Invite message");//  	    System.out.println("3) Quit");//  	    System.out.println("");//  	    System.out.print("-> ");//  	    byte inputbuf[] = new byte[5];//  	    try {//  		System.in.read(inputbuf);//  	    } catch (IOException ioe) {//  		// assume error - quit//  		break;//  	    }//  	    String input = new String(inputbuf, 0, 1);  // just want the first char	    //  	    System.out.println("input was [" + input + "]");//  	    int choice = 0;//  	    try {//  		choice = Integer.parseInt(input);//  	    } catch (NumberFormatException nfe) {//  		System.out.println("Don't understand that input, just type a number");//  	    }//  	    switch (choice) {//  	    case 1://  		tester.sendRegister();//  		break;//  	    case 2://  		tester.sendInvite();//  		break;//  	    case 3://  		System.exit(0);//  	    }//  	}    }	}      

⌨️ 快捷键说明

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