Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
pieces = port.split(":")
if "0" in pieces:
# If the --port argument didn't tightly specify the port to use,
# write down the one we actually got, so we'll keep using the
# same one later
pieces[pieces.index("0")] = str(l0.getPortnum())
if pieces[0] != "tcp":
pieces = ["tcp"] + pieces
got_port = ":".join(pieces)
f = open(os.path.join(basedir, "port"), "w")
f.write(got_port + "\n")
f.close()
tubid = tub.getTubID()
sample_furl = tub.registerReference(Referenceable())
furl_prefix = sample_furl[:sample_furl.rfind("/")+1]
f = open(os.path.join(basedir, "furl_prefix"), "w")
f.write(furl_prefix + "\n")
f.close()
f = open(os.path.join(basedir, "flappserver.tac"), "w")
stashed_path = ""
for p in sys.path:
stashed_path += " %r,\n" % p
f.write(FLAPPSERVER_TACFILE % { 'path': stashed_path })
f.close()
if not quiet:
print >>stdout, "Foolscap Application Server created in %s" % basedir
print >>stdout, "TubID %s, listening on port %s" % (tubid, got_port)
print >>stdout, "Now launch the daemon with 'flappserver start %s'" % basedir
import logging
import simplejson
from nodeset.core import routing, config, message
from nodeset.common import log
class DispatcherStats(object):
def __init__(self):
self.pubs = 0
self.subs = 0
def toJSON(self):
return simplejson.dumps({"subs": self.subs, "pubs": self.pubs})
class EventDispatcher(Referenceable, service.Service):
"""
EventDispatcher instance is running on each host as separate process. Remote Nodes can subscribe for events
on this dispatcher, too. Nodes are exchanging events through dispatcher.
"""
def __init__(self, listen='pbu://localhost:5333/dispatcher'):
self.routing = routing.RoutingTable()
self.tub = UnauthenticatedTub()
host, port, refname = self._split(listen)
self.listen_url = listen
self.host = host
self.port = port
self.stats = DispatcherStats()
self.tub.listenOn('tcp:%d' % port)
except ImportError:
pass
from zope.interface import implements
from twisted.internet import reactor, utils, defer
from twisted.python import usage, procutils, filepath, log as tw_log
from twisted.application import service, internet
from foolscap.api import Tub, Referenceable
from foolscap.logging.interfaces import RILogGatherer, RILogObserver
from foolscap.logging.incident import IncidentClassifierBase, TIME_FORMAT
from foolscap.logging import flogfile
from foolscap.util import get_local_ip_for, move_into_place
class BadTubID(Exception):
pass
class GatheringBase(service.MultiService, Referenceable):
# requires self.furlFile and self.tacFile to be set on the class, both of
# which should be relative to the basedir.
use_local_addresses = True
def __init__(self, basedir):
if basedir is None:
# This instance was created by a gatherer.tac file. Confirm that
# we're running from the right directory (the one with the .tac
# file), otherwise we'll put the logfiles in the wrong place.
basedir = os.getcwd()
tac = os.path.join(basedir, self.tacFile)
if not os.path.exists(tac):
raise RuntimeError("running in the wrong directory")
self.basedir = basedir
service.MultiService.__init__(self)
self["mode"] = int(mode, 8)
else:
self["mode"] = int(mode)
def parseArgs(self, targetdir):
self.targetdir = os.path.abspath(targetdir)
if self["allow-subdirectories"]:
raise BadServiceArguments("--allow-subdirectories is not yet implemented")
if not os.path.exists(self.targetdir):
raise BadServiceArguments("targetdir '%s' must already exist"
% self.targetdir)
if not os.access(self.targetdir, os.W_OK):
raise BadServiceArguments("targetdir '%s' must be writeable"
% self.targetdir)
class FileUploaderReader(Referenceable):
BLOCKSIZE = 1024*1024
def __init__(self, f, source):
self.f = f
self.source = source
self.d = defer.Deferred()
def read_file(self):
self.read_block()
return self.d
def read_block(self):
d = self.source.callRemote("read", self.BLOCKSIZE)
d.addCallback(self._got_data)
d.addErrback(self._got_error)
def _got_data(self, data):
#! /usr/bin/python
from twisted.internet import reactor
from foolscap.api import Referenceable, UnauthenticatedTub
class MathServer(Referenceable):
def remote_add(self, a, b):
return a+b
def remote_subtract(self, a, b):
return a-b
myserver = MathServer()
tub = UnauthenticatedTub()
tub.listenOn("tcp:12345")
tub.setLocation("localhost:12345")
url = tub.registerReference(myserver, "math-service")
print "the object is available at:", url
tub.startService()
reactor.run()
def _got_data(self, data):
if data:
self.f.write(data)
self.read_block()
else:
# no more data: we're done
self.d.callback(None)
def _got_error(self, f):
self.d.errback(f)
class BadFilenameError(Exception):
pass
class FileUploader(service.MultiService, Referenceable):
def __init__(self, basedir, tub, options):
# tub might be None. No network activity should be done until
# startService. Validate all options in the constructor. Do not use
# the Service/MultiService ".name" attribute (which would prevent
# having multiple instances of a single service type in the same
# server).
service.MultiService.__init__(self)
self.basedir = basedir
self.tub = tub
self.options = options
self.targetdir = filepath.FilePath(options.targetdir)
def remote_putfile(self, name, source):
name = six.ensure_str(name)
#if "/" in name or name == "..":
# raise BadFilenameError()
import six, os, sys, time
from zope.interface import implementer
from twisted.internet import reactor
from twisted.python import usage
from foolscap import base32
from foolscap.api import Tub, Referenceable, fireEventually
from foolscap.logging import log, flogfile
from foolscap.referenceable import SturdyRef
from foolscap.util import format_time, FORMAT_TIME_MODES, ensure_dict_str, ensure_dict_str_keys
from .interfaces import RILogObserver
def short_tubid_b2a(tubid):
return base32.encode(tubid)[:8]
@implementer(RILogObserver)
class LogSaver(Referenceable):
def __init__(self, nodeid_s, savefile):
self.nodeid_s = nodeid_s
self.f = savefile # we own this, and may close it
self.f.write(flogfile.MAGIC)
def emit_header(self, versions, pid):
flogfile.serialize_header(self.f, "tail", versions=versions, pid=pid)
def remote_msg(self, d):
try:
flogfile.serialize_wrapper(self.f, d,
from_=self.nodeid_s,
rx_time=time.time())
except Exception as ex:
print("GATHERER: unable to serialize %s: %s" % (d, ex))
""""""
def remote_barrier(taskids):
""""""
def remote_spin():
""""""
def remote_queue_status(verbose):
""""""
def remote_clear(taskids=None):
""""""
class FCTaskControllerFromTaskController(Referenceable):
"""
Adapt a `TaskController` to an `IFCTaskController`
This class is used to expose a `TaskController` over the wire using
the Foolscap network protocol.
"""
implements(IFCTaskController, IFCClientInterfaceProvider)
def __init__(self, taskController):
self.taskController = taskController
#---------------------------------------------------------------------------
# Non interface methods
#---------------------------------------------------------------------------
self.closed = False
def remote_feed_stdin(self, data):
if not isinstance(data, bytes):
raise TypeError("stdin can accept only strings of bytes, not '%s'"
% (type(data),))
if self.log_stdin:
log.msg("stdin: %r" % data)
self.process.write(data)
def remote_close_stdin(self):
if not self.closed:
self.closed = True
if self.log_stdin:
log.msg("stdin closed")
self.process.closeStdin()
class CommandRunner(service.MultiService, Referenceable):
def __init__(self, basedir, tub, options):
service.MultiService.__init__(self)
self.basedir = basedir
self.tub = tub
self.options = options
def remote_execute(self, watcher):
o = self.options
outpipe = None
if o.send_stdout:
outpipe = watcher
errpipe = None
if o.send_stderr:
errpipe = watcher
pp = CommandPP(outpipe, errpipe, watcher, o.log_stdout, o.log_stderr)
"""Foolscap interface to `ISynchronousMultiEngine`.
The methods in this interface are similar to those of
`ISynchronousMultiEngine`, but their arguments and return values are pickled
if they are not already simple Python types that can be send over XML-RPC.
See the documentation of `ISynchronousMultiEngine` and `IMultiEngine` for
documentation about the methods.
Most methods in this interface act like the `ISynchronousMultiEngine`
versions and can be called in blocking or non-blocking mode.
"""
pass
class FCSynchronousMultiEngineFromMultiEngine(Referenceable):
"""Adapt `IMultiEngine` -> `ISynchronousMultiEngine` -> `IFCSynchronousMultiEngine`.
"""
implements(IFCSynchronousMultiEngine, IFCClientInterfaceProvider)
addSlash = True
def __init__(self, multiengine):
# Adapt the raw multiengine to `ISynchronousMultiEngine` before saving
# it. This allow this class to do two adaptation steps.
self.smultiengine = ISynchronousMultiEngine(multiengine)
self._deferredIDCallbacks = {}
#---------------------------------------------------------------------------
# Non interface methods
#---------------------------------------------------------------------------