# Collection of code snippets by Arne Vajhøj 
# (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import sys
import time

sys.path.append('C:\\DivNative\\64bit\\omniORBpy-4.2.2-2.7\\lib\\python')
sys.path.append('C:\\DivNative\\64bit\\omniORBpy-4.2.2-2.7\\lib\\x86_win32')

from omniORB import CORBA
import CosNaming

import corbatest.common

def getStub(orb, name):
    nc = orb.resolve_initial_references('NameService')._narrow(CosNaming.NamingContext)
    n = [CosNaming.NameComponent(name, '')]
    return nc.resolve(n)._narrow(corbatest.common.Test)

def testFunctional(orb, name):
    tst = getStub(orb, name)
    a = 123
    b = 456
    c = tst.add(a, b)
    print(str(c))
    s = 'ABC'
    s2 = tst.dup(s)
    print(s2)
    d = corbatest.common.Data(123, 'ABC')
    d2 = tst.process(d)
    print('%d %s' % (d2.iv,d2.sv))

def testInstantiation(orb, name):
    tst1 = getStub(orb, name)
    for i in range(0, 2):
        n = tst1.getCounter()
        print(str(n))
    tst2 = getStub(orb, name)
    for i in range(0, 2):
        n = tst2.getCounter()
        print(str(n))

def testPerformance(orb, name):
    tst = getStub(orb, name)
    REP = 10000
    t1 = time.time()
    for i in range(0, REP):
        tst.noop()
    t2 = time.time()
    print('%d requests per second' % (REP / (t2 - t1)))

def test(lbl, ns, name):
    print(lbl + ':')
    orb = CORBA.ORB_init(['-ORBInitRef', ns], CORBA.ORB_ID)
    testFunctional(orb, name)
    testInstantiation(orb, name)
    testPerformance(orb, name)
    orb.destroy()

test('Java SE ORB and Java Server (old style)', 'NameService=corbaname::localhost:1050', 'TestJ1')
test('Java SE ORB and Java Server (new style)', 'NameService=corbaname::localhost:1050', 'TestJ2')
test('IIOP.NET ORB and .NET Server', 'NameService=corbaname::localhost:12345', 'TestN')
test('OmniORB ORB and C++ Server (old style)', 'NameService=corbaname::localhost:2809', 'TestC1')
test('OmniORB ORB and C++ Server (new style)', 'NameService=corbaname::localhost:2809', 'TestC2')
test('OmniORB ORB and Python Server', 'NameService=corbaname::localhost:2809', 'TestPy')
