mirror of
https://gitee.com/willfree/minoaClientWindows_gradle.git
synced 2026-10-05 01:15:44 +08:00
873 lines
31 KiB
Java
873 lines
31 KiB
Java
package cn.minoa.dataRequestInterface;
|
|
// 与服务器进行数据交互
|
|
|
|
import java.io.IOException;
|
|
import java.io.RandomAccessFile;
|
|
|
|
import org.json.JSONArray;
|
|
import org.json.JSONException;
|
|
import org.json.JSONObject;
|
|
|
|
import cn.minoa.MainApp;
|
|
import cn.minoa.model.SingleMessage;
|
|
import net.named_data.jndn.Data;
|
|
import net.named_data.jndn.Face;
|
|
import net.named_data.jndn.Interest;
|
|
import net.named_data.jndn.Name;
|
|
import net.named_data.jndn.OnData;
|
|
import net.named_data.jndn.encoding.EncodingException;
|
|
import net.named_data.jndn.security.KeyChain;
|
|
import net.named_data.jndn.security.SecurityException;
|
|
import net.named_data.jndn.security.pib.PibIdentity;
|
|
import net.named_data.jndn.security.pib.PibImpl.Error;
|
|
import net.named_data.jndn.security.pib.PibSqlite3;
|
|
import net.named_data.jndn.security.tpm.TpmBackEndMemory;
|
|
import net.named_data.jndn.util.Blob;
|
|
|
|
public class MinoaDataAPI {
|
|
static private String keyChainNameString = "/wefree";
|
|
static private String minoaServerIpString = "121.15.171.89";
|
|
// static private String pidDbPathString = "E:\\MyCode\\newEclipseWorkspace\\minoa_client_windows_JavaFX11";
|
|
static private String pidDbPathString = "";
|
|
static private String globalPrefixString = "/ndn/edu/pkusz/OA";
|
|
|
|
KeyChain keyChain;
|
|
Face face;
|
|
public DataCacheQueue dataCacheQueue;
|
|
private MainApp mainApp;
|
|
|
|
public MinoaDataAPI(MainApp mainApp) {
|
|
// TODO Auto-generated constructor stub
|
|
this.face = new Face(minoaServerIpString);
|
|
this.dataCacheQueue = new DataCacheQueue();
|
|
this.mainApp = mainApp;
|
|
runFace();
|
|
try {
|
|
// 密钥“黑盒”
|
|
PibSqlite3 pibSqlite3 = new PibSqlite3(pidDbPathString);
|
|
TpmBackEndMemory tpmBackEndMemory = new TpmBackEndMemory();
|
|
keyChain = new KeyChain(pibSqlite3, tpmBackEndMemory);
|
|
PibIdentity identity = keyChain.getPib().getIdentity(new Name(keyChainNameString));
|
|
keyChain.setDefaultIdentity(identity);
|
|
|
|
System.out.println(keyChain.getDefaultCertificateName().toUri());
|
|
} catch (Exception e) {
|
|
// TODO: handle exception
|
|
System.out.println("exception: " + e.getMessage());
|
|
}
|
|
}
|
|
|
|
// 开辟线程运行face
|
|
private void runFace() {
|
|
Thread thread = new Thread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
// TODO Auto-generated method stub
|
|
try {
|
|
while (true) {
|
|
face.processEvents();
|
|
// 线程暂时休眠
|
|
try {
|
|
Thread.sleep(50);
|
|
} catch (InterruptedException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
} catch (IOException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
} catch (EncodingException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
});
|
|
thread.start();
|
|
}
|
|
|
|
// 开辟线程监听新消息/新通知/新审批
|
|
public void bindNews() {
|
|
Thread thread = new Thread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
// TODO Auto-generated method stub
|
|
while (true) {
|
|
// 监听是否有新消息等,有,则将其加载到数据内存
|
|
bindNewsProdution();
|
|
// 线程暂时休眠
|
|
try {
|
|
Thread.sleep(500);
|
|
} catch (InterruptedException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
});
|
|
thread.start();
|
|
}
|
|
|
|
// 监听函数
|
|
public void bindNewsProdution() {
|
|
// 发送数据请求
|
|
Long seqLong = mainApp.getNewDataReqId();
|
|
OrderInfo orderInfo = new OrderInfo();
|
|
orderInfo.setSeq(seqLong);
|
|
orderInfo.setJsonString(getGetNewsJson());
|
|
mainApp.minoaDataAPI.executeOrder("/getNews", orderInfo);
|
|
// 获取回复消息
|
|
ResponseData responseData = mainApp.minoaDataAPI.dataCacheQueue.getResponseDataBySeq(seqLong);
|
|
// System.out.println("getNews-responseData: "+responseData.getResponseJsonDataString());
|
|
JSONObject jsonObject = responseData.praseRequestData();
|
|
Integer codeInteger = null;
|
|
try {
|
|
codeInteger = jsonObject.getInt("code");
|
|
} catch (JSONException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
if (codeInteger == 200) {
|
|
String dataString;
|
|
try {
|
|
dataString = jsonObject.getString("data");
|
|
if (!dataString.equals("")) {
|
|
JSONArray dataJsonArray = new JSONArray(dataString);
|
|
String newsCacheString;
|
|
JSONObject newsCacheJsonObject;
|
|
Integer typeCodeInteger;
|
|
String contentString;
|
|
// 遍历每个新消息或者通知或者审批 将其更新到前端内存数据中
|
|
for (int i = 0; i < dataJsonArray.length(); i++) {
|
|
newsCacheString = dataJsonArray.getString(i);
|
|
newsCacheJsonObject = new JSONObject(newsCacheString);
|
|
typeCodeInteger = newsCacheJsonObject.getInt("typeCode");
|
|
contentString = newsCacheJsonObject.getString("content");
|
|
if (typeCodeInteger == 0) {
|
|
// 处理新消息
|
|
handleNewSingleMessage(contentString);
|
|
} else if (typeCodeInteger == 1) {
|
|
// 处理新通知
|
|
} else if (typeCodeInteger == 2) {
|
|
// 处理新审批
|
|
}
|
|
}
|
|
}
|
|
} catch (JSONException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
// 获取是否有新消息产生的json字符串
|
|
private String getGetNewsJson() {
|
|
JSONObject jsonObject = new JSONObject();
|
|
String commandString = "/getNews";
|
|
String userNameString = mainApp.loginer.getUserName();
|
|
String uuidString = mainApp.loginer.getUuid();
|
|
try {
|
|
jsonObject.put("command", commandString);
|
|
jsonObject.put("username", userNameString);
|
|
jsonObject.put("uuid", uuidString);
|
|
} catch (Exception e) {
|
|
// TODO: handle exception
|
|
System.out.println("exception: " + e.getMessage());
|
|
}
|
|
return jsonObject.toString();
|
|
}
|
|
|
|
// 处理一条接收到的新消息
|
|
private void handleNewSingleMessage(String singleMessageString) {
|
|
try {
|
|
JSONObject singleMessageJsonObject = new JSONObject(singleMessageString);
|
|
String fromString;
|
|
JSONObject fromJsonObject;
|
|
String fromNameString;
|
|
Integer idInteger;
|
|
String msgString;
|
|
Long timeLong;
|
|
// 解析发送方姓名
|
|
fromString = singleMessageJsonObject.getString("from");
|
|
fromJsonObject = new JSONObject(fromString);
|
|
fromNameString = fromJsonObject.getString("username");
|
|
// 解析id
|
|
idInteger = singleMessageJsonObject.getInt("id");
|
|
// 解析内容
|
|
msgString = singleMessageJsonObject.getString("msg");
|
|
// 解析时间
|
|
timeLong = singleMessageJsonObject.getLong("time");
|
|
// 构建一条消息对象
|
|
SingleMessage singleMessage = new SingleMessage();
|
|
singleMessage.setFromName(fromNameString);
|
|
singleMessage.setMsg(msgString);
|
|
singleMessage.setTime(timeLong);
|
|
singleMessage.setToName(mainApp.loginer.getUserName());
|
|
singleMessage.setMsgId(idInteger);
|
|
// 加入前端数据内存
|
|
// System.out.println("MinoaDataAPI-addNewMessage-singleMessageString: "+singleMessageString);
|
|
// System.out.println("MinoaDataAPI-addNewMessage-fromName: "+fromNameString);
|
|
// mainApp.messageLogMap.get(fromNameString).add(singleMessage);
|
|
// //加入未读消息的列表中
|
|
// mainApp.notReadMessageList.addNotReadMessage(singleMessage);
|
|
// // 产生消息气泡
|
|
// mainApp.newMessageBubble(singleMessage);
|
|
// 将这条新消息交给mainApp处理
|
|
mainApp.handleGetNewMessage(singleMessage);
|
|
System.out.println("MinoaDataAPI-handleGetNewMessage success: " + singleMessageString);
|
|
} catch (JSONException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
// //判断是否有新消息产生=>废了,换接口了
|
|
// private boolean judgeNewMessageProdution() {
|
|
// //发送数据请求
|
|
// Long seqLong=mainApp.getNewDataReqId();
|
|
// OrderInfo orderInfo=new OrderInfo();
|
|
// orderInfo.setSeq(seqLong);
|
|
// orderInfo.setJsonString(getHasNewsJson());
|
|
// mainApp.minoaDataAPI.executeOrder("/hasNews", orderInfo);
|
|
// //获取回复消息
|
|
// ResponseData responseData=mainApp.minoaDataAPI.dataCacheQueue.getResponseDataBySeq(seqLong);
|
|
// JSONObject jsonObject=responseData.praseRequestData();
|
|
//// System.out.println("hasNews-JSON: "+jsonObject.toString());
|
|
// Integer codeInteger = null;
|
|
// try {
|
|
// codeInteger = jsonObject.getInt("code");
|
|
// } catch (JSONException e) {
|
|
// // TODO Auto-generated catch block
|
|
// e.printStackTrace();
|
|
// }
|
|
// if(codeInteger==200) {
|
|
// String dataString;
|
|
// try {
|
|
// dataString = jsonObject.getString("data");
|
|
// JSONObject dataJSONObject=new JSONObject(dataString);
|
|
// Integer msgInteger=dataJSONObject.getInt("msg");
|
|
// if(msgInteger>0) {
|
|
// System.out.println(msgInteger);
|
|
// return true;
|
|
// }
|
|
// } catch (JSONException e) {
|
|
// // TODO Auto-generated catch block
|
|
// e.printStackTrace();
|
|
// }
|
|
// }
|
|
// return false;
|
|
// }
|
|
//
|
|
// //获取是否有新消息产生的json字符串
|
|
// private String getHasNewsJson() {
|
|
// JSONObject jsonObject =new JSONObject();
|
|
// String commandString="/hasNews";
|
|
// String userNameString=mainApp.loginer.getUserName();
|
|
// String uuidString=mainApp.loginer.getUuid();
|
|
// try {
|
|
// jsonObject.put("command", commandString);
|
|
// jsonObject.put("username", userNameString);
|
|
// jsonObject.put("uuid", uuidString);
|
|
// } catch (Exception e) {
|
|
// // TODO: handle exception
|
|
// System.out.println("exception: " + e.getMessage());
|
|
// }
|
|
// return jsonObject.toString();
|
|
// }
|
|
|
|
// 获取注册请求字符串
|
|
static private String getRegisterJson() {
|
|
JSONObject jsonObject = new JSONObject();
|
|
String mPubKey = "Bv0CsQcuCAR3Z2gwCANLRVkICH9+4yjVUHZaCAxjZXJ0LXJlcXVlc3QICf0AAAFv\nEUSBehQJGAECGQQANu6AFf0BJjCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC\nggEBANbFutCAZqFjHEa16tYvy4lBgi9/zixKWQzE9I72vaG2Imym+6KR30qkAQOK\nh0uE5RD2YsGhRK99rde+JOqIfk2/sunhp4nctOS5f4DeuOty58ekrO+Co9ZFmNBI\nmyk5i/hakjBEd/vdD7b1VadDRep5mWs3rgCinZOUfVaZ4oKqSws7ZWMvOSlRMOOi\nVzj53P4poY109S6aWmi3xKw/D76vfJapjdOB9Dg7S3rz0Rve1F/ajHCXWLt4XU3k\nUCiBn3Qmt6UD/MyDBaaUCfOvHTN6yVPtN6aOtYJVBAWt/P1ahLYhWkSm6KvrwMt0\nRzy2lw/lPriT99REItc/0wK6XwkCAwEAARZGGwEBHBcHFQgEd2doMAgDS0VZCAh/\nfuMo1VB2Wv0A/Sb9AP4PMjAxOTEyMTdUMDAzMTQw/QD/DzIwMTkxMjI3VDAwMzEz\nORf9AQCM+8WROXfi8WD9DydfLuSUxV0v3KW/Gn422/puDrkwwi1R2ogsAUc2O8pp\npuMLjWrQOTgE6ZTg80uWoDSUjjk44aMjkL2SAfp62vHRbGFx7+dckHAtF+DS6NH7\nX2vojFHOg6fksOE1M7XqLHmEEctZrFVSnQO7nB3DfRg+BqzZC/iy+8YAvTIvqA1c\nNsKYkmNITB80aM1v2H7fKnV4ZJpZnW71G5vbaPUseeklalQ2vEuBgWdd/1PL/0Ml\nI52npzqoeKAEpLp+vBwGN5ryGtWKB12nNimpMfevQAn/9BVIg59lZ7DIlE0sWmgW\nnwDxIXKebdViOejHlKwNAykxRRWw";
|
|
String commandString = "/register";
|
|
String userNameString = "oa_wefree";
|
|
String passWordString = "12345678";
|
|
String phoneString = "10000000006";
|
|
Integer levelInteger = 0;
|
|
try {
|
|
jsonObject.put("command", commandString);
|
|
jsonObject.put("username", userNameString);
|
|
jsonObject.put("password", passWordString);
|
|
jsonObject.put("phone", phoneString);
|
|
jsonObject.put("publicKey", mPubKey);
|
|
jsonObject.put("level", levelInteger);
|
|
} catch (Exception e) {
|
|
System.out.println("exception: " + e.getMessage());
|
|
}
|
|
return jsonObject.toString();
|
|
}
|
|
|
|
// //"获取最近三天未读实时消息"
|
|
// static private String getGetRTMsgJson() {
|
|
// JSONObject jsonObject =new JSONObject();
|
|
// String commandString="/getRTMsg";
|
|
// String userNameString="oa_wefree";
|
|
// String uuidString="29c8f0a4-52d7-4478-90f0-6136996dbe96";
|
|
// try {
|
|
// jsonObject.put("command", commandString);
|
|
// jsonObject.put("username", userNameString);
|
|
// jsonObject.put("uuid", uuidString);
|
|
// } catch (Exception e) {
|
|
// // TODO: handle exception
|
|
// System.out.println("exception: " + e.getMessage());
|
|
// }
|
|
// return jsonObject.toString();
|
|
// }
|
|
|
|
// 根据命令前缀获取相应的请求字符串【JSON】
|
|
static private String getRequestStr(String orderPrefix, OrderInfo orderInfo) {
|
|
String requestStr;
|
|
switch (orderPrefix) {
|
|
case "/register":
|
|
requestStr = getRegisterJson();
|
|
break;
|
|
case "/login":
|
|
requestStr = orderInfo.getJsonString();
|
|
break;
|
|
case "/getUsers":
|
|
requestStr = orderInfo.getJsonString();
|
|
break;
|
|
case "/sendRTMsg":
|
|
requestStr = orderInfo.getJsonString();
|
|
break;
|
|
case "/getRTMsg":
|
|
requestStr = orderInfo.getJsonString();
|
|
break;
|
|
case "/getRTMsgLog":
|
|
requestStr = orderInfo.getJsonString();
|
|
break;
|
|
case "/getInform":
|
|
requestStr = orderInfo.getJsonString();
|
|
break;
|
|
case "/hasNews":
|
|
requestStr = orderInfo.getJsonString();
|
|
break;
|
|
case "/getNews":
|
|
requestStr = orderInfo.getJsonString();
|
|
break;
|
|
case "/fileAction":
|
|
requestStr = orderInfo.getJsonString();
|
|
break;
|
|
case "/fileInfo":
|
|
requestStr = orderInfo.getJsonString();
|
|
break;
|
|
case "/downloadFile":
|
|
requestStr = orderInfo.getJsonString();
|
|
break;
|
|
case "/uploadFile":
|
|
requestStr = orderInfo.getJsonString();
|
|
break;
|
|
case "/sendApproval":
|
|
requestStr = orderInfo.getJsonString();
|
|
break;
|
|
case "/getApproval":
|
|
requestStr = orderInfo.getJsonString();
|
|
break;
|
|
case "/getDataSlice":
|
|
requestStr = orderInfo.getJsonString();
|
|
break;
|
|
default:
|
|
requestStr = "*wrong command string*";
|
|
break;
|
|
}
|
|
return requestStr;
|
|
}
|
|
|
|
// 执行文件下载的特殊数据请求语句
|
|
// 执行数据请求语句
|
|
public boolean executeOrder(String orderPrefix, OrderInfo orderInfo, String localDir, String localName) {
|
|
// 获取json格式的数据请求字符串
|
|
String requestStr = getRequestStr(orderPrefix, orderInfo);
|
|
// System.out.println("requestStr: "+requestStr);
|
|
// 命令前缀格式不对的情况下返回false
|
|
if (requestStr.equals("*wrong command string*")) {
|
|
System.out.println(requestStr);
|
|
return false;
|
|
}
|
|
|
|
// 发送数据兴趣包
|
|
Name name = new Name(globalPrefixString + orderPrefix + "/" + orderInfo.getSeq());
|
|
Interest interest = new Interest(name);
|
|
interest.setMustBeFresh(true);
|
|
try {
|
|
// 文件下载
|
|
interest.setApplicationParameters(new Blob(requestStr));
|
|
// 密钥“黑盒”
|
|
keyChain.sign(interest);
|
|
this.expressTheDownloadInterest(orderPrefix, interest, orderInfo,localDir,localName);
|
|
return true;
|
|
} catch (Exception e) {
|
|
System.out.println("exception: " + e.getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// 执行数据请求语句
|
|
public boolean executeOrder(String orderPrefix, OrderInfo orderInfo,String serverpath) {
|
|
// 获取json格式的数据请求字符串
|
|
String requestStr = getRequestStr(orderPrefix, orderInfo);
|
|
// System.out.println("requestStr: "+requestStr);
|
|
// 命令前缀格式不对的情况下返回false
|
|
if (requestStr.equals("*wrong command string*")) {
|
|
System.out.println(requestStr);
|
|
return false;
|
|
}
|
|
|
|
// 发送数据兴趣包
|
|
Name name = new Name(globalPrefixString + orderPrefix + "/" + orderInfo.getSeq());
|
|
Interest interest = new Interest(name);
|
|
interest.setMustBeFresh(true);
|
|
// System.out.println("interest name: "+name.toString());
|
|
try {
|
|
if (orderPrefix.equals("/downloadFile")) {
|
|
// 文件下载
|
|
interest.setApplicationParameters(new Blob(requestStr));
|
|
// 密钥“黑盒”
|
|
keyChain.sign(interest);
|
|
this.expressTheDownloadInterest(orderPrefix, interest, orderInfo);
|
|
} else if (orderPrefix.equals("/uploadFile")) {
|
|
// 文件上传[interest和keyChain的参数设置在函数体内]
|
|
this.expressTheUploadInterest(orderPrefix, interest, orderInfo,serverpath);
|
|
}else {
|
|
interest.setApplicationParameters(new Blob(requestStr));
|
|
// System.out.println("setApplicationParameters-blob: "+requestStr);
|
|
// 密钥“黑盒”
|
|
keyChain.sign(interest);
|
|
// System.out.println(interest.toUri());
|
|
this.expressTheInterest(orderPrefix, interest, orderInfo.getSeq());
|
|
}
|
|
|
|
return true;
|
|
} catch (Exception e) {
|
|
System.out.println("exception: " + e.getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// 执行数据请求语句
|
|
public boolean executeOrder(String orderPrefix, OrderInfo orderInfo) {
|
|
// 获取json格式的数据请求字符串
|
|
String requestStr = getRequestStr(orderPrefix, orderInfo);
|
|
// System.out.println("requestStr: "+requestStr);
|
|
// 命令前缀格式不对的情况下返回false
|
|
if (requestStr.equals("*wrong command string*")) {
|
|
System.out.println(requestStr);
|
|
return false;
|
|
}
|
|
|
|
// 发送数据兴趣包
|
|
Name name = new Name(globalPrefixString + orderPrefix + "/" + orderInfo.getSeq());
|
|
Interest interest = new Interest(name);
|
|
interest.setMustBeFresh(true);
|
|
// System.out.println("interest name: "+name.toString());
|
|
try {
|
|
if (orderPrefix.equals("/downloadFile")) {
|
|
// 文件下载
|
|
interest.setApplicationParameters(new Blob(requestStr));
|
|
// 密钥“黑盒”
|
|
keyChain.sign(interest);
|
|
this.expressTheDownloadInterest(orderPrefix, interest, orderInfo);
|
|
} else if (orderPrefix.equals("/uploadFile")) {
|
|
// 文件上传[interest和keyChain的参数设置在函数体内]
|
|
this.expressTheUploadInterest(orderPrefix, interest, orderInfo);
|
|
}else {
|
|
interest.setApplicationParameters(new Blob(requestStr));
|
|
// System.out.println("setApplicationParameters-blob: "+requestStr);
|
|
// 密钥“黑盒”
|
|
keyChain.sign(interest);
|
|
// System.out.println(interest.toUri());
|
|
this.expressTheInterest(orderPrefix, interest, orderInfo.getSeq());
|
|
}
|
|
|
|
return true;
|
|
} catch (Exception e) {
|
|
System.out.println("exception: " + e.getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
//文件上传到指定服务器路径下
|
|
private void expressTheUploadInterest(String orderPrefix, Interest interest, OrderInfo orderInfo,String serverpath) {
|
|
// 设置回执前缀
|
|
ResponseData responseData = new ResponseData();
|
|
responseData.setPrefix(orderPrefix);
|
|
byte[] requestByte = null; // 请求数据
|
|
// 重写ondata
|
|
try {
|
|
// 解析并修改文件名【window转server】
|
|
String localFilePath = "";
|
|
String serverFilePath = "";
|
|
Integer offsetInteger = 0;
|
|
try {
|
|
// 解析
|
|
JSONObject jsonObject = new JSONObject(orderInfo.getJsonString());
|
|
offsetInteger = jsonObject.getInt("offset");
|
|
localFilePath = jsonObject.getString("filename");
|
|
serverFilePath = serverpath + getPureFileNameFromWindows(localFilePath);
|
|
// 修改
|
|
jsonObject.remove("filename");
|
|
jsonObject.put("filename", serverFilePath);
|
|
orderInfo.setJsonString(jsonObject.toString());
|
|
System.out.println("request head str: " + orderInfo.getJsonString());
|
|
} catch (JSONException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
// 从本地读取数据
|
|
byte[] content = new byte[10000]; // 文件分片内容
|
|
try {
|
|
RandomAccessFile randomAccessFile = new RandomAccessFile(localFilePath, "rw");
|
|
// 设置文件的读取起点
|
|
randomAccessFile.seek(offsetInteger);
|
|
// 尝试读取数据
|
|
int readLen = randomAccessFile.read(content, 0, 7000);
|
|
randomAccessFile.close();
|
|
System.out.println("file slice readLen == " + readLen);
|
|
int jsonLen = orderInfo.getJsonString().length();
|
|
requestByte = new byte[jsonLen + 1 + readLen];
|
|
System.arraycopy(orderInfo.getJsonString().getBytes(), 0, requestByte, 0, jsonLen);
|
|
requestByte[jsonLen] = 0;
|
|
System.arraycopy(content, 0, requestByte, jsonLen + 1, readLen);
|
|
} catch (IOException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
// 赋值兴趣包的附带信息,并标志区块链
|
|
System.out.println("requestByte-len: " + requestByte.length);
|
|
System.out.println("requestByte: " + requestByte);
|
|
interest.setApplicationParameters(new Blob(requestByte));
|
|
// 密钥“黑盒”
|
|
try {
|
|
keyChain.sign(interest);
|
|
} catch (Error e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
} catch (KeyChain.Error e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
} catch (net.named_data.jndn.security.tpm.TpmBackEnd.Error e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
} catch (SecurityException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
// 使用匿名内部类,发送数据请求兴趣包
|
|
face.expressInterest(interest, new OnData() {
|
|
@Override
|
|
public void onData(Interest interest, Data data) {
|
|
// 返回成功写入或失败写入消息
|
|
responseData.setResponseData(data.getContent().toString());
|
|
dataCacheQueue.setResponseData(orderInfo.getSeq(), responseData);
|
|
}
|
|
});
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
//文件上传
|
|
private void expressTheUploadInterest(String orderPrefix, Interest interest, OrderInfo orderInfo) {
|
|
// 设置回执前缀
|
|
ResponseData responseData = new ResponseData();
|
|
responseData.setPrefix(orderPrefix);
|
|
byte[] requestByte = null; // 请求数据
|
|
// 重写ondata
|
|
try {
|
|
// 解析并修改文件名【window转server】
|
|
String localFilePath = "";
|
|
String serverFilePath = "";
|
|
Integer offsetInteger = 0;
|
|
try {
|
|
// 解析
|
|
JSONObject jsonObject = new JSONObject(orderInfo.getJsonString());
|
|
offsetInteger = jsonObject.getInt("offset");
|
|
localFilePath = jsonObject.getString("filename");
|
|
serverFilePath = mainApp.fileServerBinPath + getPureFileNameFromWindows(localFilePath);
|
|
// 修改
|
|
jsonObject.remove("filename");
|
|
jsonObject.put("filename", serverFilePath);
|
|
orderInfo.setJsonString(jsonObject.toString());
|
|
System.out.println("request head str: " + orderInfo.getJsonString());
|
|
} catch (JSONException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
// 从本地读取数据
|
|
byte[] content = new byte[10000]; // 文件分片内容
|
|
try {
|
|
RandomAccessFile randomAccessFile = new RandomAccessFile(localFilePath, "rw");
|
|
// 设置文件的读取起点
|
|
randomAccessFile.seek(offsetInteger);
|
|
// 尝试读取数据
|
|
int readLen = randomAccessFile.read(content, 0, 7000);
|
|
randomAccessFile.close();
|
|
System.out.println("file slice readLen == " + readLen);
|
|
int jsonLen = orderInfo.getJsonString().length();
|
|
requestByte = new byte[jsonLen + 1 + readLen];
|
|
System.arraycopy(orderInfo.getJsonString().getBytes(), 0, requestByte, 0, jsonLen);
|
|
requestByte[jsonLen] = 0;
|
|
System.arraycopy(content, 0, requestByte, jsonLen + 1, readLen);
|
|
} catch (IOException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
// 赋值兴趣包的附带信息,并标志区块链
|
|
System.out.println("requestByte-len: " + requestByte.length);
|
|
System.out.println("requestByte: " + requestByte);
|
|
interest.setApplicationParameters(new Blob(requestByte));
|
|
// 密钥“黑盒”
|
|
try {
|
|
keyChain.sign(interest);
|
|
} catch (Error e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
} catch (KeyChain.Error e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
} catch (net.named_data.jndn.security.tpm.TpmBackEnd.Error e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
} catch (SecurityException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
// 使用匿名内部类,发送数据请求兴趣包
|
|
face.expressInterest(interest, new OnData() {
|
|
@Override
|
|
public void onData(Interest interest, Data data) {
|
|
// 返回成功写入或失败写入消息
|
|
responseData.setResponseData(data.getContent().toString());
|
|
dataCacheQueue.setResponseData(orderInfo.getSeq(), responseData);
|
|
}
|
|
});
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
// // 合并两个Byte[]
|
|
// public byte[] byteMerger(byte[] bt1, byte[] bt2) {
|
|
// byte[] bt3 = new byte[bt1.length + bt2.length];
|
|
// System.arraycopy(bt1, 0, bt3, 0, bt1.length);
|
|
// System.arraycopy(bt2, 0, bt3, bt1.length, bt2.length);
|
|
// return bt3;
|
|
// }
|
|
|
|
//下载文件的数据分片到指定路径,并将文件重命名为指定文件名
|
|
private void expressTheDownloadInterest(String orderPrefix, Interest interest, OrderInfo orderInfo,String localDir,String localName) {
|
|
// 设置回执前缀
|
|
ResponseData responseData = new ResponseData();
|
|
responseData.setPrefix(orderPrefix);
|
|
try {
|
|
// 使用匿名内部类,发送数据请求兴趣包
|
|
face.expressInterest(interest, new OnData() {
|
|
@Override
|
|
public void onData(Interest interest, Data data) {
|
|
// 解析文件名、分片编号
|
|
String filePathString = localDir;
|
|
Integer sliceSizeInteger = mainApp.sliceSize;
|
|
String filenameString = localName;
|
|
Integer sliceNoInteger = null;
|
|
try {
|
|
JSONObject jsonObject = new JSONObject(orderInfo.getJsonString());
|
|
sliceNoInteger = jsonObject.getInt("sliceNo");
|
|
} catch (JSONException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
// 写入本地文件
|
|
Integer successInfoInteger = 1;
|
|
try {
|
|
System.out.println("开始写入文件数据分片... :" + filePathString + filenameString);
|
|
RandomAccessFile randomAccessFile = new RandomAccessFile(filePathString + filenameString, "rw");
|
|
int contentSize = data.getContent().size();
|
|
System.out.println("该数据分片长度为:" + contentSize);
|
|
byte[] content = new byte[contentSize];
|
|
for (int i = 0; i < contentSize; i++) {
|
|
content[i] = data.getContent().buf().get(i);
|
|
}
|
|
// 设置数据分片写入到文件的起点位置
|
|
randomAccessFile.seek(sliceNoInteger * sliceSizeInteger);
|
|
// 写入数据分片
|
|
randomAccessFile.write(content, 0, contentSize);
|
|
randomAccessFile.close();
|
|
System.out.println("写入文件数据分片完毕:" + filePathString + filenameString);
|
|
successInfoInteger = 0;
|
|
} catch (IOException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
// 返回成功写入或失败写入消息
|
|
responseData.setResponseData(successInfoInteger.toString());
|
|
dataCacheQueue.setResponseData(orderInfo.getSeq(), responseData);
|
|
}
|
|
});
|
|
} catch (IOException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
//下载文件的数据分片到默认路径
|
|
private void expressTheDownloadInterest(String orderPrefix, Interest interest, OrderInfo orderInfo) {
|
|
// 设置回执前缀
|
|
ResponseData responseData = new ResponseData();
|
|
responseData.setPrefix(orderPrefix);
|
|
try {
|
|
// 使用匿名内部类,发送数据请求兴趣包
|
|
face.expressInterest(interest, new OnData() {
|
|
@Override
|
|
public void onData(Interest interest, Data data) {
|
|
// 解析文件名、分片编号
|
|
String filePathString = mainApp.fileLocalPath;
|
|
Integer sliceSizeInteger = mainApp.sliceSize;
|
|
String filenameString = null;
|
|
Integer sliceNoInteger = null;
|
|
try {
|
|
JSONObject jsonObject = new JSONObject(orderInfo.getJsonString());
|
|
filenameString = getPureFileName(jsonObject.getString("filename"));
|
|
sliceNoInteger = jsonObject.getInt("sliceNo");
|
|
} catch (JSONException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
// 写入本地文件
|
|
Integer successInfoInteger = 1;
|
|
try {
|
|
System.out.println("开始写入文件数据分片... :" + filePathString + filenameString);
|
|
RandomAccessFile randomAccessFile = new RandomAccessFile(filePathString + filenameString, "rw");
|
|
int contentSize = data.getContent().size();
|
|
System.out.println("该数据分片长度为:" + contentSize);
|
|
byte[] content = new byte[contentSize];
|
|
for (int i = 0; i < contentSize; i++) {
|
|
content[i] = data.getContent().buf().get(i);
|
|
}
|
|
// 设置数据分片写入到文件的起点位置
|
|
randomAccessFile.seek(sliceNoInteger * sliceSizeInteger);
|
|
// 写入数据分片
|
|
randomAccessFile.write(content, 0, contentSize);
|
|
randomAccessFile.close();
|
|
System.out.println("写入文件数据分片完毕:" + filePathString + filenameString);
|
|
successInfoInteger = 0;
|
|
} catch (IOException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
// 返回成功写入或失败写入消息
|
|
responseData.setResponseData(successInfoInteger.toString());
|
|
dataCacheQueue.setResponseData(orderInfo.getSeq(), responseData);
|
|
}
|
|
});
|
|
} catch (IOException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
private void expressTheInterest(String orderPrefix, Interest interest, Long dataReqId) {
|
|
ResponseData responseData = new ResponseData();
|
|
responseData.setPrefix(orderPrefix);
|
|
try {
|
|
// 使用匿名内部类,发送数据请求兴趣包
|
|
if(orderPrefix.equals("/getDataSlice")) {
|
|
//接收数据块
|
|
face.expressInterest(interest, new OnData() {
|
|
@Override
|
|
public void onData(Interest interest, Data data) {
|
|
int contentSize = data.getContent().size();
|
|
System.out.println("该通知的数据块分片长度为:" + contentSize);
|
|
byte[] content = new byte[contentSize];
|
|
for (int i = 0; i < contentSize; i++) {
|
|
content[i] = data.getContent().buf().get(i);
|
|
System.out.print(i+": "+content[i]+" ");
|
|
}
|
|
System.out.println("");
|
|
System.out.println("notice content: "+content);
|
|
responseData.setFileSlice(content);
|
|
dataCacheQueue.setResponseData(dataReqId, responseData);
|
|
}
|
|
});
|
|
}else {
|
|
face.expressInterest(interest, new OnData() {
|
|
@Override
|
|
public void onData(Interest interest, Data data) {
|
|
// System.out.println("on data success");
|
|
// TODO Auto-generated method stub
|
|
// System.out.println("ondata interest: "+interest.getName());
|
|
// System.out.println("ondata data: "+data.getContent().toString());
|
|
responseData.setResponseData(data.getContent().toString());
|
|
dataCacheQueue.setResponseData(dataReqId, responseData);
|
|
}
|
|
});
|
|
}
|
|
} catch (IOException e) {
|
|
// TODO Auto-generated catch block
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
// 写头像
|
|
// public Integer writeHeadPhotoIntoLocalFile(String filenameString,
|
|
// Integer sliceNo,Integer sliceSize,byte[] bytes) {
|
|
// String pathString=mainApp.fileLocalPath;
|
|
// return writeIntoLocalFile(pathString, filenameString, sliceNo, sliceSize, bytes);
|
|
// }
|
|
|
|
// 根据包含路径的文件名获取纯文件名[linux下]
|
|
public String getPureFileName(String pathfilename) {
|
|
// 标记最后一个/的下标
|
|
int indexLeft = -1;
|
|
for (int i = 0; i < pathfilename.length(); i++) {
|
|
if (pathfilename.charAt(i) == '/') {
|
|
indexLeft = i;
|
|
}
|
|
}
|
|
if (indexLeft == -1) {
|
|
System.out.println("pure name[linux]: " + pathfilename);
|
|
return pathfilename;
|
|
} else {
|
|
System.out.println("pure name[linux]: " + pathfilename.substring(indexLeft + 1, pathfilename.length()));
|
|
return pathfilename.substring(indexLeft + 1, pathfilename.length());
|
|
}
|
|
}
|
|
|
|
// 根据包含路径的文件名获取纯文件名[windows本地]
|
|
public String getPureFileNameFromWindows(String pathfilename) {
|
|
// 标记最后一个/的下标
|
|
int indexLeft = -1;
|
|
for (int i = 0; i < pathfilename.length(); i++) {
|
|
if (pathfilename.charAt(i) == '\\') {
|
|
indexLeft = i;
|
|
}
|
|
}
|
|
if (indexLeft == -1) {
|
|
System.out.println("pure name[windows]: " + pathfilename);
|
|
return pathfilename;
|
|
} else {
|
|
System.out.println("pure name[windows]: " + pathfilename.substring(indexLeft + 1, pathfilename.length()));
|
|
return pathfilename.substring(indexLeft + 1, pathfilename.length());
|
|
}
|
|
}
|
|
// // 向指定本地文件写入指定数据分片=》被ondata调用
|
|
// public Integer writeIntoLocalFile(String pathString,String filenameString,
|
|
// Integer sliceNo,Integer sliceSize,byte[] bytes) {
|
|
// try {
|
|
// @SuppressWarnings("resource")
|
|
// RandomAccessFile randomAccessFile=new RandomAccessFile(pathString+filenameString, "rw");
|
|
// int contentSize=
|
|
// randomAccessFile.write(bytes,sliceNo*sliceSize,bytes.length);
|
|
// return 0;
|
|
// } catch (IOException e) {
|
|
// // TODO Auto-generated catch block
|
|
// e.printStackTrace();
|
|
// return 1;
|
|
// }
|
|
// }
|
|
|
|
// 关闭数据连接
|
|
public void closeDataConn() {
|
|
this.face.shutdown();
|
|
}
|
|
}
|