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

📄 displaypictureretrieveworker.java

📁 Java MSN Library of protocol 12
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package jml.message.p2p;// JRE ioimport jml.*;import jml.event.MsnAdapter;import jml.message.*;import jml.protocol.msnslp.MsnslpMessage;import jml.protocol.msnslp.MsnslpRequest;import jml.protocol.msnslp.MsnslpResponse;import jml.util.*;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import java.io.ByteArrayOutputStream;import java.io.IOException;/** * This class implements the process to retrieve a display picture or emoticon. * Other objects must be done through file transer. An instance of this class * can only be used once. * * @author Angel Barragán Chacón */public class DisplayPictureRetrieveWorker extends MsnAdapter {	/**	 * Loger for the class.	 */    private static final Log log =    	LogFactory.getLog(DisplayPictureRetrieveWorker.class);    ////////////////////////////////////////////////////////////////////////////	/**	 * Instance of the MsnMessenger instance where we must perform the process.	 */	private MsnMessenger messenger = null;	/**	 * Instance of the MsnObject to be retrieved.	 */	private MsnObject msnObject = null;	/**	 * Instance of the listerner to notify about the retrieval progress.	 */	private DisplayPictureListener listener = null;	////////////////////////////////////////////////////////////////////////////	/**	 * Creates a new instance of the process to retrieve a MsnObject.	 *	 * @param messenger Instance of the messenger client.	 * @param msnObject Instance of the MsnObject to be retrieved.	 * @param listener Listener for the process progress.	 */	public DisplayPictureRetrieveWorker(			MsnMessenger messenger,			MsnObject msnObject,			DisplayPictureListener listener) {		this.messenger = messenger;		this.msnObject = msnObject;		this.listener = listener;		// Add listener for messages		messenger.addMessageListener(this);	}	////////////////////////////////////////////////////////////////////////////	/**	 * Creator of the MsnObject.	 */	private Email creator = null;	/**	 * Switchboard where the retrieval will be performed.	 */	private MsnSwitchboard switchboard = null;	/**	 * Signals if this task is active.	 */	private boolean active = true;	/**	 * Flag to denote if the notification has already been sended.	 */	private boolean notified = false;	/**	 * Start the process of retrieving the object.	 */	public void start() {		// Clear everithing		if (!active) {			return;		}		// Get the creator of the object		creator = Email.parseStr(msnObject.getCreator());		// Add this object as a switchboard listener		messenger.addSwitchboardListener(this);		// Create the switchboard		messenger.newSwitchboard(this);	}	/**	 * Perform the final steps of the object retrieval.	 */	private synchronized void finishProcess() {		// Check that it isn't already disabled		if (!active) {			return;		}		// Disable this worker		active = false;		// Remove the switchboard listener		messenger.removeSwitchboardListener(this);		// Remove listeners		messenger.removeMessageListener(this);		// If the switchboard has been created, close it		switchboard.close();	}	/**	 * Finish this process and notify the listener about finalization.	 *	 * @param result Result of the finalization.	 * @param context Context of the finalization.	 * @param finishProcess True if we must finish the process and false if not.	 */	private synchronized void notifyFinalization(			DisplayPictureListener.ResultStatus result,			Object context,			boolean finishProcess) {		// First finish the switchboard		if (finishProcess) {			finishProcess();		}		// Check if the notification has already been sent		if (notified) {			return;		}		// Mark the notification		notified = true;		// Check if we must remove the file		if (buffer != null &&			result != DisplayPictureListener.ResultStatus.GOOD) {                        // clear the buffer as creating it once again                        buffer = new ByteArrayOutputStream();//			storeFile.delete();		}                byte[] res;                if(buffer == null)                    res = new byte[0];                else                    res = buffer.toByteArray();		// Notify the result		listener.notifyMsnObjectRetrieval(				messenger,				this,				msnObject,				result,                                res,				context);	}	////////////////////////////////////////////////////////////////////////////	/**	 * Base identifier for the messages.	 */	private int baseId = NumberUtils.getIntRandom();	/**	 * Retrieves the next identifier to send a message.	 *	 * @return Next identifier to send messages.	 */	private int getNextIdentifier() {		return ++baseId;	}	/**	 * Retrieves the last generated identifier.	 *	 * @return Last generated identifier.	 */	private int getLastIdentifier() {		return baseId;	}	////////////////////////////////////////////////////////////////////////////	/**	 * Generates a new Call identifier for the invite command.	 *	 * @return The new call identifier.	 */	private String generateNewCallId() {		// Generate the variant number		int variable = NumberUtils.getIntRandom();		// Convert to hex value		String hex =  NumberUtils.toHexValue(variable);		// Compose the final call id		return "{2B073406-65D8-A7B2-5B13-B287" + hex + "}";	}	////////////////////////////////////////////////////////////////////////////	/**	 * Session identification for this MsnObject transmision.	 */	private int transferSessionId = -1;	/**	 * Call identifier.	 */	private String callId;	/**	 * Random identifier for the messages.	 */	private int lastRandomIdentifier = 0;	/**	 * Sends a P2P invitation request to init the MsnObject retrieval.	 */	private void sendP2PInviteRequest() {		// Create the session identifier		transferSessionId = NumberUtils.getIntRandom();		lastRandomIdentifier = NumberUtils.getIntRandom();		callId = generateNewCallId();		// Create an invitation message		MsnslpRequest req = new MsnslpRequest();		MsnP2PInvitationMessage msg = new MsnP2PInvitationMessage();		msg.setSlpMessage(req);		// Set the destination for the message (the MsnObject creator)		msg.setP2PDest(creator.getEmailAddress());		// Set the binary Header		msg.setSessionId(0);		msg.setIdentifier(getNextIdentifier());		msg.setFlag(0);		msg.setField7(lastRandomIdentifier);		msg.setField8(0);		msg.setField9(0);		// Body		req.setRequestMethod(MsnP2PInvitationMessage.METHOD_INVITE);		req.setRequestURI("MSNMSGR:" + creator.getEmailAddress());		req.setTo("<msnmsgr:" + creator.getEmailAddress() + ">");		req.setFrom("<msnmsgr:" +				    messenger.getOwner().getEmail().getEmailAddress() + ">");		req.setVia("MSNSLP/1.0/TLP ;branch=" + callId);		req.setCSeq(0);		req.setCallId(callId);		req.setMaxForwards(0);		req.setContentType("application/x-msnmsgr-sessionreqbody");		StringHolder body = new StringHolder();		body.setProperty(MsnP2PInvitationMessage.KEY_GUID_EUF,				         MsnP2PInvitationMessage.GUID_EUF);		body.setProperty("SessionID", transferSessionId);		body.setProperty("AppID", 1);		body.setProperty(MsnP2PInvitationMessage.KEY_CONTEXT,		                 StringUtils.encodeBase64(msnObject.toString()));		req.setBody(body.toString() +				    JmlConstants.LINE_SEPARATOR + "\0");		// Get the size of the message to be setted		int slpMessageLength = Charset.encodeAsByteArray(req.toString()).length;		msg.setTotalLength(slpMessageLength);		msg.setCurrentLength(slpMessageLength);		// Binary Footer		msg.setAppId(0);		// Send the message		switchboard.sendMessage(msg);		// Log		log.info("Sended Invite request for retrieval of avatar for " +				  msnObject.getCreator());		// Log the message//		log.info("Sending P2P message\n" + msg.toDebugString());	}	/**	 * Send a P2P ACK message for a received P2P message.	 *	 * @param message Received P2P message.	 * @param sessionIdFlag True if the session Id must be setted.	 */	private void sendP2PAck(MsnP2PMessage message, boolean sessionIdFlag) {		// Create the ACK message		MsnP2PAckMessage ack = new MsnP2PAckMessage(				getNextIdentifier(),				creator.getEmailAddress(),				message);		if (sessionIdFlag) {			ack.setSessionId(transferSessionId);		}		// Send the message		switchboard.sendMessage(ack);	}	/**	 * Send the bye Message.	 */	private void sendP2PByeMessage() {		MsnslpRequest req = new MsnslpRequest();		MsnP2PByeMessage bye = new MsnP2PByeMessage();		bye.setSlpMessage(req);		lastRandomIdentifier = NumberUtils.getIntRandom();		// Set the destination for the message (the MsnObject creator)		bye.setP2PDest(creator.getEmailAddress());		// Set the binary Header		bye.setSessionId(0);		bye.setIdentifier(getNextIdentifier());		bye.setFlag(MsnP2PMessage.FLAG_BYE);		bye.setField7(lastRandomIdentifier);		bye.setField8(0);		bye.setField9(0);		// Set body		req.setRequestMethod(MsnP2PByeMessage.METHOD_BYE);		req.setRequestURI("MSNMSGR:" + creator.getEmailAddress());		req.setTo("<msnmsgr:" + creator.getEmailAddress() + ">");		req.setFrom("<msnmsgr:" +				    messenger.getOwner().getEmail().getEmailAddress() + ">");		req.setVia(			"MSNSLP/1.0/TLP ;branch=" + callId);		req.setCSeq(0);		req.setCallId(callId);		req.setMaxForwards(0);		req.setContentType("application/x-msnmsgr-sessionclosebody");		req.setBody(JmlConstants.LINE_SEPARATOR + "\0");		// Get the size of the message to be setted		int slpMessageLength = Charset.encodeAsByteArray(req.toString()).length;		bye.setTotalLength(slpMessageLength);		bye.setCurrentLength(slpMessageLength);		// Set footer		bye.setAppId(0);		// Send the message		switchboard.sendMessage(bye);		// Log		log.info("Sended Bye request for retrieval of avatar for " +				  msnObject.getCreator());	}	/**	 * Send a response to deny a direct connection invitation.	 *	 * @param invite Original invitation message.	 */	private void sendDirectConnectionDeny(MsnP2PInvitationMessage invite) {        MsnslpMessage msnslpRequest = invite.getSlpMessage();		MsnslpResponse okSlpMessage = new MsnslpResponse();		okSlpMessage.setStatusCode(405);		okSlpMessage.setReasonPhrase("Not supported");		okSlpMessage.setTo(msnslpRequest.getFrom());		okSlpMessage.setFrom(msnslpRequest.getTo());		okSlpMessage.setVia(msnslpRequest.getVia());		okSlpMessage.setCSeq(msnslpRequest.getCSeq() + 1);		okSlpMessage.setCallId(msnslpRequest.getCallId());		okSlpMessage.setMaxForwards(msnslpRequest.getMaxForwards());		okSlpMessage.setContentType(msnslpRequest.getContentType());		StringHolder body = new StringHolder();		body.setProperty("SessionID", transferSessionId);		okSlpMessage.setBody(body.toString() + JmlConstants.LINE_SEPARATOR				+ "\0");		int okSlpMessageLength = Charset.encodeAsByteArray(okSlpMessage				.toString()).length;

⌨️ 快捷键说明

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