www.pudn.com > cmppNeu.rar > Connect.java


package cmpp.v2_0; 
 
import java.util.Arrays; 
import java.util.Calendar; 
import java.util.Date; 
 
public class Connect extends CMPP { 
	private  final int 	commandId 		= CMPP.ID_CMPP_CONNECT; 
	private	 final int	commandLength 	= 27; 
	 
    private  String	SourceAddr			= null;			// 	sp_id 
    private  byte[] AuthenticatorSource	= null;			//	AuthenticatorSource 
    private	 String SharedSecret		= null;			//  password 
    private	 int	Version 	= CMPP.CMPP_VERSION;	//	Version; 
    private	 int 	Timestamp	= 0;					// 	Timestamp; 
     
    public Connect() 
    { 
        super(CMPP.ID_CMPP_CONNECT); 
    } 
    public Connect(CMPP cmpp) 
    { 
        super(cmpp); 
    } 
    /** 
     * getters for read 
     * @return 
     */ 
    public String getSourceAddr() 
    { 
        return SourceAddr; 
    } 
    public byte[] getAuthenticatorSource() 
    { 
        return AuthenticatorSource; 
    } 
    public int getVersion() 
    { 
        return Version; 
    } 
    public int getTimestamp() 
    { 
        return Timestamp; 
    } 
    public boolean checkSharedSecret(String localSecret) 
    { 
    	byte[] localAuthenticator = getAuthenticator(SourceAddr,localSecret,Timestamp); 
    	if(Arrays.equals(localAuthenticator,AuthenticatorSource)) { 
            return true; 
    	} else { 
    		return false; 
    	} 
    } 
 
    /** 
     * setters for write 
     */ 
    public void setSourceAddr(String sourceAddr) 
    { 
    	SourceAddr = sourceAddr; 
    } 
    public void setSharedSecret(String sharedSecret) 
    { 
    	SharedSecret = sharedSecret; 
    } 
 
    /** 
     *  
     * @return 
     */ 
    protected int parseBody() 
    { 
    	if( super.bodyLength < commandLength ) {	//length too short. 
    		return -1; 
    	} 
    	 
    	int off = 0; 
        byte abyte0[] = new byte[16]; 
 
        // SourceAddr 
        SourceAddr = new String(super.bodybytes, off, 6 ); 
        off += 6; 
        // AuthenticatorSource 
        AuthenticatorSource = new byte[16]; 
        CMPP.BytesCopy(super.bodybytes, AuthenticatorSource, off, off + 15, 0); 
        off += 16; 
        // Version 
        Version = super.bodybytes[off]; 
        off += 1; 
        //Timestamp 
        CMPP.BytesCopy(super.bodybytes, abyte0, off, off + 4 - 1, 0); 
        Timestamp = CMPP.Bytes4ToInt(abyte0); 
        off += 4; 
         
        return 0; 
    } 
 
    /** 
     *  
     * @return 
     */ 
    protected int makeBody() { 
    	 
    	// make bodybytes 
    	super.bodyLength = commandLength;  
    	super.bodybytes  = new byte[super.bodyLength]; 
    	Arrays.fill(super.bodybytes,(byte)0); 
  
    	// make SourceAddr 
    	if( SourceAddr == null ) { 
    		SourceAddr = ""; 
    	} 
    	// make Timestamp 
    	if( Timestamp == 0  ) { // need generate 
    		Calendar calendar = Calendar.getInstance(); 
     		calendar.setTime(new Date()); 
     		Timestamp = (calendar.get(Calendar.MONTH) + 1) * 0x5f5e100  
        	+ calendar.get(Calendar.DAY_OF_MONTH) * 0xf4240 
            + calendar.get(Calendar.HOUR) * 10000 
            + calendar.get(Calendar.MINUTE) * 100  
            + calendar.get(Calendar.SECOND); 
    	} 
    	// make AuthenticatorSource 
    	AuthenticatorSource = getAuthenticator(SourceAddr, SharedSecret,Timestamp ); 
    	 
    	// make body 
    	int off = 0; 
    	CMPP.BytesCopy(SourceAddr.getBytes(), super.bodybytes, 0, 5, off ); 
    	off += 6; 
    	CMPP.BytesCopy(AuthenticatorSource, super.bodybytes, 0, 15, off ); 
    	off += 16; 
    	super.bodybytes[off] = CMPP.IntToByte(Version); 
    	off += 1; 
    	CMPP.BytesCopy(CMPP.IntToBytes4(Timestamp),super.bodybytes, 0, 3, off ); 
    	off += 4; 
    	 
    	return 0; 
    } 
   
	/** 
	 *  
	 * @param addr 
	 * @param secret 
	 * @param timestamp 
	 * @return 
	 */ 
    private byte[] getAuthenticator(String addr, String secret, int timestamp) { 
    	 
    	String strTimestamp = Integer.toString(timestamp); 
    	while( strTimestamp.length()<10 ) { 
    		strTimestamp = "0" + strTimestamp; 
    	} 
    		 
    	byte[] buffer = new byte[6 + 9 + secret.length() + 10]; 
    	Arrays.fill(buffer, (byte)0); 
    	 
    	int off = 0; 
    	CMPP.BytesCopy(addr.getBytes(),  buffer, 0, 5, off );  
    	off += 6; 
    	off += 9; 
    	CMPP.BytesCopy(secret.getBytes(), buffer, 0, secret.length()-1, off );  
    	off += secret.length(); 
    	 
    	CMPP.BytesCopy(strTimestamp.getBytes(), buffer, 0, strTimestamp.length()-1, off); 
    	off += 10; 
    	 
    	return getMD5(buffer);  
    } 
     
    /** 
     *  
     * @return 
     */ 
    public String toString() { 
    	StringBuffer sb = new StringBuffer(); 
 
    	sb.append("SourceAddr=" + SourceAddr + "\n"); 
    	sb.append("AuthenticatorSource=" + AuthenticatorSource + "\n"); 
    	sb.append("Version=" + Version + "\n"); 
    	sb.append("Timestamp=" + Timestamp + "\n"); 
    	 
    	return sb.toString(); 
    } 
	public int getCommandId() { 
		return commandId; 
	} 
	public int getCommandLength() { 
		return commandLength; 
	} 
}