Exemplo n.º 1
1
 private byte[] readPacket() throws IOException {
   byte[] reslutPacket = null;
   byte p_head, p_type;
   byte[] p_lenByte = new byte[4];
   int p_lenInt;
   int index, buf;
   while (true) {
     try {
       p_head = in.readByte();
       if (p_head == ProtocolVar.PacketType.PACKET_HEAD) {
         p_type = in.readByte();
         in.read(p_lenByte);
         p_lenInt = (int) ConvertTools.byteArrToLong_littleEnd(p_lenByte, 0);
         reslutPacket = new byte[p_lenInt];
         reslutPacket[0] = p_head;
         reslutPacket[1] = p_type;
         ConvertTools.copyByteArr(p_lenByte, reslutPacket, 0, 2, 4);
         index = 6;
         /** 直到收到所有数据 */
         while (index < p_lenInt) {
           if (in.available() != 0) {
             buf = in.read(reslutPacket, index, p_lenInt - index);
             if (buf != -1) {
               index += buf;
             }
           }
         }
         break;
       }
     } catch (ArrayIndexOutOfBoundsException ex) {
       Logger.getLogger(CameraTask.class.getName()).log(Level.INFO, devSeqStr, ex);
     }
   }
   return reslutPacket;
 }
Exemplo n.º 2
0
 private void clearStreamResource() {
   if (in != null) {
     try {
       in.close();
     } catch (IOException ex) {
       Logger.getLogger(CameraTask.class.getName())
           .log(
               Level.INFO,
               ConvertTools.devSeqByteArr2Str(devSeq)
                   + "inputStream close 异常 in closeLink()Method",
               ex);
     }
   }
   if (out != null) {
     try {
       out.close();
     } catch (IOException ex) {
       Logger.getLogger(CameraTask.class.getName())
           .log(
               Level.INFO,
               ConvertTools.devSeqByteArr2Str(devSeq)
                   + "outputStream close 异常 in closeLink()Method",
               ex);
     }
   }
   if (cameraSocket != null && !cameraSocket.isClosed()) {
     try {
       cameraSocket.close();
     } catch (IOException ex) {
       Logger.getLogger(CameraTask.class.getName()).log(Level.SEVERE, null, ex);
     }
   }
 }
Exemplo n.º 3
0
  public void connect() {
    try {
      devSeqStr = cameraUtil.getDevSeq();
      devSeq = ConvertTools.devSeqStr2ByteArr(devSeqStr);
      serverIP = cameraUtil.getServerIP();

      cameraSocket = new Socket();
      SocketAddress sa = new InetSocketAddress(ConvertTools.ipByteArr2Str(serverIP), 22616);
      cameraSocket.connect(sa, 60 * 1000);

      port = cameraSocket.getLocalPort();
      cameraUtil.setOnline(true);
      localIP = cameraSocket.getInetAddress().getAddress();

      in = new DataInputStream(cameraSocket.getInputStream());
      out = new DataOutputStream(cameraSocket.getOutputStream());

      heartbeatTimer = new Timer();
      alarmTimer = new Timer();

    } catch (IOException ex) {
      Logger.getLogger(CameraTask.class.getName())
          .log(Level.INFO, "设备:" + devSeqStr + " 端口:" + port, ex);
      destoryTask(CLEAR_LEVEL_3);
      return;
    }

    // ---------先登录注册 建立可发送指令的连接
    if (proKeyReq() == GlobalVar.SUCCESSED) {
      try {
        if (proLoginReq() == GlobalVar.SUCCESSED) {
          Camera.addCameraTask(devSeqStr, this);
          receivePacket();
        }
      } catch (IOException ex) {
        Logger.getLogger(CameraTask.class.getName()).log(Level.SEVERE, devSeqStr, ex);
        destoryTask(CLEAR_LEVEL_3);
      }
    }
    destoryTask(CLEAR_LEVEL_3);
  }
Exemplo n.º 4
0
  /**
   * 发送 申请密钥 数据包
   *
   * @return
   * @throws IOException
   */
  private int proKeyReq() {
    // -------------------------------------发送 申请密钥 数据包
    byte[] keyReqByteArr, keyRespByteArr;
    try {
      keyReqByteArr =
          InitReqSecretKeyPacket.init(ConvertTools.devSeqStr2ByteArr(cameraUtil.getDevSeq()))
              .toByteArray();

      //            Logger.getLogger(CameraTask.class.getName()).log(Level.INFO, "申请密钥数据包:");
      //            PrintHelper.printByteArr(keyReqByteArr, 0);
      sendPacket(keyReqByteArr);

      // -------------------------------------接收 申请密钥 数据包
      keyRespByteArr = readPacket();

      //            Logger.getLogger(CameraTask.class.getName()).log(Level.INFO, "回复密钥数据包:");
      //            PrintHelper.printByteArr(keyRespByteArr, 0);
      Kaer_SecretKey_resp keyResp = new Kaer_SecretKey_resp();

      if (GlobalVar.FAILED == keyResp.parsePacket(keyRespByteArr)) {
        Logger.getLogger(CameraTask.class.getName()).log(Level.INFO, "申请密钥数据包解析错误");
        return GlobalVar.FAILED;
      }

      // 判断是不是申请密钥 请求类型
      if (keyResp.getP_type() != ProtocolVar.PacketType.SCRETKEY_PRO) {
        Logger.getLogger(CameraTask.class.getName()).log(Level.INFO, "申请密钥数据包类型错误");
        return GlobalVar.FAILED;
      }
      PUID = keyResp.getPUID();
      MD5Key = keyResp.getSecretKey();
    } catch (IOException ex) {
      Logger.getLogger(CameraTask.class.getName())
          .log(Level.SEVERE, devSeqStr + "申请密钥操作失败  端口:" + port, ex);
      return GlobalVar.FAILED;
    }
    return GlobalVar.SUCCESSED;
  }