Дорабатываю добавление блоков.
This commit is contained in:
AidarKC
2025-12-24 17:29:50 +03:00
parent 4e14f300f9
commit bead78b372
6 changed files with 156 additions and 145 deletions
@@ -20,9 +20,9 @@ public final class InboundMessageProcessor {
private static final Map<Integer, MessageHandler> HANDLERS = Map.of(
WireCodes.Op.PING, new PingHandler(),
// WireCodes.Op.ADD_BLOCK, new AddBlockHandler(),
WireCodes.Op.GET_BLOCKCHAIN,new GetBlockchainHandler(),
WireCodes.Op.SEARCH_USERS, new SearchUsersHandler(),
WireCodes.Op.GET_LAST_BLOCK_INFO,new GetLastBlockInfoHandler()
WireCodes.Op.GET_BLOCKCHAIN,new GetBlockchainHandler()
// WireCodes.Op.SEARCH_USERS, new SearchUsersHandler(),
// WireCodes.Op.GET_LAST_BLOCK_INFO,new GetLastBlockInfoHandler()
);
@@ -1,66 +1,66 @@
package server.logic.ws_protocol.binary.handlers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import server.logic.ws_protocol.WireCodes;
import utils.blockchain.BchInfoEntry;
import utils.blockchain.BchInfoManager;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
/**
* Возврат информации о последнем блоке цепочки (GET_LAST_BLOCK_INFO).
*/
public class GetLastBlockInfoHandler implements MessageHandler {
private static final Logger log = LoggerFactory.getLogger(GetLastBlockInfoHandler.class);
@Override
public byte[] handle(byte[] msg) {
try {
if (msg.length < 12)
return intTo4Bytes(WireCodes.Status.BAD_REQUEST);
long blockchainId = ByteBuffer.wrap(msg, 4, 8)
.order(ByteOrder.BIG_ENDIAN)
.getLong();
BchInfoManager mgr = BchInfoManager.getInstance();
BchInfoEntry entry = mgr.getBchInfo(blockchainId);
if (entry == null)
return intTo4Bytes(WireCodes.Status.CHAIN_NOT_FOUND);
int lastNum = entry.lastBlockNumber;
byte[] hash = hexToBytes(entry.lastBlockHash);
ByteBuffer out = ByteBuffer.allocate(4 + 4 + 32).order(ByteOrder.BIG_ENDIAN);
out.putInt(WireCodes.Status.OK);
out.putInt(lastNum);
out.put(hash);
return out.array();
} catch (Exception e) {
log.error("GET_LAST_BLOCK_INFO: ошибка", e);
return intTo4Bytes(WireCodes.Status.INTERNAL_ERROR);
}
}
private static byte[] intTo4Bytes(int code) {
return ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN).putInt(code).array();
}
private static byte[] hexToBytes(String hex) {
if (hex == null || hex.isEmpty()) return new byte[32];
int len = hex.length();
byte[] out = new byte[len / 2];
for (int i = 0; i < len; i += 2)
out[i / 2] = (byte) Integer.parseInt(hex.substring(i, i + 2), 16);
if (out.length < 32) { // добиваем нулями
byte[] full = new byte[32];
System.arraycopy(out, 0, full, 32 - out.length, out.length);
return full;
}
return Arrays.copyOf(out, 32);
}
}
//package server.logic.ws_protocol.binary.handlers;
//
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import server.logic.ws_protocol.WireCodes;
//import utils.blockchain.BchInfoEntry;
//import utils.blockchain.BchInfoManager;
//
//import java.nio.ByteBuffer;
//import java.nio.ByteOrder;
//import java.util.Arrays;
//
///**
// * Возврат информации о последнем блоке цепочки (GET_LAST_BLOCK_INFO).
// */
//public class GetLastBlockInfoHandler implements MessageHandler {
// private static final Logger log = LoggerFactory.getLogger(GetLastBlockInfoHandler.class);
//
// @Override
// public byte[] handle(byte[] msg) {
// try {
// if (msg.length < 12)
// return intTo4Bytes(WireCodes.Status.BAD_REQUEST);
//
// long blockchainId = ByteBuffer.wrap(msg, 4, 8)
// .order(ByteOrder.BIG_ENDIAN)
// .getLong();
//
// BchInfoManager mgr = BchInfoManager.getInstance();
// BchInfoEntry entry = mgr.getBchInfo(blockchainId);
// if (entry == null)
// return intTo4Bytes(WireCodes.Status.CHAIN_NOT_FOUND);
//
// int lastNum = entry.lastBlockNumber;
// byte[] hash = hexToBytes(entry.lastBlockHash);
//
// ByteBuffer out = ByteBuffer.allocate(4 + 4 + 32).order(ByteOrder.BIG_ENDIAN);
// out.putInt(WireCodes.Status.OK);
// out.putInt(lastNum);
// out.put(hash);
// return out.array();
//
// } catch (Exception e) {
// log.error("GET_LAST_BLOCK_INFO: ошибка", e);
// return intTo4Bytes(WireCodes.Status.INTERNAL_ERROR);
// }
// }
//
// private static byte[] intTo4Bytes(int code) {
// return ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN).putInt(code).array();
// }
//
// private static byte[] hexToBytes(String hex) {
// if (hex == null || hex.isEmpty()) return new byte[32];
// int len = hex.length();
// byte[] out = new byte[len / 2];
// for (int i = 0; i < len; i += 2)
// out[i / 2] = (byte) Integer.parseInt(hex.substring(i, i + 2), 16);
// if (out.length < 32) { // добиваем нулями
// byte[] full = new byte[32];
// System.arraycopy(out, 0, full, 32 - out.length, out.length);
// return full;
// }
// return Arrays.copyOf(out, 32);
// }
//}
@@ -1,59 +1,59 @@
package server.logic.ws_protocol.binary.handlers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import server.logic.ws_protocol.WireCodes;
import utils.search.UserSearchService;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.util.List;
/**
* Поиск пользователей по логину (SEARCH_USERS).
*/
public class SearchUsersHandler implements MessageHandler {
private static final Logger log = LoggerFactory.getLogger(SearchUsersHandler.class);
@Override
public byte[] handle(byte[] msg) {
try {
if (msg.length < 8)
return intTo4Bytes(WireCodes.Status.BAD_REQUEST);
int N = ByteBuffer.wrap(msg, 4, 4).order(ByteOrder.BIG_ENDIAN).getInt();
if (N < 0 || msg.length < 8 + N)
return intTo4Bytes(WireCodes.Status.BAD_REQUEST);
String query = new String(msg, 8, N, StandardCharsets.UTF_8);
List<UserSearchService.Pair> found = UserSearchService.getInstance().searchFirst5(query);
return pack(found);
} catch (Exception e) {
log.error("SEARCH_USERS: ошибка", e);
return intTo4Bytes(WireCodes.Status.INTERNAL_ERROR);
}
}
private static byte[] pack(List<UserSearchService.Pair> pairs) {
if (pairs == null) pairs = List.of();
int total = 8;
var chunks = new java.util.ArrayList<byte[]>();
for (var p : pairs) {
byte[] packed = UserSearchService.packPair(p);
chunks.add(packed);
total += packed.length;
}
ByteBuffer out = ByteBuffer.allocate(total).order(ByteOrder.BIG_ENDIAN);
out.putInt(WireCodes.Status.OK);
out.putInt(pairs.size());
for (var c : chunks) out.put(c);
return out.array();
}
private static byte[] intTo4Bytes(int code) {
return ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN).putInt(code).array();
}
}
//package server.logic.ws_protocol.binary.handlers;
//
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import server.logic.ws_protocol.WireCodes;
//import utils.search.UserSearchService;
//
//import java.nio.ByteBuffer;
//import java.nio.ByteOrder;
//import java.nio.charset.StandardCharsets;
//import java.util.List;
//
///**
// * Поиск пользователей по логину (SEARCH_USERS).
// */
//public class SearchUsersHandler implements MessageHandler {
// private static final Logger log = LoggerFactory.getLogger(SearchUsersHandler.class);
//
// @Override
// public byte[] handle(byte[] msg) {
// try {
// if (msg.length < 8)
// return intTo4Bytes(WireCodes.Status.BAD_REQUEST);
//
// int N = ByteBuffer.wrap(msg, 4, 4).order(ByteOrder.BIG_ENDIAN).getInt();
// if (N < 0 || msg.length < 8 + N)
// return intTo4Bytes(WireCodes.Status.BAD_REQUEST);
//
// String query = new String(msg, 8, N, StandardCharsets.UTF_8);
// List<UserSearchService.Pair> found = UserSearchService.getInstance().searchFirst5(query);
// return pack(found);
//
// } catch (Exception e) {
// log.error("SEARCH_USERS: ошибка", e);
// return intTo4Bytes(WireCodes.Status.INTERNAL_ERROR);
// }
// }
//
// private static byte[] pack(List<UserSearchService.Pair> pairs) {
// if (pairs == null) pairs = List.of();
// int total = 8;
// var chunks = new java.util.ArrayList<byte[]>();
// for (var p : pairs) {
// byte[] packed = UserSearchService.packPair(p);
// chunks.add(packed);
// total += packed.length;
// }
//
// ByteBuffer out = ByteBuffer.allocate(total).order(ByteOrder.BIG_ENDIAN);
// out.putInt(WireCodes.Status.OK);
// out.putInt(pairs.size());
// for (var c : chunks) out.put(c);
// return out.array();
// }
//
// private static byte[] intTo4Bytes(int code) {
// return ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN).putInt(code).array();
// }
//}