整理依赖

This commit is contained in:
cheney
2022-04-02 17:31:58 +08:00
parent 3aa8237a4d
commit 2f103856c8
8 changed files with 471 additions and 471 deletions
@@ -1,2 +1,3 @@
/proto/
/script/
/dto/
+381 -381
View File
@@ -1,381 +1,381 @@
package racal.sunyard.main;
import com.sunyard.SydApiException;
import com.sunyard.SydApiSDF;
import com.sunyard.entity.DeviceInfo;
import com.sunyard.proto.Packet;
import com.sunyard.proto.PacketSection;
import com.sunyard.proto.Util;
import com.sunyard.trans.PacketSN;
import racal.sunyard.main.proto.Proto0100;
import racal.sunyard.main.proto.Proto0101;
import racal.sunyard.main.proto.ProtoSK;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketTimeoutException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class SydApiSDF4j implements SydApiSDF {
private static boolean debug = false;
private static long sTime = System.currentTimeMillis();
private static String ip1;
private static String ip2;
private static int port;
private static int timeout;
private Socket socket;
//初始化变量
static {
String d = System.getProperty("com.sunyard.sydapi4j.debug");
if ("true".equals(d)) {
debug = true;
}
}
private static void println(Object i) {
if (debug) {
System.out.println(i);
}
}
private static void print(Object i) {
if (debug) {
System.out.print(i);
}
}
//gc回收
@Override
protected void finalize() throws Throwable {
super.finalize();
if (null != this.socket) {
this.socket.close();
}
}
/**
* 确认链接可用
* 确认读缓存为空
*/
private void testSocket() {
// 就是简单对象模式,链接长短完全由用户自己控制
if (null == socket) {
// 全局热备模式(old shool)
if (null != ip2) {
Socket s1 = new Socket();
SocketAddress address = new InetSocketAddress(ip1, port);
try {
s1.connect(address, timeout);
socket = s1;
socket.setSoTimeout(timeout);
} catch (Exception e) {
Socket s2 = new Socket();
SocketAddress add2 = new InetSocketAddress(ip2, port);
try {
s2.connect(add2, timeout);
socket = s2;
socket.setSoTimeout(timeout);
} catch (Exception e2) {
throw new SydApiException(-2);
}
}
}
}
clear();
}
protected byte[] syncSend(ByteBuffer bb) {
byte[] buff;
if (bb.position() == bb.limit()) {
buff = bb.array();
} else {
buff = new byte[bb.position()];
System.arraycopy(bb.array(), 0, buff, 0, buff.length);
}
println("\n\n发送数据");
print("HEX数据: \t");
println(Util.bytes2HexString(buff));
testSocket();
try {
socket.getOutputStream().write(buff);
} catch (IOException e) {
e.printStackTrace();
}
return buff;
}
private void clear() {
try {
InputStream is = socket.getInputStream();
while (is.available() > 0) {
is.read();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private ByteBuffer read(Socket socket) throws IOException {
InputStream is = socket.getInputStream();
int d;
int high = is.read();
int low = is.read();
int len = (high << 8) + low;
len += 2;
print("H\t");
println(Integer.toHexString(high));
print("L\t");
println(Integer.toHexString(low));
print("Len\t");
println(len);
ByteBuffer bb = ByteBuffer.allocate(len);
bb.put((byte) (high & 0xFF));
bb.put((byte) (low & 0xFF));
while (true) {
d = is.read();
if (-1 == d) {
break;
}
bb.put((byte) (d & 0xFF));
if (bb.position() == len) {
break;
}
}
print("HEX数据: \t");
println(Util.bytes2HexString(bb.array()));
return bb;
}
/**
* 阻塞读取
* 前两个字节是长度
*
* @return
* @buffer 如果出错,需要补发的数据。
*/
protected ByteBuffer syncRead(byte[] buffer) {
println("\n\n接收数据");
try {
// 正常接收
ByteBuffer bb = read(socket);
return bb;
} catch (SocketTimeoutException e) {
throw new SydApiException("接收数据超时", -2);
} catch (Exception e) {
e.printStackTrace();
}
throw new SydApiException("通信数据错误", -3);
}
@Override
synchronized public SydApiSDF connect(String ip, int port, String pwd, int timeout) {
if (null != ip2) {
throw new SydApiException("双机热备模式不用调用此方法", -1);
}
Socket socket = new Socket();
SocketAddress address = new InetSocketAddress(ip, port);
try {
if (timeout > 0) {
socket.connect(address, timeout);
} else {
socket.connect(address);
}
socket.setSoTimeout(timeout);
} catch (Exception e) {
throw new SydApiException(-2);
}
this.socket = socket;
return this;
}
@Override
synchronized public SydApiSDF connect(String ip, int port, String pwd, int connectTimeout, int readTimeout) {
if (null != ip2) {
throw new SydApiException("双机热备模式不用调用此方法", -1);
}
Socket socket = new Socket();
SocketAddress address = new InetSocketAddress(ip, port);
try {
socket.connect(address, connectTimeout);
socket.setSoTimeout(readTimeout);
} catch (Exception e) {
throw new SydApiException(-2);
}
this.socket = socket;
return this;
}
@Override
synchronized public int disconnect() {
if (null != socket) {
try {
socket.getInputStream().close();
} catch (Exception e1) {
}
try {
socket.getOutputStream().close();
} catch (Exception e2) {
}
try {
socket.close();
} catch (Exception e) {
}
}
return 0;
}
@Override
public DeviceInfo getStatus() {
Proto0100 proto = new Proto0100();
HashMap<String, Object> packet = new HashMap<String, Object>();
HashMap<String, Object> data = new HashMap<String, Object>();
PacketSN sn = PacketSN.gen();
packet.put("header", sn.getSn().array());
packet.put("data", data);
// 渲染发送
ByteBuffer bb = proto.rend(packet);
synchronized (this) {
// 响应解析
bb = syncRead(syncSend(bb));
}
Packet p = proto.parse(bb);
Map<String, Object> ret = p.toMap();
if (
!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())
) {
throw new SydApiException("包序号不匹配", -4);
}
ret = (Map<String, Object>) ret.get("data");
PacketSection error = (PacketSection) ret.get("error");
byte[] be = error.getBytes();
if ((0 != (be[0] ^ be[1]))) {
throw new SydApiException(error.getInfo(), error.autoInt());
}
byte[] deviceinfo = ((PacketSection) ret.get("deviceinfo")).getBytes();
return new DeviceInfo(deviceinfo);
}
@Override
public byte[] directBytes(byte[] data) {
//初始化一个和byte长度一样的bb
ByteBuffer bb = ByteBuffer.allocate(data.length);
// 数组放到bb中
bb.put(data);
//发送数据,解析
synchronized (this) {
bb = syncRead(syncSend(bb));
}
//重置 limit 和postion 值
bb.flip();
//获取bb中有效大小
int len = bb.limit() - bb.position();
byte[] bytes = new byte[len];
for (int i = 0; i < bytes.length; i++) {
bytes[i] = bb.get();
}
return bytes;
}
/**
* 获取随机数
* @param randLen 随机数长度
* @return
*/
public byte[] genRandom(int randLen) {
//获取协议
Proto0101 proto = new Proto0101();
//填充数据
PacketSN sn = PacketSN.gen();
HashMap<String, Object> packet = new HashMap<String, Object>();
packet.put("header", sn.getSn().array());
HashMap<String, Object> data = new HashMap<String, Object>();
data.put("randLen", randLen);
packet.put("data", data);
//渲染发送
ByteBuffer bb = proto.rend(packet);
synchronized (this) {
//解析响应
bb = syncRead(syncSend(bb));
}
Packet p = proto.parse(bb, packet);
Map<String, Object> ret = p.toMap();
if (
!Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())
) {
throw new SydApiException("包序号不匹配", -4);
}
ret = (Map<String, Object>) ret.get("data");
PacketSection error = (PacketSection) ret.get("error");
byte[] be = error.getBytes();
if ((0 != (be[0] ^ be[1]))) {
throw new SydApiException(error.getInfo(), error.autoInt());
}
return ((PacketSection) ret.get("randData")).getBytes();
}
}
//package racal.sunyard.main;
//
//import com.sunyard.SydApiException;
//import com.sunyard.SydApiSDF;
//import com.sunyard.entity.DeviceInfo;
//import com.sunyard.proto.Packet;
//import com.sunyard.proto.PacketSection;
//import com.sunyard.proto.Util;
//import com.sunyard.trans.PacketSN;
//import racal.sunyard.main.proto.Proto0100;
//import racal.sunyard.main.proto.Proto0101;
//import racal.sunyard.main.proto.ProtoSK;
//
//import java.io.IOException;
//import java.io.InputStream;
//import java.net.InetSocketAddress;
//import java.net.Socket;
//import java.net.SocketAddress;
//import java.net.SocketTimeoutException;
//import java.nio.ByteBuffer;
//import java.util.Arrays;
//import java.util.HashMap;
//import java.util.Map;
//
//public class SydApiSDF4j implements SydApiSDF {
//
// private static boolean debug = false;
//
// private static long sTime = System.currentTimeMillis();
//
// private static String ip1;
// private static String ip2;
// private static int port;
// private static int timeout;
//
// private Socket socket;
//
// //初始化变量
// static {
//
// String d = System.getProperty("com.sunyard.sydapi4j.debug");
// if ("true".equals(d)) {
// debug = true;
// }
// }
//
// private static void println(Object i) {
// if (debug) {
// System.out.println(i);
// }
//
// }
//
// private static void print(Object i) {
// if (debug) {
// System.out.print(i);
// }
//
// }
//
// //gc回收
// @Override
// protected void finalize() throws Throwable {
// super.finalize();
// if (null != this.socket) {
// this.socket.close();
// }
// }
//
// /**
// * 确认链接可用
// * 确认读缓存为空
// */
// private void testSocket() {
//
// // 就是简单对象模式,链接长短完全由用户自己控制
// if (null == socket) {
//
// // 全局热备模式(old shool)
// if (null != ip2) {
//
// Socket s1 = new Socket();
// SocketAddress address = new InetSocketAddress(ip1, port);
//
// try {
// s1.connect(address, timeout);
// socket = s1;
// socket.setSoTimeout(timeout);
// } catch (Exception e) {
// Socket s2 = new Socket();
// SocketAddress add2 = new InetSocketAddress(ip2, port);
// try {
// s2.connect(add2, timeout);
// socket = s2;
// socket.setSoTimeout(timeout);
// } catch (Exception e2) {
// throw new SydApiException(-2);
// }
// }
//
// }
// }
//
// clear();
//
// }
//
// protected byte[] syncSend(ByteBuffer bb) {
//
//
// byte[] buff;
// if (bb.position() == bb.limit()) {
// buff = bb.array();
// } else {
// buff = new byte[bb.position()];
// System.arraycopy(bb.array(), 0, buff, 0, buff.length);
// }
//
//
// println("\n\n发送数据");
// print("HEX数据: \t");
// println(Util.bytes2HexString(buff));
//
// testSocket();
//
// try {
// socket.getOutputStream().write(buff);
// } catch (IOException e) {
// e.printStackTrace();
// }
//
// return buff;
// }
//
// private void clear() {
// try {
// InputStream is = socket.getInputStream();
// while (is.available() > 0) {
// is.read();
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
//
//
// private ByteBuffer read(Socket socket) throws IOException {
// InputStream is = socket.getInputStream();
// int d;
// int high = is.read();
// int low = is.read();
// int len = (high << 8) + low;
//
// len += 2;
//
// print("H\t");
// println(Integer.toHexString(high));
// print("L\t");
// println(Integer.toHexString(low));
// print("Len\t");
// println(len);
//
//
// ByteBuffer bb = ByteBuffer.allocate(len);
// bb.put((byte) (high & 0xFF));
// bb.put((byte) (low & 0xFF));
//
// while (true) {
// d = is.read();
// if (-1 == d) {
// break;
// }
//
// bb.put((byte) (d & 0xFF));
//
// if (bb.position() == len) {
// break;
// }
// }
//
// print("HEX数据: \t");
// println(Util.bytes2HexString(bb.array()));
// return bb;
// }
//
// /**
// * 阻塞读取
// * 前两个字节是长度
// *
// * @return
// * @buffer 如果出错,需要补发的数据。
// */
// protected ByteBuffer syncRead(byte[] buffer) {
// println("\n\n接收数据");
// try {
// // 正常接收
// ByteBuffer bb = read(socket);
// return bb;
//
// } catch (SocketTimeoutException e) {
// throw new SydApiException("接收数据超时", -2);
// } catch (Exception e) {
// e.printStackTrace();
// }
//
// throw new SydApiException("通信数据错误", -3);
// }
//
// @Override
// synchronized public SydApiSDF connect(String ip, int port, String pwd, int timeout) {
//
// if (null != ip2) {
// throw new SydApiException("双机热备模式不用调用此方法", -1);
// }
// Socket socket = new Socket();
// SocketAddress address = new InetSocketAddress(ip, port);
//
// try {
// if (timeout > 0) {
// socket.connect(address, timeout);
// } else {
// socket.connect(address);
// }
// socket.setSoTimeout(timeout);
// } catch (Exception e) {
// throw new SydApiException(-2);
// }
//
// this.socket = socket;
// return this;
// }
//
// @Override
// synchronized public SydApiSDF connect(String ip, int port, String pwd, int connectTimeout, int readTimeout) {
//
// if (null != ip2) {
// throw new SydApiException("双机热备模式不用调用此方法", -1);
// }
// Socket socket = new Socket();
// SocketAddress address = new InetSocketAddress(ip, port);
//
// try {
// socket.connect(address, connectTimeout);
// socket.setSoTimeout(readTimeout);
// } catch (Exception e) {
// throw new SydApiException(-2);
// }
//
// this.socket = socket;
// return this;
// }
//
// @Override
// synchronized public int disconnect() {
//
// if (null != socket) {
// try {
// socket.getInputStream().close();
// } catch (Exception e1) {
// }
//
//
// try {
// socket.getOutputStream().close();
// } catch (Exception e2) {
// }
//
// try {
// socket.close();
// } catch (Exception e) {
// }
//
// }
//
// return 0;
// }
//
// @Override
// public DeviceInfo getStatus() {
//
// Proto0100 proto = new Proto0100();
//
// HashMap<String, Object> packet = new HashMap<String, Object>();
// HashMap<String, Object> data = new HashMap<String, Object>();
// PacketSN sn = PacketSN.gen();
// packet.put("header", sn.getSn().array());
// packet.put("data", data);
//
// // 渲染发送
// ByteBuffer bb = proto.rend(packet);
//
// synchronized (this) {
// // 响应解析
// bb = syncRead(syncSend(bb));
// }
//
// Packet p = proto.parse(bb);
// Map<String, Object> ret = p.toMap();
//
// if (
// !Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())
// ) {
// throw new SydApiException("包序号不匹配", -4);
// }
//
// ret = (Map<String, Object>) ret.get("data");
// PacketSection error = (PacketSection) ret.get("error");
// byte[] be = error.getBytes();
// if ((0 != (be[0] ^ be[1]))) {
// throw new SydApiException(error.getInfo(), error.autoInt());
// }
//
// byte[] deviceinfo = ((PacketSection) ret.get("deviceinfo")).getBytes();
//
// return new DeviceInfo(deviceinfo);
// }
//
// @Override
// public byte[] directBytes(byte[] data) {
// //初始化一个和byte长度一样的bb
// ByteBuffer bb = ByteBuffer.allocate(data.length);
// // 数组放到bb中
// bb.put(data);
//
// //发送数据,解析
// synchronized (this) {
// bb = syncRead(syncSend(bb));
// }
// //重置 limit 和postion 值
// bb.flip();
// //获取bb中有效大小
// int len = bb.limit() - bb.position();
//
// byte[] bytes = new byte[len];
// for (int i = 0; i < bytes.length; i++) {
// bytes[i] = bb.get();
// }
// return bytes;
// }
//
// /**
// * 获取随机数
// * @param randLen 随机数长度
// * @return
// */
// public byte[] genRandom(int randLen) {
// //获取协议
// Proto0101 proto = new Proto0101();
//
// //填充数据
// PacketSN sn = PacketSN.gen();
// HashMap<String, Object> packet = new HashMap<String, Object>();
// packet.put("header", sn.getSn().array());
// HashMap<String, Object> data = new HashMap<String, Object>();
// data.put("randLen", randLen);
// packet.put("data", data);
//
// //渲染发送
// ByteBuffer bb = proto.rend(packet);
//
// synchronized (this) {
// //解析响应
// bb = syncRead(syncSend(bb));
// }
// Packet p = proto.parse(bb, packet);
// Map<String, Object> ret = p.toMap();
// if (
// !Arrays.equals(sn.getSn().array(), ((PacketSection) ret.get("header")).getBytes())
// ) {
// throw new SydApiException("包序号不匹配", -4);
// }
// ret = (Map<String, Object>) ret.get("data");
// PacketSection error = (PacketSection) ret.get("error");
// byte[] be = error.getBytes();
// if ((0 != (be[0] ^ be[1]))) {
// throw new SydApiException(error.getInfo(), error.autoInt());
// }
//
// return ((PacketSection) ret.get("randData")).getBytes();
// }
//}
@@ -1,10 +0,0 @@
package racal.sunyard.main;
public class TestClass implements TestInf{
@Override
public void run() {
System.out.println( "class=" + TestClass.class.getName() );
System.out.println( "classloader" + TestClass.class.getClassLoader().getClass().getName() );
}
}
@@ -1,40 +0,0 @@
package racal.sunyard.main;
import com.sunyard.SydApi;
import java.util.Scanner;
import java.io.UnsupportedEncodingException;
public class TestDecryptDataWithFile
{
public static void main(String[] args) throws Exception {
System.out.println("SydApi 测试程序启动");
SydApi key = new SydApi4j().connect("192.168.0.71",8889,null,10000);
System.out.println("连接成功");
String seesionKey = "X246F7CEC2EF09A7A8F0475C6B1CE4B05";
String inputPath = "c:\\Users\\KJ-Z285\\Desktop\\dataEncrypt.txt";
String outputPath = "c:\\Users\\KJ-Z285\\Desktop\\dataDncrypt.txt";
System.out.println("[Tips]:" + "正在解密,请稍候...\n" );
if (!key.SYMDecryptDataWithFile(0x01, seesionKey, inputPath, outputPath))
{
System.out.println("[Error]:" + "解密失败!\n" );
}
System.out.println("[Tips]:" + "解密完成!\n" );
System.out.println("执行结束\n\n" );
key.disconnect();
//
// Scanner scanner = new Scanner(System.in);
// String in = scanner.nextLine();
}
}
@@ -1,5 +0,0 @@
package racal.sunyard.main;
public interface TestInf {
public void run();
}