Ejemplo n.º 1
0
	def run(self):	
		#Main SessionHandler loop (runs while session is active)
		while self.SessionActive():
			#update local members from session table row
			self.update()
			#instantiate a NodeHandler object for each node in the session handled
			#subsequent calls will not create duplicates
			self.createNodes()
			#start the NodeHandler housekeeping threads
			#but first check if they have already been started
			for node in self.nodes:
				#if not started
				if not self.nodesDict[node.devID] :
					#start the thread
					node.start()
					#ensure start is not called again
					self.nodesDict[node.devID] = True

			self.NodesHealthAlert()
			#TODO write a function to relate nodes and check relations
			#how often do we want to repeat this? do not want to put too much strain on the DB
			#should check report times every minute or so
			sleep(2*60)
		
		# # # # # # After Session Expires # # # # # # 
		
		#---signal to stop to all housekeeping threads
		#tell the threads to stop
		for node in self.nodes:			
			self.logger.info(self.phrase+": signalling node:"+node.devID+" to stop running")
			node.keepRunning = False
		#wait for the threads to finish their work
		for node in self.nodes:				
			if node.is_alive():
				node.join()
				self.logger.info(self.phrase+": node:"+node.devID+" thread stopped")
		
		#---check if all nodes have been returned		
		# initialize list of unreturned nodes: using list comprehension
		unreturnedNodes = [node for node in self.nodes if not node.returned()]	
		# fire off an alert for every node that has not been returned
		for unreturnedNode in unreturnedNodes:
			#fire off an alerts to let session leader and node holder know this node has not been returned
			title = "Unreturned Node"
			message =  unreturnedNode.devID+" has not been returned."
			#create an alert for the session leader
			sessionAlert = Alert(self.contactID, message, title)
			self.queue.put(sessionAlert)
			#create an alert for the unreturned node holder
			nodeAlert = Alert(unreturnedNode.contactID, message, title)
			self.queue.put(nodeAlert)
		
		# now loop until all nodes are returned
		while unreturnedNodes:
			#check if any nodes have been returned
			unreturnedNodes = [node for node in unreturnedNodes if not node.returned()]
			#delay before checking again?
			sleep(5*60)

		# after all nodes are returned, archive the session table and delete its row from the database
		db = DBManager()
		db.deleteSession(self.phrase)
		db.close()
Ejemplo n.º 2
0
parser.add_argument("-S", "--SESSIONS", help="list all active sessions", action="store_true")
#-N to list all nodes and include [SESSIONNAME] to list all nodes in a session
parser.add_argument("-N", "--NODES", help="list all nodes in a session, use 0 for a list of all nodes", metavar='passphrase')

args = parser.parse_args()

db = DBManager()

if args.session != None:
	#create a new session
	db.createSession(args.session[0], int(args.session[1]))
	print "Created session with passphrase: " + args.session[0] + " for " + args.session[1] + " days"
	
elif args.archive != None:
	#archive session
	db.deleteSession(args.archive)
	print "Deleted session with passphrase: " + args.archive
	
elif args.node != None:
	#create a new node
	db.createNode(args.node)

	print "Created node with device ID: " + args.node
	
elif args.delete != None:
	#delete a node
	db.deleteNode(args.delete)
	print "Deleted node with device ID: " + args.delete
	
elif args.activate != None:
	#activate a node