コード例 #1
0
	def __init__(self, slave_infos=swarm_settings.SLAVE_INFOS):
		self.slave_infos = slave_infos
		self.heartbeat_client = HeartbeatClient()
		self.status_listener = SwarmMaster.SwarmStatusListener(self)
		self.master_httpd = MasterHTTPD(self)
コード例 #2
0
class SwarmMaster:
	"""The class which manages all of the master's roles: heartbeat generator, status listener, and httpd"""
	def __init__(self, slave_infos=swarm_settings.SLAVE_INFOS):
		self.slave_infos = slave_infos
		self.heartbeat_client = HeartbeatClient()
		self.status_listener = SwarmMaster.SwarmStatusListener(self)
		self.master_httpd = MasterHTTPD(self)
		
	class SwarmStatusListener(StatusListener):
		def __init__(self, swarm_master):
			self.swarm_master = swarm_master
			StatusListener.__init__(self)
		def handle_status(self, status):
			"""
			CHANGE ME: This is where you'd implement your application's reaction
			to any tests you've defined.
			"""
			print 'The SwarmStatusListener received status: %s' % status
	
	def start(self):
		self.heartbeat_client.start()
		self.status_listener.start()
		self.master_httpd.start()
	
	def stop(self):
		self.heartbeat_client.stop()
		self.status_listener.stop()
		self.master_httpd.stop()

	def receive_message(self, slave_id, action, message):
		"""
		CHANGE ME: This is where you'd implement your application specific functions.
		You could triggering a change in the master hardware or use self.send_message(...)
		to trigger actions by the slaves.
		"""
		print 'Master received %s: "%s" from slave %s' % (action, message, slave_id)

	def send_message(self, slave_host, action, message):
		"""Send a message to a host"""
		params = urllib.urlencode({swarm_settings.ACTION_PARAMETER_NAME:action, swarm_settings.MESSAGE_PARAMETER_NAME:message})
		headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
		try:
			conn = httplib.HTTPConnection('%s:%s' % (slave_host, swarm_settings.SLAVE_WEB_PORT))
			conn.request("POST", "/master/message/", params, headers)
			response = conn.getresponse()
			response.read()
			conn.close()	
			return True
		except:
			return False

	def narrowcast_message(self, slave_id, action, message):
		"""Send a message to a single slave"""
		for slave_info in self.slave_infos:
			if slave_info[0] == slave_id:
				return self.send_message(slave_info[1], action, message)
		return false
		
	def broadcast_message(self, action, message):
		"""Send a message to all of the slaves"""
		for slave_info in self.slave_infos: 
			if not self.send_message(slave_info[1], action, message):
				print 'Error'