Example #1
0
def txrx(cmd):
	queue = Queue(10)
	wire  = JsonWire('', 3482, queue, accept=False)
	
	try:
		wire.start()
	
		while True:
			try:
				msg = queue.get(block=False)
			except Empty:
				time.sleep(0.1)
				continue
			if isinstance(msg, Connected):
				break
			print('Garbage messsage: %s' % unicode(msg))
	
		wire.send(cmd.serialize())

		while True:
			try:
				msg = queue.get(block=False)
			except Empty:
				time.sleep(0.1)
				continue
			if isinstance(msg, JsonResult):
				print msg
				break
			print('Garbage messsage: %s' % unicode(msg))

	except KeyboardInterrupt:
		pass
	wire.stop()
Example #2
0
	def __init__(self):
		Thread.__init__(self, target=Conman.run, name='Conman')
		settings = self.load_settings()
		self.queue    = Queue(100)
		self.backend  = FileSystem(out_queue=self.queue, **settings['backend'])
		self.streamer = Streamer(self.backend, self.queue)
		self.jsonwire = JsonWire('', 3484, self.queue, accept=False)
		self.backend.start()
		self.streamer.start()
		self.jsonwire.start()
Example #3
0
	def run(self):
		while self.state != STOPPED:

			if self.state == PAUSED:
				time.sleep(0.5)
				continue

			msg = None
			try:
				msg = self.queue.get(block=True, timeout=0.5)
			except Empty:
				if (not self.jsonwire.is_alive()) and self.state == RUNNING:
					self.state = STARTING
					self.connected = False
					self.jsonwire = JsonWire('', 3484, self.queue, accept=False)
					self.jsonwire.start()
				continue
			except Exception, e:
				print 'VERY BAD!'
				traceback.print_exc()

			if type(msg) in [Accepting, Connected]:
				self.connected |= (type(msg) == Connected)
				self.accepting |= (type(msg) == Accepting)
				if self.connected and self.accepting and self.state == STARTING:
					# ready to hail the DM with all necessary info about conman
					# subsystems
					self.send_hail()
					self.state = RUNNING
				continue

			if isinstance(msg, JsonResult):
				if msg.guid in self.handlers:
					(orig_msg, handler, user) = self.get_handler(msg)
					handler(self, msg, orig_msg, user)
				else:
					print msg
				continue

			self.backend.in_queue.put(msg)
Example #4
0
class Conman(Thread):
	label     = None
	state     = STARTING
	backend   = None
	streamer  = None
	jsonwire  = None
	queue     = None
	handlers  = {}
	connected = False
	accepting = False

	def __init__(self):
		Thread.__init__(self, target=Conman.run, name='Conman')
		settings = self.load_settings()
		self.queue    = Queue(100)
		self.backend  = FileSystem(out_queue=self.queue, **settings['backend'])
		self.streamer = Streamer(self.backend, self.queue)
		self.jsonwire = JsonWire('', 3484, self.queue, accept=False)
		self.backend.start()
		self.streamer.start()
		self.jsonwire.start()

	def load_settings(self):
 		path = os.path.join(os.environ['DWITE_CFG_DIR'], 'conman.json')
		settings = {}
		if os.path.exists(path):
			f = open(path)
			try:
				settings = json.load(f)
			except:
				print('ERROR: Could not load settings file %s' % path)
				settings = {}
			f.close()

		if 'backend' not in settings:
			settings['backend'] = FileSystem.dump_defaults()
		return settings

	def save_settings(self):
		path = os.path.join(os.environ['DWITE_CFG_DIR'], 'conman.json')
		try:
			f = open(path, 'w')
			settings = json.load(f)
			f.close()
		except:
			settings = {'backend':{}}
		settings['backend'] = self.backend.dump_settings()
		f = open(path, 'w')
		json.dump(settings, f, indent=4)
		f.close()

	def get_handler(self, msg):
		if msg.guid in self.handlers:
			return self.handlers[msg.guid]
		return None

	def stop(self, hard=False):
		self.streamer.stop()
		self.jsonwire.stop(hard)
		self.backend.stop()
		self.state = STOPPED

	def send_hail(self):
		def handle_hail(self, msg, orig_msg, user):
			assert type(msg) == JsonResult
			if msg.errno:
				print msg.errstr
				self.stop()
		guid = random.randint(1, 1000000)
		hail = Hail(guid, self.backend.name, 0, self.streamer.port)
		self.handlers[guid] = (hail, handle_hail, None)
		self.jsonwire.send(hail.serialize())

	def run(self):
		while self.state != STOPPED:

			if self.state == PAUSED:
				time.sleep(0.5)
				continue

			msg = None
			try:
				msg = self.queue.get(block=True, timeout=0.5)
			except Empty:
				if (not self.jsonwire.is_alive()) and self.state == RUNNING:
					self.state = STARTING
					self.connected = False
					self.jsonwire = JsonWire('', 3484, self.queue, accept=False)
					self.jsonwire.start()
				continue
			except Exception, e:
				print 'VERY BAD!'
				traceback.print_exc()

			if type(msg) in [Accepting, Connected]:
				self.connected |= (type(msg) == Connected)
				self.accepting |= (type(msg) == Accepting)
				if self.connected and self.accepting and self.state == STARTING:
					# ready to hail the DM with all necessary info about conman
					# subsystems
					self.send_hail()
					self.state = RUNNING
				continue

			if isinstance(msg, JsonResult):
				if msg.guid in self.handlers:
					(orig_msg, handler, user) = self.get_handler(msg)
					handler(self, msg, orig_msg, user)
				else:
					print msg
				continue

			self.backend.in_queue.put(msg)

		self.save_settings()
Example #5
0
def main():
	# check for directory of configuration files
	path = os.environ['DWITE_CFG_DIR']
	if not os.path.exists(path):
		os.mkdir(path)
	if not os.path.isdir(path):
		raise Exception('No configuration directory "%s"' % path)

	# a queue to be used by all newly created wires to drop messages here.
	queue = Queue(100)

	try:
		# threaded "wire" objects handle the socket connections with devices,
		# content managers and user interfaces.
		ui_wire = JsonWire('', 3482, queue, accept=True)
		dm_wire = SlimWire('', 3483, queue, accept=True)
		cm_wire = JsonWire('', 3484, queue, accept=True)
		ui_wire.start()
		dm_wire.start()
		cm_wire.start()

		# wait for Connected messages from the wires. whenever one gets
		# connected create a new one so that more devices, etc, can connect
		# to dwite.
		while True:
			msg = None
			try:
				msg = queue.get(block=True, timeout=0.1)
			except Empty:
				continue

			if type(msg) == Connected:
				if msg.wire == ui_wire:
					UiConnection(ui_wire, queue).start()
					ui_wire = JsonWire('', 3482, queue, accept=True)
					ui_wire.start()
				elif msg.wire == dm_wire:
					# we need more information about the remote end before a
					# fully proper DM representation can be created. in the
					# meanwhile we still have to do *something*, so we use
					# the base class Device as a placeholder. it will make the
					# necessary corrections itself when more about the remote
					# end becomes known.
					Device(dm_wire, queue).start()
					dm_wire = SlimWire('', 3483, queue, accept=True)
					dm_wire.start()
				elif msg.wire == cm_wire:
					CmConnection(cm_wire, queue).start()
					cm_wire = JsonWire('', 3484, queue, accept=True)
					cm_wire.start()
				continue

			raise Exception('INTERNAL ERROR: Garbage message: %s' % msg)

	except KeyboardInterrupt:
		# the user pressed CTRL-C
		pass
	except:
		traceback.print_exc()

	# stop all threaded objects and quit
	ui_wire.stop(hard=True)
	dm_wire.stop(hard=True)
	cm_wire.stop(hard=True)
	for dm in dms.values():
		dm.stop()
	for cm in cms.values():
		cm.stop()
	for ui in uis.values():
		ui.stop()

	while threading.active_count() > 1:
		print [t.name for t in threading.enumerate()]