logo logo logo logo http://netkiller.mefound.com/
http://netkiller.mooo.com/
http://netkiller.8800.org/

Hello CQ DE BG7NYT - I enjoy Outdoor,Photography and Amateur Radio

版权声明:你可以任意转载,转载时请务必标明文章原始出处和作者信息及本声明。

Fnorb



学习笔记

学习笔记

 

下载:Fnorb-1.3.tar.gz

http://heanet.dl.sourceforge.net/sourceforge/fnorb/Fnorb-1.3.tar.gz

[root@redhat8 root]# wget http://heanet.dl.sourceforge.net/sourceforge/fnorb/Fnorb-1.3.tar.gz

解包:

[root@redhat8 root]# tar zxvf Fnorb-1.3.tar.gz

安装:

[root@redhat8 root]# cd Fnorb-1.3

[root@redhat8 Fnorb-1.3]#

[root@redhat8 Fnorb-1.3]# python setup.py install

测试环境:

[root@redhat8 Fnorb-1.3]# cd examples/

[root@redhat8 examples]# ls

hello-world  misc  naming  threaded  tkinter  unions

[root@redhat8 examples]# cd hello-world/

[root@redhat8 hello-world]# ls

client.py  HelloWorld.idl  README  server.py

[root@redhat8 hello-world]#

HelloWorld.idl

[root@redhat8 hello-world]# cat HelloWorld.idl

//---------------------------------------------------------------------------

// Copyright (C) DSTC Pty Ltd (ACN 052 372 577) 1997, 1998, 1999

// All Rights Reserved.

//

// The software contained on this media is the property of the DSTC Pty

// Ltd.  Use of this software is strictly in accordance with the

// license agreement in the accompanying LICENSE.HTML file.  If your

// distribution of this software does not contain a LICENSE.HTML file

// then you have no rights to use this software in any manner and

// should contact DSTC at the address below to determine an appropriate

// licensing arrangement.

//

//      DSTC Pty Ltd

//      Level 7, GP South

//      Staff House Road

//      University of Queensland

//      St Lucia, 4072

//      Australia

//      Tel: +61 7 3365 4310

//      Fax: +61 7 3365 4311

//      Email: enquiries@dstc.edu.au

//

// This software is being provided "AS IS" without warranty of any

// kind.  In no event shall DSTC Pty Ltd be liable for damage of any

// kind arising out of or in connection with the use or performance of

// this software.

//

// Project:      Fnorb

// File:         $Source: /cvsroot/fnorb/fnorb/examples/hello-world/HelloWorld.idl,v $

// Version:      @(#)$RCSfile: HelloWorld.idl,v $ $Revision: 1.5 $

//

//---------------------------------------------------------------------------

 

#pragma prefix "dstc.edu.au"

 

//

// "Hello World" example!

//

module HelloWorld {

 

    const string Message = "Hello CORBA World!";

 

    interface HelloWorldIF {

        //

        // The IDL version of the old faithful, 'Hello World' example!

        //

        // This operation takes no parameters and returns a string.

        //

        string hello_world();

    };

};

[root@redhat8 hello-world]#

server.py

[root@redhat8 hello-world]# cat server.py

#!/usr/bin/env python

#############################################################################

# Copyright (C) DSTC Pty Ltd (ACN 052 372 577) 1997, 1998, 1999, 2000

# All Rights Reserved.

#

# The software contained on this media is the property of the DSTC Pty

# Ltd.  Use of this software is strictly in accordance with the

# license agreement in the accompanying LICENSE.HTML file.  If your

# distribution of this software does not contain a LICENSE.HTML file

# then you have no rights to use this software in any manner and

# should contact DSTC at the address below to determine an appropriate

# licensing arrangement.

#

#      DSTC Pty Ltd

#      Level 7, GP South

#      Staff House Road

#      University of Queensland

#      St Lucia, 4072

#      Australia

#      Tel: +61 7 3365 4310

#      Fax: +61 7 3365 4311

#      Email: enquiries@dstc.edu.au

#

# This software is being provided "AS IS" without warranty of any

# kind.  In no event shall DSTC Pty Ltd be liable for damage of any

# kind arising out of or in connection with the use or performance of

# this software.

#

# Project:      Fnorb

# File:         $Source: /cvsroot/fnorb/fnorb/examples/hello-world/server.py,v $

# Version:      @(#)$RCSfile: server.py,v $ $Revision: 1.8 $

#

#############################################################################

""" Implementation of the HelloWorldIF interface. """

 

 

# Standard/built-in modules.

import sys

 

# Fnorb modules.

from Fnorb.orb import BOA, CORBA

 

# Stubs and skeletons generated by 'fnidl'.

import HelloWorld, HelloWorld_skel

 

 

class HelloWorldServer(HelloWorld_skel.HelloWorldIF_skel):

    """ Implementation of the 'HelloWorldIF' interface. """

 

    def hello_world(self):

        print HelloWorld.Message

        return HelloWorld.Message

 

#############################################################################

 

def main(argv):

    """ Do it! """

 

    print 'Initialising the ORB...'

 

    # Initialise the ORB.

    orb = CORBA.ORB_init(argv, CORBA.ORB_ID)

 

    print 'Initialising the BOA...'

 

    # Initialise the BOA.

    boa = BOA.BOA_init(argv, BOA.BOA_ID)

 

    print 'Creating object reference...'

 

    # Create an object reference ('fred' is the object key).

    obj = boa.create('fred', HelloWorldServer._FNORB_ID)

 

    print 'Creating implementation...'

 

    # Create an instance of the implementation class.

    impl = HelloWorldServer()

 

    print 'Activating the implementation...'

 

    # Activate the implementation (ie. connect the generated object reference

    # to the implementation).  Note that the implementation will not receive

    # any operation requests until we start the event loop (see below).

    boa.obj_is_ready(obj, impl)

 

    # Write the stringified object reference to a file (this is just a 'cheap

    # and cheerful' way of making the object reference available to the

    # client!).

    f = open('Server.ref', 'w')

    f.write(orb.object_to_string(obj))

    f.flush()

 

    print 'Server created and accepting requests...'

 

    # Start the event loop.

    boa._fnorb_mainloop()

 

    return 0

 

#############################################################################

 

if __name__ == '__main__':

    # Do it!

    try:

        sys.exit(main(sys.argv))

    except KeyboardInterrupt:

        print

        print "Server done"

#############################################################################

[root@redhat8 hello-world]#

client.py

[root@redhat8 hello-world]# cat client.py

#!/usr/bin/env python

#############################################################################

# Copyright (C) DSTC Pty Ltd (ACN 052 372 577) 1997, 1998, 1999, 2000

# All Rights Reserved.

#

# The software contained on this media is the property of the DSTC Pty

# Ltd.  Use of this software is strictly in accordance with the

# license agreement in the accompanying LICENSE.HTML file.  If your

# distribution of this software does not contain a LICENSE.HTML file

# then you have no rights to use this software in any manner and

# should contact DSTC at the address below to determine an appropriate

# licensing arrangement.

#

#      DSTC Pty Ltd

#      Level 7, GP South

#      Staff House Road

#      University of Queensland

#      St Lucia, 4072

#      Australia

#      Tel: +61 7 3365 4310

#      Fax: +61 7 3365 4311

#      Email: enquiries@dstc.edu.au

#

# This software is being provided "AS IS" without warranty of any

# kind.  In no event shall DSTC Pty Ltd be liable for damage of any

# kind arising out of or in connection with the use or performance of

# this software.

#

# Project:      Fnorb

# File:         $Source: /cvsroot/fnorb/fnorb/examples/hello-world/client.py,v $

# Version:      @(#)$RCSfile: client.py,v $ $Revision: 1.8 $

#

#############################################################################

""" Client for the "Hello World" example. """

 

 

# Standard/built-in modules.

import sys

 

# Fnorb modules.

from Fnorb.orb import CORBA

 

# Stubs generated by 'fnidl'.

import HelloWorld

 

 

def main(argv):

    """ Do it! """

 

    print 'Initialising the ORB...'

 

    # Initialise the ORB.

    orb = CORBA.ORB_init(argv, CORBA.ORB_ID)

 

    # Read the server's stringified IOR from a file (this is just a 'cheap and

    # cheerful' way of locating the server - in practise the client would use

    # the naming or trader services).

    stringified_ior = open('Server.ref').read()

 

    # Convert the stringified IOR into an active object reference.

    server = orb.string_to_object(stringified_ior)

 

    # Make sure that the server is not a 'nil object reference' (represented

    # in Python by the value 'None').

    if server is None:

        raise 'Nil object reference!'

 

    # Make sure that the object implements the expected interface!

    if not server._is_a('IDL:dstc.edu.au/HelloWorld/HelloWorldIF:1.0'):

        raise 'This is not a "HelloWorldIF" server!'

 

    # Call the server!

    print server.hello_world()

 

    return 0

 

#############################################################################

 

if __name__ == '__main__':

    # Do it!

    sys.exit(main(sys.argv))

 

#############################################################################

[root@redhat8 hello-world]#

README

[root@redhat8 hello-world]# cat README

Fnorb ``Hello World'' example.

 

The files for this example are located in the examples/hello-world

directory, the following instructions assume that you are in that

directory and have a working Fnorb installation.

 

1. Create the Stub and Skeleton Modules

   Type in:

   $ fnidl HelloWorld.idl

 

2. Start the Server

   Type in:

   $ python server.py

 

3. Run the Client

   Open up another window and type:

   $ python client.py

 

To shutdown the server, simply bring the server window to focus and press control-C.

 

[root@redhat8 hello-world]#

 

[root@redhat8 hello-world]# fnidl HelloWorld.idl

[root@redhat8 hello-world]# ls

client.py  HelloWorld  HelloWorld.idl  HelloWorld_skel  README  server.py

[root@redhat8 hello-world]# ls HelloWorld

__init__.py  __init__.pyc

[root@redhat8 hello-world]# ls HelloWorld_skel/

__init__.py  __init__.pyc

[root@redhat8 hello-world]#

 

测试:

启动server.py

[root@redhat8 hello-world]# python server.py

Initialising the ORB...

Initialising the BOA...

Creating object reference...

Creating implementation...

Activating the implementation...

Server created and accepting requests...

Client.py

[root@redhat8 hello-world]# python client.py

Initialising the ORB...

Hello CORBA World!

[root@redhat8 hello-world]#

 

 

 

 

开发环境:

[root@redhat8 root]# mkdir python

[root@redhat8 root]# cd python/

[root@redhat8 python]#

netkiller.idl

[root@redhat8 python]# vi netkiller.idl

#pragma prefix "9812.net"

 

module netkiller {

    const string Message = "Hello CORBA World!";

    interface netkillerif {

        string getMessage();

        string read_file(in string filename);

        void insert(in string name, in string sex, in string age);

    };

};

 

编译netkiller.idl (这个操作会很慢,要等一会)

[root@redhat8 python]# fnidl netkiller.idl

[root@redhat8 python]# ls

netkiller  netkiller.idl  netkiller_skel

[root@redhat8 python]#

 

server.py

[root@redhat8 python]# vi server.py

[root@redhat8 python]# cat server.py

#!/usr/bin/python

import sys

from Fnorb.orb import BOA, CORBA

import netkiller, netkiller_skel

 

class NetkillerServer(netkiller_skel.netkillerif_skel):

    def getMessage(self):

        return netkiller.Message

 

    def read_file(self,filename):

        buffer = open(filename,'r').read()

        return buffer

 

#########################################################

 

def main(argv):

    print 'Initialising the ORB...'

    orb = CORBA.ORB_init(argv, CORBA.ORB_ID)

    print 'Initialising the BOA...'

    boa = BOA.BOA_init(argv, BOA.BOA_ID)

    print 'Creating object reference...'

    obj = boa.create('fred', NetkillerServer._FNORB_ID)

    print 'Creating implementation...'

    impl = NetkillerServer()

    print 'Activating the implementation...'

    boa.obj_is_ready(obj, impl)

    f = open('Server.ref', 'w')

    f.write(orb.object_to_string(obj))

    f.flush()

 

    print 'Server created and accepting requests...'

    boa._fnorb_mainloop()

 

    return 0

 

#########################################################

 

if __name__ == '__main__':

    try:

        sys.exit(main(sys.argv))

    except KeyboardInterrupt:

        print

        print "Server done"

#############################################################################

 

[root@redhat8 python]#

 

[root@redhat8 python]# chmod 700 client.py server.py

 

client.py

[root@redhat8 python]# cat client.py

#!/usr/bin/python

 

import sys

from Fnorb.orb import CORBA

import netkiller

 

def main(argv):

    print 'Initialising the ORB...'

    orb = CORBA.ORB_init(argv, CORBA.ORB_ID)

    stringified_ior = open('Server.ref').read()

    server = orb.string_to_object(stringified_ior)

    if server is None:

        raise 'Nil object reference!'

    if not server._is_a('IDL:9812.net/netkiller/netkillerif:1.0'):

        raise 'This is not a "netkillerif" server'

    print server.getMessage()

    print server.read_file('Server.ref')

    print server.read_file('/etc/passwd')

 

    return 0

 

##################################################################

 

if __name__ == '__main__':

    sys.exit(main(sys.argv))

 

##################################################################

[root@redhat8 python]#

 

启动server.py

[root@redhat8 python]# ./server.py

Initialising the ORB...

Initialising the BOA...

Creating object reference...

Creating implementation...

Activating the implementation...

Server created and accepting requests...

 

关闭server.py (Crtl + C)

[root@redhat8 python]# ./server.py

Initialising the ORB...

Initialising the BOA...

Creating object reference...

Creating implementation...

Activating the implementation...

Server created and accepting requests...

 

Server done

[root@redhat8 python]#

 

运行客户端测试

[root@redhat8 python]# ./client.py

Initialising the ORB...

Hello CORBA World!

IOR:000000000000002749444C3A393831322E6E65742F6E65746B696C6C65722F6E65746B696C6C657269663A312E300000000000010000000000000048000102000000000872656468617438000431000000000004667265640000000100000001000000200000000000010020000000010001000100010109000000020501000100010100

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

daemon:x:2:2:daemon:/sbin:/sbin/nologin

adm:x:3:4:adm:/var/adm:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

sync:x:5:0:sync:/sbin:/bin/sync

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

halt:x:7:0:halt:/sbin:/sbin/halt

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

news:x:9:13:news:/etc/news:

uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

games:x:12:100:games:/usr/games:/sbin/nologin

gopher:x:13:30:gopher:/var/gopher:/sbin/nologin

ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

nobody:x:99:99:Nobody:/:/sbin/nologin

ntp:x:38:38::/etc/ntp:/sbin/nologin

rpc:x:32:32:Portmapper RPC user:/:/sbin/nologin

vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin

nscd:x:28:28:NSCD Daemon:/:/sbin/nologin

sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

rpm:x:37:37::/var/lib/rpm:/bin/bash

mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin

smmsp:x:51:51::/var/spool/mqueue:/sbin/nologin

rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin

nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin

pcap:x:77:77::/var/arpwatch:/sbin/nologin

xfs:x:43:43:X Font Server:/etc/X11/fs:/sbin/nologin

wnn:x:49:49:Wnn System Account:/home/wnn:/bin/bash

named:x:25:25:Named:/var/named:/sbin/nologin

gdm:x:42:42::/var/gdm:/sbin/nologin

postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash

apache:x:48:48:Apache:/var/www:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

squid:x:23:23::/var/spool/squid:/dev/null

webalizer:x:67:67:Webalizer:/var/www/html/usage:/sbin/nologin

chen:x:500:500:chen:/home/chen:/bin/bash

piranha:x:60:60::/home/piranha:/dev/null

ldap:x:55:55:LDAP User:/var/lib/ldap:/bin/false

snow:x:501:26::/home/snow:/bin/bash

hacluster:x:502:502::/home/hacluster:/bin/bash

 

[root@redhat8 python]#

 

修改server.py,client.py 支持数据库。

 

Python + Corba + PostgreSQL 开发分布式组件

 

安装PostgreSQL-7.4

[root@redhat8 pgsql]# ls

postgresql-7.4-0.2PGDG.i386.rpm          postgresql-pl-7.4-0.2PGDG.i386.rpm

postgresql-contrib-7.4-0.2PGDG.i386.rpm  postgresql-python-7.4-0.2PGDG.i386.rpm

postgresql-devel-7.4-0.2PGDG.i386.rpm    postgresql-server-7.4-0.2PGDG.i386.rpm

postgresql-docs-7.4-0.2PGDG.i386.rpm     postgresql-tcl-7.4-0.2PGDG.i386.rpm

postgresql-jdbc-7.4-0.2PGDG.i386.rpm     postgresql-test-7.4-0.2PGDG.i386.rpm

postgresql-libs-7.4-0.2PGDG.i386.rpm

[root@redhat8 pgsql]# rpm -Uvh --nodeps `ls -1`

Preparing...                ########################################### [100%]

   1:postgresql-test        ########################################### [  9%]

   2:postgresql             ########################################### [ 18%]

   3:postgresql-contrib     ########################################### [ 27%]

   4:postgresql-devel       ########################################### [ 36%]

   5:postgresql-docs        ########################################### [ 45%]

   6:postgresql-jdbc        ########################################### [ 55%]

   7:postgresql-libs        ########################################### [ 64%]

   8:postgresql-pl          ########################################### [ 73%]

   9:postgresql-python      ########################################### [ 82%]

  10:postgresql-server      ########################################### [ 91%]

  11:postgresql-tcl         ########################################### [100%]

[root@redhat8 pgsql]#

 

启动数据库初始化

[root@redhat8 pgsql]# service postgresql start

Initializing database:                                     [  OK  ]

Starting postgresql service:                               [  OK  ]

[root@redhat8 pgsql]#

 

配置数据库

[root@redhat8 pgsql]# vi /var/lib/pgsql/data/postgresql.conf

#tcpip_socket = false

改为

tcpip_socket = true

bash-2.05b$ vi /var/lib/pgsql/data/pg_hba.conf

最后加入

host    all         all         127.0.0.1         255.255.255.255   md5

 

[root@redhat8 pgsql]# service postgresql restart

Stopping postgresql service:                               [  OK  ]

Starting postgresql service:                               [  OK  ]

[root@redhat8 pgsql]#

[root@redhat8 pgsql]# su postgres

bash-2.05b$ createdb

CREATE DATABASE

bash-2.05b$ psql

Welcome to psql 7.4, the PostgreSQL interactive terminal.

 

Type:  \copyright for distribution terms

       \h for help with SQL commands

       \? for help on internal slash commands

       \g or terminate with semicolon to execute query

       \q to quit

 

postgres=# CREATE USER netkiller WITH PASSWORD 'chen';

CREATE USER

postgres=# CREATE DATABASE netkiller WITH OWNER = netkiller TEMPLATE = template0 ENCODING = 'UNICODE';

CREATE DATABASE

postgres=# \q

bash-2.05b$ createlang plpgsql netkiller

bash-2.05b$ psql -h127.0.0.1 -Unetkiller netkiller

Password:

Welcome to psql 7.4, the PostgreSQL interactive terminal.

 

Type:  \copyright for distribution terms

       \h for help with SQL commands

       \? for help on internal slash commands

       \g or terminate with semicolon to execute query

       \q to quit

 

netkiller=>

 

SQL DDL

create table person(

    id Serial NOT NULL,

    name Varchar(20) NOT NULL,

    sex boolean Default false,

    age Varchar(50) NOT NULL

);

 

netkiller=> create table person(

netkiller(>     id Serial NOT NULL,

netkiller(>     name Varchar(20) NOT NULL,

netkiller(>     sex boolean Default false,

netkiller(>     age Varchar(50) NOT NULL

netkiller(> );

NOTICE:  CREATE TABLE will create implicit sequence "person_id_seq" for "serial" column "person.id"

CREATE TABLE

netkiller=>

测试

netkiller=> \d

               List of relations

 Schema |     Name      |   Type   |   Owner

--------+---------------+----------+-----------

 public | person        | table    | netkiller

 public | person_id_seq | sequence | netkiller

(2 rows)

 

netkiller=> \d person

                                  Table "public.person"

 Column |         Type          |                       Modifiers

--------+-----------------------+--------------------------------------------------------

 id     | integer               | not null default nextval('public.person_id_seq'::text)

 name   | character varying(20) | not null

 sex    | boolean               | default false

 age    | character varying(50) | not null

 

netkiller=> insert into person(name,sex,age) values('netkiller',true,'23');

INSERT 17153 1

netkiller=>

server.py

[root@redhat8 python]# cat server.py

#!/usr/bin/python

 

#############################################################

# Created    2003-12-6

# Modified   2003-12-8

# Project    PostgreSQL Converse

# Model      Create Java Entity Bean (not EJB CMP)

# Company    Organizations

# Author     netkiller

# Email      openunix@163.com

# Version    1.1

# Database PostgreSQL 7.3.4

#############################################################

 

import sys

from Fnorb.orb import BOA, CORBA

import netkiller, netkiller_skel

import pg

 

class NetkillerServer(netkiller_skel.netkillerif_skel):

    def getMessage(self):

        return netkiller.Message

 

    def read_file(self,filename):

        buffer = open(filename,'r').read()

        return buffer

 

    def insert(self,name,sex,age):

        sql = "insert into person (name,sex,age) values('"+name+"','"+sex+"','"+age+"');"

db = pg.DB(dbname='netkiller', host='127.0.0.1', user='netkiller',passwd='chen')

        db.query(sql)

        db.close()

 

#########################################################

 

def main(argv):

    print 'Initialising the ORB...'

    orb = CORBA.ORB_init(argv, CORBA.ORB_ID)

    print 'Initialising the BOA...'

    boa = BOA.BOA_init(argv, BOA.BOA_ID)

    print 'Creating object reference...'

    obj = boa.create('fred', NetkillerServer._FNORB_ID)

    print 'Creating implementation...'

    impl = NetkillerServer()

    print 'Activating the implementation...'

    boa.obj_is_ready(obj, impl)

    f = open('Server.ref', 'w')

    f.write(orb.object_to_string(obj))

    f.flush()

 

    print 'Server created and accepting requests...'

    boa._fnorb_mainloop()

 

    return 0

 

#########################################################

 

if __name__ == '__main__':

    try:

        sys.exit(main(sys.argv))

    except KeyboardInterrupt:

        print

        print "Server done"

#############################################################################

 

[root@redhat8 python]#

 

client.py

[root@redhat8 python]# cat client.py

#!/usr/bin/python

 

import sys

from Fnorb.orb import CORBA

import netkiller

 

def main(argv):

    print 'Initialising the ORB...'

    orb = CORBA.ORB_init(argv, CORBA.ORB_ID)

    stringified_ior = open('Server.ref').read()

    server = orb.string_to_object(stringified_ior)

    if server is None:

        raise 'Nil object reference!'

    if not server._is_a('IDL:9812.net/netkiller/netkillerif:1.0'):

        raise 'This is not a "netkillerif" server'

    print server.getMessage()

    print server.read_file('Server.ref')

    print server.read_file('/etc/passwd')

    server.insert('chen','true','23')

    return 0

 

##################################################################

 

if __name__ == '__main__':

    sys.exit(main(sys.argv))

 

##################################################################

[root@redhat8 python]#

 

About Us | Site Map | Privacy Policy | Contact Us
Copyright © 2002-2004 netkiller All Rights Reserved