当前位置: 首页>>代码示例>>Python>>正文


Python Message.close方法代码示例

本文整理汇总了Python中message.Message.close方法的典型用法代码示例。如果您正苦于以下问题:Python Message.close方法的具体用法?Python Message.close怎么用?Python Message.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在message.Message的用法示例。


在下文中一共展示了Message.close方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Master

# 需要导入模块: from message import Message [as 别名]
# 或者: from message.Message import close [as 别名]
class Master(MessageProtocol):

	def __init__(self, peername, logfile):
		self.peername = peername
		# discovery module
		self.bcaster = Broadcaster(setting.UDP_BROADCAST, setting.CTR_MASTER_BCAST_PORT)
		# message module
		self.message = Message(setting.CTR_HOST, setting.CTR_MASTER_PORT, setting.CTR_SLAVE_PORT, self)
		# slaves
		self.slaves = {}
		self.peers = {}
		# logging
		self.logfile = logfile if logfile else '%s-%s' % (time.strftime("%m-%d"),str(int(time.time()) % 100).zfill(3))
		self.logfile = os.path.join(setting.LOG_DIR, self.logfile)
		self.logger = logging.getLogger() 
		self.logger.setLevel(logging.INFO)
		fhandler = logging.FileHandler(self.logfile+'.log')  
		formatter = logging.Formatter('%(asctime)s - %(message)s')
		fhandler.setFormatter(formatter)
		self.logger.addHandler(fhandler)
		'Plot module'
		import plot
		self.plot = plot

	#def loginfo(self, message):
	#	print '%s %s\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), message)
	#	#self.loghandler.write('%s %s\n', time.strftime('%Y-%m-%d %H:%M:%S'), message)

	'Open API'
	def run(self):
		self.bcaster.run()
		self.message.run()
		self.plot_rate_init()

	def close(self):
		self.bcaster.close()
		self.message.close()
		self.plot_rate_finish()

	def send_order_to_slave(self, peername, order):
		if peername in self.slaves:
			self.message.sendto(self.slaves[peername], order)

	def start_bidder_of_slave(self, peername):
		self.send_order_to_slave(peername, 'B1')

	def stop_bidder_of_slave(self, peername):
		self.send_order_to_slave(peername, 'B2')

	def start_auctioneer_of_slave(self, peername):
		self.send_order_to_slave(peername, 'A1')

	def stop_auctioneer_of_slave(self, peername):
		self.send_order_to_slave(peername, 'A2')
		

	'MessageProtocol'
	def on_msg_from_peer(self, data, peer):
		# parse log message
		try:
			tag, pack = data.split(':',1)
		except:
			return
		# response
		if tag == 'INT':#introduce
			self.slaves[pack] = peer
			self.peers[peer] = pack
			self.logger.info('%[email protected] ip=%s' % (pack, peer))
			self.on_slave_join(pack)
		else:
			try:
				peername = self.peers[peer]
			except:
				return
			if tag == 'P':
				self.on_slave_play(peername, pack)
			elif tag == 'B':
				self.on_slave_bid(peername, pack)
			elif tag == 'A':
				self.on_slave_auction(peername, pack)
			elif tag == 'D':
				self.on_slave_decide(peername, pack)
			elif tag == 'T':
				self.on_slave_transport(peername, pack)

	'Recall API'
	def on_slave_play(self, peername, pack):
		rate, duration, delay = struct.unpack('!fff', pack)
		self.plot_rate_add((rate,duration,delay))
		self.logger.info('%[email protected] rate=%0.2f, duration=%0.2f, rebuffer=%0.2f'% (peername, rate, duration, delay))

	def on_slave_bid(self, peername, pack):
		self.logger.info('%[email protected] %s' % (peername, pack))

	def on_slave_auction(self, peername, pack):
		self.logger.info('%[email protected] %s' % (peername, pack))

	def on_slave_decide(self, peername, pack):
		#[self.auction_index-1, ip, allocs[ip][0], allocs[ip][1], allocs[ip][2]]
		index, ip, tasks, bitrate, price = eval(pack)
#.........这里部分代码省略.........
开发者ID:NeilSoul,项目名称:Auction,代码行数:103,代码来源:controller.py

示例2: Bidder

# 需要导入模块: from message import Message [as 别名]
# 或者: from message.Message import close [as 别名]
class Bidder(object):
	def __init__(self, bidder_params, logger):
		# INIT properties
		self.bidder_params = bidder_params
		self.peername = bidder_params['peer']
		self.silent = bidder_params['silent']
		self.streaming_url = bidder_params['url']
		self.buffer_folder = setting.BUFFER_DIR
		# discovery center
		self.discovery_center = Broadcaster(
			bidder_params['broadcast'], 
			setting.DIS_BID_PORT)
		# message center
		self.message_center = Message(
			setting.MSG_HOST, 
			setting.MSG_BID_PORT, 
			setting.MSG_AUC_PORT, 
			BidderProtocol(self))
		# transport center
		self.transport_center = transport.TransportServer(
			setting.TRP_HOST,
			setting.TRP_PORT,
			TransportProtocol(self))
		# player
		self.player = BidderPlayer(self)
		# log center
		self.logger = logger #self.logger = log.LogClient(peername, bidder_params['broadcast'])
		self.running = 0
		
	""" Bidder(receiver, player) Life Cycle """
	def run(self):
		print ''
		print '### Bidder', self.peername, '( kcapacity = ', self.bidder_params['kcapacity'],') running...'
		print ''
		self.running = 1
		self.prepare()
		self.player.play()
		self.discovery_center.run()
		self.message_center.run()
		self.transport_center.start()
		

	def join(self):
		self.transport_center.join()

	def close(self):
		self.discovery_center.close()
		self.message_center.close()
		self.transport_center.close()
		self.player.close()
		self.running = 0
		self.task_timeout_cond.acquire()
		self.task_timeout_cond.notify()
		self.task_timeout_cond.release()
		print ''
		print '### Bidder', self.peername, 'stopped'
		print ''

	def prepare(self):
		self.player.prepare2play()
		# the index of which trunk is ready to write into buffer  
		self.ready_index = 0
		# wait for auction : priority queue [segment number]
		self.auction_waiting_queue = PriorityQueue()
		for i in range(self.player.get_segment_number()):
			self.auction_waiting_queue.put(i)
		# wait for retrieve : dictionary { index: (rate,time) }
		self.retrieving = {}
		# retrieved : dictionary  { index:(rate,data) }
		self.retrieved = {}
		self.task_cond = threading.Condition()
		threading.Thread(target=self.task_loop, args=(1.0, )).start()
		# timeout protection
		self.task_timeout_cond = threading.Condition()
		threading.Thread(target=self.task_timeout, args=(setting.AUCTIONEER_DOWNLOAD_TIMEOUT,)).start()
		# core
		self.core = BidderCore(self, self.bidder_params)

	def buffer_size(self):
		buffer_in_player = self.player.get_buffer()
		buffer_in_auction = self.player.get_segment_duration() * (len(self.retrieved) + len(self.retrieving))
		return buffer_in_player + buffer_in_auction

	""" Bid Factory.
	bid : bid for auction.
	send task : send segment task for auction.
	"""
	def bid(self, ip, auction):
		bid_pack = self.core.bid2auction(auction)
		if bid_pack:
			# unpack
			auction_peer, auction_index, bid = bid_pack
			# logging
			#self.logger.log('B', [self.peername, auction_peer, auction_index, self.buffer_size(), bid])
			logcontent = 'auctioneer=%s, index=%d, buffer=%0.2f, bitrates=%s, prices=%s, gains=%s' % (auction_peer, int(auction_index), self.buffer_size(), str([ r/1024.0/1024.0 for r in bid[0]]), str(bid[1]), str(bid[2]))
			self.logger.slave_bid(logcontent)
			# response
			bid_info = ','.join([auction_index, str(bid)])
			self.message_center.sendto(ip, ':'.join(['BID', bid_info]))

#.........这里部分代码省略.........
开发者ID:NeilSoul,项目名称:Auction,代码行数:103,代码来源:bidder.py

示例3: Slave

# 需要导入模块: from message import Message [as 别名]
# 或者: from message.Message import close [as 别名]
class Slave(ListenerProtocol, MessageProtocol):

	def __init__(self, peername):
		self.peername = peername
		# discovery module
		self.listener = Listener(setting.CTR_HOST, setting.CTR_MASTER_BCAST_PORT, self)
		# message module
		self.message = Message(setting.CTR_HOST, setting.CTR_SLAVE_PORT, setting.CTR_MASTER_PORT, self)

	def log(self, tag, content):
		for peer in self.listener.peers.keys():
			#print 'send', ':'.join([tag, str(content)]), 'to', peer
			self.message.sendto(peer, ':'.join([tag, str(content)]))

	'Open API'
	def run(self):
		self.listener.run()
		self.message.run()

	def close(self):
		self.listener.close()
		self.message.close()

	def introduce(self):
		time.sleep(1.0) #prepare time to find master
		self.log('INT', self.peername)

	def slave_play(self, desc):
		#rate, duration, delay = desc
		pack = struct.pack('!fff', *desc)
		self.log('P', pack)

	def slave_bid(self, desc):
		#[auction_peer, auction_index, self.buffer_size(), bid]
		pack = str(desc)
		self.log('B', pack)

	def slave_auction(self, desc):
		self.log('A', desc)

	def slave_decide(self, desc):
		#[self.auction_index-1, ip, allocs[ip][0], allocs[ip][1], allocs[ip][2]]
		self.log('D', str(desc))

	def slave_transport(self, desc):
		#[ip, index, size, duration]
		self.log('T', str(desc))

	'MessageProtocol'
	def on_msg_from_peer(self, data, peer):
		#print 'received', data, 'from', peer
		try:
			inst, info = data.split(':',1)
		except:
			return
		if inst == 'B1':
			self.bidder_start_from_master(peer, info)
		elif inst == 'B2':
			self.bidder_stop_from_master(peer, info)
		elif inst == 'A1':
			self.auctioneer_start_from_master(peer, info)
		elif inst == 'A2':
			self.auctioneer_stop_from_master(peer, info)


	'Override'
	def bidder_start_from_master(self, master_ip, info):
		pass

	def bidder_stop_from_master(self, master_ip, info):
		pass

	def auctioneer_start_from_master(self, master_ip, info):
		pass

	def auctioneer_stop_from_master(self, master_ip, info):
		pass
开发者ID:NeilSoul,项目名称:Auction,代码行数:79,代码来源:controller.py

示例4: Auctioneer

# 需要导入模块: from message import Message [as 别名]
# 或者: from message.Message import close [as 别名]
class Auctioneer(ListenerProtocol):

	def __init__(self, auctioneer_params, logger):
		# propertys
		self.auctioneer_params = auctioneer_params
		self.peername = auctioneer_params['peer']
		self.delay = auctioneer_params['delay'] if auctioneer_params['delay'] > 1.0 else 1.0
		# discovery center
		self.discovery_center = Listener(
			setting.DIS_HOST,#auctioneer_params['broadcast'], 
			setting.DIS_BID_PORT,
			self)
		# message center
		self.message_center = Message(
			setting.MSG_HOST, 
			setting.MSG_AUC_PORT, 
			setting.MSG_BID_PORT, 
			AuctionProtocol(self))
		# transport center
		self.transport = transport.TransportClient(
			setting.TRP_PORT,
			transport.Protocol())
		# log center
		self.logger = logger #self.logger = log.LogClient(peername, self.auctioneer_params['broadcast'])
		# algorithm core
		self.core = AuctioneerCore(self, self.auctioneer_params)
		self.running = 0

	""" Auctioneer Life Cycle"""
	def run(self):
		print ''
		print '### Auctioneer', self.peername, '( delay = ', self.auctioneer_params['delay'],') running...'
		print ''
		# INIT
		self.running = 1
		self.transport_queue = Queue()
		self.bids = {}# bids {ip:bid}
		self.tasks = {}# tasks {ip:task number}
		self.auction_index = 0
		# launch
		self.discovery_center.run()
		self.message_center.run()
		threading.Thread(target=self.auction_loop).start() # join ignore

	def close(self):
		self.discovery_center.close()
		self.message_center.close()
		self.running = 0
		print ''
		print '### Auctioneer', self.peername, 'stopped.'
		print ''


	def auction_loop(self):
		# timeout mechanism
		while self.running:
			try:
				ip,task = self.transport_queue.get(timeout=0.3)
			except:
				#Time out
				self.auction()
				time.sleep(0.1)
				#repeated broadcast
				''' deprecated
				for i in range(3):
					self.auction()
					time.sleep(0.033)
				'''
				self.decide_auction()
			else:
				if not ip in self.tasks or self.tasks[ip] <= 0:
					continue
				index, url = task.split(',',1)
				size, duration, success = self.transport.transport(ip, index, url)
				size = float(size) / 1024 / 128 #bytes to mb
				#delay
				if self.delay > 1.0:
					#print 'delay', duration  * (self.delay - 1.0)
					time.sleep(duration * (self.delay - 1.0))
					duration = duration * self.delay
				capacity = size / duration if duration > 0 else self.auctioneer_params['capacity']
				self.core.estimate_capacity(capacity)
				self.tasks[ip] = self.tasks[ip] - 1
				if success:
					#logging 
					#self.logger.log('T', [ip, index, size, duration])#self.logger.transport_complete(ip, index, size, duration)
					self.logger.slave_transport([ip, index, size, duration])
					print '[A  completed] No.%s, size=%0.2f(mb), capacity=%0.2f~%0.2f(mbps), url=%s, at %s' % (index, size, capacity, self.core.capacity, url, time.strftime("%H:%M:%S"))
				else:
					print '[A     failed] No.%s' % index
			


	""" Auction Factory.
	auction : make an auction.
	receive_bid : receive a bid.
	"""
	def auction(self):
		# logging
		'''
#.........这里部分代码省略.........
开发者ID:NeilSoul,项目名称:Auction,代码行数:103,代码来源:auctioneer.py


注:本文中的message.Message.close方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。