callprocessing.java

来自「java 开发的sip软电话 源码 jain sip」· Java 代码 · 共 1,497 行 · 第 1/4 页

JAVA
1,497
字号
            
        }
    }

    //-------------------------- Requests ---------------------------------
    void processInvite(ServerTransaction serverTransaction, Request invite)
    {
        try {
        	
            Dialog dialog = serverTransaction.getDialog();
            
        	if(!isBusy){
        	Call call = callDispatcher.createCall(dialog, invite);
            sipManCallback.fireCallReceived(call);
            //change status
            call.setState(Call.ALERTING);
            //sdp description may be in acks - bug report Laurent Michel
            ContentLengthHeader cl = invite.getContentLength();
            if (cl != null
                && cl.getContentLength() > 0) {
                call.setRemoteSdpDescription(new String(invite.getRawContent()));
            }
            //Are we the one they are looking for?
            URI calleeURI = ( (ToHeader) invite.getHeader(ToHeader.NAME)).
                getAddress().getURI();
            /** @todo We shoud rather ask the user what to do here as some
               would add prefixes or change user URIs*/
            if (calleeURI.isSipURI()) {
                boolean assertUserMatch = Boolean.valueOf(Utils.getProperty("net.java.mais.sip.FAIL_CALLS_ON_DEST_USER_MISMATCH")).booleanValue();
                //user info is case sensitive according to rfc3261
                if (assertUserMatch)
                {
                    String calleeUser = ( (SipURI) calleeURI).getUser();
                    String localUser = sipManCallback.getLocalUser();
                    if (calleeUser != null && !calleeUser.equals(localUser)) {
                        sipManCallback.fireCallRejectedLocally(
                                "The user specified by the caller did not match the local user!",
                                invite
                        );
                        call.setState(Call.DISCONNECTED);
                        Response notFound = null;
                        try {
                            notFound = sipManCallback.messageFactory.createResponse(
                                    Response.NOT_FOUND,
                                    invite
                            );
                            sipManCallback.attachToTag(notFound, dialog);
                        }
                        catch (ParseException ex) {
                            call.setState(Call.DISCONNECTED);
                            sipManCallback.fireCommunicationsError(
                                    new CommunicationsException(
                                            "Failed to create a NOT_FOUND response to an INVITE request!"
                                            , ex)
                            );
                            return;
                        }
                        try {
                            serverTransaction.sendResponse(notFound);
                                
                        }
                        catch (SipException ex) {
                            call.setState(Call.DISCONNECTED);
                            sipManCallback.fireCommunicationsError(
                                    new CommunicationsException(
                                            "Failed to send a NOT_FOUND response to an INVITE request!"
                                            , ex)
                            );
                            return;
                        }
                        return;
                    }
                }
            }

            //Send RINGING
            Response ringing = null;
            try {
                ringing = sipManCallback.messageFactory.createResponse(
                    Response.RINGING,
                    invite
                    );
                sipManCallback.attachToTag(ringing, dialog);
            }
            catch (ParseException ex) {
                call.setState(Call.DISCONNECTED);
                sipManCallback.fireCommunicationsError(
                    new CommunicationsException(
                    "Failed to create a RINGING response to an INVITE request!"
                    , ex)
                    );
                return;
            }
            try {
                serverTransaction.sendResponse(ringing);
                    
            }
            catch (SipException ex) {
                call.setState(Call.DISCONNECTED);
                sipManCallback.fireCommunicationsError(
                    new CommunicationsException(
                    "Failed to send a RINGING response to an INVITE request!"
                    , ex)
                    );
                return;
            }
        	}else{
                //Send BUSY_HERE
                Response busy = null;
                try {
                    busy = sipManCallback.messageFactory.createResponse(
                        Response.BUSY_HERE,
                        invite
                        );
                    sipManCallback.attachToTag(busy, dialog);
                }
                catch (ParseException ex) {
                    sipManCallback.fireCommunicationsError(
                        new CommunicationsException(
                        "Failed to create a RINGING response to an INVITE request!"
                        , ex)
                        );
                    return;
                }
                try {
                    serverTransaction.sendResponse(busy);
                }
                catch (SipException ex) {
                    sipManCallback.fireCommunicationsError(
                        new CommunicationsException(
                        "Failed to send a RINGING response to an INVITE request!"
                        , ex)
                        );
                    return;
                }
        	}
        }
        finally
        {
            
        }

    }

    void processTimeout(Transaction transaction, Request request)
    {
        try
        {
            

            Call call = callDispatcher.findCall(transaction.getDialog());
            if (call == null) {
                return;
            }
            sipManCallback.fireCommunicationsError(
                new CommunicationsException("The remote party has not replied!"
                                            + "The call will be disconnected")
                );
            //change status
            call.setState(Call.DISCONNECTED);
        }
        finally
        {
            
        }
    }

    void processBye(ServerTransaction serverTransaction, Request byeRequest)
    {
        try
        {
            

            //find the call
            Call call = callDispatcher.findCall(serverTransaction.
                                                getDialog());
            if (call == null) {
                sipManCallback.fireUnknownMessageReceived(byeRequest);
                return;
            }
            //change status
            call.setState(Call.DISCONNECTED);
            //Send OK
            Response ok = null;
            try {
                ok = sipManCallback.messageFactory.
                    createResponse(Response.OK, byeRequest);
                sipManCallback.attachToTag(ok, call.getDialog());
            }
            catch (ParseException ex) {
                sipManCallback.fireCommunicationsError(
                    new CommunicationsException(
                    "Failed to construct an OK response to a BYE request!",
                    ex)
                    );
                return;
            }
            try {
                serverTransaction.sendResponse(ok);
                    
            }
            catch (SipException ex) {
                //This is not really a problem according to the RFC
                //so just dump to stdout should someone be interested
            }
        }
        finally
        {
            
        }

    }

    void processAck(ServerTransaction serverTransaction, Request ackRequest)
    {
        try
        {
            

            if (!serverTransaction.getDialog().getFirstTransaction().getRequest().
                getMethod().equals(Request.INVITE)) {
                
                return;
            }
            //find the call
            Call call = callDispatcher.findCall(serverTransaction.
                                                getDialog());
            if (call == null) {
                //this is most probably the ack for a killed call - don't signal it
                //sipManCallback.fireUnknownMessageReceived(ackRequest);
                
                return;
            }
            ContentLengthHeader cl = ackRequest.getContentLength();
            if (cl != null
                && cl.getContentLength() > 0)
            {
                call.setRemoteSdpDescription(new String(ackRequest.getRawContent()));
            }
            //change status
            call.setState(Call.CONNECTED);
        }
        finally
        {
            
        }

    }

    void processCancel(ServerTransaction serverTransaction,
                       Request cancelRequest)
    {
        try
        {
            

            if (!serverTransaction.getDialog().getFirstTransaction().getRequest().
                getMethod().equals(Request.INVITE)) {
                //For someone else
                
                return;
            }
            //find the call
            Call call = callDispatcher.findCall(serverTransaction.
                                                getDialog());
            if (call == null) {
                sipManCallback.fireUnknownMessageReceived(cancelRequest);
                return;
            }
            //change status
            call.setState(Call.DISCONNECTED);
            // Cancels should be OK-ed and the initial transaction - terminated
            // (report and fix by Ranga)
            try {
                Response ok = sipManCallback.messageFactory.createResponse(Response.
                    OK, cancelRequest);
                sipManCallback.attachToTag(ok, call.getDialog());
                serverTransaction.sendResponse(ok);
                    
            }
            catch (ParseException ex) {
                sipManCallback.fireCommunicationsError(
                    new CommunicationsException(
                    "Failed to create an OK Response to an CANCEL request.", ex));
            }
            catch (SipException ex) {
                sipManCallback.fireCommunicationsError(
                    new CommunicationsException(
                    "Failed to send an OK Response to an CANCEL request.", ex));
            }
            try {
                //stop the invite transaction as well
                Transaction tran = call.getDialog().getFirstTransaction();
                //should be server transaction and misplaced cancels should be
                //filtered by the stack but it doesn't hurt checking anyway
                if (! (tran instanceof ServerTransaction)) {
                    sipManCallback.fireCommunicationsError(
                        new CommunicationsException(
                        "Received a misplaced CANCEL request!"));
                    return;
                }
                ServerTransaction inviteTran = (ServerTransaction) tran;
                Request invite = call.getDialog().getFirstTransaction().getRequest();
                Response requestTerminated =
                    sipManCallback.
                    messageFactory.
                    createResponse(Response.REQUEST_TERMINATED, invite);
                sipManCallback.attachToTag(requestTerminated, call.getDialog());
                inviteTran.sendResponse(requestTerminated);
                
            }
            catch (ParseException ex) {
                sipManCallback.fireCommunicationsError(
                    new CommunicationsException(
                    "Failed to create a REQUEST_TERMINATED Response to an INVITE request.",
                    ex));
            }
            catch (SipException ex) {
                sipManCallback.fireCommunicationsError(
                    new CommunicationsException(
                    "Failed to send an REQUEST_TERMINATED Response to an INVITE request.",
                    ex));
            }
        }
        finally
        {
            
        }

    }

    // ----------------- Responses --------------------------
    //NOT FOUND
    void processNotFound(ClientTransaction clientTransaction, Response response)
    {
        try
        {
            

            if (!clientTransaction.getDialog().getFirstTransaction().getRequest().
                getMethod().equals(Request.INVITE)) {
                //Not for us
                
                return;
            }
            //find the call
            Call call = callDispatcher.findCall(clientTransaction.
                                                getDialog());
            call.setState(Call.DISCONNECTED);
            sipManCallback.fireCallRejectedRemotely(
                "Server returned a NOT FOUND Response",
                response
                );
        }
        finally
        {
            
        }

    }

    void processNotImplemented(ClientTransaction clientTransaction,
                               Response response)
    {
        try
        {
            if (!clientTransaction.getDialog().getFirstTransaction().getRequest().
                getMethod().equals(Request.INVITE)) {
                //Not for us
                
                return;
            }
            //find the call
            Call call = callDispatcher.findCall(clientTransaction.
                                                getDialog());

⌨️ 快捷键说明

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