Skip to content

Commit

Permalink
🐳 Simplify the implementation class of ActionParserListener related t…
Browse files Browse the repository at this point in the history
…o ProtoDataCodec. and #386
  • Loading branch information
iohao committed Oct 22, 2024
1 parent e3abc36 commit 435e516
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,20 +153,6 @@ public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
return actionMethod.getAnnotation(annotationClass);
}

/**
* get class ConstructorAccess
*
* @return ConstructorAccess
* @since 21.19
*/
public ConstructorAccess<?> getActionControllerConstructorAccess() {
if (Objects.isNull(this.actionControllerConstructorAccess)) {
this.actionControllerConstructorAccess = ConstructorAccess.get(this.actionControllerClazz);
}

return actionControllerConstructorAccess;
}

/**
* {@link ActionCommand} 命令的构建器
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import com.iohao.game.action.skeleton.annotation.ActionMethod;
import com.iohao.game.action.skeleton.core.action.parser.ActionParserContext;
import com.iohao.game.action.skeleton.core.action.parser.ActionParserListener;
import com.iohao.game.action.skeleton.core.action.parser.ProtobufActionParserListener;
import com.iohao.game.action.skeleton.core.action.parser.ProtobufCheckActionParserListener;
import com.iohao.game.action.skeleton.core.codec.ProtoDataCodec;
import com.iohao.game.action.skeleton.core.doc.ActionCommandDoc;
import com.iohao.game.action.skeleton.core.doc.ActionDoc;
Expand All @@ -38,12 +36,12 @@
import lombok.Setter;
import lombok.experimental.Accessors;
import lombok.experimental.FieldDefaults;
import org.jctools.maps.NonBlockingHashMap;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Stream;

/**
Expand Down Expand Up @@ -312,7 +310,7 @@ private void executeActionListeners() {
// action command, actionMethod
actionCommandRegion.getSubActionCommandMap().forEach((subCmd, command) -> {
// action 构建时的上下文
ActionParserContext context = new ActionParserContext()
var context = new ActionParserContext()
.setBarSkeleton(barSkeleton)
.setActionCommand(command);

Expand All @@ -327,29 +325,34 @@ private void executeActionListeners() {

@FieldDefaults(level = AccessLevel.PRIVATE)
final class ActionParserListeners {
final List<ActionParserListener> listeners = new CopyOnWriteArrayList<>();
final Map<Class<?>, ActionParserListener> map = new NonBlockingHashMap<>();

void addActionParserListener(ActionParserListener listener) {
this.listeners.add(listener);
Objects.requireNonNull(listener);
this.map.putIfAbsent(listener.getClass(), listener);
}

void onActionCommand(ActionParserContext context) {
this.listeners.forEach(listener -> listener.onActionCommand(context));
this.map.forEach((clazz, listener) -> listener.onActionCommand(context));
}

void onAfter(BarSkeleton barSkeleton) {
this.listeners.forEach(listener -> listener.onAfter(barSkeleton));
this.map.forEach((clazz, listener) -> listener.onAfter(barSkeleton));
}

public boolean isEmpty() {
return this.listeners.isEmpty();
}

public ActionParserListeners() {
ActionParserListeners() {
// 监听器 - 预先创建协议代理类
if (DataCodecKit.getDataCodec() instanceof ProtoDataCodec) {
this.addActionParserListener(ProtobufActionParserListener.me());
this.addActionParserListener(ProtobufCheckActionParserListener.me());
this.addActionParserListener(new ProtobufActionParserListener());
this.addActionParserListener(new ProtobufCheckActionParserListener());
}

// prepared actionControllerConstructorAccess
this.addActionParserListener(context -> {
var actionCommand = context.getActionCommand();
if (!actionCommand.isDeliveryContainer()) {
actionCommand.actionControllerConstructorAccess = ConstructorAccess.get(actionCommand.actionControllerClazz);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.iohao.game.action.skeleton.core.action.parser;
package com.iohao.game.action.skeleton.core;

import com.baidu.bjf.remoting.protobuf.annotation.ProtobufClass;
import com.iohao.game.action.skeleton.core.ActionCommand;
import com.iohao.game.action.skeleton.core.BarSkeleton;
import com.iohao.game.action.skeleton.core.action.parser.ActionParserContext;
import com.iohao.game.action.skeleton.core.action.parser.ActionParserListener;
import com.iohao.game.action.skeleton.protocol.wrapper.*;
import com.iohao.game.common.kit.ProtoKit;
import lombok.AccessLevel;
import lombok.experimental.FieldDefaults;
import lombok.extern.slf4j.Slf4j;
import org.jctools.maps.NonBlockingHashSet;

import java.util.Objects;
Expand All @@ -33,21 +34,38 @@
import java.util.function.Predicate;

/**
* 预先生成 proto 协议代理类
* Prepared action proto
*
* @author 渔民小镇
* @date 2024-05-01
* @since 21.7
*/
@FieldDefaults(level = AccessLevel.PRIVATE)
public final class ProtobufActionParserListener implements ActionParserListener {
final Set<Class<?>> protoSet = new NonBlockingHashSet<>();
final class ProtobufActionParserListener implements ActionParserListener {
static final Set<Class<?>> protoSet = new NonBlockingHashSet<>();

static {
// create a protobuf proxy class
ProtoKit.create(ByteValueList.class);

ProtoKit.create(IntValue.class);
ProtoKit.create(IntValueList.class);

ProtoKit.create(BoolValue.class);
ProtoKit.create(BoolValueList.class);

ProtoKit.create(LongValue.class);
ProtoKit.create(LongValueList.class);

ProtoKit.create(StringValue.class);
ProtoKit.create(StringValueList.class);
}

@Override
public void onActionCommand(ActionParserContext context) {
// 添加了 ProtobufClass 注解的类
Predicate<Class<?>> protobufClassPredicate = c -> Objects.nonNull(c.getAnnotation(ProtobufClass.class));
collect(context, protobufClassPredicate, this.protoSet);
collect(context, protobufClassPredicate, protoSet);
}

static void collect(ActionParserContext context, Predicate<Class<?>> protobufClassPredicate, Set<Class<?>> protoSet) {
Expand Down Expand Up @@ -80,32 +98,38 @@ static void collect(ActionParserContext context, Predicate<Class<?>> protobufCla

@Override
public void onAfter(BarSkeleton barSkeleton) {
this.protoSet.forEach(ProtoKit::create);
protoSet.forEach(ProtoKit::create);
}
}

private ProtobufActionParserListener() {
// create a protobuf proxy class
ProtoKit.create(ByteValueList.class);

ProtoKit.create(IntValue.class);
ProtoKit.create(IntValueList.class);

ProtoKit.create(BoolValue.class);
ProtoKit.create(BoolValueList.class);

ProtoKit.create(LongValue.class);
ProtoKit.create(LongValueList.class);
/**
* proto 协议类型添检测
*
* @author 渔民小镇
* @date 2024-05-02
* @since 21.7
*/
@Slf4j
final class ProtobufCheckActionParserListener implements ActionParserListener {
static final Set<Class<?>> protoSet = new NonBlockingHashSet<>();

ProtoKit.create(StringValue.class);
ProtoKit.create(StringValueList.class);
@Override
public void onActionCommand(ActionParserContext context) {
// 添加了 ProtobufClass 注解的类
Predicate<Class<?>> protobufClassPredicate = c -> c.getAnnotation(ProtobufClass.class) == null;
ProtobufActionParserListener.collect(context, protobufClassPredicate, protoSet);
}

public static ProtobufActionParserListener me() {
return Holder.ME;
}
@Override
public void onAfter(BarSkeleton barSkeleton) {
if (protoSet.isEmpty()) {
return;
}

/** 通过 JVM 的类加载机制, 保证只加载一次 (singleton) */
private static class Holder {
static final ProtobufActionParserListener ME = new ProtobufActionParserListener();
log.error("======== 注意,协议类没有添加 ProtobufClass 注解 ========");
for (Class<?> protoClass : protoSet) {
log.error(protoClass.toString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.iohao.game.action.skeleton.core.runner.Runner;
import com.iohao.game.action.skeleton.core.runner.Runners;
import com.iohao.game.common.kit.concurrent.executor.*;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
Expand Down Expand Up @@ -55,8 +56,10 @@ public final class BarSkeletonBuilder {
/** action class */
final List<Class<?>> actionControllerClazzList = new LinkedList<>();
/** 错误码 */
@Deprecated
final List<MsgExceptionInfo> msgExceptionInfoList = new ArrayList<>();
/** action 构建时的钩子方法 */
@Setter(AccessLevel.PRIVATE)
ActionParserListeners actionParserListeners = new ActionParserListeners();
/** action工厂 */
ActionFactoryBean<Object> actionFactoryBean = new DefaultActionFactoryBean<>();
Expand Down

This file was deleted.

0 comments on commit 435e516

Please sign in to comment.