分类:
Java
2012-07-16 14:19
1228人阅读
收藏
举报
- package com.cvicse.naba.service.impl.user;
-
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- import com.cvicse.naba.common.utils.ByteConvert;
- import com.cvicse.naba.common.utils.ObjectUtils;
- import com.cvicse.naba.service.api.msg.IMessageManSrv;
- import com.cvicse.naba.service.api.user.IUserManageSrv;
- import com.cvicse.unicorn.framework.entity.CB_User;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class UserManageSrvImpl implements IUserManageSrv {
-
- private static final int STRUCTS_LENGTH = 316;
-
- private IMessageManSrv messageManSrv;
-
-
-
-
-
-
-
-
-
-
-
-
- @Override
- public String saveUser(CB_User user, Map<String, String> map) throws Exception {
-
- user.setC_x(20);
- user.setC_y(1);
-
-
- byte[] returnMsg = messageManSrv.exeCommand(user, getSaveUserMessage(map));
- String result = null;
- if (returnMsg != null) {
- if (handleResult(returnMsg)) {
- result = "true";
- } else {
- result = "false";
- }
- } else {
- result = "logout";
- }
-
- return result;
- }
-
-
-
-
-
-
-
-
-
-
-
- @Override
- public String updateUser(CB_User user, Map<String, String> map) throws Exception {
- user.setC_x(20);
- user.setC_y(4);
-
-
- byte[] returnMsg = messageManSrv.exeCommand(user, getUpdateUserMessage(map));
- String result = null;
- if (returnMsg != null) {
- if (handleResult(returnMsg)) {
- result = "true";
- } else {
- result = "false";
- }
- } else {
- result = "logout";
- }
-
- return result;
- }
-
-
-
-
-
-
-
-
- @Override
- public boolean updatePassword(CB_User user, Map<String, String> map) throws Exception {
-
- String passInfo = "";
- byte[] returnMsg = messageManSrv.exeCommand(user, passInfo);
-
- return handleResult(returnMsg);
- }
-
-
-
-
-
-
-
-
-
-
-
-
- @Override
- public String deleteUser(CB_User user, String str) throws Exception {
- user.setC_x(20);
- user.setC_y(3);
-
- byte[] returnMsg = messageManSrv.exeCommand(user, str);
- String result = null;
- if (returnMsg != null && handleResult(returnMsg)) {
- if (handleResult(returnMsg)) {
- result = "true";
- } else {
- result = "false";
- }
- } else {
- result = "logout";
- }
- return result;
- }
-
-
-
-
- @Override
- public Map<String, String> getUserMap(CB_User user, String userID) throws Exception {
-
- Map<String, String> map = null;
- byte[] returnMsg = messageManSrv.exeCommand(user, "");
-
- byte[] struct = new byte[returnMsg.length - 7];
- System.arraycopy(returnMsg, 7, struct, 0, returnMsg.length - 7);
- map = this.getUserInfoFromByte(struct);
- return map;
- }
-
-
-
-
-
-
-
- @Override
- public List<Map<String, String>> getUserListMap(CB_User user) throws Exception {
-
- user.setC_x(20);
- user.setC_y(2);
-
- List<Map<String, String>> list = null;
-
- byte[] returnMsg = messageManSrv.exeCommand(user, "");
- if (returnMsg != null) {
-
- if (handleResult(returnMsg)) {
-
- byte[] structs = new byte[returnMsg.length - 7];
-
- System.arraycopy(returnMsg, 7, structs, 0, returnMsg.length - 7);
-
- list = this.getUserInfoListFromByte(structs);
- }
- } else {
-
- list = new ArrayList<Map<String, String>>();
- Map<String, String> map = new HashMap<String, String>();
- map.put("logout", "logout");
- list.add(map);
- }
-
- return list;
- }
-
-
-
-
-
-
-
- private boolean handleResult(byte[] msg) {
- boolean flag = false;
- if (msg.length >= 7) {
-
- int result = msg[4];
- if (0 == result) {
- flag = true;
- }
- }
- return flag;
- }
-
-
-
-
-
-
- public void setMesageManSrv(IMessageManSrv messageManSrv) {
- this.messageManSrv = messageManSrv;
- }
-
-
-
-
-
-
- public void unSetMessageManSrv(IMessageManSrv messageManSrv) {
- this.messageManSrv = null;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- private byte[] getSaveUserMessage(Map<String, String> map) {
-
- byte[] msgByte = new byte[336];
-
- byte[] preSixMsg = new byte[6];
- preSixMsg[0] = 0;
- preSixMsg[1] = 0;
- preSixMsg[2] = 1;
-
- preSixMsg[3] = Byte.parseByte(ObjectUtils.toString(map.get("isUkey")));
- preSixMsg[4] = 0;
-
-
- preSixMsg[5] = Byte.parseByte(ObjectUtils.toString(map.get("state")));
- System.arraycopy(preSixMsg, 0, msgByte, 0, preSixMsg.length);
-
-
- byte[] completeZero = {0,0};
- System.arraycopy(completeZero, 0, msgByte, 6, completeZero.length);
-
- byte[] permissionByte = ByteConvert.intToLBytes(
- Integer.parseInt(ObjectUtils.toString(map.get("permission"))));
- System.arraycopy(permissionByte, 0, msgByte, 8, permissionByte.length);
-
-
- byte[] authtypeByte = {0, 0, 0, 0};
- System.arraycopy(authtypeByte, 0, msgByte, 12, authtypeByte.length);
-
-
- byte[] userid = new byte[16];
- userid = buildByteArray(ObjectUtils.toString(map.get("userId")).getBytes(), userid);
- System.arraycopy(userid, 0, msgByte, 16, userid.length);
-
-
- byte[] username = new byte[16];
- username = buildByteArray(ObjectUtils.toString(map.get("userName")).getBytes(), username);
- System.arraycopy(username, 0, msgByte, 32, username.length);
-
-
- byte[] passfirst = new byte[16];
- passfirst = buildByteArray(ObjectUtils.toString(map.get("password")).getBytes(), passfirst);
- System.arraycopy(passfirst, 0, msgByte, 48, passfirst.length);
-
-
- byte[] passsecond = new byte[16];
- passsecond = buildByteArray(ByteConvert.intToBytes(0), passsecond);
- System.arraycopy(passsecond, 0, msgByte, 64, passsecond.length);
-
-
- byte[] emailaddr = new byte[64];
- emailaddr = buildByteArray(ObjectUtils.toString(map.get("email")).getBytes(), emailaddr);
- System.arraycopy(emailaddr, 0, msgByte, 80, emailaddr.length);
-
-
- byte[] dept = new byte[32];
- dept = buildByteArray(ObjectUtils.toString(map.get("dept")).getBytes(), dept);
- System.arraycopy(dept, 0, msgByte, 144, dept.length);
-
-
- byte[] mobile = new byte[16];
- mobile = buildByteArray(ObjectUtils.toString(map.get("mobile")).getBytes(), mobile);
- System.arraycopy(mobile, 0, msgByte, 176, mobile.length);
-
-
- byte[] role = new byte[16];
- role = buildByteArray(ObjectUtils.toString(map.get("role")).getBytes(), role);
- System.arraycopy(role, 0, msgByte, 192, role.length);
-
-
- byte[] memo = new byte[128];
- memo = buildByteArray(ObjectUtils.toString(map.get("memo")).getBytes(), memo);
- System.arraycopy(memo, 0, msgByte, 208, memo.length);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- return msgByte;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- private byte[] getUpdateUserMessage(Map<String, String> map){
-
- byte[] msgByte = new byte[368];
-
- byte[] preTwo = new byte[2];
- preTwo[0] = 0;
- preTwo[1] = 0;
- System.arraycopy(preTwo, 0, msgByte, 0, preTwo.length);
-
-
- byte[] userid = new byte[16];
- userid = buildByteArray(ObjectUtils.toString(map.get("userId")).getBytes(), userid);
- System.arraycopy(userid, 0, msgByte, 2, userid.length);
-
-
- byte[] chpass = new byte[1];
- chpass[0] = Byte.parseByte(ObjectUtils.toString(map.get("chpass")));
- System.arraycopy(chpass, 0, msgByte, 18, chpass.length);
-
-
- byte[] oldPass = new byte[16];
-
- byte[] newPass = new byte[16];
-
- if ("1".equals(ObjectUtils.toString(map.get("chpass")))) {
- oldPass = buildByteArray(ObjectUtils.toString(map.get("oldPass")).getBytes(), oldPass);
- newPass = buildByteArray(ObjectUtils.toString(map.get("newPass")).getBytes(), newPass);
- }
- System.arraycopy(oldPass, 0, msgByte, 19, oldPass.length);
- System.arraycopy(newPass, 0, msgByte, 35, newPass.length);
-
-
- byte[] passflag = new byte[1];
- passflag[0] = Byte.parseByte(ObjectUtils.toString(map.get("isUkey")));
- System.arraycopy(passflag, 0, msgByte, 51, passflag.length);
-
-
- byte[] priority = {0};
- System.arraycopy(priority, 0, msgByte, 52, priority.length);
-
-
- byte[] state = new byte[1];
- state[0] = Byte.parseByte(ObjectUtils.toString(map.get("state")));
- System.arraycopy(state, 0, msgByte, 53, state.length);
-
-
- byte[] completeZero = {0, 0};
- System.arraycopy(completeZero, 0, msgByte, 54, completeZero.length);
-
-
- byte[] permissionByte = ByteConvert.intToLBytes(
- Integer.parseInt(ObjectUtils.toString(map.get("permission"))));
- System.arraycopy(permissionByte, 0, msgByte, 56, permissionByte.length);
-
- byte[] authtypeByte = {0, 0, 0, 0};
- System.arraycopy(authtypeByte, 0, msgByte, 60, authtypeByte.length);
-
-
- byte[] username = new byte[16];
- username = buildByteArray(ObjectUtils.toString(map.get("userName")).getBytes(), username);
- System.arraycopy(username, 0, msgByte, 64, username.length);
-
-
- byte[] passfirst = new byte[16];
- passfirst = buildByteArray(ByteConvert.intToBytes(0), passfirst);
- System.arraycopy(passfirst, 0, msgByte, 80, passfirst.length);
-
-
- byte[] passsecond = new byte[16];
- passsecond = buildByteArray(ByteConvert.intToBytes(0), passsecond);
- System.arraycopy(passsecond, 0, msgByte, 96, passsecond.length);
-
-
- byte[] emailaddr = new byte[64];
- emailaddr = buildByteArray(ObjectUtils.toString(map.get("email")).getBytes(), emailaddr);
- System.arraycopy(emailaddr, 0, msgByte, 112, emailaddr.length);
-
-
- byte[] dept = new byte[32];
- dept = buildByteArray(ObjectUtils.toString(map.get("dept")).getBytes(), dept);
- System.arraycopy(dept, 0, msgByte, 176, dept.length);
-
-
- byte[] mobile = new byte[16];
- mobile = buildByteArray(ObjectUtils.toString(map.get("mobile")).getBytes(), mobile);
- System.arraycopy(mobile, 0, msgByte, 208, mobile.length);
-
-
- byte[] role = new byte[16];
- role = buildByteArray(ObjectUtils.toString(map.get("role")).getBytes(), role);
- System.arraycopy(role, 0, msgByte, 224, role.length);
-
-
- byte[] memo = new byte[128];
- memo = buildByteArray(ObjectUtils.toString(map.get("memo")).getBytes(), memo);
- System.arraycopy(memo, 0, msgByte, 240, memo.length);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- return msgByte;
- }
-
-
-
-
-
-
-
-
-
- private byte[] buildByteArray(byte[] srcArry, byte[] destArry) {
- int length = srcArry.length;
- if (srcArry.length > destArry.length) {
- length = destArry.length;
- }
- System.arraycopy(srcArry, 0, destArry, 0, length);
- return destArry;
- }
-
-
-
-
-
-
-
-
-
- private List<Map<String, String>> getUserInfoListFromByte(byte[] structs) throws Exception {
- List<Map<String, String>> list = new ArrayList<Map<String, String>>();
-
- for (int i = 0; i < structs.length / STRUCTS_LENGTH; i++) {
-
- byte[] struct = new byte[STRUCTS_LENGTH];
-
- int srcPosition = i * STRUCTS_LENGTH;
- System.arraycopy(structs, srcPosition, struct, 0, STRUCTS_LENGTH);
-
- Map<String, String> temp = getUserInfoFromByte(struct);
- list.add(temp);
- }
- return list;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- private Map<String, String> getUserInfoFromByte(byte[] struct) throws Exception {
- Map<String, String> map = new HashMap<String, String>();
- map.put("isUkey", String.valueOf(struct[0]));
- map.put("state", String.valueOf(struct[2]));
- map.put("userId", getFieldValue(struct, 5, 16, "string"));
- map.put("userName", getFieldValue(struct, 21, 16, "string"));
- map.put("email", getFieldValue(struct, 37, 64, "string"));
- map.put("dept", getFieldValue(struct, 101, 32, "string"));
- map.put("mobile", getFieldValue(struct, 133, 16, "string"));
- map.put("role", getFieldValue(struct, 149, 16, "string"));
- map.put("permission", getFieldValue(struct, 168, 4, "integer"));
- map.put("createtime", getFieldValue(struct, 172, 4, "time"));
- map.put("lasttime", getFieldValue(struct, 176, 4, "time"));
- map.put("logintimes", getFieldValue(struct, 180, 4, "integer"));
- map.put("failtimes", getFieldValue(struct, 184, 4, "integer"));
- map.put("memo", getFieldValue(struct, 188, 128, "string"));
- return map;
- }
-
-
-
-
-
-
-
-
-
- private String getFieldValue(byte[] bty, int srcPosition, int length, String converFlag) throws Exception {
-
- byte[] temp = new byte[length];
- String returnStr = "";
- System.arraycopy(bty, srcPosition, temp, 0, length);
- if ("".equals(converFlag) || "string".equals(converFlag)) {
-
- int position = 0;
-
-
-
-
- for (int i = temp.length - 1; i >= 0; i--) {
- position = i;
- if (temp[i] != 0) {
- break;
- }
- }
-
- byte[] validValue = new byte[position + 1];
- System.arraycopy(temp, 0, validValue, 0, position + 1);
- returnStr = new String(validValue);
- } else if ("time".equals(converFlag)) {
- long time = ByteConvert.lBytesToInt(temp);
- returnStr = ObjectUtils.get24Time(time*1000);
-
-
-
- } else if ("integer".equals(converFlag)) {
- returnStr = String.valueOf(ByteConvert.lBytesToInt(temp));
- }
-
- return returnStr;
- }
-
- }
PS:
1、C中int类型的数据在内存中存放和int类型占用的字节数有关,因此有些地方需要补零操作。例如,C在系统中占用4个字节,int类型的数据
在内存中存放开始字节是被4整除的地址,通过socket传递过来的字节数组在相应的位置会进行补零(C端进行操作),在Java中解析字节时或者构造相
应的结构体字节数组时都要对补零的地方进行操作,去掉补充的零占位字节或者添加相应的补零操作,然后进行传递。
2、在C中的字符串会有结束标识"\0",从Java端传递的数据不要占满整个字节数组,例如:unsigned char emailaddr[64],java字节数组如果填充满了该字符数组,C进行构造该字节数组时会舍去一部分,会造成乱码。
|