示例#1
0
  /** Process the ACK request. Send the bye and complete the call flow. */
  public void processAck(RequestEvent requestEvent, ServerTransaction serverTransaction) {
    SipProvider sipProvider = (SipProvider) requestEvent.getSource();
    try {
      // System.out.println("*** shootme: got an ACK "
      // + requestEvent.getRequest());
      if (serverTransaction == null) {
        System.out.println("null server transaction -- ignoring the ACK!");
        return;
      }
      Dialog dialog = serverTransaction.getDialog();
      this.createdCount++;
      System.out.println(
          "Dialog Created = "
              + dialog.getDialogId()
              + " createdCount "
              + this.createdCount
              + " Dialog State = "
              + dialog.getState());

      if (this.dialogIds.contains(dialog.getDialogId())) {
        System.out.println("OOPS ! I already saw " + dialog.getDialogId());
      } else {
        this.dialogIds.add(dialog.getDialogId());
      }

      Request byeRequest = dialog.createRequest(Request.BYE);
      ClientTransaction tr = sipProvider.getNewClientTransaction(byeRequest);
      // System.out.println("shootme: got an ACK -- sending bye! ");
      dialog.sendRequest(tr);

    } catch (Exception ex) {
      ex.printStackTrace();
      System.exit(0);
    }
  }
  /**
   * Creates and sends a SUBSCRIBE request to the subscription <tt>Address</tt>/Request URI of a
   * specific <tt>Subscription</tt> in order to request receiving event notifications and adds the
   * specified <tt>Subscription</tt> to the list of subscriptions managed by this instance. The
   * added <tt>Subscription</tt> may later receive notifications to process the <tt>Request</tt>s
   * and/or <tt>Response</tt>s which constitute the signaling session associated with it. If the
   * attempt to create and send the SUBSCRIBE request fails, the specified <tt>Subscription</tt> is
   * not added to the list of subscriptions managed by this instance.
   *
   * @param subscription a <tt>Subscription</tt> which specifies the properties of the SUBSCRIBE
   *     request to be created and sent, to be added to the list of subscriptions managed by this
   *     instance
   * @throws OperationFailedException if we fail constructing or sending the subscription request.
   */
  public void subscribe(Subscription subscription) throws OperationFailedException {
    Dialog dialog = subscription.getDialog();

    if ((dialog != null) && DialogState.TERMINATED.equals(dialog.getState())) dialog = null;

    // create the subscription
    ClientTransaction subscribeTransaction = null;
    try {
      subscribeTransaction =
          (dialog == null)
              ? createSubscription(subscription, subscriptionDuration)
              : createSubscription(subscription, dialog, subscriptionDuration);
    } catch (OperationFailedException ex) {
      ProtocolProviderServiceSipImpl.throwOperationFailedException(
          "Failed to create the subscription", OperationFailedException.INTERNAL_ERROR, ex, logger);
    }

    // we register the contact to find him when the OK will arrive
    CallIdHeader callIdHeader =
        (CallIdHeader) subscribeTransaction.getRequest().getHeader(CallIdHeader.NAME);
    String callId = callIdHeader.getCallId();
    addSubscription(callId, subscription);

    // send the message
    try {
      if (dialog == null) subscribeTransaction.sendRequest();
      else dialog.sendRequest(subscribeTransaction);
    } catch (SipException ex) {
      // this contact will never been accepted or rejected
      removeSubscription(callId, subscription);

      ProtocolProviderServiceSipImpl.throwOperationFailedException(
          "Failed to send the subscription", OperationFailedException.NETWORK_FAILURE, ex, logger);
    }
  }
示例#3
0
  public void processResponse(ResponseEvent responseReceivedEvent) {
    System.out.println("Got a response");
    Response response = (Response) responseReceivedEvent.getResponse();
    Transaction tid = responseReceivedEvent.getClientTransaction();

    System.out.println(
        "Response received with client transaction id " + tid + ":\n" + response.getStatusCode());
    if (tid == null) {
      System.out.println("Stray response -- dropping ");
      return;
    }
    System.out.println("transaction state is " + tid.getState());
    System.out.println("Dialog = " + tid.getDialog());
    System.out.println("Dialog State is " + tid.getDialog().getState());

    try {
      if (response.getStatusCode() == Response.OK
          && ((CSeqHeader) response.getHeader(CSeqHeader.NAME))
              .getMethod()
              .equals(Request.INVITE)) {
        // Request cancel = inviteTid.createCancel();
        // ClientTransaction ct =
        //  sipProvider.getNewClientTransaction(cancel);
        // ct.sendRequest();
        Dialog dialog = tid.getDialog();
        CSeqHeader cseq = (CSeqHeader) response.getHeader(CSeqHeader.NAME);
        Request ackRequest = dialog.createAck(cseq.getSeqNumber());
        System.out.println("Sending ACK");
        dialog.sendAck(ackRequest);

        // Send a Re INVITE but this time force it
        // to use UDP as the transport. Else, it will
        // Use whatever transport was used to create
        // the dialog.
        if (reInviteCount == 0) {
          Request inviteRequest = dialog.createRequest(Request.INVITE);
          ((SipURI) inviteRequest.getRequestURI()).removeParameter("transport");
          ((ViaHeader) inviteRequest.getHeader(ViaHeader.NAME)).setTransport("udp");
          inviteRequest.addHeader(contactHeader);
          try {
            Thread.sleep(100);
          } catch (Exception ex) {
          }
          ClientTransaction ct = udpProvider.getNewClientTransaction(inviteRequest);
          dialog.sendRequest(ct);
          reInviteCount++;
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
      System.exit(0);
    }
  }
示例#4
0
  /** Process the any in dialog request - MESSAGE, BYE, INFO, UPDATE. */
  public void processInDialogRequest(
      RequestEvent requestEvent, ServerTransaction serverTransactionId) {
    SipProvider sipProvider = (SipProvider) requestEvent.getSource();
    Request request = requestEvent.getRequest();
    Dialog dialog = requestEvent.getDialog();
    System.out.println("local party = " + dialog.getLocalParty());
    try {
      System.out.println("b2bua:  got a bye sending OK.");
      Response response = messageFactory.createResponse(200, request);
      serverTransactionId.sendResponse(response);
      System.out.println("Dialog State is " + serverTransactionId.getDialog().getState());

      Dialog otherLeg = (Dialog) dialog.getApplicationData();
      Request otherBye = otherLeg.createRequest(request.getMethod());
      ClientTransaction clientTransaction = sipProvider.getNewClientTransaction(otherBye);
      clientTransaction.setApplicationData(serverTransactionId);
      serverTransactionId.setApplicationData(clientTransaction);
      otherLeg.sendRequest(clientTransaction);

    } catch (Exception ex) {
      ex.printStackTrace();
      System.exit(0);
    }
  }
    /** Refreshes the <tt>Subscription</tt> associated with this <tt>TimerTask</tt>. */
    @Override
    public void run() {
      Dialog dialog = subscription.getDialog();

      if (dialog == null) {
        logger.warn(
            "null dialog associated with " + subscription + ", can't refresh the subscription");
        return;
      }

      ClientTransaction transac = null;
      try {
        transac = createSubscription(subscription, dialog, subscriptionDuration);
      } catch (OperationFailedException e) {
        logger.error("Failed to create subscriptionTransaction.", e);
        return;
      }

      try {
        dialog.sendRequest(transac);
      } catch (SipException e) {
        logger.error("Can't send the request", e);
      }
    }
  /**
   * Creates and sends a SUBSCRIBE request to a specific subscription <tt>Address</tt>/Request URI
   * if it matches a <tt>Subscription</tt> with an id tag of its Event header of a specific value in
   * the list of subscriptions managed by this instance with an Expires header value of zero in
   * order to terminate receiving event notifications and removes the specified
   * <tt>Subscription</tt> from the list of subscriptions managed by this instance. The removed
   * <tt>Subscription</tt> may receive notifications to process the <tt>Request</tt>s and/or
   * <tt>Response</tt>s which constitute the signaling session associated with it. If the attempt to
   * create the SUBSCRIBE request fails, the associated <tt>Subscription</tt> is not removed from
   * the list of subscriptions managed by this instance. If the specified <tt>Address</tt> does not
   * identify an existing <tt>Subscription</tt> in the list of subscriptions managed by this
   * instance, an assertion may optionally be performed or no reaction can be taken.
   *
   * @param toAddress a subscription <tt>Address</tt>/Request URI which identifies a
   *     <tt>Subscription</tt> to be removed from the list of subscriptions managed by this instance
   * @param eventId the id tag placed in the Event header of the <tt>Subscription</tt> to be matched
   *     if there is one or <tt>null</tt> if the <tt>Subscription</tt> should have no id tag in its
   *     Event header
   * @param assertSubscribed <tt>true</tt> to assert if the specified subscription
   *     <tt>Address</tt>/Request URI does not identify an existing <tt>Subscription</tt> in the
   *     list of subscriptions managed by this instance; <tt>false</tt> to not assert if the
   *     mentioned condition is met
   * @throws IllegalArgumentException if <tt>assertSubscribed</tt> is <tt>true</tt> and
   *     <tt>toAddress</tt> and <tt>eventId</tt> do not identify an existing <tt>Subscription</tt>
   *     in the list of subscriptions managed by this instance
   * @throws OperationFailedException if we fail constructing or sending the unSUBSCRIBE request.
   */
  public void unsubscribe(Address toAddress, String eventId, boolean assertSubscribed)
      throws IllegalArgumentException, OperationFailedException {
    Subscription subscription = getSubscription(toAddress, eventId);
    if (subscription == null)
      if (assertSubscribed)
        throw new IllegalArgumentException("trying to unregister a not registered contact");
      else return;

    Dialog dialog = subscription.getDialog();

    // we stop the subscription if we're subscribed to this contact
    if (dialog != null) {
      String callId = dialog.getCallId().getCallId();

      ClientTransaction subscribeTransaction;
      try {
        subscribeTransaction = createSubscription(subscription, dialog, 0);
      } catch (OperationFailedException e) {
        if (logger.isDebugEnabled()) logger.debug("failed to create the unsubscription", e);
        throw e;
      }

      // we are not anymore subscribed to this contact
      // this ensure that the response of this request will be
      // handled as an unsubscription response
      removeSubscription(callId, subscription);

      try {
        dialog.sendRequest(subscribeTransaction);
      } catch (SipException e) {
        if (logger.isDebugEnabled()) logger.debug("Can't send the request", e);
        throw new OperationFailedException(
            "Failed to send the subscription message", OperationFailedException.NETWORK_FAILURE, e);
      }
    }
  }
  public void sendSubscribe(String localURL, String buddyURI, boolean EXPIRED) {
    try {
      logger.debug("Sending SUBSCRIBE in progress to the buddy: " + buddyURI);
      int proxyPort = imUA.getProxyPort();
      String proxyAddress = imUA.getProxyAddress();
      String imProtocol = imUA.getIMProtocol();
      SipStack sipStack = imUA.getSipStack();
      SipProvider sipProvider = imUA.getSipProvider();
      MessageFactory messageFactory = imUA.getMessageFactory();
      HeaderFactory headerFactory = imUA.getHeaderFactory();
      AddressFactory addressFactory = imUA.getAddressFactory();

      // Request-URI:
      // URI requestURI=addressFactory.createURI(buddyURI);
      SipURI requestURI = addressFactory.createSipURI(null, proxyAddress);
      requestURI.setPort(proxyPort);
      requestURI.setTransportParam(imProtocol);

      // Call-Id:
      CallIdHeader callIdHeader = null;

      // CSeq:
      CSeqHeader cseqHeader = null;

      // To header:
      ToHeader toHeader = null;

      // From Header:
      FromHeader fromHeader = null;

      // Via header
      String branchId = Utils.generateBranchId();
      ViaHeader viaHeader =
          headerFactory.createViaHeader(
              imUA.getIMAddress(), imUA.getIMPort(), imProtocol, branchId);
      Vector viaList = new Vector();
      viaList.addElement(viaHeader);

      PresenceManager presenceManager = imUA.getPresenceManager();
      Presentity presentity = presenceManager.getPresentity(buddyURI);
      Dialog dialog = null;
      if (presentity != null) dialog = presentity.getDialog();

      if (dialog != null) {

        // We have to remove the subscriber and the Presentity related
        // with this Buddy...
        presenceManager.removePresentity(buddyURI);
        Subscriber subscriber = presenceManager.getSubscriber(buddyURI);
        if (subscriber == null) {
          // It means that the guy does not have us in his buddy list
          // nothing to do!!!
        } else {
          presenceManager.removeSubscriber(buddyURI);
        }

        Address localAddress = dialog.getLocalParty();
        Address remoteAddress = dialog.getRemoteParty();

        fromHeader = headerFactory.createFromHeader(localAddress, dialog.getLocalTag());
        toHeader = headerFactory.createToHeader(remoteAddress, dialog.getRemoteTag());

        long cseq = dialog.getLocalSeqNumber();
        cseqHeader = headerFactory.createCSeqHeader(cseq, "MESSAGE");

        callIdHeader = dialog.getCallId();
      } else {
        String localTag = Utils.generateTag();

        Address toAddress = addressFactory.createAddress(buddyURI);
        Address fromAddress = addressFactory.createAddress(localURL);

        fromHeader = headerFactory.createFromHeader(fromAddress, localTag);
        toHeader = headerFactory.createToHeader(toAddress, null);

        // CSeq:
        cseqHeader = headerFactory.createCSeqHeader(1L, "SUBSCRIBE");

        callIdCounter++;
        // Call-ID:
        callIdHeader =
            (CallIdHeader)
                headerFactory.createCallIdHeader("nist-sip-im-subscribe-callId" + callIdCounter);
      }

      // MaxForwards header:
      MaxForwardsHeader maxForwardsHeader = headerFactory.createMaxForwardsHeader(70);

      Request request =
          messageFactory.createRequest(
              requestURI,
              "SUBSCRIBE",
              callIdHeader,
              cseqHeader,
              fromHeader,
              toHeader,
              viaList,
              maxForwardsHeader);

      RouteHeader rh = this.imUA.getRouteToProxy();
      request.setHeader(rh);

      // Contact header:
      SipURI sipURI = addressFactory.createSipURI(null, imUA.getIMAddress());
      sipURI.setPort(imUA.getIMPort());
      sipURI.setTransportParam(imUA.getIMProtocol());
      Address contactAddress = addressFactory.createAddress(sipURI);
      ContactHeader contactHeader = headerFactory.createContactHeader(contactAddress);
      request.setHeader(contactHeader);

      ExpiresHeader expiresHeader = null;
      if (EXPIRED) {
        expiresHeader = headerFactory.createExpiresHeader(0);
      } else {
        expiresHeader = headerFactory.createExpiresHeader(presenceManager.getExpiresTime());
      }
      request.setHeader(expiresHeader);

      // WE have to add a new Header: "Event"
      Header eventHeader = headerFactory.createHeader("Event", "presence");
      request.setHeader(eventHeader);

      // Add Acceptw Header
      Header acceptHeader = headerFactory.createHeader("Accept", "application/pidf+xml");
      request.setHeader(acceptHeader);

      // ProxyAuthorization header if not null:
      ProxyAuthorizationHeader proxyAuthHeader = imUA.getProxyAuthorizationHeader();
      if (proxyAuthHeader != null) request.setHeader(proxyAuthHeader);

      ClientTransaction clientTransaction = sipProvider.getNewClientTransaction(request);

      if (dialog != null) {
        dialog.sendRequest(clientTransaction);
      } else {
        clientTransaction.sendRequest();
      }

      logger.debug("IMSubscribeProcessing, sendSubscribe(), SUBSCRIBE sent:\n" + request);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }