#!/usr/bin/python

# This test suite is deprecated and is basically used for quick tests.
# Look at test-suite/ and examples/ for something useful.

print 'import CORBA'
import CORBA
import sys
print 'import Fruit'
import Fruit, Fruit__POA
import random

print 'creating Instances'
class Blah(Fruit__POA.Blah):
	pass

class Instance(Fruit__POA.Instance):
	def __init__(self, fruit):
		self.fruit = fruit
		self.left = 100

	def noop(self): pass

	def bite(self, size):
		if self.left - size < 0:
			exdata = Fruit.Instance.BigBite(too_much_by = size - self.left)
			raise Fruit.Instance.BigBite, exdata
			return

		self.left = self.left - size
		print "Eating %d%%, %d%% left" % (size, self.left)
		if self.left == 0:
			raise Fruit.Instance.AllEaten

	def get_factory(self):
		return Factory()._this()

	def discard_factory(self, ref):
		servant = poa.reference_to_servant(ref)
		poa.deactivate_object(servant)


class Factory(Fruit__POA.Factory):
	def __init__(self):
		self.fruit_list = []

	def raise_ex(self):
		raise Fruit.Factory.AlreadyExists

	def add_fruit(self, f):
		for fruit in self.fruit_list:
			if fruit.name == f.name:
				raise Fruit.Factory.AlreadyExists
		self.fruit_list.append(f)

	def get_instance(self, fruit):
		return Instance(fruit)._this()

	def get_one_instance(self):
		print "----------------------------------------------------------------"
		return Instance(self.fruit_list[0])._this()

	def discard_instance(self, ref):
		servant = poa.reference_to_servant(ref)
		poa.deactivate_object(servant)

	def get_random_fruit(self):
		#return 0, self.fruit_list[0]
		print "--- get random fruit -------------------------------------"
		index = random.randint(0, len(self.fruit_list) - 1)
		return index, self.fruit_list[index]

	def test_union(self, color):
		if color == Fruit.orange:
			return Fruit.Factory.TestUnion(color, "foobar")
		elif color == Fruit.red:
			return Fruit.Factory.TestUnion(color, 42)
		elif color == Fruit.yellow:
			return Fruit.Factory.TestUnion(color, 2.71828)
		elif color == Fruit.green:
			return Fruit.Factory.TestUnion(color, CORBA.TRUE)

	def test_any(self):
		pick = random.randint(0, 2)
		if pick == 0:
			return CORBA.Any(CORBA.TypeCode("IDL:CORBA/String:1.0"), "abc123")
		elif pick == 1:
			return CORBA.Any(CORBA.TypeCode("IDL:CORBA/Short:1.0"), 42)
		elif pick == 2:
			new_props = Fruit.Properties(name = "pineapple", color = Fruit.yellow)
			return CORBA.Any(CORBA.TypeCode(new_props), new_props)

	def test_list(self, n):
		return list(range (n))

	def die(self):
		orb.shutdown(CORBA.TRUE)

	def quickcall(self): pass

	def foobar(self): return "blub"

	def get_objseq(self):
		seq = [Blah()._this(), Blah()._this(), Blah()._this(), Blah()._this()]
		return seq

	def clean_objseq(self, o):
		for z in o:
			servant = poa.reference_to_servant(z)
			poa.deactivate_object(servant)
	
	def test_inst(self):
		return Blah()._this()

	def clean_inst(self, o):
		servant = poa.reference_to_servant(o)
		poa.deactivate_object(servant)

	def get_prop(self):
		f = Fruit.Properties(name = "orange", color = Fruit.orange)
		return Instance(f)._this()

	def set_prop(self, o):
		print o

	def test_octseq(self, o):
		print map(ord, o) == [0x31, 0x58, 0x68, 0x78, 0x78, 0x68]
		print map(ord, o) == (0x31, 0x58, 0x68, 0x78, 0x78, 0x68)
		for c in o:
			print ord(c)

	def get_octseq(self):
		return ('a', 'b', 'c', 'd')
#		return "abcd"

print 'CORBA.ORB_init()'
orb = CORBA.ORB_init()
print 'f = Factory()'
f = Factory()
print 'resolve_initial_references'
poa = orb.resolve_initial_references("RootPOA")
print 'activating'
ref = f._this() # implicit activatin
print ref
print 'writing ior'
open("./test-server.ior", "w").write(orb.object_to_string(ref))

o = CORBA.ORB_init()
manager = poa.the_POAManager()
print manager
print dir (manager)
manager.activate ()
orb.run()
