本文整理汇总了Python中twisted.application.internet.StreamServerEndpointService.startService方法的典型用法代码示例。如果您正苦于以下问题:Python StreamServerEndpointService.startService方法的具体用法?Python StreamServerEndpointService.startService怎么用?Python StreamServerEndpointService.startService使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.application.internet.StreamServerEndpointService
的用法示例。
在下文中一共展示了StreamServerEndpointService.startService方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: GoApiWorker
# 需要导入模块: from twisted.application.internet import StreamServerEndpointService [as 别名]
# 或者: from twisted.application.internet.StreamServerEndpointService import startService [as 别名]
class GoApiWorker(BaseWorker):
class CONFIG_CLASS(BaseWorker.CONFIG_CLASS):
worker_name = ConfigText(
"Name of this Go API worker.", required=True, static=True)
twisted_endpoint = ConfigServerEndpoint(
"Twisted endpoint to listen on.", required=True, static=True)
web_path = ConfigText(
"The path to serve this resource on.", required=True, static=True)
health_path = ConfigText(
"The path to server the health resource on.", default='/health/',
static=True)
redis_manager = ConfigDict(
"Redis client configuration.", default={}, static=True)
riak_manager = ConfigDict(
"Riak client configuration.", default={}, static=True)
_web_service = None
def _rpc_resource_for_user(self, username):
rpc = GoApiServer(username, self.vumi_api)
addIntrospection(rpc)
return rpc
def get_health_response(self):
return "OK"
@inlineCallbacks
def setup_worker(self):
config = self.get_static_config()
self.vumi_api = yield VumiApi.from_config_async({
'redis_manager': config.redis_manager,
'riak_manager': config.riak_manager,
})
self.realm = GoUserRealm(self._rpc_resource_for_user)
site = build_web_site({
config.web_path: GoUserAuthSessionWrapper(
self.realm, self.vumi_api),
config.health_path: httprpc.HttpRpcHealthResource(self),
})
self._web_service = StreamServerEndpointService(
config.twisted_endpoint, site)
self._web_service.startService()
@inlineCallbacks
def teardown_worker(self):
if self._web_service is not None:
yield self._web_service.stopService()
def setup_connectors(self):
pass
示例2: ControlAMPService
# 需要导入模块: from twisted.application.internet import StreamServerEndpointService [as 别名]
# 或者: from twisted.application.internet.StreamServerEndpointService import startService [as 别名]
class ControlAMPService(Service):
"""
Control Service AMP server.
Convergence agents connect to this server.
:ivar dict _current_command: A dictionary containing information about
connections to which state updates are currently in progress. The keys
are protocol instances. The values are ``_UpdateState`` instances.
:ivar IReactorTime _reactor: An ``IReactorTime`` provider to be used to
schedule delays in sending updates.
:ivar set _connections_pending_update: A ``set`` of connections that are
currently pending getting an update of state and configuration. An
empty set indicates that there is no update pending.
:ivar IDelayedCall _current_pending_update_delayed_call: The
``IDelayedCall`` provider for the currently pending call to update
state/configuration on connected nodes.
"""
logger = Logger()
def __init__(self, reactor, cluster_state, configuration_service, endpoint,
context_factory):
"""
:param reactor: See ``ControlServiceLocator.__init__``.
:param ClusterStateService cluster_state: Object that records known
cluster state.
:param ConfigurationPersistenceService configuration_service:
Persistence service for desired cluster configuration.
:param endpoint: Endpoint to listen on.
:param context_factory: TLS context factory.
"""
self.connections = set()
self._reactor = reactor
self._connections_pending_update = set()
self._current_pending_update_delayed_call = None
self._current_command = {}
self.cluster_state = cluster_state
self.configuration_service = configuration_service
self.endpoint_service = StreamServerEndpointService(
endpoint,
TLSMemoryBIOFactory(
context_factory,
False,
ServerFactory.forProtocol(lambda: ControlAMP(reactor, self))
)
)
# When configuration changes, notify all connected clients:
self.configuration_service.register(self._schedule_broadcast_update)
def startService(self):
self.endpoint_service.startService()
def stopService(self):
if self._current_pending_update_delayed_call:
self._current_pending_update_delayed_call.cancel()
self._current_pending_update_delayed_call = None
self.endpoint_service.stopService()
for connection in self.connections:
connection.transport.loseConnection()
def _send_state_to_connections(self, connections):
"""
Send desired configuration and cluster state to all given connections.
:param connections: A collection of ``AMP`` instances.
"""
configuration = self.configuration_service.get()
state = self.cluster_state.as_deployment()
# Connections are separated into three groups to support a scheme which
# lets us avoid sending certain updates which we know are not
# necessary. This reduces traffic and associated costs (CPU, memory).
#
# Other schemes are possible and might produce even better performance.
# See https://clusterhq.atlassian.net/browse/FLOC-3140 for some
# brainstorming.
# Collect connections for which there is currently no unacknowledged
# update. These can receive a new update right away.
can_update = []
# Collect connections for which there is an unacknowledged update.
# Since something has changed, these should receive another update once
# that acknowledgement is received.
delayed_update = []
# Collect connections which were already set to receive a delayed
# update and still haven't sent an acknowledgement. These will still
# receive a delayed update but we'll also note that we're going to skip
# sending one intermediate update to them.
elided_update = []
for connection in connections:
try:
update = self._current_command[connection]
except KeyError:
# There's nothing in the tracking state for this connection.
# That means there's no unacknowledged update. That means we
# can send another update right away.
can_update.append(connection)
#.........这里部分代码省略.........
示例3: TestEndpointService
# 需要导入模块: from twisted.application.internet import StreamServerEndpointService [as 别名]
# 或者: from twisted.application.internet.StreamServerEndpointService import startService [as 别名]
class TestEndpointService(TestCase):
"""
Tests for L{twisted.application.internet}.
"""
def setUp(self):
"""
Construct a stub server, a stub factory, and a
L{StreamServerEndpointService} to test.
"""
self.fakeServer = FakeServer()
self.factory = Factory()
self.svc = StreamServerEndpointService(self.fakeServer, self.factory)
def test_privilegedStartService(self):
"""
L{StreamServerEndpointService.privilegedStartService} calls its
endpoint's C{listen} method with its factory.
"""
self.svc.privilegedStartService()
self.assertIdentical(self.factory, self.fakeServer.factory)
def test_synchronousRaiseRaisesSynchronously(self, thunk=None):
"""
L{StreamServerEndpointService.startService} should raise synchronously
if the L{Deferred} returned by its wrapped
L{IStreamServerEndpoint.listen} has already fired with an errback and
the L{StreamServerEndpointService}'s C{_raiseSynchronously} flag has
been set. This feature is necessary to preserve compatibility with old
behavior of L{twisted.internet.strports.service}, which is to return a
service which synchronously raises an exception from C{startService}
(so that, among other things, twistd will not start running). However,
since L{IStreamServerEndpoint.listen} may fail asynchronously, it is
a bad idea to rely on this behavior.
"""
self.fakeServer.failImmediately = ZeroDivisionError()
self.svc._raiseSynchronously = True
self.assertRaises(ZeroDivisionError, thunk or self.svc.startService)
def test_synchronousRaisePrivileged(self):
"""
L{StreamServerEndpointService.privilegedStartService} should behave the
same as C{startService} with respect to
L{TestEndpointService.test_synchronousRaiseRaisesSynchronously}.
"""
self.test_synchronousRaiseRaisesSynchronously(
self.svc.privilegedStartService)
def test_failReportsError(self):
"""
L{StreamServerEndpointService.startService} and
L{StreamServerEndpointService.privilegedStartService} should both log
an exception when the L{Deferred} returned from their wrapped
L{IStreamServerEndpoint.listen} fails.
"""
self.svc.startService()
self.fakeServer.result.errback(ZeroDivisionError())
logged = self.flushLoggedErrors(ZeroDivisionError)
self.assertEquals(len(logged), 1)
def test_synchronousFailReportsError(self):
"""
Without the C{_raiseSynchronously} compatibility flag, failing
immediately has the same behavior as failing later; it logs the error.
"""
self.fakeServer.failImmediately = ZeroDivisionError()
self.svc.startService()
logged = self.flushLoggedErrors(ZeroDivisionError)
self.assertEquals(len(logged), 1)
def test_startServiceUnstarted(self):
"""
L{StreamServerEndpointService.startService} sets the C{running} flag,
and calls its endpoint's C{listen} method with its factory, if it
has not yet been started.
"""
self.svc.startService()
self.assertIdentical(self.factory, self.fakeServer.factory)
self.assertEquals(self.svc.running, True)
def test_startServiceStarted(self):
"""
L{StreamServerEndpointService.startService} sets the C{running} flag,
but nothing else, if the service has already been started.
"""
self.test_privilegedStartService()
self.svc.startService()
self.assertEquals(self.fakeServer.listenAttempts, 1)
self.assertEquals(self.svc.running, True)
def test_stopService(self):
"""
#.........这里部分代码省略.........
示例4: ControlAMPService
# 需要导入模块: from twisted.application.internet import StreamServerEndpointService [as 别名]
# 或者: from twisted.application.internet.StreamServerEndpointService import startService [as 别名]
class ControlAMPService(Service):
"""
Control Service AMP server.
Convergence agents connect to this server.
"""
logger = Logger()
def __init__(self, cluster_state, configuration_service, endpoint):
"""
:param ClusterStateService cluster_state: Object that records known
cluster state.
:param ConfigurationPersistenceService configuration_service:
Persistence service for desired cluster configuration.
:param endpoint: Endpoint to listen on.
"""
self.connections = set()
self.cluster_state = cluster_state
self.configuration_service = configuration_service
self.endpoint_service = StreamServerEndpointService(
endpoint, ServerFactory.forProtocol(lambda: ControlAMP(self)))
# When configuration changes, notify all connected clients:
self.configuration_service.register(
lambda: self._send_state_to_connections(self.connections))
def startService(self):
self.endpoint_service.startService()
def stopService(self):
self.endpoint_service.stopService()
for connection in self.connections:
connection.transport.loseConnection()
def _send_state_to_connections(self, connections):
"""
Send desired configuration and cluster state to all given connections.
:param connections: A collection of ``AMP`` instances.
"""
configuration = self.configuration_service.get()
state = self.cluster_state.as_deployment()
with LOG_SEND_CLUSTER_STATE(self.logger,
configuration=configuration,
state=state):
for connection in connections:
action = LOG_SEND_TO_AGENT(self.logger, agent=connection)
with action.context():
d = DeferredContext(connection.callRemote(
ClusterStatusCommand,
configuration=configuration,
state=state,
eliot_context=action
))
d.addActionFinish()
d.result.addErrback(lambda _: None)
def connected(self, connection):
"""
A new connection has been made to the server.
:param ControlAMP connection: The new connection.
"""
self.connections.add(connection)
self._send_state_to_connections([connection])
def disconnected(self, connection):
"""
An existing connection has been disconnected.
:param ControlAMP connection: The lost connection.
"""
self.connections.remove(connection)
def node_changed(self, state_changes):
"""
We've received a node state update from a connected client.
:param bytes hostname: The hostname of the node.
:param list state_changes: One or more ``IClusterStateChange``
providers representing the state change which has taken place.
"""
self.cluster_state.apply_changes(state_changes)
self._send_state_to_connections(self.connections)
示例5: BenchmarkAPITestsMixin
# 需要导入模块: from twisted.application.internet import StreamServerEndpointService [as 别名]
# 或者: from twisted.application.internet.StreamServerEndpointService import startService [as 别名]
class BenchmarkAPITestsMixin(object):
"""
Tests for BenchmarkAPI.
"""
# The default timeout of 0.005 seconds is not always enough,
# because we test HTTP requests via an actual TCP/IP connection.
run_tests_with = AsynchronousDeferredRunTest.make_factory(timeout=1)
RESULT = {u"userdata": {u"branch": "master"}, u"run": 1, u"result": 1,
u"timestamp": datetime(2016, 1, 1, 0, 0, 5).isoformat(), }
NO_TIMESTAMP = {u"userdata": {u"branch": "master"}, u"run": 1,
u"result": 1, }
BAD_TIMESTAMP = {u"userdata": {u"branch": "master"}, u"run": 1,
u"result": 1, u"timestamp": "noonish", }
def setUp(self):
super(BenchmarkAPITestsMixin, self).setUp()
api = BenchmarkAPI_V1(self.backend)
site = server.Site(api.app.resource())
def make_client(listening_port):
addr = listening_port.getHost()
self.agent = client.ProxyAgent(
endpoints.TCP4ClientEndpoint(
self.reactor,
addr.host,
addr.port,
),
self.reactor,
)
listening = Deferred()
listening.addCallback(make_client)
endpoint = TestEndpoint(self.reactor, listening)
self.service = StreamServerEndpointService(endpoint, site)
self.service.startService()
self.addCleanup(self.service.stopService)
return listening
def submit(self, result):
"""
Submit a result.
"""
json = dumps(result)
body = StringProducer(json)
req = self.agent.request("POST", "/benchmark-results",
bodyProducer=body)
def add_cleanup(response):
if response.code == http.CREATED:
location = response.headers.getRawHeaders(b'Location')[0]
self.addCleanup(lambda: self.agent.request("DELETE", location))
return response
req.addCallback(add_cleanup)
return req
def check_response_code(self, response, expected_code):
"""
Response has the expected reponse code.
"""
self.assertEqual(
response.code, expected_code, "Incorrect response code")
return response
def parse_submit_response_body(self, body):
"""
Check that response to a submit request has the expected
structure and version.
Returns an identifier assigned to the submitted object.
"""
data = loads(body)
self.assertIn('version', data)
self.assertEqual(data['version'], 1)
self.assertIn('id', data)
return data['id']
def test_submit_success(self):
"""
Valid JSON can be successfully submitted.
"""
req = self.submit(self.RESULT)
req.addCallback(self.check_response_code, http.CREATED)
return req
def test_no_timestamp(self):
"""
Valid JSON with a missing timestamp is an HTTP BAD_REQUEST.
"""
req = self.submit(self.NO_TIMESTAMP)
req.addCallback(self.check_response_code, http.BAD_REQUEST)
req.addCallback(lambda _: flush_logged_errors(BadRequest))
return req
def test_bad_timestamp(self):
"""
#.........这里部分代码省略.........
示例6: notify
# 需要导入模块: from twisted.application.internet import StreamServerEndpointService [as 别名]
# 或者: from twisted.application.internet.StreamServerEndpointService import startService [as 别名]
def notify(self, title, message):
if self.wait > 0:
reactor.callLater(self.wait + 1, # @UndefinedVariable
self.notify,
*(title, message))
return
else:
self.wait += 1
reactor.callLater(1, self.notified) # @UndefinedVariable
if platform == 'win':
icon = 'logo.ico'
timeout = 10
else:
icon = 'logo.png'
timeout = 10000
kwargs = {'app_icon': os.path.join(
os.path.dirname(os.path.realpath(__file__)), icon),
'app_name': 'onDemand',
'title': title,
'message': message,
'timeout': timeout}
notification.notify(**kwargs)
if __name__ == '__main__':
log.startLogging(sys.stdout)
endpoint = TCP4ServerEndpoint(reactor, 4343)
factory = ServiceFactory()
service = StreamServerEndpointService(endpoint, factory)
service.startService()
reactor.run() # @UndefinedVariable