Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package edu.upf;
- import java.net.InetAddress;
- import java.util.ArrayList;
- import java.util.Properties;
- import java.util.Random;
- import javax.sip.*;
- import javax.sip.address.Address;
- import javax.sip.address.AddressFactory;
- import javax.sip.header.CSeqHeader;
- import javax.sip.header.CallIdHeader;
- import javax.sip.header.ContactHeader;
- import javax.sip.header.FromHeader;
- import javax.sip.header.HeaderFactory;
- import javax.sip.header.MaxForwardsHeader;
- import javax.sip.header.ToHeader;
- import javax.sip.header.ViaHeader;
- import javax.sip.message.MessageFactory;
- import javax.sip.message.Request;
- import javax.sip.message.Response;
- import javax.swing.*;
- /**
- *
- * @author Alex
- */
- public class SipClient extends JFrame implements SipListener {
- private long cseq = 1L;
- // Objects used to communicate to the JAIN SIP API.
- SipFactory sipFactory; // Used to access the SIP API.
- SipStack sipStack; // The SIP stack.
- SipProvider sipProvider; // Used to send SIP messages.
- MessageFactory messageFactory; // Used to create SIP message factory.
- HeaderFactory headerFactory; // Used to create SIP headers.
- AddressFactory addressFactory; // Used to create SIP URIs.
- ListeningPoint listeningPoint; // SIP listening IP address/port.
- Properties properties; // Other properties.
- // Objects keeping local configuration.
- String ip; // The local IP address.
- int port = 6060; // The local port.
- String protocol = "udp"; // The local protocol (UDP).
- int tag = (new Random()).nextInt(); // The local tag.
- Address contactAddress; // The contact address.
- ContactHeader contactHeader; // The contact header.
- /**
- * Creates new form SipClient
- */
- public SipClient() {
- initComponents();
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- scrollPane = new javax.swing.JScrollPane();
- textArea = new javax.swing.JTextArea();
- buttonRegisterStateless = new javax.swing.JButton();
- buttonRegisterStatefull = new javax.swing.JButton();
- buttonInvite = new javax.swing.JButton();
- buttonBye = new javax.swing.JButton();
- textField = new javax.swing.JTextField();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- setTitle("SIP Client");
- setLocationByPlatform(true);
- addWindowListener(new java.awt.event.WindowAdapter() {
- public void windowOpened(java.awt.event.WindowEvent evt) {
- onOpen(evt);
- }
- });
- textArea.setEditable(false);
- textArea.setColumns(20);
- textArea.setRows(5);
- scrollPane.setViewportView(textArea);
- buttonRegisterStateless.setText("Reg (SL)");
- buttonRegisterStateless.setEnabled(false);
- buttonRegisterStateless.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- onRegisterStateless(evt);
- }
- });
- buttonRegisterStatefull.setText("Reg (SF)");
- buttonRegisterStatefull.setEnabled(false);
- buttonRegisterStatefull.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- onRegisterStatefull(evt);
- }
- });
- buttonInvite.setText("Invite");
- buttonInvite.setEnabled(false);
- buttonInvite.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- onInvite(evt);
- }
- });
- buttonBye.setText("Bye");
- buttonBye.setEnabled(false);
- buttonBye.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- onBye(evt);
- }
- });
- textField.setText("sip:alice@localhost:5060");
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addContainerGap()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(scrollPane)
- .addGroup(layout.createSequentialGroup()
- .addComponent(buttonRegisterStateless, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(buttonRegisterStatefull, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(buttonInvite, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(buttonBye, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(0, 2, Short.MAX_VALUE))
- .addComponent(textField))
- .addContainerGap())
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addContainerGap()
- .addComponent(textField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(scrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 230, Short.MAX_VALUE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(buttonRegisterStateless)
- .addComponent(buttonInvite)
- .addComponent(buttonBye)
- .addComponent(buttonRegisterStatefull))
- .addContainerGap())
- );
- pack();
- }// </editor-fold>
- private void onOpen(java.awt.event.WindowEvent evt) {
- try {
- buttonRegisterStateless.setEnabled(true);
- // Get the local IP address.
- this.ip = InetAddress.getLocalHost().getHostAddress();
- // Create the SIP factory and set the path name.
- this.sipFactory = SipFactory.getInstance();
- this.sipFactory.setPathName("gov.nist");
- // Create and set the SIP stack properties.
- this.properties = new Properties();
- this.properties.setProperty("javax.sip.STACK_NAME", "stack");
- // Create the SIP stack.
- this.sipStack = this.sipFactory.createSipStack(this.properties);
- // Create the SIP message factory.
- this.messageFactory = this.sipFactory.createMessageFactory();
- // Create the SIP header factory.
- this.headerFactory = this.sipFactory.createHeaderFactory();
- // Create the SIP address factory.
- this.addressFactory = this.sipFactory.createAddressFactory();
- // Create the SIP listening point and bind it to the local IP address, port and protocol.
- this.listeningPoint = this.sipStack.createListeningPoint(this.ip, this.port, this.protocol);
- // Create the SIP provider.
- this.sipProvider = this.sipStack.createSipProvider(this.listeningPoint);
- // Add our application as a SIP listener.
- this.sipProvider.addSipListener(this);
- // Create the contact address used for all SIP messages.
- this.contactAddress = this.addressFactory.createAddress("sip:" + 1000 + ":" + this.port);
- // Create the contact header used for all SIP messages.
- this.contactHeader = this.headerFactory.createContactHeader(contactAddress);
- // Display the local IP address and port in the text area.
- this.textArea.append("Local address: " + this.ip + ":" + this.port + "\n");
- } catch (Exception e) {
- // If an error occurs, display an error message box and exit.
- JOptionPane.showMessageDialog(this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
- System.exit(-1);
- }
- }
- private void onRegisterStateless(java.awt.event.ActionEvent evt) {
- try {
- // Get the destination address from the text field.
- Address addressTo = this.addressFactory.createAddress(this.textField.getText());
- // Create the request URI for the SIP message.
- javax.sip.address.URI requestURI = addressTo.getURI();
- // Create the SIP message headers.
- // The "Via" headers.
- ArrayList viaHeaders = new ArrayList();
- ViaHeader viaHeader = this.headerFactory.createViaHeader(this.ip, this.port, "udp", null);
- viaHeaders.add(viaHeader);
- // The "Max-Forwards" header.
- MaxForwardsHeader maxForwardsHeader = this.headerFactory.createMaxForwardsHeader(70);
- // The "Call-Id" header.
- CallIdHeader callIdHeader = this.sipProvider.getNewCallId();
- // The "CSeq" header.
- CSeqHeader cSeqHeader = this.headerFactory.createCSeqHeader(cseq, "REGISTER");
- // The "From" header.
- FromHeader fromHeader = this.headerFactory.createFromHeader(this.contactAddress, String.valueOf(this.tag));
- // The "To" header.
- ToHeader toHeader = this.headerFactory.createToHeader(addressTo, null);
- // Create the REGISTER request.
- Request request = this.messageFactory.createRequest(
- requestURI,
- "REGISTER",
- callIdHeader,
- cSeqHeader,
- fromHeader,
- toHeader,
- viaHeaders,
- maxForwardsHeader);
- // Add the "Contact" header to the request.
- request.addHeader(contactHeader);
- // Send the request statelessly through the SIP provider.
- this.sipProvider.sendRequest(request);
- // Display the message in the text area.
- this.textArea.append(
- "Request sent:\n" + request.toString() + "\n\n");
- } catch (Exception e) {
- // If an error occurred, display the error.
- this.textArea.append("Request sent failed: " + e.getMessage() + "\n");
- }
- }
- private void onRegisterStatefull(java.awt.event.ActionEvent evt) {
- // A method called when you click on the "Reg (SF)" button.
- }
- private void onInvite(java.awt.event.ActionEvent evt) {
- // A method called when you click on the "Invite" button.
- }
- private void onBye(java.awt.event.ActionEvent evt) {
- // A method called when you click on the "Bye" button.
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(SipClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(SipClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(SipClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(SipClient.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- @Override
- public void run() {
- new SipClient().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton buttonBye;
- private javax.swing.JButton buttonInvite;
- private javax.swing.JButton buttonRegisterStatefull;
- private javax.swing.JButton buttonRegisterStateless;
- private javax.swing.JScrollPane scrollPane;
- private javax.swing.JTextArea textArea;
- private javax.swing.JTextField textField;
- // End of variables declaration
- @Override
- public void processRequest(RequestEvent requestEvent) {
- // A method called when you receive a SIP request.
- }
- @Override
- public void processResponse(ResponseEvent responseEvent) {
- // Get the response.
- Response response = responseEvent.getResponse();
- // Display the response message in the text area.
- this.textArea.append("\nReceived response: " + response.toString());
- }
- @Override
- public void processTimeout(TimeoutEvent timeoutEvent) {
- // A method called when a SIP operation times out.
- }
- @Override
- public void processIOException(IOExceptionEvent exceptionEvent) {
- // A method called when a SIP operation results in an I/O error.
- }
- @Override
- public void processTransactionTerminated(TransactionTerminatedEvent transactionTerminatedEvent) {
- // A method called when a SIP transaction terminates.
- }
- @Override
- public void processDialogTerminated(DialogTerminatedEvent dialogTerminatedEvent) {
- // A method called when a SIP dialog terminates.
- }
- public void register(Response response) {
- try {
- cseq++;
- ArrayList viaHeaders = new ArrayList();
- ViaHeader viaHeader = this.headerFactory.createViaHeader(this.ip,
- this.port, "udp", null);
- viaHeaders.add(viaHeader);
- // The &quot;Max-Forwards&quot; header.
- MaxForwardsHeader maxForwardsHeader = this.headerFactory
- .createMaxForwardsHeader(70);
- // The &quot;Call-Id&quot; header.
- CallIdHeader callIdHeader = this.sipProvider.getNewCallId();
- // The &quot;CSeq&quot; header.
- CSeqHeader cSeqHeader = this.headerFactory.createCSeqHeader(cseq,
- "REGISTER");
- Address fromAddress = addressFactory.createAddress( "sip:" + username + '@' +
- );
- FromHeader fromHeader = this.headerFactory.createFromHeader(
- fromAddress, String.valueOf(this.tag));
- // The &quot;To&quot; header.
- ToHeader toHeader = this.headerFactory.createToHeader(fromAddress,
- null);
- // this.contactHeader = this.headerFactory
- // .createContactHeader(contactAddress);
- request = this.messageFactory.createRequest( & amp;quot;
- REGISTER sip:&quot;
- +server + & amp;quot;SIP / 2.0\r\n\r\n & amp;quot;);
- request.addHeader(callIdHeader);
- request.addHeader(cSeqHeader);
- request.addHeader(fromHeader);
- request.addHeader(toHeader);
- request.addHeader(maxForwardsHeader);
- request.addHeader(viaHeader);
- request.addHeader(contactHeader);
- if (response != null) {
- retry = true;
- AuthorizationHeader authHeader = Utils.makeAuthHeader(headerFactory, response,
- request, username, password);
- request.addHeader(authHeader);
- }
- inviteTid = sipProvider.getNewClientTransaction(request);
- // send the request out.
- inviteTid.sendRequest();
- this.dialog = inviteTid.getDialog();
- // Send the request statelessly through the SIP provider.
- // this.sipProvider.sendRequest(request);
- // Display the message in the text area.
- logger.debug( & amp;quot;
- Request sent:\n & amp;quot;+request.toString() + & amp;quot;\n\n & amp;quot;);
- } catch (Exception e) {
- // If an error occurred, display the error.
- e.printStackTrace();
- logger.debug( & amp;quot;
- Request sent failed:
- & amp;quot;+e.getMessage() + & amp;quot;\n & amp;quot;);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement