def initialize(): """ Starts the server main loop. """ from twisted.internet import reactor from twisted.python.logfile import LogFile import sys import os,os.path # load our run-time configuration import tops.core.utility.config as config verbose = config.initialize() # use file-based logging for ourself (print statements are automatically redirected) logpath = config.getfilename('logger','logfile') if not logpath or logpath == 'stdout': log.startLogging(sys.stdout) else: (logpath,logfile) = os.path.split(logpath) log.startLogging(LogFile(logfile,logpath)) print 'Executing',__file__,'as PID',os.getpid() try: # create a record buffer to connect our feed watchers to our clients feed = FeedBuffer() # initialize socket servers to listen for local and network log message producers factory = Factory() factory.protocol = LogServer factory.feed = feed reactor.listenTCP(config.getint('logger','tcp_port'),factory) reactor.listenUNIX(config.getfilename('logger','unix_addr'),factory) # initialize an HTTP server to handle feed watcher requests via http prepareWebServer( portNumber = config.getint('logger','http_port'), handlers = {"feed":FeedUpdate()}, properties = {"feed":feed}, filterLogs = True ) # fire up the reactor print 'Waiting for clients...' reactor.run() except Exception: print 'Reactor startup failed' # How do I release my unix socket cleanly here? #reactor.stop() raise
# load our run-time configuration verbose = config.initialize("start") # prompt for the decryption passphrase used for private data if requested private_key = None if config.getboolean("start", "get_passphrase"): passphrase = getpass.getpass("Enter the pass phrase: ") engine = secret.SecretEngine(passphrase=passphrase) private_key = engine.key del engine # collect a list of services to start and perform some checks services = {} for section in config.theParser.sections(): service_name = config.get(section, "service") launch_order = config.getint(section, "launch_order") if service_name and launch_order and config.getboolean(section, "enable"): # convert this service name to a filesystem path try: __import__(service_name, globals(), locals(), ["__file__"], -1) except ImportError: print "start: Unable to import service %s" % service_name sys.exit(-1) path = sys.modules[service_name].__file__ # check that a readable file exists at this path if not os.path.isfile(path): print "start: Mo such file %s" % path sys.exit(-2) if verbose: print "start: Located %s at %s" % (service_name, path) services[launch_order] = (service_name, path)
import time class Broadcaster(object): """ Broadcasts UDP packets to simulate the TCC """ def __init__(self,port,verbose=False): self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.socket.connect(("<broadcast>",port)) self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) def transmit(self): p = Packet.generate() self.socket.send(p.write()) def transmitPeriodically(self,interval=1.0): try: while True: self.transmit() time.sleep(interval) except KeyboardInterrupt: print 'bye' if __name__ == '__main__': import tops.core.utility.config as config verbose = config.initialize() b = Broadcaster(config.getint('tcc.broadcast','udp_port'),verbose) b.transmitPeriodically()