Android Open Source - msghandle Msg Request Handle






From Project

Back to project page msghandle.

License

The source code is released under:

GNU General Public License

If you think the Android project msghandle listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.anlong.msghandle.handle;
//from   w w  w.ja v a 2 s .com
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import com.anlong.msghandle.common.HandleStaticValue;
import com.anlong.msghandle.request.BaseRequest;
import com.anlong.msghandle.socket.InitMsgSocketServer;
import com.anlong.msghandle.util.ByteAndInt;
import com.anlong.msghandle.util.IMLog;
import com.anlong.msghandle.util.ReflectionUtil;

/**
 * @ClassName: RequestHandle 
 * @Package: com.anlong.imsghandle.common
 * @company ShenZhen anlong Technology CO.,LTD.  
 * @Description: TODO  ?????????? 
 * @author anlong 
 * @date 2013-4-25 ????5:30:25 
 * @version V1.0
 */
public class MsgRequestHandle{
  private ByteArrayOutputStream byteOutputStream = null;
  private DataOutputStream  dataOutputStream = null;
  private static MsgRequestHandle instance = null;

  public MsgRequestHandle(){}
  
  public static MsgRequestHandle getInstance(){
    if ( instance == null )
      instance = new MsgRequestHandle();
    return instance;
  }
  
  
  /**
   * @Title: encode 
   * @Description: TODO ????
   * @author anlong 
   * @param request
   * @param Exception     
   * @throws
   */
  public void encode(Object request) throws Exception {
    if(request instanceof BaseRequest){
      // TODO ????????
      OutputStream outputStream = InitMsgSocketServer.getOutputStream(); 
      if (outputStream == null)
        return ;
      
      // ????????
      BaseRequest baseRequest=(BaseRequest)request;
      
      baseRequest.setMsgSerial(0);
      
      // ??????
      IMLog.anlong(baseRequest.toString());
      
      byteOutputStream = new ByteArrayOutputStream();
      dataOutputStream = new DataOutputStream(byteOutputStream);
      
      // TODO ???????
      this.writeHead(baseRequest,dataOutputStream);    
      
      // TODO ????????
      this.wirteData(request,dataOutputStream);
      
      byte[] buf = byteOutputStream.toByteArray();//???????????????
      
      IMLog.anlong("[??]??????"+buf.length);
      
      byte[] buff = toHH( buf.length );
      for ( int i = 0 ; i < 4 ; i++ ){
        buf[i] = buff[i];
      }
      
      dataOutputStream.close();          
      byteOutputStream.close();

      // TODO ?????????????
      outputStream.write(buf);
      outputStream.flush();
      IMLog.anlong("?????????????!");
    } else {
      IMLog.anlong("?????????");
    }
           
  }
  
  /**
  * @Title: setRequestDataHead 
  * @Description: TODO ??????????
  * @author anlong 
  * @param @param dataOutputStream
  * @param @param baseMessage
  * @param @return
  * @param @throws IOException    ???? 
  * @return DataOutputStream    ???? 
  * @throws
  */
  public void writeHead(BaseRequest baseRequest, DataOutputStream dataOutputStream) throws IOException {
    // ???????????? ,??4
    dataOutputStream.writeInt(baseRequest.getMsgSize());  
    // ????????? ,??4 
    dataOutputStream.writeShort(baseRequest.getBCode());     
    // ?????? ,??4 
    dataOutputStream.writeInt(baseRequest.getKey());  
    // ??????ID,??4
    dataOutputStream.writeInt(baseRequest.getUid());  
    // ?????????
    dataOutputStream.writeByte(baseRequest.getApId());
    // ??????????
    dataOutputStream.writeInt(baseRequest.getMsgSerial());
  }
  
  /**
  * @Title: WriteValue 
  * @Description: TODO  ??IO??????
  * @author anlong 
  * @param @param obj    ???? 
  * @return void    ???? 
  * @throws
  */
  @SuppressWarnings({ "unchecked" })
  private void wirteData(Object obj, DataOutputStream dataOutputStream){
    try {
      // ?????????
      Object instance = obj.getClass().newInstance();
      Field arrField = obj.getClass().getDeclaredField("fieldArr");
      arrField.setAccessible(true);
        String[] fieldList = (String[]) arrField.get(instance);
      
      // ????
      String fieldType = null;
      // ??????????
      String fieldNameUpper = null;
      // ???????
      Object result = null;
      
      for ( String fieldName : fieldList ) {
        Field field= obj.getClass().getDeclaredField(fieldName); 
        fieldType = field.getType().getSimpleName();
        
        fieldNameUpper = Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
        result = ReflectionUtil.invokeMethod(obj, "get"+fieldNameUpper);
        
        if( fieldType.equals("Byte") ){   
          if ( result != null )
            dataOutputStream.writeByte((Byte)result);
          else 
            dataOutputStream.writeByte((byte) 0);
          
        } else if ( fieldType.equals("Short") ){
          if ( result != null ) 
            dataOutputStream.writeShort((Short)result);
          else 
            dataOutputStream.writeShort((short) 0);
          
        } else if ( fieldType.equals("Integer") ){
          if ( result != null )
            dataOutputStream.writeInt((Integer) result);
          else 
            dataOutputStream.writeInt(0);
            
        } else if ( fieldType.equals("String") ){
          if ( result != null ) { 
            String strResult = (String)result;
            byte[] byteArr = strResult.getBytes(HandleStaticValue.CHARSET_NAME);
            dataOutputStream.writeShort((short) byteArr.length);
            dataOutputStream.write(byteArr);
          } else {
            dataOutputStream.writeShort((short) 0);
          }
          
        } else if (fieldType.equals("List")){
          Type fc =  field.getGenericType(); 
          // ???????????
          if( fc instanceof ParameterizedType ){
            ParameterizedType pt = (ParameterizedType) fc;
             // ??????class?????
            Class<Object> genericClazz = (Class<Object>)pt.getActualTypeArguments()[0]; 
            
            List<Object> list = (List<Object>) result;
            if (list != null) {
              // ?????????
              dataOutputStream.writeShort((short) list.size());
              
              for(int i=0;i<list.size();i++){
                // ????????
                if( genericClazz.getSimpleName().equals("Byte") ){     
                  dataOutputStream.writeByte((Byte) list.get(i));
                  
                }else if( genericClazz.getSimpleName().equals("Short") ){
                  dataOutputStream.writeShort((Short) list.get(i));
                  
                }else if( genericClazz.getSimpleName().equals("Integer") ){
                  dataOutputStream.writeInt((Integer) list.get(i));
                  
                }else if( genericClazz.getSimpleName().equals("String") ){
                  String str = (String) list.get(i);
                  byte[] strByteArr = str.getBytes(HandleStaticValue.CHARSET_NAME);
                  
                  dataOutputStream.writeShort((short) strByteArr.length);
                  dataOutputStream.write(strByteArr);
                }else{
                  
                  // ???????
                  ByteArrayOutputStream byteArrayOutputStream2 = new ByteArrayOutputStream();
                  DataOutputStream dataOutputStream2 =  new DataOutputStream(byteArrayOutputStream2);
                  
                  // ???????
                  dataOutputStream2.writeShort((short) 0);
                  wirteData(list.get(i), dataOutputStream2);
                  
                  // ????????
                  byte[] buf1 =  byteArrayOutputStream2.toByteArray();
                  // ???????
                  //IMLog.anlong("???????????" + (buf1.length - 2));
                  byte[] buff = ByteAndInt.short2ByteArray((short) ( buf1.length - 2 ));
                  for(int j=0;j<2;j++){
                    buf1[j] = buff[j];
                  }
                  
                  dataOutputStream.write(buf1, 0, buf1.length);
                  
                  dataOutputStream2.close();
                  byteArrayOutputStream2.close();
                }
              }
            } else {
              dataOutputStream.writeShort((short) 0);
            }
          } 
        }else{  //?????????
          wirteData(result, dataOutputStream);
        }
      }  
    } catch (IOException e) {
      e.printStackTrace();
    } catch (IllegalArgumentException e) {
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      e.printStackTrace();
    } catch (NoSuchFieldException e) {
      e.printStackTrace();
    } catch (InstantiationException e) {
      e.printStackTrace();
    } 
  }
  
    public static byte[] toHH(int n) {
      byte[] b = new byte[4];
      b[3] = (byte) (n & 0xff);
      b[2] = (byte) (n >> 8 & 0xff);
      b[1] = (byte) (n >> 16 & 0xff);
      b[0] = (byte) (n >> 24 & 0xff);
      return b;
    }
    
  /*@Override
  public void handleMessageEvent(MessageEvent event) {
    // TODO ???????????????????
    if (event != null && event.getbCode() == HandleStaticValue.BCODE1000){
      IMLog.anlong("????,?????" + event.getbCode());
      socket = null;
      isConn = true;
    }
  }
  
  static class getSocketTimer extends TimerTask {
    @Override
    public void run() {
      socket = InitServerManagerImpl.getInstance().getSocketIntance();
      IMLog.anlong("[Request]Socket Instance?"+socket);
    } 
  }*/
    
}




Java Source Code List

com.anlong.msghandle.common.BaseMessage.java
com.anlong.msghandle.common.HandleStaticValue.java
com.anlong.msghandle.common.HandleTimer.java
com.anlong.msghandle.common.MessageTimer.java
com.anlong.msghandle.event.MessageEventListener.java
com.anlong.msghandle.event.MessageEventSource.java
com.anlong.msghandle.event.MessageEvent.java
com.anlong.msghandle.file.InitImageFileServer.java
com.anlong.msghandle.handle.ImageRequestHandle.java
com.anlong.msghandle.handle.ImageResponseHandle.java
com.anlong.msghandle.handle.MsgRequestHandle.java
com.anlong.msghandle.handle.MsgResponseHandle.java
com.anlong.msghandle.impl.AppParamImpl.java
com.anlong.msghandle.impl.AppSystemMessageReportImpl.java
com.anlong.msghandle.impl.AuthenticationImpl.java
com.anlong.msghandle.impl.BroadcastImpl.java
com.anlong.msghandle.impl.CommonContactImpl.java
com.anlong.msghandle.impl.DepImpl.java
com.anlong.msghandle.impl.DepInfoListImpl.java
com.anlong.msghandle.impl.EditGroupImpl.java
com.anlong.msghandle.impl.EditGroupUserImpl.java
com.anlong.msghandle.impl.EditUserInfoImpl.java
com.anlong.msghandle.impl.ExitPreventGroupImpl.java
com.anlong.msghandle.impl.GetGroupInfoImpl.java
com.anlong.msghandle.impl.GetHistoryMessageImpl.java
com.anlong.msghandle.impl.GetUserInfoImpl105.java
com.anlong.msghandle.impl.GroupInfoListImpl.java
com.anlong.msghandle.impl.GroupShareImpl.java
com.anlong.msghandle.impl.HeartImpl103.java
com.anlong.msghandle.impl.LocationImpl.java
com.anlong.msghandle.impl.LoginImpl101.java
com.anlong.msghandle.impl.MessageStateReportImpl.java
com.anlong.msghandle.impl.OnlineStateImpl102.java
com.anlong.msghandle.impl.OnlineUserListImpl110.java
com.anlong.msghandle.impl.SendImageFile.java
com.anlong.msghandle.impl.SendMessageImpl203.java
com.anlong.msghandle.impl.SystemParaImpl.java
com.anlong.msghandle.impl.UnreadMessageImpl.java
com.anlong.msghandle.impl.UpdatePasswordImpl.java
com.anlong.msghandle.impl.UserInfoListImpl.java
com.anlong.msghandle.interfac.AbstractMsgHandle.java
com.anlong.msghandle.interfac.BaseActivity.java
com.anlong.msghandle.interfac.MessageHandleActivity.java
com.anlong.msghandle.interfac.MsgHandle.java
com.anlong.msghandle.message.InitRequMessageServer.java
com.anlong.msghandle.message.InitRespMessageServer.java
com.anlong.msghandle.request.BaseRequest.java
com.anlong.msghandle.request.Request100.java
com.anlong.msghandle.request.Request101.java
com.anlong.msghandle.request.Request102.java
com.anlong.msghandle.request.Request103.java
com.anlong.msghandle.request.Request105.java
com.anlong.msghandle.request.Request110.java
com.anlong.msghandle.request.Request203.java
com.anlong.msghandle.request.Request301.java
com.anlong.msghandle.response.BaseResponse.java
com.anlong.msghandle.response.Response1000.java
com.anlong.msghandle.response.Response1010.java
com.anlong.msghandle.response.Response1020.java
com.anlong.msghandle.response.Response1030.java
com.anlong.msghandle.response.Response1050.java
com.anlong.msghandle.response.Response1100.java
com.anlong.msghandle.response.Response2030.java
com.anlong.msghandle.response.Response3010.java
com.anlong.msghandle.service.InitServerManager.java
com.anlong.msghandle.service.ServerManager.java
com.anlong.msghandle.socket.InitFileSocketServer.java
com.anlong.msghandle.socket.InitMsgSocketServer.java
com.anlong.msghandle.util.ByteAndInt.java
com.anlong.msghandle.util.FormatTransfer.java
com.anlong.msghandle.util.IMLog.java
com.anlong.msghandle.util.ReflectionUtil.java
com.anlong.msghandle.util.Utils.java
com.anlong.msghandle.vo.AppMessage.java
com.anlong.msghandle.vo.Application.java
com.anlong.msghandle.vo.BroadcastMessage.java
com.anlong.msghandle.vo.DepInfo.java
com.anlong.msghandle.vo.GroupInfo.java
com.anlong.msghandle.vo.GroupShare.java
com.anlong.msghandle.vo.OnlineUser.java
com.anlong.msghandle.vo.ReportState.java
com.anlong.msghandle.vo.SimpleUserInfo.java
com.anlong.msghandle.vo.UserInfo.java
com.anlong.msghandle.vo.UserMessage.java