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

📄 displaypictureretrieveworker.java

📁 Java MSN Library of protocol 12
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		MsnP2PSlpMessage okMessage = new MsnP2PSlpMessage();		okMessage.setSlpMessage(okSlpMessage);		okMessage.setIdentifier(getNextIdentifier());		okMessage.setTotalLength(okSlpMessageLength);		okMessage.setCurrentLength(okSlpMessageLength);		okMessage.setField7(NumberUtils.getIntRandom());		okMessage.setP2PDest(creator.getEmailAddress());		// Send the message		switchboard.sendMessage(okMessage);		// Log		log.info("Denied direct-Connection invitation for retrieval of avatar for " +				  msnObject.getCreator());	}	////////////////////////////////////////////////////////////////////////////	//                                                                        //	//                  MsnSwitchboardListener Implementation                 //	//                                                                        //	////////////////////////////////////////////////////////////////////////////	/**	 * @see jml.event.MsnSwitchboardListener#switchboardStarted(MsnSwitchboard)	 */	public void switchboardStarted(MsnSwitchboard switchboard) {		// Check if it is the switchboard we are looking for		if (switchboard.getAttachment() != this) {			return;		}		// Log		log.info("Switchboard started for avatar download for " +				  msnObject.getCreator());		// Store the switchboard		this.switchboard = switchboard;		// Add the contact to the switchboard		switchboard.inviteContact(creator);	}	/**	 * @see jml.event.MsnSwitchboardListener#switchboardClosed(MsnSwitchboard)	 */	public void switchboardClosed(MsnSwitchboard switchboard) {		// Check if it is the switchboard we are looking for		if (switchboard.getAttachment() != this) {			return;		}		// Notify the finalization		notifyFinalization(				DisplayPictureListener.ResultStatus.PROTOCOL_ERROR,				0,				true);	}	/**	 * @see jml.event.MsnSwitchboardListener#contactJoinSwitchboard(MsnSwitchboard,	 *                                                    MsnContact)	 */	public void contactJoinSwitchboard(MsnSwitchboard switchboard,			                           MsnContact contact) {		// Check if it is the switchboard we are looking for		if (switchboard.getAttachment() != this) {			return;		}		// Send the invite request		sendP2PInviteRequest();	}	/**	 * @see jml.event.MsnSwitchboardListener#contactLeaveSwitchboard(MsnSwitchboard,	 *                                                     MsnContact)	 */	public void contactLeaveSwitchboard(MsnSwitchboard switchboard,			                            MsnContact contact) {		// Check if it is the switchboard we are looking for		if (switchboard.getAttachment() != this) {			return;		}		// Notify the finalization		notifyFinalization(				DisplayPictureListener.ResultStatus.PROTOCOL_ERROR,                0,				true);	}	////////////////////////////////////////////////////////////////////////////	//                                                                        //	//                    MsnMessageListener Implementation                   //	//                                                                        //	//////////////////////////////////////////////////////////////////////////////	/**//	 * File to store the MsnObject.//	 *///	private RandomAccessFile buffer = null;        private ByteArrayOutputStream buffer = null;	/**	 * State of P2P messages reception.	 */	private TransferState state = TransferState.WAITING_INVITE_ACK;	/**	 * Ammount of received data for the avatar.	 */	private int receivedData = 0;	/**	 * States of messages reception	 */	private enum TransferState {		/**		 * We are waiting for the ACK for the INVITE request.		 */		WAITING_INVITE_ACK,		/**		 * We are waiting fro the 200 ok message.		 */		WAITING_200_OK,		/**		 * We are waiting for the data preparation message.		 */		WAITING_DATA_PREPARATION,		/**		 * We are waiting for the data messages.		 */		WAITING_DATA,		/**		 * We are waiting the Bye ack message.		 */		WAITING_BYE_ACK	}	/**	 * @see jml.event.MsnMessageListener#p2pMessageReceived(MsnSwitchboard,	 *                                            MsnP2PMessage,	 *                                            MsnContact)	 */	public void p2pMessageReceived(MsnSwitchboard switchboard,			                       MsnP2PMessage message,			                       MsnContact contact) {		// Check that is for our switchboard		if (switchboard.getAttachment() != this) {			return;		}		// Check for the incoming message so we can process it //		// Check if it is a Bye ACK (so it is saying good bye)		if (message.getFlag() == 0x40) {			// Check if the process is active or not			if (state == TransferState.WAITING_BYE_ACK) {				// Finish the process				finishProcess();			}			else {				// Notify about the error and finish the process//				notifyFinalization(//						DisplayPictureListener.ResultStatus.PROTOCOL_ERROR,//						new Integer(message.getFlag()),//						true);			}		}		// Check if we receive a wait		else if (message.getFlag() == 0x4) {//			MIME-Version: 1.0//			Content-Type: text/x-keepalive			// Send the keep-alive message//			sendKeepAliveMessage();		}		// Check if we receive and ACK		else if (message.getFlag() == MsnP2PMessage.FLAG_ACK) {			// We can only receive an ACK for the invitation or for the			// Bye message			if (state == TransferState.WAITING_INVITE_ACK &&				message.getField7() == baseId) {				// Set next step				state = TransferState.WAITING_200_OK;			}			else if (state == TransferState.WAITING_BYE_ACK &&				message.getField7() == getLastIdentifier()) {				// Notify the final of the object retrieval        		finishProcess();			}		}		// Check if we receive a SLP response		else if (message instanceof MsnP2PSlpMessage &&			((MsnP2PSlpMessage) message).getSlpMessage() instanceof MsnslpResponse) {			// Check that the response is for us			MsnP2PSlpMessage p2pSlpMessage = (MsnP2PSlpMessage) message;			MsnslpResponse slpResponse =				(MsnslpResponse) p2pSlpMessage.getSlpMessage();			int sessionId = slpResponse.getBodys().getIntProperty("SessionID");			if (sessionId == transferSessionId) {				// Check that it is a 200 response				if (slpResponse.getStatusCode() == 200) {					// Set next step					state = TransferState.WAITING_DATA_PREPARATION;					// Send ACK					sendP2PAck(message, false);				}				// It is an error				else {					// Send a Bye message					sendP2PByeMessage();					// Set the next step					state = TransferState.WAITING_BYE_ACK;					// Notify about the error					notifyFinalization(							DisplayPictureListener.ResultStatus.PROTOCOL_ERROR,                            message.getFlag(),							false);				}			}		}		// Check if we receive a data preparation message		else if (message instanceof MsnP2PPreperationMessage &&			     message.getSessionId() == transferSessionId) {				// Set next step				state = TransferState.WAITING_DATA;				// Send the ACK				sendP2PAck(message, true);		}		// Check if we receive data messages		else if (message instanceof MsnP2PDataMessage &&				 message.getSessionId() == transferSessionId) {			// Set next step			state = TransferState.WAITING_DATA;        	// Get the message        	MsnP2PDataMessage dataMsg = (MsnP2PDataMessage) message;        	try {	        	// Check if we have created a buffer for the Object	        	if (buffer == null) {//	        		buffer = new RandomAccessFile(storeFile, "rw");                                buffer = new ByteArrayOutputStream();	        	}	        	// Get the data	        	byte[] data = dataMsg.bodyToMessage();	        	// Add to the already data//	       		buffer.seek(dataMsg.getOffset());//	       		buffer.write(data);                        buffer.write(data, 0, data.length);	       		// Set the ammount of received data	       		receivedData += data.length;	    		// Log	    		log.info("Received data message ('" + receivedData + "','" +	    				  dataMsg.getTotalLength() +	    				  "') for retrieval of avatar for " +	    				  msnObject.getCreator());	        	// Check if we have finished//	        	if (buffer.length() == dataMsg.getTotalLength()) {                        if (buffer.size() == dataMsg.getTotalLength()) {		    		// Log		    		log.info("Finished receiving data for retrieval of avatar for " +		    				  msnObject.getCreator());	        		// Close the file	        		buffer.close();	        		// Send the data acknowledgement					sendP2PAck(message, true);	        		// Send Bye message					sendP2PByeMessage();	        		// Change the state	        		state = TransferState.WAITING_BYE_ACK;		    		// Log		    		log.info("Notifing about retrieved avatar for " +		    				  msnObject.getCreator());	        		// Notify that we have the file	        		notifyFinalization(	        				DisplayPictureListener.ResultStatus.GOOD,	        				null,	        				false);	        	}        	} catch (IOException e) {        		// Send Bye message				sendP2PByeMessage();        		// Change the state        		state = TransferState.WAITING_BYE_ACK;        		// Notify about the error        		notifyFinalization(        			DisplayPictureListener.ResultStatus.FILE_ACCESS_ERROR,        			e,        			false);        	}		}		// Check if it is an invitation for a direct connection		else if (message instanceof MsnP2PInvitationMessage) {			// Check that it is an invitation for direct connection			MsnP2PInvitationMessage invite = (MsnP2PInvitationMessage) message;			MsnslpRequest inviteRequest = (MsnslpRequest) invite.getSlpMessage();			if (inviteRequest.getContentType().equals(					"application/x-msnmsgr-transreqbody")) {				// Check that the SessionID and the CallID are the same				int sessionId = inviteRequest.getBodys().getIntProperty("SessionID");				String callId = inviteRequest.getCallId();				if (sessionId == transferSessionId && callId.equals(callId)) {					// Send an ACK for the message					sendP2PAck(message, false);					// Send a deny for direct connection					sendDirectConnectionDeny(invite);				}			}		}	}	/**	 * @see jml.event.MsnMessageListener#controlMessageReceived(MsnSwitchboard,	 *                                                MsnControlMessage,	 *                                                MsnContact)	 */	public void controlMessageReceived(MsnSwitchboard switchboard,			                           MsnControlMessage message,			                           MsnContact contact) {	}	/**	 * @see jml.event.MsnMessageListener#datacastMessageReceived(MsnSwitchboard,	 *                                                 MsnDatacastMessage,	 *                                                 MsnContact)	 */	public void datacastMessageReceived(MsnSwitchboard switchboard,			                            MsnDatacastMessage message,			                            MsnContact contact) {	}	/**	 * @see jml.event.MsnMessageListener#instantMessageReceived(MsnSwitchboard,	 *                                                MsnInstantMessage,	 *                                                MsnContact)	 */	public void instantMessageReceived(MsnSwitchboard switchboard,			                           MsnInstantMessage message,			                           MsnContact contact) {	}	/**	 * @see jml.event.MsnMessageListener#systemMessageReceived(MsnMessenger,	 *                                               MsnSystemMessage)	 */	public void systemMessageReceived(MsnMessenger messenger,			                          MsnSystemMessage message) {	}	/**	 * @see jml.event.MsnMessageListener#unknownMessageReceived(MsnSwitchboard,	 *                                                MsnUnknownMessage,	 *                                                MsnContact)	 */	public void unknownMessageReceived(MsnSwitchboard switchboard,			                           MsnUnknownMessage message,			                           MsnContact contact) {	}}

⌨️ 快捷键说明

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