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

📄 sipmanager.java.svn-base

📁 开源项目openfire的完整源程序
💻 SVN-BASE
📖 第 1 页 / 共 5 页
字号:
            return maxForwardsHeader;
        }
        catch (InvalidArgumentException ex) {
            throw new CommunicationsException(
                    "A problem occurred while creating MaxForwardsHeader",
                    ex);
        }

    }

    /**
     * Returns the user used to create the From Header URI.
     *
     * @return the user used to create the From Header URI.
     */
    public String getLocalUser() {
        try {

            return ((SipURI) getFromHeader().getAddress().getURI()).getUser();
        }
        catch (CommunicationsException ex) {
            return "";
        }
    }

    /**
     * Generates a ToTag (the containingDialog's hashCode())and attaches it to
     * response's ToHeader.
     *
     * @param response         the response that is to get the ToTag.
     * @param containingDialog the Dialog instance that is to extract a unique Tag value
     *                         (containingDialog.hashCode())
     */
    public void attachToTag(Response response, Dialog containingDialog) {
        ToHeader to = (ToHeader) response.getHeader(ToHeader.NAME);
        if (to == null) {
            fireCommunicationsError(new CommunicationsException(
                    "No TO header found in, attaching a to tag is therefore impossible"));
        }
        try {
            if (to.getTag() == null || to.getTag().trim().length() == 0) {

                // the containing dialog may be null (e.g. when called by
                // sendNotImplemented). Attach sth else in that case.
                // Bug Report - Joe Provino - SUN Microsystems
                int toTag = containingDialog != null ? containingDialog
                        .hashCode() : (int) System.currentTimeMillis();

                to.setTag(Integer.toString(toTag));
            }
        }
        catch (ParseException ex) {
            fireCommunicationsError(new CommunicationsException(
                    "Failed to attach a TO tag to an outgoing response"));
        }
    }

    // ================================ PROPERTIES ================================
    protected void initProperties() {
        try {

            // ------------------ stack properties --------------

            // network address management is handled by
            // common.NetworkAddressManager
            // stackAddress = Utils.getProperty("javax.sip.IP_ADDRESS");
            // if (stackAddress == null) {

            stackAddress = getLocalHostAddress();
            // Add the host address to the properties that will pass the stack
            SIPConfig.setIPAddress(stackAddress);
            SIPConfig.setSystemProperties();

            // ensure IPv6 address compliance
            if (stackAddress.indexOf(':') != stackAddress.lastIndexOf(':')
                    && stackAddress.charAt(0) != '[') {
                stackAddress = '[' + stackAddress.trim() + ']';
            }
            stackName = SIPConfig.getStackName();
            if (stackName == null) {
                stackName = "SIPark@" + Integer.toString(hashCode());
                // Add the stack name to the properties that will pass the stack
            }

            String retransmissionFilter = SIPConfig.getRetransmissionFilter();

            // ------------ application properties --------------
            //currentlyUsedURI = SIPConfig.getPublicAddress();
            if (currentlyUsedURI == null) {
                currentlyUsedURI = SIPConfig.getUserName() + "@" + stackAddress;
            }
            if (!currentlyUsedURI.trim().toLowerCase().startsWith("sip:")) {
                currentlyUsedURI = "sip:" + currentlyUsedURI.trim();
            }

            // at this point we are sure we have a sip: prefix in the uri
            // we construct our pres: uri by replacing that prefix.
            String presenceUri = "pres"
                    + currentlyUsedURI.substring(currentlyUsedURI.indexOf(':'));

            registrarAddress = SIPConfig.getRegistrarAddress();
            try {
                registrarPort = SIPConfig.getRegistrarPort();
            }
            catch (NumberFormatException ex) {
                registrarPort = 5060;
            }
            registrarTransport = SIPConfig.getRegistrarTransport();

            if (registrarTransport == null) {
                registrarTransport = DEFAULT_TRANSPORT;
            }
            try {
                registrationsExpiration = SIPConfig.getRegistrationExpiration();
            }
            catch (NumberFormatException ex) {
                registrationsExpiration = 3600;
            }
            sipStackPath = SIPConfig.getStackPath();
            if (sipStackPath == null) {
                sipStackPath = "gov.nist";
            }

            String routerPath = SIPConfig.getRouterPath();

            transport = SIPConfig.getTransport();

            if (transport.equals("")) {
                transport = DEFAULT_TRANSPORT;
            }
            try {
                localPort = SIPConfig.getLocalPort();
            }
            catch (NumberFormatException exc) {
                localPort = 5060;
            }
            displayName = SIPConfig.getDisplayName();

        }
        catch (Exception e) {
            Log.error("initProperties", e);
        }
    }

    // ============================ SECURITY ================================

    /**
     * Sets the SecurityAuthority instance that should be consulted later on for
     * user credentials.
     *
     * @param authority the SecurityAuthority instance that should be consulted later
     *                  on for user credentials.
     */
    public void setSecurityAuthority(SecurityAuthority authority) {
        // keep a copy
        this.securityAuthority = authority;
        sipSecurityManager.setSecurityAuthority(authority);
    }

    /**
     * Adds the specified credentials to the security manager's credentials
     * cache so that they get tried next time they're needed.
     *
     * @param realm       the realm these credentials should apply for.
     * @param credentials a set of credentials (username and pass)
     */
    public void cacheCredentials(String realm, Credentials credentials) {
        sipSecurityManager.cacheCredentials(realm, credentials);
    }

    // ============================ EVENT DISPATHING ============================

    /**
     * Adds a CommunicationsListener to SipManager.
     *
     * @param listener The CommunicationsListener to be added.
     */
    public void addCommunicationsListener(CommunicationsListener listener) {
        try {
            listeners.add(listener);
        }
        catch (Exception e) {
            Log.error("addCommunicationsListener", e);
        }
    }

    // ------------ call received dispatch
    void fireCallReceived(Call call) {
        try {
            CallEvent evt = new CallEvent(call);
            for (int i = listeners.size() - 1; i >= 0; i--) {
                ((CommunicationsListener) listeners.get(i)).callReceived(evt);
            }
        }
        catch (Exception e) {
            Log.error("fireCallReceived", e);
        }
    } // call received

    void fireCallEnded(Call call) {
        try {
            CallEvent evt = new CallEvent(call);
            for (int i = listeners.size() - 1; i >= 0; i--) {
                ((CommunicationsListener) listeners.get(i)).callEnded(evt);
            }
        }
        catch (Exception e) {
            Log.error("fireCallEnded", e);
        }
    } // call received

    // ------------ call received dispatch
    void fireMessageReceived(Request message) {

        MessageEvent evt = new MessageEvent(message);
        for (int i = listeners.size() - 1; i >= 0; i--) {
            ((CommunicationsListener) listeners.get(i))
                    .messageReceived(evt);
        }
    } // call received

    // ------------ registerred
    void fireRegistered(String address) {
        SIPTransaction.setRegisterTimeout(64);
        try {
            registerProcessing.subscribe(registrarAddress, registrarPort, registrarTransport);
        } catch (CommunicationsException e) {
            Log.error(e);
        }
        RegistrationEvent evt = new RegistrationEvent(address);
        for (int i = listeners.size() - 1; i >= 0; i--) {
            ((CommunicationsListener) listeners.get(i)).registered(evt);
        }
    } // call received

    // ------------ registering
    void fireRegistering(String address) {
        RegistrationEvent evt = new RegistrationEvent(address);
        for (int i = listeners.size() - 1; i >= 0; i--) {
            ((CommunicationsListener) listeners.get(i)).registering(evt);
        }
    } // call received

    // ------------ unregistered
    public void fireUnregistered(String address) {
        RegistrationEvent evt = new RegistrationEvent(address);
        for (int i = listeners.size() - 1; i >= 0; i--) {
            ((CommunicationsListener) listeners.get(i)).unregistered(evt);
        }
    }

    void fireRegistrationFailed(String address, RegistrationEvent.Type type) {
        if (registrationFailed(type)) {
            RegistrationEvent evt = new RegistrationEvent(address, type);
            for (int i = listeners.size() - 1; i >= 0; i--) {
                ((CommunicationsListener) listeners.get(i)).registrationFailed(evt);
            }
        }
    }

    void fireUnregistering(String address) {
        RegistrationEvent evt = new RegistrationEvent(address);
        for (int i = listeners.size() - 1; i >= 0; i--) {
            ((CommunicationsListener) listeners.get(i)).unregistering(evt);
        }
    }

    // ---------------- received unknown message
    void fireUnknownMessageReceived(Message message) {
        UnknownMessageEvent evt = new UnknownMessageEvent(message);
        for (int i = listeners.size() - 1; i >= 0; i--) {
            ((CommunicationsListener) listeners.get(i))
                    .receivedUnknownMessage(evt);
        }
    }

    // ---------------- rejected a call
    public void fireCallRejectedLocally(String reason, Message invite, Call call) {
        CallRejectedEvent evt = new CallRejectedEvent(reason, invite, call);
        for (int i = listeners.size() - 1; i >= 0; i--) {
            ((CommunicationsListener) listeners.get(i))
                    .callRejectedLocally(evt);
        }
    }

    void fireCallRejectedRemotely(String reason, Message invite, Call call) {
        CallRejectedEvent evt = new CallRejectedEvent(reason, invite, call);
        for (int i = listeners.size() - 1; i >= 0; i--) {
            ((CommunicationsListener) listeners.get(i))
                    .callRejectedRemotely(evt);
        }

    }

    // call rejected
    // ---------------- error occurred
    public void fireCommunicationsError(Throwable throwable) {
        try {
            CommunicationsErrorEvent evt = new CommunicationsErrorEvent(
                    throwable);
            for (int i = listeners.size() - 1; i >= 0; i--) {
                ((CommunicationsListener) listeners.get(i)).communicationsErrorOccurred(evt);
            }
        }
        catch (Throwable e) {
            Log.error("fireCommunicationsError", e);
        }

    } // error occurred

    // ============================= SIP LISTENER METHODS ==============================
    public void processRequest(RequestEvent requestReceivedEvent) {
        Log.debug(requestReceivedEvent.getRequest().toString());

        ServerTransaction serverTransaction = requestReceivedEvent
                .getServerTransaction();

        Request request = requestReceivedEvent.getRequest();

        if (serverTransaction == null) {
            try {
                serverTransaction = sipProvider
                        .getNewServerTransaction(request);
            }
            catch (TransactionAlreadyExistsException ex) {
                return;
            }
            catch (TransactionUnavailableException ex) {
                return;
            }
        }
        Dialog dialog = serverTransaction.getDialog();

        if (request.getMethod().equals(Request.NOTIFY)) {
            Response ok = null;
            try {
                ok = messageFactory.createResponse(Response.OK,
                        request);
            }
            catch (ParseException ex) {
                ex.printStackTrace();
            }

⌨️ 快捷键说明

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