gpt4 book ai didi

twisted - 使用另一个 Socket/TCP/RPC 服务扩展现有的 Twisted Service 以获取服务信息

转载 作者:行者123 更新时间:2023-12-04 06:14:19 35 4
gpt4 key购买 nike

我正在实现一个基于 Twisted 的心跳客户端/服务器组合,基于 this example .这是我的第一个 Twisted 项目。

基本上它由一个 UDP 监听器( Receiver )组成,它在接收包时调用监听器方法( DetectorService.update )。 DetectorService 始终保存当前事件/非事件客户端的列表(我对示例进行了大量扩展,但核心仍然相同),从而可以对似乎在指定超时时间内断开连接的客户端使用react。

这是从网站上获取的来源:

UDP_PORT = 43278; CHECK_PERIOD = 20; CHECK_TIMEOUT = 15

import time
from twisted.application import internet, service
from twisted.internet import protocol
from twisted.python import log

class Receiver(protocol.DatagramProtocol):
"""Receive UDP packets and log them in the clients dictionary"""

def datagramReceived(self, data, (ip, port)):
if data == 'PyHB':
self.callback(ip)

class DetectorService(internet.TimerService):
"""Detect clients not sending heartbeats for too long"""

def __init__(self):
internet.TimerService.__init__(self, CHECK_PERIOD, self.detect)
self.beats = {}

def update(self, ip):
self.beats[ip] = time.time()

def detect(self):
"""Log a list of clients with heartbeat older than CHECK_TIMEOUT"""
limit = time.time() - CHECK_TIMEOUT
silent = [ip for (ip, ipTime) in self.beats.items() if ipTime < limit]
log.msg('Silent clients: %s' % silent)

application = service.Application('Heartbeat')
# define and link the silent clients' detector service
detectorSvc = DetectorService()
detectorSvc.setServiceParent(application)
# create an instance of the Receiver protocol, and give it the callback
receiver = Receiver()
receiver.callback = detectorSvc.update
# define and link the UDP server service, passing the receiver in
udpServer = internet.UDPServer(UDP_PORT, receiver)
udpServer.setServiceParent(application)
# each service is started automatically by Twisted at launch time
log.msg('Asynchronous heartbeat server listening on port %d\n'
'press Ctrl-C to stop\n' % UDP_PORT)

此心跳服务器在后台作为守护进程运行。

现在我的问题 :

我需要能够“外部”运行脚本以在控制台上打印离线/在线客户端的数量,接收者在其一生中收集到的数量( self.beats )。像这样:
$ pyhb showactiveclients
3 clients online
$ pyhb showofflineclients
1 client offline

所以我需要向我的 DetectorService 添加某种额外的服务器(Socket、Tcp、RPC - 没关系。重点是我能够构建一个具有上述行为的客户端脚本),它允许从外部连接到它。它应该只是对请求做出响应。

该服务器需要访问正在运行的检测器服务实例的内部变量,所以我的猜测是我必须使用某种附加服务来扩展 DetectorService。

经过几个小时的尝试将检测器服务与其他几个服务结合起来,我仍然不知道实现这种行为的最佳方法是什么。所以我希望有人至少可以给我一些基本的提示,如何开始解决这个问题。
提前致谢!!!

最佳答案

我认为您已经对这里的解决方案有了大致的了解,因为您已经将其应用于 Receiver 之间的交互。和 DetectorService .这个想法是让你的对象引用其他对象,让它们做他们需要做的事情。

因此,请考虑使用基于 beats 的结果响应请求的 Web 服务。数据:

from twisted.web.resource import Resource

class BeatsResource(Resource):
# It has no children, let it respond to the / URL for brevity.
isLeaf = True

def __init__(self, detector):
Resource.__init__(self)
# This is the idea - BeatsResource has a reference to the detector,
# which has the data needed to compute responses.
self._detector = detector

def render_GET(self, request):
limit = time.time() - CHECK_TIMEOUT
# Here, use that data.
beats = self._detector.beats
silent = [ip for (ip, ipTime) in beats.items() if ipTime < limit]
request.setHeader('content-type', 'text/plain')
return "%d silent clients" % (len(silent),)

# Integrate this into the existing application
application = service.Application('Heartbeat')
detectorSvc = DetectorService()
detectorSvc.setServiceParent(application)
.
.
.
from twisted.web.server import Site
from twisted.application.internet import TCPServer

# The other half of the idea - make sure to give the resource that reference
# it needs.
root = BeatsResource(detectorSvc)
TCPServer(8080, Site(root)).setServiceParent(application)

关于twisted - 使用另一个 Socket/TCP/RPC 服务扩展现有的 Twisted Service 以获取服务信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7445888/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com