Skip to content

Commit

Permalink
ref[es]: refactor es protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Jul 8, 2024
1 parent 52b2bfb commit 6651154
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,12 @@ public void mergerProtocol(List<ProtocolRegistration> registrations) throws IOEx
var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("ecmascript/ProtocolManagerTemplate.mjs");
var protocol_imports = new StringBuilder();
var protocol_manager_registrations = new StringBuilder();
protocol_imports.append("import Protocols from './Protocols.mjs';").append(LS);
protocol_imports.append("import * as Protocols from './Protocols.mjs';").append(LS);
for (var registration : registrations) {
var protocol_id = registration.protocolId();
var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName();
protocol_manager_registrations.append(StringUtils.format("protocols.set({}, Protocols.{});", protocol_id, protocol_name)).append(LS);
protocol_manager_registrations.append(StringUtils.format("protocols.set({}, new Protocols.{}Registration());", protocol_id, protocol_name)).append(LS);
protocol_manager_registrations.append(StringUtils.format("protocolIdMap.set(Protocols.{}, {});", protocol_name, protocol_id)).append(LS);
}
var placeholderMap = Map.of(CodeTemplatePlaceholder.protocol_imports, protocol_imports.toString()
, CodeTemplatePlaceholder.protocol_manager_registrations, protocol_manager_registrations.toString());
Expand All @@ -103,8 +104,8 @@ public void mergerProtocol(List<ProtocolRegistration> registrations) throws IOEx
var protocol_id = registration.protocolId();
var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName();
// protocol
protocol_class.append(formatProtocolClassTemplate(registration)).append(LS);
protocol_registration.append(StringUtils.format("Protocols.{} = {}", protocol_name, protocol_name)).append(LS);
protocol_class.append(protocol_class(registration).replace("class ", "export class ")).append(LS);
protocol_registration.append(protocol_registration(registration)).append(LS);
}
var protocolTemplate = ClassUtils.getFileFromClassPathToString("ecmascript/ProtocolsTemplate.mjs");
var formatProtocolTemplate = CodeTemplatePlaceholder.formatTemplate(protocolTemplate, Map.of(
Expand All @@ -129,8 +130,11 @@ public void foldProtocol(List<ProtocolRegistration> registrations) throws IOExce
for (var protocol : registrations) {
var protocol_id = protocol.protocolId();
var protocol_name = protocol.protocolConstructor().getDeclaringClass().getSimpleName();

protocol_imports.append(StringUtils.format("import {} from './{}/{}.mjs';", protocol_name, GenerateProtocolPath.protocolPathSlash(protocol.protocolId()), protocol_name)).append(LS);
protocol_manager_registrations.append(StringUtils.format("protocols.set({}, {});", protocol_id, protocol_name)).append(LS);
protocol_imports.append(StringUtils.format("import { {}Registration } from './{}/{}.mjs';", protocol_name, GenerateProtocolPath.protocolPathSlash(protocol.protocolId()), protocol_name)).append(LS);
protocol_manager_registrations.append(StringUtils.format("protocols.set({}, new {}Registration());", protocol_id, protocol_name)).append(LS);
protocol_manager_registrations.append(StringUtils.format("protocolIdMap.set({}, {});", protocol_name, protocol_id)).append(LS);
}
var placeholderMap = Map.of(CodeTemplatePlaceholder.protocol_imports, protocol_imports.toString()
, CodeTemplatePlaceholder.protocol_manager_registrations, protocol_manager_registrations.toString());
Expand All @@ -143,7 +147,12 @@ public void foldProtocol(List<ProtocolRegistration> registrations) throws IOExce
for (var registration : registrations) {
var protocol_id = registration.protocolId();
var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName();
var formatProtocolTemplate = formatProtocolTemplate(registration);
var protocolTemplate = ClassUtils.getFileFromClassPathToString("ecmascript/ProtocolTemplate.mjs");
var formatProtocolTemplate = CodeTemplatePlaceholder.formatTemplate(protocolTemplate, Map.of(
CodeTemplatePlaceholder.protocol_name, protocol_name
, CodeTemplatePlaceholder.protocol_class, protocol_class(registration)
, CodeTemplatePlaceholder.protocol_registration, protocol_registration(registration)
));
var outputPath = StringUtils.format("{}/{}/{}.mjs", protocolOutputPath, GenerateProtocolPath.protocolPathSlash(protocol_id), protocol_name);
var file = new File(outputPath);
FileUtils.writeStringToFile(file, formatProtocolTemplate, true);
Expand All @@ -155,16 +164,17 @@ public void foldProtocol(List<ProtocolRegistration> registrations) throws IOExce
public void defaultProtocol(List<ProtocolRegistration> registrations) throws IOException {
createTemplateFile();


// 生成ProtocolManager.mjs文件
var protocolManagerTemplate = ClassUtils.getFileFromClassPathToString("ecmascript/ProtocolManagerTemplate.mjs");
var protocol_imports = new StringBuilder();
var protocol_manager_registrations = new StringBuilder();
for (var protocol : registrations) {
var protocol_id = protocol.protocolId();
var protocol_name = protocol.protocolConstructor().getDeclaringClass().getSimpleName();
for (var registration : registrations) {
var protocol_id = registration.protocolId();
var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName();
protocol_imports.append(StringUtils.format("import {} from './{}.mjs';", protocol_name, protocol_name)).append(LS);
protocol_manager_registrations.append(StringUtils.format("protocols.set({}, {});", protocol_id, protocol_name)).append(LS);
protocol_imports.append(StringUtils.format("import { {}Registration } from './{}.mjs';", protocol_name, protocol_name)).append(LS);
protocol_manager_registrations.append(StringUtils.format("protocols.set({}, new {}Registration());", protocol_id, protocol_name)).append(LS);
protocol_manager_registrations.append(StringUtils.format("protocolIdMap.set({}, {});", protocol_name, protocol_id)).append(LS);
}
var placeholderMap = Map.of(CodeTemplatePlaceholder.protocol_imports, protocol_imports.toString()
, CodeTemplatePlaceholder.protocol_manager_registrations, protocol_manager_registrations.toString());
Expand All @@ -177,8 +187,13 @@ public void defaultProtocol(List<ProtocolRegistration> registrations) throws IOE
for (var registration : registrations) {
var protocol_id = registration.protocolId();
var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName();
var formatProtocolTemplate = formatProtocolTemplate(registration);
var outputPath = StringUtils.format("{}/{}.mjs", protocolOutputPath, protocol_name, protocol_name);
var protocolTemplate = ClassUtils.getFileFromClassPathToString("ecmascript/ProtocolTemplate.mjs");
var formatProtocolTemplate = CodeTemplatePlaceholder.formatTemplate(protocolTemplate, Map.of(
CodeTemplatePlaceholder.protocol_name, protocol_name
, CodeTemplatePlaceholder.protocol_class, protocol_class(registration)
, CodeTemplatePlaceholder.protocol_registration, protocol_registration(registration)
));
var outputPath = StringUtils.format("{}/{}.mjs", protocolOutputPath, protocol_name);
var file = new File(outputPath);
FileUtils.writeStringToFile(file, formatProtocolTemplate, true);
logger.info("Generated EcmaScript protocol file:[{}] is in path:[{}]", file.getName(), file.getAbsolutePath());
Expand All @@ -195,30 +210,27 @@ private void createTemplateFile() throws IOException {
}
}

public String formatProtocolTemplate(ProtocolRegistration registration) {
public String protocol_class(ProtocolRegistration registration) {
var protocol_id = registration.protocolId();
var protocol_name = registration.getConstructor().getDeclaringClass().getSimpleName();
var protocolTemplate = ClassUtils.getFileFromClassPathToString("ecmascript/ProtocolTemplate.mjs");
var protocolTemplate = ClassUtils.getFileFromClassPathToString("ecmascript/ProtocolClassTemplate.mjs");
var placeholderMap = Map.of(
CodeTemplatePlaceholder.protocol_note, GenerateProtocolNote.protocol_note(protocol_id, CodeLanguage.EcmaScript)
, CodeTemplatePlaceholder.protocol_name, protocol_name
, CodeTemplatePlaceholder.protocol_id, String.valueOf(protocol_id)
, CodeTemplatePlaceholder.protocol_field_definition, protocol_field_definition(registration)
, CodeTemplatePlaceholder.protocol_write_serialization, protocol_write_serialization(registration)
, CodeTemplatePlaceholder.protocol_read_deserialization, protocol_read_deserialization(registration)
);
return CodeTemplatePlaceholder.formatTemplate(protocolTemplate, placeholderMap);
}

public String formatProtocolClassTemplate(ProtocolRegistration registration) {
public String protocol_registration(ProtocolRegistration registration) {
var protocol_id = registration.protocolId();
var protocol_name = registration.getConstructor().getDeclaringClass().getSimpleName();
var protocolTemplate = ClassUtils.getFileFromClassPathToString("ecmascript/ProtocolClassTemplate.mjs");
var protocolTemplate = ClassUtils.getFileFromClassPathToString("ecmascript/ProtocolRegistrationTemplate.mjs");
var placeholderMap = Map.of(
CodeTemplatePlaceholder.protocol_note, GenerateProtocolNote.protocol_note(protocol_id, CodeLanguage.EcmaScript)
, CodeTemplatePlaceholder.protocol_name, protocol_name
, CodeTemplatePlaceholder.protocol_id, String.valueOf(protocol_id)
, CodeTemplatePlaceholder.protocol_field_definition, protocol_field_definition(registration)
, CodeTemplatePlaceholder.protocol_write_serialization, protocol_write_serialization(registration)
, CodeTemplatePlaceholder.protocol_read_deserialization, protocol_read_deserialization(registration)
);
Expand Down
28 changes: 0 additions & 28 deletions protocol/src/main/resources/ecmascript/ProtocolClassTemplate.mjs
Original file line number Diff line number Diff line change
@@ -1,32 +1,4 @@
${protocol_note}
class ${protocol_name} {
${protocol_field_definition}

static PROTOCOL_ID = ${protocol_id};

protocolId() {
return ${protocol_name}.PROTOCOL_ID;
}

static write(buffer, packet) {
if (packet === null) {
buffer.writeInt(0);
return;
}
${protocol_write_serialization}
}

static read(buffer) {
const length = buffer.readInt();
if (length === 0) {
return null;
}
const beforeReadIndex = buffer.getReadOffset();
const packet = new ${protocol_name}();
${protocol_read_deserialization}
if (length > 0) {
buffer.setReadOffset(beforeReadIndex + length);
}
return packet;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
${protocol_imports}

const protocols = new Map();
const protocolIdMap = new Map();

// initProtocol
${protocol_manager_registrations}

class ProtocolManager {
static getProtocolId(clazz) {
const protocolId = protocolIdMap.get(clazz);
if (protocolId === null || protocolId === undefined) {
throw '[protocol:' + clazz + '] not exist';
}
return protocolId;
}
static getProtocol(protocolId) {
const protocol = protocols.get(protocolId);
if (protocol === null) {
Expand All @@ -15,7 +23,7 @@ class ProtocolManager {
}

static write(buffer, packet) {
const protocolId = packet.protocolId();
const protocolId = ProtocolManager.getProtocolId(packet.constructor);
buffer.writeShort(protocolId);
const protocol = ProtocolManager.getProtocol(protocolId);
protocol.write(buffer, packet);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export class ${protocol_name}Registration {
protocolId() {
return ${protocol_id};
}

write(buffer, packet) {
if (packet === null) {
buffer.writeInt(0);
return;
}
${protocol_write_serialization}
}

read(buffer) {
const length = buffer.readInt();
if (length === 0) {
return null;
}
const beforeReadIndex = buffer.getReadOffset();
const packet = new ${protocol_name}();
${protocol_read_deserialization}
if (length > 0) {
buffer.setReadOffset(beforeReadIndex + length);
}
return packet;
}
}
32 changes: 2 additions & 30 deletions protocol/src/main/resources/ecmascript/ProtocolTemplate.mjs
Original file line number Diff line number Diff line change
@@ -1,33 +1,5 @@
${protocol_note}
class ${protocol_name} {
${protocol_field_definition}
${protocol_class}

static PROTOCOL_ID = ${protocol_id};
${protocol_registration}

protocolId() {
return ${protocol_name}.PROTOCOL_ID;
}

static write(buffer, packet) {
if (packet === null) {
buffer.writeInt(0);
return;
}
${protocol_write_serialization}
}

static read(buffer) {
const length = buffer.readInt();
if (length === 0) {
return null;
}
const beforeReadIndex = buffer.getReadOffset();
const packet = new ${protocol_name}();
${protocol_read_deserialization}
if (length > 0) {
buffer.setReadOffset(beforeReadIndex + length);
}
return packet;
}
}
export default ${protocol_name};
7 changes: 1 addition & 6 deletions protocol/src/main/resources/ecmascript/ProtocolsTemplate.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
class Protocols {
}

${protocol_class}

${protocol_registration}

export default Protocols;
${protocol_registration}

0 comments on commit 6651154

Please sign in to comment.