#! /usr/bin/python
#
# Sample CORBA server -- Python version
#
# Roland Mas <99.roland.mas@gna.org>

import CORBA

CORBA._load_idl ("sample.idl")

import Module_1, Module_1__POA
import Module_2, Module_2__POA

class Sample_interface (Module_1__POA.Sample_interface):
    def __init__ (self):
        self.int = 0
        self.str = ""
    def incr_int (self):
        print "Entered incr_int"
        self.int = self.int + 1
    def change_str (self, str):
        print "Entered change_str"
        if len (str) > 20:
            ex = Module_1.Sample_exception
            raise Module_1.Sample_exception, ex
        self.str = str
    def str_length (self):
        print "Entered str_length"
        return len (self.str)

class Factory (Module_1__POA.Factory):
    def new_sample (self, int, str):
        global poa
        print "Entered new_sample"
        i = Sample_interface ()
        i.int = int
        i.str = str
        poa.activate_object (i)
        ref = poa.servant_to_reference (i)
        return ref
        
class Stack (Module_2__POA.Stack):
    def __init__ (self):
        self._list = []
    def _get_depth (self): # This only *looks* like an attribute (cf. IDL)
        print "Entered _get_depth"
        return len (self._list)
    def pop (self):
        print "Entered pop"
        if len (self._list) <= 0:
            ex = Module_1.Sample_exception
            raise Module_1.Sample_exception, ex
        return self._list.pop ()
    def push (self, int):
        print "Entered push"
        self._list.append (int)

orb = CORBA.ORB_init ((), CORBA.ORB_ID)
poa = orb.resolve_initial_references("RootPOA")

f = Factory ()
poa.activate_object (f)
ref = poa.servant_to_reference (f)
open ("factory.ior", "w").write (orb.object_to_string (ref))

p = Stack ()
poa.activate_object (p)
ref = poa.servant_to_reference (p)
open ("stack.ior", "w").write (orb.object_to_string (ref))

poa._get_the_POAManager ().activate ()
print "Entering mainloop"
orb.run ()
