<commit_before>#!/usr/bin/env python
# encoding: utf-8
"""
supervisor.py

Created by <NAME> on 2009-08-25.
Copyright (c) 2009 Exa Networks. All rights reserved.
"""

import time
import signal

from bgp.data import Notification
from bgp.protocol import Protocol,Network
from bgp.peer import Peer

class Supervisor (object):
	debug = True
	
	# [hex(ord(c)) for c in os.popen('clear').read()]
	clear = ''.join([chr(int(c,16)) for c in ['0x1b', '0x5b', '0x48', '0x1b', '0x5b', '0x32', '0x4a']])
	
	def __init__ (self,configuration):
		self.configuration = configuration
		self._peers = {}
		self._shutdown = False
		self.reload()
		
		signal.signal(signal.SIGTERM, self.sigterm)
		signal.signal(signal.SIGHUP, self.sighup)
	
	def sigterm (self,signum, frame):
		print "SIG TERM received"
		self.shutdown()

	def sighup (self,signum, frame):
		print "SIG TERM received"
		self.reload()
	
	def run (self):
		start = time.time()
		while self._peers:
			try:
				for ip in self._peers.keys():
					peer = self._peers[ip]
					peer.run()
				if self._shutdown:
					for ip in self._peers.keys():
						self._peers[ip].shutdown()
				# MUST not more than one KEEPALIVE / sec
				time.sleep(1.0)
			except KeyboardInterrupt:
				if self.debug: print "^C received"
				self.shutdown()
	
	def _add_peer (self,neighbor):
		ip = neighbor.peer_address.human()
		assert ip not in self._peers
		network = Network(ip)
		peer = Peer(Protocol(neighbor,network),self)
		self._peers[ip] = peer

	def reload (self):
		self.configuration.reload()
		for ip in self._peers.keys():
			if ip not in self.configuration.neighbor:
				self._peers[ip].shutdown()
		
		for _,neighbor in self.configuration.neighbor.iteritems():
			ip = neighbor.peer_address.human()
			if ip in self._peers:
				continue
			self._add_peer(neighbor)
		
	def shutdown (self):
		self._shutdown = True
	
	def unschedule (self,ip):
		del self._peers[ip]<commit_msg>fix bugs in last commit<commit_after>#!/usr/bin/env python
# encoding: utf-8
"""
supervisor.py

Created by <NAME> on 2009-08-25.
Copyright (c) 2009 Exa Networks. All rights reserved.
"""

import time
import signal

from bgp.data import Notification
from bgp.protocol import Protocol,Network
from bgp.peer import Peer

class Supervisor (object):
	debug = True
	
	# [hex(ord(c)) for c in os.popen('clear').read()]
	clear = ''.join([chr(int(c,16)) for c in ['0x1b', '0x5b', '0x48', '0x1b', '0x5b', '0x32', '0x4a']])
	
	def __init__ (self,configuration):
		self.configuration = configuration
		self._peers = {}
		self._shutdown = False
		self._reload = False
		self.reload()
		
		signal.signal(signal.SIGTERM, self.sigterm)
		signal.signal(signal.SIGHUP, self.sighup)
	
	def sigterm (self,signum, frame):
		print "SIG TERM received"
		self.shutdown()

	def sighup (self,signum, frame):
		print "SIG HUP received"
		self._reload = True
	
	def run (self):
		start = time.time()
		while self._peers:
			try:
				for ip in self._peers.keys():
					peer = self._peers[ip]
					peer.run()
				if self._shutdown:
					for ip in self._peers.keys():
						self._peers[ip].shutdown()
				if self._reload:
					self.reload()
				# MUST not more than one KEEPALIVE / sec
				time.sleep(1.0)
			except KeyboardInterrupt:
				if self.debug: print "^C received"
				self.shutdown()
	
	def _add_peer (self,neighbor):
		ip = neighbor.peer_address.human()
		assert ip not in self._peers
		network = Network(ip)
		peer = Peer(Protocol(neighbor,network),self)
		self._peers[ip] = peer

	def reload (self):
		self._reload = False
		self.configuration.reload()
		for ip in self._peers.keys():
			print ip, [n.human() for n in self.configuration.neighbor]
			if ip not in [n.human() for n in self.configuration.neighbor]:
				self._peers[ip].shutdown()
		
		for _,neighbor in self.configuration.neighbor.iteritems():
			ip = neighbor.peer_address.human()
			if ip in self._peers:
				continue
			self._add_peer(neighbor)
		
	def shutdown (self):
		self._shutdown = True
	
	def unschedule (self,ip):
		del self._peers[ip]
{'fraction_non_alphanumeric': 0.13318340829585207, 'fraction_numerical': 0.01649175412293853, 'mean_word_length': 3.554038680318544, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6052817', 'n_tokens_mistral': 1654, 'n_tokens_neox': 1401, 'n_words': 376}