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


package cmpp.v2_0; 
 
import java.util.Arrays; 
 
public class ConnectResp extends CMPP 
{ 
    private  final int commandId 		= CMPP.ID_CMPP_CONNECT_RESP; 
    private  final int commandLength 	= 18; 
     
    private  int 	Status				= 0; 
    private  byte[] AuthenticatorISMG	= null; 
    private  int 	Version = CMPP.CMPP_VERSION; 
     
    private	 byte[] AuthenticatorSource	= null; 
    private	 String SharedSecret		= null; 
     
    public ConnectResp() 
    { 
        super(CMPP.ID_CMPP_CONNECT_RESP); 
    } 
    public ConnectResp(CMPP cmpp) 
    { 
        super(cmpp); 
    } 
 
    /** 
     * getters 
     * @return 
     */ 
    public int getStatus() 
    { 
        return Status; 
    } 
    public byte[] getAuthenticatorISMG() 
    { 
        return AuthenticatorISMG; 
    } 
    public int getVersion() 
    { 
        return Version; 
    } 
    public boolean checkSharedSecret(byte[] authenticatorSource, String localSecret) 
    { 
    	byte[] localAuthenticator = getAuthenticator(Status, authenticatorSource, localSecret); 
    	if(Arrays.equals(localAuthenticator,AuthenticatorISMG)) { 
            return true; 
    	} else { 
    		return false; 
    	} 
    } 
 
    /** 
     * setters 
     * @param result 
     */ 
    public void setStatus(int status)  { 
        Status = status; 
    } 
    public void setAuthenticatorSource(byte[] authenticatorSource) { 
    	AuthenticatorSource = authenticatorSource; 
    } 
    public void setSharedSecret(String sharedSecret) { 
    	SharedSecret = sharedSecret; 
    } 
 
    /** 
     *  
     */ 
    protected int parseBody() 
    { 
    	if( super.bodyLength < commandLength ) {	//length too short. 
    		return -1; 
    	} 
    	 
    	int off = 0; 
    	//Status 
    	Status = super.bodybytes[off]; 
        off += 1; 
    	//AuthenticatorISMG 
        AuthenticatorISMG = new byte[16]; 
        CMPP.BytesCopy(super.bodybytes, AuthenticatorISMG, off, off + 15, 0); 
    	off += 16; 
    	//Version 
    	Version = super.bodybytes[off]; 
    	off += 1; 
 
        return 0; 
    } 
 
    /** 
     *  
     */ 
    protected int makeBody() { 
    	 
    	// make bodybytes 
    	super.bodyLength = commandLength;  
    	super.bodybytes  = new byte[super.bodyLength]; 
    	Arrays.fill(super.bodybytes,(byte)0); 
 
    	//make AuthenticatorSource 
    	if( AuthenticatorSource == null ) { 
    		AuthenticatorSource = new byte[0]; 
    	} 
    	//make SharedSecret  
    	if( SharedSecret == null ){ 
    		SharedSecret = ""; 
    	} 
    	//make AuthenticatorISMG 
    	AuthenticatorISMG = getAuthenticator(Status, AuthenticatorSource, SharedSecret); 
    	 
    	// make body 
    	int off = 0; 
    	CMPP.BytesCopy(CMPP.IntToBytes4(Status), super.bodybytes, 0, 3, off ); 
    	off += 4; 
    	CMPP.BytesCopy(AuthenticatorISMG,super.bodybytes, 0, 15, off ); 
    	off += 16; 
    	super.bodybytes[off] = CMPP.IntToByte(Version); 
    	off += 1; 
     
    	return 0; 
    } 
     
    /** 
     *  
     * @param status 
     * @param AuthenticatorSource 
     * @param secret 
     * @return 
     */ 
    private byte[] getAuthenticator(int status, byte[] authenticatorSource, String secret) { 
 
    	if( authenticatorSource == null )  
    		authenticatorSource =  new byte[0]; 
    	if( secret == null )  
    		secret = ""; 
    	 
    	byte[] buffer = new byte[ 1 + authenticatorSource.length + secret.length()]; 
    	Arrays.fill(buffer, (byte)0); 
    	 
    	int off =  0; 
    	buffer[off] = CMPP.IntToByte(status); 
    	off += 1; 
    	CMPP.BytesCopy(authenticatorSource, buffer, 0, authenticatorSource.length-1, off ); 
    	off += authenticatorSource.length; 
    	CMPP.BytesCopy(secret.getBytes(), buffer, 0, secret.length()-1, off); 
    	off += secret.length(); 
    	 
    	return getMD5(buffer);  
    } 
 
    /** 
     *  
     * @return 
     */ 
    public String toString() { 
    	StringBuffer sb = new StringBuffer(); 
 
    	sb.append("Status=" + Status + "\n"); 
    	sb.append("AuthenticatorISMG=" + AuthenticatorISMG + "\n"); 
    	sb.append("Version=" + Version + "\n"); 
    	 
    	return sb.toString(); 
    } 
	public int getCommandId() { 
		return commandId; 
	} 
	public int getCommandLength() { 
		return commandLength; 
	} 
     
}