本机c ++中Observer模式的分布式实现 [英] distributed implementation of Observer pattern in native c++

查看:84
本文介绍了本机c ++中Observer模式的分布式实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果观察者(发布者)和主题(订阅者)使用的进程不同
前任.发布者应用程序在一台PC上运行,而订阅者进程在另一台PC上运行,我如何同时实现观察者和订阅者,以便订阅者可以订阅并且观察者可以通知?

此处描述的概念是针对本地应用程序(两个应用程序都在同一进程中)

if the Observer (publisher) and Subject (subscriber) are on different process
ex. publisher application is running on one pc and the subscriber process in another pc how can i implement both observer and subscriber so that the subscriber can subscribe and the observer can notify ?

the concept which described here is for local application (where both applications are on same process )

thanks!

推荐答案

通常的方法是通过网络套接字.观察者将连接到主题PC上的网络端口,并且主题会将通知传递给所有观察者.

由于没有标准的C ++套接字API,因此如何编写它完全取决于您的平台,但是几乎每个操作系统都具有Berkerley套接字的变体.这里有一个很好的介绍:
http://www.troubleshooters.com/codecorn/sockets/ [ ^ ]

下一个问题是如何将数据从主题传输到观察者,因为您无法通过网络将指针发送到共享内存区域.为此,您必须提出一个合适的协议,该协议通常是基于文本的.
The usual way to do this is via network sockets. The observers would connect to a network port on the subject PC, and the subject would pass notifications to all observers.

Since there''s no standard C++ socket API, so how to code it exactly depends on your platform, but pretty much every OS has a variant of the Berkerley socket. There''s a good introduction to it here: http://www.troubleshooters.com/codecorn/sockets/[^]

The next problem is how to transfer data from subject to observers, since you can''t send pointers to areas of shared memory over the network. For this, you''ll have to come up with a suitable protocol, which would normally be text-based.


这篇关于本机c ++中Observer模式的分布式实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆