예제 #1
0
  /**
   * Wait for a request.
   *
   * @param method the method expected.
   * @param strict if <code>true</code>, fails if received a request with the wrong method else,
   *     ignore request with wrong method.
   * @param timeout
   * @return
   */
  public synchronized ServerTransaction waitForRequest(
      String method, boolean strict, long timeout) {

    if (!isLastRequestUncommited()) {
      try {
        wait(timeout);
      } catch (Exception e) {
      }
    }
    RequestEvent event = getLastRequest();
    if (event == null) fail("No event received");
    Request request = event.getRequest();

    if (!isLastRequestUncommited())
      fail("Response has been already sent for last request:\n" + event);

    if (strict && !request.getMethod().equals(method))
      fail("Received " + request.getMethod() + " when expected " + method);

    while (!request.getMethod().equals(method)) {
      try {
        wait(timeout);
      } catch (Exception e) {
      }
      event = getLastRequest();
      request = event.getRequest();
    }

    ServerTransaction tx = event.getServerTransaction();

    if (tx == null) {
      if (Request.ACK.equals(method)) return null;

      try {
        tx = _sipPhone.getParent().getSipProvider().getNewServerTransaction(request);
      } catch (Exception ex) {
        throw new RuntimeException(ex);
      }
    }

    return tx;
  }
    public void processRequest(RequestEvent requestEvent) {
      try {
        Request request = requestEvent.getRequest();

        ViaHeader viaHeader =
            headerFactory.createViaHeader(
                this.myHost,
                this.myPort,
                ListeningPoint.UDP,
                "z9hG4bK" + Math.random() * 31 + "" + System.currentTimeMillis());
        // Decreasing the Max Forward Header
        MaxForwardsHeader maxForwardsHeader =
            (MaxForwardsHeader) request.getHeader(MaxForwardsHeader.NAME);
        if (maxForwardsHeader == null) {
          maxForwardsHeader = headerFactory.createMaxForwardsHeader(70);
          request.addHeader(maxForwardsHeader);
        } else {
          maxForwardsHeader.setMaxForwards(maxForwardsHeader.getMaxForwards() - 1);
        }
        // Add the via header to the top of the header list.
        request.addHeader(viaHeader);
        // Removing first routeHeader if it is for us
        RouteHeader routeHeader = (RouteHeader) request.getHeader(RouteHeader.NAME);
        if (routeHeader != null) {
          SipURI routeUri = (SipURI) routeHeader.getAddress().getURI();
          if (routeUri.getHost().equalsIgnoreCase(myHost) && routeUri.getPort() == myPort) {
            request.removeFirst(RouteHeader.NAME);
          }
        }

        // Record route the invite so the bye comes to me.
        if (request.getMethod().equals(Request.INVITE)
            || request.getMethod().equals(Request.SUBSCRIBE)) {
          SipURI sipUri =
              addressFactory.createSipURI(
                  null, sipProvider.getListeningPoint(ListeningPoint.UDP).getIPAddress());
          sipUri.setPort(sipProvider.getListeningPoint(ListeningPoint.UDP).getPort());
          // See RFC 3261 19.1.1 for lr parameter
          sipUri.setLrParam();
          Address address = addressFactory.createAddress(sipUri);
          address.setURI(sipUri);
          RecordRouteHeader recordRoute = headerFactory.createRecordRouteHeader(address);
          request.addHeader(recordRoute);

          // Adding Route Header
          SipURI routeSipUri = addressFactory.createSipURI(null, IP_ADDRESS);
          routeSipUri.setPort(5070);
          routeSipUri.setLrParam();
          RouteHeader route =
              headerFactory.createRouteHeader(addressFactory.createAddress(routeSipUri));
          request.addFirst(route);
        } else if (!Request.ACK.equals(request.getMethod())) {
          // Adding Route Header
          if (((SipURI) request.getRequestURI()).getPort() == 5070) {
            SipURI routeSipUri = addressFactory.createSipURI(null, IP_ADDRESS);
            routeSipUri.setPort(5080);
            routeSipUri.setLrParam();
            RouteHeader route =
                headerFactory.createRouteHeader(addressFactory.createAddress(routeSipUri));
            request.addFirst(route);
          }
        }
        // sending request
        sipProvider.sendRequest(request);
      } catch (Exception ex) {
        ex.printStackTrace();
      }
    }