处理通信协议对象的模式

发布于 2024-11-17 18:30:09 字数 1135 浏览 3 评论 0原文

我正在使用 protobuf 来实现 Java 应用程序和用 C++ 编写的本机应用程序之间的通信协议。消息是事件驱动的:当 C++ 应用程序中发生事件时,将构造并发送 protobuf 消息。

message MyInterProcessMessage {

   int32 id = 1;

   message EventA { ... }
   message EventB { ... }
   ...
}

在 Java 中,我在套接字上收到一个类的对象:MyInterProcessMessageProto。由此,我可以非常轻松地获取数据,因为它们彼此封装在一起: myMessage.getEventA().getName();

我面临两个问题:

  1. 如何委托处理收到消息了吗?

    因为,分析整个消息并区分不同的事件类型及其隐含的操作会导致一个庞大且不可维护的方法,其中包含许多 if-cases。

  2. 我想找到一种模式,我可以在其中保留消息,不仅可以应用它们,还可以撤消它们,就像 命令模式用于实现这一点。

我的第一种方法是:使用指定的 apply() 和 undo() 方法为每个事件创建不同的包装类,并以这种方式委托作业。

但是我不确定这是否是正确的方法或者是否有更好的解决方案。


为了澄清我的应用程序:

Java 应用程序对正在运行的 Java 虚拟机进行建模并保存信息,例如线程、监视器、内存等。

每个事件都会更改建模的 JVM 的当前状态。例如,启动一个新线程,另一个线程进入阻塞状态,释放内存等。同样,事件也被建模:ThreadEvent、MemoryEvent 等。

这意味着必须按顺序处理消息。为了迭代回到 JVM 之前的状态,我想实现这个撤消功能。

对于撤消我已经尝试过。清除所有状态,应用事件直到事件 #i。

不幸的是,对于 20,000 多个事件,这是完全低效的。

I am using protobuf for implementing a communication protocol between a Java application and a native application written in C++. The messages are event driven: when an event occurs in the C++ application a protobuf message is conructed and sent.

message MyInterProcessMessage {

   int32 id = 1;

   message EventA { ... }
   message EventB { ... }
   ...
}

In Java I receive on my socket an object of the class: MyInterProcessMessageProto. From this I can get my data very easily since they are encapsulated into each other: myMessage.getEventA().getName();

I am facing two problems:

  1. How to delegate the processing of the received messages?

    Because, analysising the whole message and distinguishing the different event types and the actions they imply resulted in a huge and not maintainable method with many if-cases.

  2. I would like to find a pattern, where I can preserve the messages and not only apply them, but also undo them, like the Command pattern is used to implement this.

My first approach would be: create different wrapper classes for each event with a specified apply() and undo() method and delegate the job this way.

However I am not sure if this is the right way or whether there are not any better solutions.


To clarify my application:

The Java application models a running Java Virtual Machine and holds information, for instance Threads, Monitors, Memory, etc.

Every event changes the current state of the modeled JVM. For instance, a new thread was launched, another thread goes into blocking state, memory was freed etc. In the same meaning the events are modeled: ThreadEvent, MemoryEvent, etc.

This means, the messages have to be processed sequentially. In order to iterate back to previous states of the JVM, I would like to implement this undo functionality.

For undo I already tried. clearAllStates, apply Events until Event #i.

Unfortunately with 20.000+ events this is total inefficient.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无人接听 2024-11-24 18:30:09

为了提供量身定制的答案,最好知道您正在对收到的消息执行什么操作,是否可以同时处理这些消息,以及撤消如何影响之后收到的消息的处理 >撤消已编辑的消息。

但是,这里有一个通用建议:典型的方法是将收到的消息委托给 类似队列的处理程序类,通常运行在自己的线程中(让消息接收者尽快为下一个传入消息做好准备)并顺序处理接收到的消息。您可以使用 类似堆栈 类来为了撤消功能而跟踪已处理的消息。您还可以针对不同的事件类型使用特定的队列和堆栈。

基本上,这类似于线程池模式

To provide a tailored answer it would be good to know what you're doing with received messages, if they can be processed concurrently or not, and how an undo impacts the processing of messages received after and undo'ed message.

However, here's a generic suggestion: A typical approach is to delegate received messages to a queue-like handler class, which usually runs in an own thread (to let the message receiver get ready for the next incoming message as soon as possible) and sequentially processes received messages. You could use a stack-like class to keep track of processed messages for the sake of the undo feature. You could also use specific queues and stacks for different event types.

Basically this resembles the thread pool pattern.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文