Ejemplo n.º 1
0
 public void processRegister(RequestEvent requestEvent, ServerTransaction serverTransactionId) {
   Request request = requestEvent.getRequest();
   ContactHeader contact = (ContactHeader) request.getHeader(ContactHeader.NAME);
   SipURI contactUri = (SipURI) contact.getAddress().getURI();
   FromHeader from = (FromHeader) request.getHeader(FromHeader.NAME);
   SipURI fromUri = (SipURI) from.getAddress().getURI();
   registrar.put(fromUri.getUser(), contactUri);
   try {
     Response response = this.messageFactory.createResponse(200, request);
     ServerTransaction serverTransaction = sipProvider.getNewServerTransaction(request);
     serverTransaction.sendResponse(response);
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
  public void processResponse(ResponseEvent responseReceivedEvent) {
    /// System.out.println("=> BUNDLE: br.ufes.inf.ngn.televoto.client.logic | CLASS: LogicListener
    // | METOD: processResponse ");//By Ju
    try {
      Response myResponse = responseReceivedEvent.getResponse();
      logger.info("<<< " + myResponse.toString());

      ClientTransaction thisClientTransaction = responseReceivedEvent.getClientTransaction();

      int myStatusCode = myResponse.getStatusCode();

      switch (status) {
        case WAIT_PROV:
          if (!thisClientTransaction.equals(myClientTransaction))
            myClientTransaction = thisClientTransaction;

          if (myStatusCode < 200) {
            status = WAIT_FINAL;
            myDialog = thisClientTransaction.getDialog();
          } else if (myStatusCode < 300) {
            myDialog = thisClientTransaction.getDialog();
            CSeqHeader originalCSeq =
                (CSeqHeader) myClientTransaction.getRequest().getHeader(CSeqHeader.NAME);
            long numseq = originalCSeq.getSeqNumber();
            Request myAck = myDialog.createAck(numseq);
            myAck.addHeader(myContactHeader);
            myDialog.sendAck(myAck);
            // logger.info(">>> "+myAck.toString());
            status = ESTABLISHED;
            byte[] cont = (byte[]) myResponse.getContent();
            answerInfo = mySdpManager.getSdp(cont);

          } else {
            if (myStatusCode == 401) {

            } else {
              status = IDLE;
              CSeqHeader originalCSeq =
                  (CSeqHeader) myClientTransaction.getRequest().getHeader(CSeqHeader.NAME);
              long numseq = originalCSeq.getSeqNumber();
              Request myAck = myDialog.createAck(numseq);
              myAck.addHeader(myContactHeader);
              myDialog.sendAck(myAck);
              // logger.info(">>> "+myAck.toString());
            }
          }
          break;

        case WAIT_FINAL:
          if (!thisClientTransaction.equals(myClientTransaction))
            myClientTransaction = thisClientTransaction;

          if (myStatusCode < 200) {
            status = WAIT_FINAL;
            myDialog = thisClientTransaction.getDialog();
          } else if (myStatusCode < 300) {
            if (useQueue.equals("yes")) {
              status = IDLE;
            } else {
              status = ESTABLISHED;
            }

            myDialog = thisClientTransaction.getDialog();
            CSeqHeader originalCSeq =
                (CSeqHeader) myClientTransaction.getRequest().getHeader(CSeqHeader.NAME);
            long numseq = originalCSeq.getSeqNumber();
            Request myAck = myDialog.createAck(numseq); // Aquiiii
            myAck.addHeader(myContactHeader);
            myDialog.sendAck(myAck);
            // logger.info(">>> "+myAck.toString());
            byte[] cont = (byte[]) myResponse.getContent();
            answerInfo = mySdpManager.getSdp(cont);

          } else {
            // Cancelando requisição ao cliente
            Request myCancelRequest = myClientTransaction.createCancel();
            ClientTransaction myCancelClientTransaction =
                mySipProvider.getNewClientTransaction(myCancelRequest);
            myCancelClientTransaction.sendRequest();
            // logger.info(">>> " + myCancelRequest.toString());
            status = IDLE;
          }
          break;

        case REGISTERING:
          if (!thisClientTransaction.equals(myClientTransaction))
            myClientTransaction = thisClientTransaction;

          if (myStatusCode == 200) {
            status = IDLE;
            if (!Unregistring) {

              System.out.println(myName + ": Registrado");

              // inserir captura de timestamp
              // logger.info(myName + ";INVITE;" +  getTime());
              // Random gerador = new Random();
              // int temp = gerador.nextInt(10);
              // System.out.println(myName + ": Esperando " + temp + "segundos");
              // Thread.sleep(temp);

              System.out.println(myName + ": chamando " + destination);

              SdpInfo offerInfo = new SdpInfo();
              offerInfo.IpAddress = myIP;
              offerInfo.aport = myAudioPort;
              offerInfo.aformat = 0;
              byte[] content = mySdpManager.createSdp(offerInfo);

              // Via
              // ViaHeader comentei
              myViaHeader = myHeaderFactory.createViaHeader(myIP, myPort, "udp", null);
              myViaHeader.setRPort();
              ArrayList viaHeaders = new ArrayList();
              viaHeaders.add(myViaHeader);

              // From
              Address addressOfRecord = myAddressFactory.createAddress("<sip:" + myUserID + ">");
              FromHeader myFromHeader = myHeaderFactory.createFromHeader(addressOfRecord, "456249");

              // To
              String dstURI[] = destination.split("@");
              String dstSipAlias = dstURI[0];
              Address destinationAddress =
                  myAddressFactory.createAddress("<sip:" + destination + ">");
              javax.sip.address.URI myRequestURI = destinationAddress.getURI();
              ToHeader myToHeader = myHeaderFactory.createToHeader(destinationAddress, null);

              // MaxForwards
              MaxForwardsHeader myMaxForwardsHeader = myHeaderFactory.createMaxForwardsHeader(70);

              // Call-ID
              CallIdHeader myCallIdHeader = mySipProvider.getNewCallId();

              // CSeq
              Random random = new Random();
              CSeqHeader myCSeqHeader =
                  myHeaderFactory.createCSeqHeader(random.nextInt(1000) * 1L, "INVITE");

              // Create SIP request
              Request myRequest =
                  myMessageFactory.createRequest(
                      myRequestURI,
                      "INVITE",
                      myCallIdHeader,
                      myCSeqHeader,
                      myFromHeader,
                      myToHeader,
                      viaHeaders,
                      myMaxForwardsHeader);

              // Contact
              Address contactAddress =
                  myAddressFactory.createAddress(
                      "<sip:" + mySipAlias + "@" + myIP + ":" + myPort + ";transport=udp>");
              myContactHeader = myHeaderFactory.createContactHeader(contactAddress);
              myRequest.addHeader(myContactHeader);

              // Allow
              Allow myAllow = new Allow();
              myAllow.setMethod(
                  "INVITE, ACK, CANCEL, BYE, MESSAGE, OPTIONS, NOTIFY, PRACK, UPDATE, REFER");
              myRequest.addHeader(myAllow);

              // Privacy
              Privacy myPrivacy = new Privacy("none");
              myRequest.addHeader(myPrivacy);

              // PPreferredIdentity
              HeaderFactoryImpl myHeaderFactoryImpl = new HeaderFactoryImpl();
              PPreferredIdentityHeader myPPreferredIdentityHeader =
                  myHeaderFactoryImpl.createPPreferredIdentityHeader(
                      myAddressFactory.createAddress("sip:" + myName + '@' + myServer));
              myRequest.addHeader(myPPreferredIdentityHeader);

              // Route
              Address routeAddress =
                  myAddressFactory.createAddress("sip:orig@scscf." + myServer + ":6060;lr");
              // RouteHeader comentei
              myRouteHeader = myHeaderFactory.createRouteHeader(routeAddress);
              myRequest.addHeader(myRouteHeader);

              // Para proxy funcionar
              SipURI outboundProxyURI = myAddressFactory.createSipURI("proxy", myProxyIP);
              outboundProxyURI.setLrParam();
              outboundProxyURI.setPort(myProxyPort);
              myRouteHeader =
                  myHeaderFactory.createRouteHeader(
                      myAddressFactory.createAddress(outboundProxyURI));
              myRequest.addFirst(myRouteHeader);

              // Content Type
              ContentTypeHeader contentTypeHeader =
                  myHeaderFactory.createContentTypeHeader("application", "sdp");
              myRequest.setContent(content, contentTypeHeader);

              // ClientTransaction comentei aqui
              myClientTransaction = mySipProvider.getNewClientTransaction(myRequest);
              myClientTransaction.setRetransmitTimer(700);
              myClientTransaction.sendRequest();

              status = WAIT_PROV;

              logger.info(">>> " + myRequest.toString());

            } else {
              this.setOff(mySipStack);
            }
          } else {
            if (myStatusCode == 403) {

              System.out.println("Problemas com credenciais!!!!\n");
            } else if (myStatusCode == 401) {

              myName = (String) this.getLocalName();
              myUserID = (String) this.getLocalName() + "@" + myServer;

              Address contactAddress =
                  myAddressFactory.createAddress("sip:" + myName + '@' + myIP + ":" + myPort);
              myContactHeader = myHeaderFactory.createContactHeader(contactAddress);

              myViaHeader = myHeaderFactory.createViaHeader(myIP, myPort, "udp", null);

              fromAddress = myAddressFactory.createAddress(myName + " <sip:" + myUserID + ">");

              Address registrarAddress = myAddressFactory.createAddress("sip:" + myServer);
              Address registerToAddress = fromAddress;
              Address registerFromAddress = fromAddress;

              ToHeader myToHeader = myHeaderFactory.createToHeader(registerToAddress, null);
              FromHeader myFromHeader =
                  myHeaderFactory.createFromHeader(registerFromAddress, "647554");

              ArrayList myViaHeaders = new ArrayList();
              myViaHeaders.add(myViaHeader);

              // System.out.println("myClientTransaction.getRequest():"+
              // myClientTransaction.getRequest());
              CSeqHeader originalCSeq =
                  (CSeqHeader) myClientTransaction.getRequest().getHeader(CSeqHeader.NAME);
              long numseq = originalCSeq.getSeqNumber();
              MaxForwardsHeader myMaxForwardsHeader = myHeaderFactory.createMaxForwardsHeader(70);
              CSeqHeader myCSeqHeader = myHeaderFactory.createCSeqHeader(numseq + 1L, "REGISTER");

              CallIdHeader myCallID =
                  (CallIdHeader) myClientTransaction.getRequest().getHeader(CallIdHeader.NAME);
              CallIdHeader myCallIDHeader = myCallID;
              SipURI myRequestURI = (SipURI) registrarAddress.getURI();
              Request myRegisterRequest =
                  myMessageFactory.createRequest(
                      myRequestURI,
                      "REGISTER",
                      myCallIDHeader,
                      myCSeqHeader,
                      myFromHeader,
                      myToHeader,
                      myViaHeaders,
                      myMaxForwardsHeader);
              myRegisterRequest.addHeader(myContactHeader);

              // Expires
              ExpiresHeader myExpiresHeader;
              if (Unregistring) {
                myExpiresHeader = myHeaderFactory.createExpiresHeader(0);
                myContactHeader.setExpires(0);
              } else {
                myExpiresHeader = myHeaderFactory.createExpiresHeader(60000);
              }
              myRegisterRequest.addHeader(myExpiresHeader);

              // Allow
              Allow myAllow = new Allow();
              myAllow.setMethod(
                  "INVITE, ACK, CANCEL, BYE, MESSAGE, OPTIONS, NOTIFY, PRACK, UPDATE, REFER");
              myRegisterRequest.addHeader(myAllow);

              // Privacy
              Privacy myPrivacy = new Privacy("none");
              myRegisterRequest.addHeader(myPrivacy);

              // PPreferredIdentity
              HeaderFactoryImpl myHeaderFactoryImpl = new HeaderFactoryImpl();
              PPreferredIdentityHeader myPPreferredIdentityHeader =
                  myHeaderFactoryImpl.createPPreferredIdentityHeader(
                      myAddressFactory.createAddress("sip:" + myName + '@' + myServer));
              myRegisterRequest.addHeader(myPPreferredIdentityHeader);

              // Supported
              Supported mySupported = new Supported("path");
              myRegisterRequest.addHeader(mySupported);

              myWWWAuthenticateHeader =
                  Utils.makeAuthHeader(
                      myHeaderFactory, myResponse, myRegisterRequest, myUserID, myPassword);
              myRegisterRequest.addHeader(myWWWAuthenticateHeader);

              myClientTransaction = mySipProvider.getNewClientTransaction(myRegisterRequest);
              myClientTransaction.sendRequest();

              // logger.info(">>> "+myRegisterRequest.toString());
              // System.out.println(">>> " + myRegisterRequest.toString());
              status = REGISTERING;
            }
          }
          break;
      }
    } catch (Exception excep) {
      excep.printStackTrace();
    }
  }
  public void setup() {
    /// System.out.println("=> BUNDLE: br.ufes.inf.ngn.televoto.client.logic | CLASS: LogicListener
    // | METOD: setup ");//By Ju
    try {
      // String log4jConfPath = "//etc//osgi//log4j.properties"; //comentei
      // PropertyConfigurator.configure(log4jConfPath);
      if (!Unregistring) { // Se é um registro
        Object parametros[] = new Object[9];
        parametros = getArguments();
        myDataSource = Activator.ds;
        myServer = (String) parametros[3];
        destination = (String) parametros[6];
        dialTimes = (Integer) parametros[7];
        useQueue = (String) parametros[8];

        try {
          myIP = InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
          e.printStackTrace();
        }

        mySipAlias = (String) parametros[0];
        myName = (String) this.getLocalName();
        myUserID = (String) parametros[1];
        ;
        myPassword = (String) parametros[5];
        myPort = (Integer) parametros[2];
        myAudioPort = (Integer) parametros[4];
        myProxyIP = (String) parametros[9];
        myProxyPort = (Integer) parametros[10];
        afmt = Activator.afmt;
        mySipFactory = SipFactory.getInstance();
        mySipFactory.setPathName("gov.nist");
      }
      mySdpManager = new SdpManager();
      answerInfo = new SdpInfo();
      offerInfo = new SdpInfo();

      myProperties = new Properties();
      myProperties.setProperty("javax.sip.STACK_NAME", myName);
      myProperties.setProperty(
          "javax.sip.OUTBOUND_PROXY", myProxyIP + ":" + myProxyPort + "/UDP"); // Proxy
      mySipStack = mySipFactory.createSipStack(myProperties);
      myMessageFactory = mySipFactory.createMessageFactory();
      myHeaderFactory = mySipFactory.createHeaderFactory();
      myAddressFactory = mySipFactory.createAddressFactory();
      if (!Unregistring) {
        myListeningPoint = mySipStack.createListeningPoint(myIP, myPort, "udp");
        mySipProvider = mySipStack.createSipProvider(myListeningPoint);
        mySipProvider.addSipListener(this);
      }

      myViaHeader = myHeaderFactory.createViaHeader(myIP, myPort, "udp", null);

      Address routeAddress = myAddressFactory.createAddress("sip:" + myServer + ";lr");
      myRouteHeader = myHeaderFactory.createRouteHeader(routeAddress);

      fromAddress = myAddressFactory.createAddress(myName + " <sip:" + myUserID + ">");

      Address registrarAddress = myAddressFactory.createAddress("sip:" + myServer);
      Address registerToAddress = fromAddress;
      Address registerFromAddress = fromAddress;

      ToHeader myToHeader = myHeaderFactory.createToHeader(registerToAddress, null);
      FromHeader myFromHeader = myHeaderFactory.createFromHeader(registerFromAddress, "647554");

      ArrayList myViaHeaders = new ArrayList();
      myViaHeaders.add(myViaHeader);

      MaxForwardsHeader myMaxForwardsHeader = myHeaderFactory.createMaxForwardsHeader(70);
      Random random = new Random();
      CSeqHeader myCSeqHeader =
          myHeaderFactory.createCSeqHeader(random.nextInt(1000) * 1L, "REGISTER");

      CallIdHeader myCallIDHeader = mySipProvider.getNewCallId();
      SipURI myRequestURI = (SipURI) registrarAddress.getURI();
      Request myRegisterRequest =
          myMessageFactory.createRequest(
              myRequestURI,
              "REGISTER",
              myCallIDHeader,
              myCSeqHeader,
              myFromHeader,
              myToHeader,
              myViaHeaders,
              myMaxForwardsHeader);

      // Expires
      ExpiresHeader myExpiresHeader;
      if (Unregistring) {
        myContactHeader.setExpires(0);
        myExpiresHeader = myHeaderFactory.createExpiresHeader(0);
      } else {
        myExpiresHeader = myHeaderFactory.createExpiresHeader(60000);
      }
      myRegisterRequest.addHeader(myExpiresHeader);

      // Allow
      Allow myAllow = new Allow();
      myAllow.setMethod("INVITE, ACK, CANCEL, BYE, MESSAGE, OPTIONS, NOTIFY, PRACK, UPDATE, REFER");
      myRegisterRequest.addHeader(myAllow);

      // Contact
      Address contactAddress =
          myAddressFactory.createAddress(
              "sip:" + myName + '@' + myIP + ":" + myPort + ";transport=udp");
      myContactHeader = myHeaderFactory.createContactHeader(contactAddress);
      myRegisterRequest.addHeader(myContactHeader);

      // Authorization
      Authorization myAuthorization = new Authorization();
      myAuthorization.setScheme("Digest");
      myAuthorization.setUsername(myUserID);
      myAuthorization.setRealm(myServer);
      myAuthorization.setNonce("");
      myAuthorization.setURI(myRegisterRequest.getRequestURI());
      myAuthorization.setResponse("");
      myRegisterRequest.addHeader(myAuthorization);

      // PPreferredIdentity
      HeaderFactoryImpl myHeaderFactoryImpl = new HeaderFactoryImpl();
      PPreferredIdentityHeader myPPreferredIdentityHeader =
          myHeaderFactoryImpl.createPPreferredIdentityHeader(
              myAddressFactory.createAddress("sip:" + myName + '@' + myServer));
      myRegisterRequest.addHeader(myPPreferredIdentityHeader);

      // Privacy
      Privacy myPrivacy = new Privacy("none");
      myRegisterRequest.addHeader(myPrivacy);

      // Supported
      Supported mySupported = new Supported("path");
      myRegisterRequest.addHeader(mySupported);

      // Envia requisição SIP
      myClientTransaction = mySipProvider.getNewClientTransaction(myRegisterRequest);
      myClientTransaction.sendRequest();

      // logger.info(myRegisterRequest.toString());
      // System.out.println(">>> " + myRegisterRequest.toString());
      status = REGISTERING;
    } catch (Exception e) {
      e.printStackTrace();
    }
  }