def processMessage(self, raw, transport):
		'''
		A template method for processing messages arriving at winds.

		@param raw: the raw message which arrived
		@param transport: the wind on which the message arrived
		'''

		# First of all, we parse the message to check if it contains valid XML
		# data, which is the kind of thing we are expecting
		try:
			message = Message(data=raw)

		# Is not XML, so let's say it loud and quit processing it.
		except ExpatError as e:
			cantParseIt = '-+- Can\'t parse the following raw data -+-'
			print >> stderr, (cantParseIt + '\n' + raw + '\n')
			raise BogusMessageError

		# Protocol debug data
		# Just to show the message content to the users. Nothing more.
		print '-+- Message Received -+-\n', message.toprettyxml()

		# If the message is a reply to another earlier sent message, we avoid
		# the overlay code knowing about it and send it directly to the reply
		# handler set during the message sending.
		if (message.getKind() == 'reply'):
			MessageKeeper.callback((transport, message))

		# However, if the message is not a reply, we first check if the overlay
		# wish to consume it and, if so, we don't send it to the handlers.
		elif self.tryToInfer(transport, message):
			# Ok, if we get to this point the overlay know nothing about the
			# kind of the message. So, it is probably a plugin/management
			# component related message and we must send it to its handler.
			try:
				self.handle(transport, message)

			# If the handler was not found, we show the following error
			# message so everyone notices it!
			except MessageNotHandledError:
				print >> stderr, '\n * This message was not handled\n'

		# Protocol debug data
		# List the peers we are directly connected to.
		print '-+- Ours peers -+-'

		for i in self.peerList:
			print ' *', i
Esempio n. 2
0
def main():
	from twisted.internet import reactor

	from configparser import ConfigParser
	from wind import WindManager
	from overlay import OverlayManager
	from message import MessageKeeper
	from extension import ComponentManager
	from extension import PluginManager

	from sys import stderr

	print '-+- Parsing configuration file and CLI -+-'

	cfg = ConfigParser()

	try:
		cfg.parseOptions()
	except RuntimeError as e:
		print >> stderr, ' * Error while parsing configurations'

		for l in str(e).splitlines():
			print >> stderr, '\t>', l

		return -1

	except Warning as e:
		errors = str(e).splitlines()

		print >> stderr, ' *', errors[0]
		for l in errors[1:]:
			print >> stderr, '\t>', l

	print '-+- Loading Winds -+-'

	try:
		WindManager.load(cfg['winds'])

	except RuntimeError as e:
		print >> stderr, ' * Error while loading winds:'

		for l in str(e).splitlines():
			print >>  stderr, '\t>', l

	wdesc = WindManager.getDescriptors()

	if len(wdesc) == 0:
		print >> stderr, " * No wind was loaded. Exiting with error."
		return -1

	print '-+- Loading Overlay -+-'

	try:
		OverlayManager.load(cfg['overlay'])

	except RuntimeError as e:
		print >> stderr, (' * Error while loading overlay\n\t' + str(e))
		return -1

	print '-+- Loading Management Components -+-'

	try:
		ComponentManager.load(cfg['managementComponents'])

	except RuntimeError as e:
		print >> stderr, (
			' * Error while loading management component\n\t' + str(e))
		return -1

	overlay = OverlayManager.getOverlay()
	overlay.start(cfg['node'], WindManager.getDescriptors())

	MessageKeeper.startToKeep()
	WindManager.startListening(overlay)
	ComponentManager.register(overlay)

	if len(cfg['bootstrap']) > 0:
		reactor.callWhenRunning(overlay.bootstrap, None, cfg['bootstrap'])

	reactor.run()