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

📄 messageprocessor.java

📁 是一个用java实现的
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
		SipURI calleeURI =
			(SipURI) ((FromHeader) cancel.getHeader(FromHeader.NAME))
				.getAddress()
				.getURI();
		String callee =
			"sip:" + calleeURI.getUser()
				+ "@"
				+ calleeURI.getHost().trim().toLowerCase();
		//Find the Audio call
		AudioCall call = callManager.findAudioCall(callee);
		//Send OK
		try {
			Response ok =
				(Response) MessageListener.messageFactory.createResponse(
					Response.OK,
					cancel);
			serverTransaction.sendResponse(ok);
			call.setStatus(AudioCall.CANCEL);
			messageListener.sipMeetingManager.notifyObserversNewCallStatus(
				call);
			System.out.println(
				"Audio Call removed : " + call.getDialog().getDialogId());
			callManager.removeAudioCall(call);
		} catch (SipException se) {
			se.printStackTrace();
		} catch (ParseException pe) {
			pe.printStackTrace();
		}

	}

	/**
	 * Process the MESSAGE received request. MESSAGE may be handled statefully or
	 * statelessly.
	 * @param serverTransaction - the server transaction associated with the request
	 * @param message - the request 
	 */
	public void processMessage(
		ServerTransaction serverTrans,
		Request message) {
                    
               ServerTransaction serverTransaction = null;

                try {
		
                    serverTransaction = 
			(serverTrans == null? 
				messageListener.sipProvider.getNewServerTransaction(message): 
				serverTrans);
                } catch (javax.sip.TransactionAlreadyExistsException ex) {
                    ex.printStackTrace();
                    return;
                } catch (javax.sip.TransactionUnavailableException ex1) {
                    ex1.printStackTrace();
                    return;
                }
                           

                       

		//Send OK
		try {
			String sender = null;
			Address address =
				((FromHeader) message.getHeader(FromHeader.NAME)).getAddress();
			if (address.getURI().isSipURI()) {
				SipURI sipURI = ((SipURI) address.getURI());
				String host = sipURI.getHost();
				String user = sipURI.getUser();
				sender = user + "@" + host;
			}
                        
			//Getting the sdp body for creating the response
			//This sdp body will present what codec has been chosen
			//and on which port every media will be received
			ContentTypeHeader contentTypeHeader =
				(ContentTypeHeader) message.getHeader(ContentTypeHeader.NAME);
			if (contentTypeHeader == null
				|| contentTypeHeader.getContentType() == null
				|| contentTypeHeader.getContentSubType() == null)
				return;
			String subType = contentTypeHeader.getContentSubType();
			//If we have a voice messaging plays it
			if (contentTypeHeader.getContentType().equals("audio")) {
				if (contentTypeHeader.getContentSubType().equals("x-gsm")
					|| contentTypeHeader.getContentSubType().equals("gsm")) {
					Response ok =
						(
							Response) MessageListener
								.messageFactory
								.createResponse(
							Response.OK,
							message);
					ToHeader toHeader =
						(ToHeader) message.getHeader(ToHeader.NAME);
					if (toHeader.getTag() == null)
						toHeader.setTag(
							new Integer((int) (Math.random() * 10000))
								.toString());
					serverTransaction.sendResponse(ok);
					byte[] voiceMessage = message.getRawContent();
					//if(!voicePlayer.isInitialized())
					voicePlayer.initialize(voiceMessage);
					/*else					
						voicePlayer.setData(voiceMessage);*/
					voicePlayer.play();
					message.removeContent();
				} else {
					System.out.println(
						"Cannot handle this codec "
							+ contentTypeHeader.getContentSubType());
				}
			} else if (
				contentTypeHeader.getContentType().equals("text")
					&& contentTypeHeader.getContentSubType().equals("plain")) {
				Response ok =
					(Response) MessageListener.messageFactory.createResponse(
						Response.OK,
						message);
				ToHeader toHeader = (ToHeader) message.getHeader(ToHeader.NAME);
				if (toHeader.getTag() == null) {
					toHeader.setTag(
						new Integer((int) (Math.random() * 10000)).toString());
                                        IMCall imcall = this.messengerManager.callManager.findIMCall(sender);
                                        if (imcall == null) {
                                                imcall = new IMCall(sender);
                                                imcall.setDialog(serverTransaction.getDialog());
                                                this.messengerManager.callManager.addIMCall(imcall);
                                        } else imcall.setDialog(serverTransaction.getDialog());
                                }
				serverTransaction.sendResponse(ok);
				String content = new String(message.getRawContent());
				FromHeader fromHeader =
					(FromHeader) message.getHeader(FromHeader.NAME);
				messageListener.sipMeetingManager.notifyObserversIMReceived(
					content,
					sender);
			} else {
				// send a Not Acceptable here message.
				Response notok =
					(Response) MessageListener.messageFactory.createResponse(
						Response.NOT_ACCEPTABLE,
						message);
				ToHeader toHeader = (ToHeader) message.getHeader(ToHeader.NAME);
				serverTransaction.sendResponse(notok);
				System.out.println(
					"Cannot handle this content Type " + contentTypeHeader);
			}
                        
			message.removeContent();
		} catch (SipException se) {
			se.printStackTrace();
		} catch (ParseException pe) {
			pe.printStackTrace();
		}
	}

	/**
	 * Process the SUBSCRIBE received request
	 * @param serverTransaction - the server transaction associated with the request
	 * @param subscribe - the request 
	 */
	public void processSubscribe(
		ServerTransaction serverTransaction,
		Request subscribe) {
		Address address =
			((FromHeader) subscribe.getHeader(FromHeader.NAME)).getAddress();
		String sender = null;
		if (address.getURI().isSipURI()) {
			SipURI sipURI = ((SipURI) address.getURI());
			String host = sipURI.getHost();
			String user = sipURI.getUser();
			sender = user + "@" + host;
		}
		Subscriber subscriber = new Subscriber(sender);
		subscriber.setDialog(serverTransaction.getDialog());
		messageListener.sipMeetingManager.notifySubscribe(subscriber);
	}

	/**
	 * Process the NOTIFY received request
	 * @param serverTransaction - the server transaction associated with the request
	 * @param notify - the request 
	 */
	public void processNotify(
		ServerTransaction serverTransaction,
		Request notify) {
		//Send OK
		try {
			Response ok =
				(Response) MessageListener.messageFactory.createResponse(
					Response.OK,
					notify);
			ToHeader toHeader = (ToHeader) notify.getHeader(ToHeader.NAME);
			if (toHeader.getTag() == null)
				toHeader.setTag(
					new Integer((int) (Math.random() * 10000)).toString());
			serverTransaction.sendResponse(ok);

			Address address =
				((FromHeader) notify.getHeader(FromHeader.NAME)).getAddress();
			String sender = null;
			if (address.getURI().isSipURI()) {
				SipURI sipURI = ((SipURI) address.getURI());
				String host = sipURI.getHost();
				String user = sipURI.getUser();
				sender = user + "@" + host;
			}
			ContentTypeHeader contentTypeHeader =
				(ContentTypeHeader) notify.getHeader(ContentTypeHeader.NAME);
			if (contentTypeHeader != null) {
				String xmlType = contentTypeHeader.getContentSubType();

				if (xmlType.equals("xpidf+xml")) {
					String pidfContent = new String(notify.getRawContent());
					int endDocIndex = pidfContent.indexOf("<presence>");
					pidfContent = pidfContent.substring(endDocIndex);
					XMLpidfParser pidfParser = new XMLpidfParser();
					pidfParser.parsePidfString(pidfContent);
					PresenceTag presenceTag = pidfParser.getPresenceTag();
					presenceTag.setAddress(sender);
					messageListener.sipMeetingManager.notifyPresence(
						presenceTag);
				}
			}
		} catch (SipException se) {
			se.printStackTrace();
		} catch (ParseException pe) {
			pe.printStackTrace();
		}
	}

	/**
	 * After a
	 * @param clientTransaction
	 * @param response
	 */
	public void processRequestTerminated(
		ClientTransaction clientTransaction,
		Response response) {

	}

	/**********************************************************************/
	/*                                                                    */
	/*                    Handling response messages                      */
	/*                                                                    */
	/**********************************************************************/

	/**
	 * Process the Not found received response
	 * @param clientTransaction - the client transaction associated with the response
	 * @param notFound - the not found response
	 */
	public void processNotFound(
		ClientTransaction clientTransaction,
		Response notFound) {

	}

	/**
	 * Process the Not Implemented received response
	 * @param clientTransaction - the client transaction associated with the response
	 * @param notImplemented - the not found response
	 */
	public void processNotImplemented(
		ClientTransaction clientTransaction,
		Response notImplemented) {

	}

	/**
	 * Process the Trying received response
	 * @param clientTransaction - the client transaction associated with the response
	 * @param trying - the trying response
	 */
	public void processTrying(
		ClientTransaction clientTransaction,
		Response trying) {
		CallManager callManager =
			messageListener.sipMeetingManager.getCallManager();
		//Strip out the callee
		SipURI calleeURI =
			(SipURI) ((ToHeader) trying.getHeader(ToHeader.NAME))
				.getAddress()
				.getURI();
		String callee =
			"sip:" + calleeURI.getUser() + "@" + calleeURI.getHost();
		//Find the Audio call
		AudioCall call = callManager.findAudioCall(callee);
		if (call != null) {
			call.setStatus(AudioCall.TRYING);
			messageListener.sipMeetingManager.notifyObserversNewCallStatus(
				call);
		}
	}

	/**
	 * Process the Ringing received response
	 * @param clientTransaction - the client transaction associated with the response
	 * @param ringing - the ringing response
	 */
	public void processRinging(
		ClientTransaction clientTransaction,
		Response ringing) {
		CallManager callManager =
			messageListener.sipMeetingManager.getCallManager();
		//Strip out the callee
		SipURI calleeURI =
			(SipURI) ((ToHeader) ringing.getHeader(ToHeader.NAME))
				.getAddress()
				.getURI();
		String callee =
			"sip:" + calleeURI.getUser() + "@" + calleeURI.getHost();
		//Find the Audio call
		AudioCall call = callManager.findAudioCall(callee);
		call.setStatus(AudioCall.RINGING);
		messageListener.sipMeetingManager.notifyObserversNewCallStatus(call);
	}

	/**
	 * Process the OK received response for a REGISTER 
	 * @param clientTransaction - the client transaction associated with the response
	 * @param registerOK - the OK received response for a REGISTER 
	 */
	public void processRegisterOK(
		ClientTransaction clientTransaction,
		Response registerOK) {
		FromHeader fromHeader =
			((FromHeader) registerOK.getHeader(FromHeader.NAME));
		Address address = fromHeader.getAddress();
		ExpiresHeader expires = registerOK.getExpires();
		if (expires != null && expires.getExpires() == 0) {
			if (messageListener.sipMeetingManager.reRegisterFlag)
				// This is an OK for a cleared registration.
				messageListener.sipMeetingManager.register();
			else
				messageListener.sipMeetingManager.setRegisterStatus(
					RegisterStatus.NOT_REGISTERED);
		} else {
			messageListener.sipMeetingManager.setRegisterStatus(
				RegisterStatus.REGISTERED);
		}
	}

	/**

⌨️ 快捷键说明

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