Skip to content

Commit

Permalink
ref[kotlin]: refactor kotlin protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Jul 7, 2024
1 parent a62d5dd commit fdb37c5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void mergerProtocol(List<ProtocolRegistration> registrations) throws IOEx
for (var registration : registrations) {
var protocol_id = registration.protocolId();
var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName();
protocol_manager_registrations.append(StringUtils.format("protocols[{}] = registration{}", protocol_id, protocol_name)).append(LS);
protocol_manager_registrations.append(StringUtils.format("protocols[{}] = Registration{}()", protocol_id, protocol_name)).append(LS);
protocol_manager_registrations.append(StringUtils.format("protocolIdMap.put({}::class.java, {})", protocol_name, protocol_id)).append(LS);
}

Expand All @@ -111,7 +111,7 @@ public void mergerProtocol(List<ProtocolRegistration> registrations) throws IOEx
// protocol
protocol_class.append(protocol_class(registration)).append(LS);
// registration
protocol_registration.append(protocol_registration_merger(registration)).append(LS);
protocol_registration.append(protocol_registration(registration)).append(LS);
}
var protocolTemplate = ClassUtils.getFileFromClassPathToString("kotlin/ProtocolsTemplate.kt");
var formatProtocolTemplate = CodeTemplatePlaceholder.formatTemplate(protocolTemplate, Map.of(
Expand All @@ -137,7 +137,8 @@ public void foldProtocol(List<ProtocolRegistration> registrations) throws IOExce
var protocol_id = registration.protocolId();
var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName();
protocol_imports.append(StringUtils.format("import {}.{}.{}", protocolPackage, GenerateProtocolPath.protocolPathPeriod(protocol_id), protocol_name)).append(LS);
protocol_manager_registrations.append(StringUtils.format("protocols[{}] = {}.registration{}", protocol_id, protocol_name, protocol_name)).append(LS);
protocol_imports.append(StringUtils.format("import {}.{}.Registration{}", protocolPackage, GenerateProtocolPath.protocolPathPeriod(protocol_id), protocol_name)).append(LS);
protocol_manager_registrations.append(StringUtils.format("protocols[{}] = Registration{}()", protocol_id, protocol_name)).append(LS);
protocol_manager_registrations.append(StringUtils.format("protocolIdMap[{}::class.java] = {}.toShort()", protocol_name, protocol_id)).append(LS);
}

Expand All @@ -157,9 +158,7 @@ public void foldProtocol(List<ProtocolRegistration> registrations) throws IOExce
var formatProtocolTemplate = CodeTemplatePlaceholder.formatTemplate(protocolTemplate, Map.of(
CodeTemplatePlaceholder.protocol_root_path, protocol_root_path
, CodeTemplatePlaceholder.protocol_imports, protocol_imports_fold(registration)
, CodeTemplatePlaceholder.protocol_note, GenerateProtocolNote.protocol_note(protocol_id, CodeLanguage.Kotlin)
, CodeTemplatePlaceholder.protocol_name, protocol_name
, CodeTemplatePlaceholder.protocol_field_definition, protocol_field_definition(registration)
, CodeTemplatePlaceholder.protocol_class, protocol_class(registration)
, CodeTemplatePlaceholder.protocol_registration, protocol_registration(registration)
));
var outputPath = StringUtils.format("{}/{}/{}.kt", protocolOutputPath, GenerateProtocolPath.protocolPathSlash(protocol_id), protocol_name);
Expand All @@ -179,7 +178,7 @@ public void defaultProtocol(List<ProtocolRegistration> registrations) throws IOE
for (var registration : registrations) {
var protocol_id = registration.protocolId();
var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName();
protocol_manager_registrations.append(StringUtils.format("protocols[{}] = {}.registration{}", protocol_id, protocol_name, protocol_name)).append(LS);
protocol_manager_registrations.append(StringUtils.format("protocols[{}] = Registration{}()", protocol_id, protocol_name)).append(LS);
protocol_manager_registrations.append(StringUtils.format("protocolIdMap[{}::class.java] = {}.toShort()", protocol_name, protocol_id)).append(LS);
}

Expand All @@ -198,9 +197,7 @@ public void defaultProtocol(List<ProtocolRegistration> registrations) throws IOE
var formatProtocolTemplate = CodeTemplatePlaceholder.formatTemplate(protocolTemplate, Map.of(
CodeTemplatePlaceholder.protocol_root_path, protocol_root_path
, CodeTemplatePlaceholder.protocol_imports, StringUtils.EMPTY
, CodeTemplatePlaceholder.protocol_note, GenerateProtocolNote.protocol_note(protocol_id, CodeLanguage.Kotlin)
, CodeTemplatePlaceholder.protocol_name, protocol_name
, CodeTemplatePlaceholder.protocol_field_definition, protocol_field_definition(registration)
, CodeTemplatePlaceholder.protocol_class, protocol_class(registration)
, CodeTemplatePlaceholder.protocol_registration, protocol_registration(registration)
));
var outputPath = StringUtils.format("{}/{}.kt", protocolOutputPath, protocol_name);
Expand Down Expand Up @@ -253,20 +250,6 @@ private String protocol_registration(ProtocolRegistration registration) {
return formatProtocolTemplate;
}

private String protocol_registration_merger(ProtocolRegistration registration) {
var protocol_id = registration.protocolId();
var protocol_name = registration.protocolConstructor().getDeclaringClass().getSimpleName();
var protocolTemplate = ClassUtils.getFileFromClassPathToString("kotlin/ProtocolRegistrationMergerTemplate.kt");
var formatProtocolTemplate = CodeTemplatePlaceholder.formatTemplate(protocolTemplate, Map.of(
CodeTemplatePlaceholder.protocol_name, protocol_name
, CodeTemplatePlaceholder.protocol_id, String.valueOf(protocol_id)
, CodeTemplatePlaceholder.protocol_write_serialization, protocol_write_serialization(registration)
, CodeTemplatePlaceholder.protocol_read_deserialization, protocol_read_deserialization(registration)
));
return formatProtocolTemplate;
}


private String protocol_imports_fold(ProtocolRegistration registration) {
var protocolId = registration.getId();
var subProtocols = ProtocolAnalysis.getAllSubProtocolIds(protocolId);
Expand Down

This file was deleted.

45 changes: 21 additions & 24 deletions protocol/src/main/resources/kotlin/ProtocolRegistrationTemplate.kt
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
companion object {
@JvmField
val registration${protocol_name}: IProtocolRegistration = object : IProtocolRegistration {
override fun protocolId(): Short {
return ${protocol_id}
}
class Registration${protocol_name} : IProtocolRegistration {
override fun protocolId(): Short {
return ${protocol_id}
}

override fun write(buffer: ByteBuffer, packet: Any?) {
if (packet == null) {
buffer.writeInt(0)
return
}
val message = packet as ${protocol_name}
${protocol_write_serialization}
override fun write(buffer: ByteBuffer, packet: Any?) {
if (packet == null) {
buffer.writeInt(0)
return
}
val message = packet as ${protocol_name}
${protocol_write_serialization}
}

override fun read(buffer: ByteBuffer): Any {
val length = buffer.readInt()
val packet = ${protocol_name}()
if (length == 0) {
return packet
}
val beforeReadIndex = buffer.readOffset()
${protocol_read_deserialization}
if (length > 0) {
buffer.setReadOffset(beforeReadIndex + length)
}
override fun read(buffer: ByteBuffer): Any {
val length = buffer.readInt()
val packet = ${protocol_name}()
if (length == 0) {
return packet
}
val beforeReadIndex = buffer.readOffset()
${protocol_read_deserialization}
if (length > 0) {
buffer.setReadOffset(beforeReadIndex + length)
}
return packet
}
}
8 changes: 3 additions & 5 deletions protocol/src/main/resources/kotlin/ProtocolTemplate.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
${protocol_root_path}
${protocol_imports}
${protocol_note}
class ${protocol_name} {
${protocol_field_definition}

${protocol_registration}
}
${protocol_class}

${protocol_registration}

0 comments on commit fdb37c5

Please sign in to comment.