Example of Using a Shared Object
\The Sandbox \PB History & Future \New In PB6 \Shared Objects \Shared Object Example
Shared Objects
Execution
Shared Object
Example

Description

The example below illustrates the use of a shared object to retrieve database information. The server application uses a timer to perform retrieval operations against the database at regular intervals. The result set is stored in an instance variable of the shared object so that client applications can get to the data without having to access the database directly. To get the data, the client application calls a user object function that returns the data stored in the object's instance variable.

Shared object definition on the server

The server application has an object called uo_customers that operates as a shared object.

Instance variables

The uo_customers object has the following instance variables:

    datastore ids_datastore
    uo_timer iuo_mytimer

The ids_datastore variable keeps track of customer data retrieved from the database.

Constructor

The Constructor event for the uo_customers object first connects to the database and creates the datastore that will be used to for database retrieval. Then the Constructor creates an instance of the user object uo_timer, which is inherited from the Timing object, and calls the Start function. The Start function activates the timer with an interval of 300 seconds so that database retrieval operations are performed at 5-minute intervals. The last line of the Constructor passes a reference to the uo_customers object to the timer object.

    SQLCA.DBMS = "ODBC"
    SQLCA.Database = "Powersoft Demo DB V6"
    SQLCA.AutoCommit = False
    SQLCA.DBParm = "ConnectString='DSN=Powersoft Demo DB V6;UID=dba;PWD=sql'"
    connect using sqlca;

    if sqlca.sqlcode = 0 then
    MessageBox ("Connect to Database Successful", sqlca.sqlerrtext)
    end if

    ids_datastore = create datastore
    ids_datastore.dataobject = "d_customer"
    ids_datastore.SetTransObject (SQLCA)

    iuo_mytimer = CREATE uo_timer
    iuo_mytimer.Start(300)
    iuo_mytimer.PassObject(this)

refresh_custlist()

The refresh_custlist() function retrieves a list of customers and returns the number of rows retrieved. The function has no arguments.

Here is the script:

    long ll_rowcount

    ll_rowcount = ids_datastore.Retrieve()
    return ll_rowcount

retrieve_custlist()

The retrieve_custlist() function returns the customer data in the ids_datastore instance variable. The function has no arguments.

Here is the script:

    s_customers custdata
    custdata.customers = ids_datastore.object.data

    return custdata

Destructor

The Destructor event for the uo_customers object stops the timer and disconnects from the database:

    iuo_mytimer.Stop()
    disconnect using sqlca;

Timing object definition

The uo_timer object is a standard class user object that inherits from the Timing object. The object's Timer event has a script that retrieves a list of customers from the database by calling the refresh_custlist function of the shared object.

Instance variables

The uo_timer object has an instance variable that provides a reference to the shared object uo_customers.

    uo_customers iuo_shared_object

PassObject function

The PassObject() function caches the shared object reference in the iuo_shared_object instance variable. The function takes the argument auo_customers, which is of type uo_customers. The function returns an integer.

Here is the script:

    iuo_shared_object = auo_customers
    return 1

Timer event

The Timer event calls the refresh_custlist function by referencing the shared object.

    long rowcount
    rowcount = iuo_shared_object.refresh_custlist()

Remote object definition on the server

The server application has an object called uo_custdata that provides an interface between the client application and the shared object on the server.

Instance variables

The uo_custdata object has an instance variable that provides an object reference to the shared object uo_customers.

    uo_customers iuo_shared_object

Constructor

The Constructor event for the uo_custdata object gets a reference to the shared object instance for uo_customers.

    SharedObjectGet("share1",iuo_shared_object)

retrieve_custlist()

The retrieve_custlist() function calls the corresponding function in the shared object uo_customers. The function retrieves the structure that contains the list of customers retrieved from the database. The function has no arguments.

Here is the script:

    s_customers custdata
    custdata = iuo_shared_object.retrieve_custlist()
    return custdata

Main window on the server

At start up time, the server application starts the listener and displays a window that provides buttons for registering and unregistering a shared object.

Register button

The Register button registers uo_customers as a shared object with the name share1:

    result = SharedObjectRegister("uo_customers","share1")

Unregister button

The Unregister button unregisters share1:

    SharedObjectUnRegister("share1")

Client application window

The client application has a window that contains a DataWindow control that allows the user to display customer data. When the user presses the Retrieve button, the client connects to the server application to get the data. Once the data is retrieved, it is displayed in the DataWindow control in the client window.

Retrieve button

The Retrieve button in the client window invokes the retrieve_custlist function by referencing the uo_custdata object.

    s_customers custdata

    connection myconnect
    myconnect = create connection
    myconnect.driver = "Winsock"
    myconnect.application = "pbserver"
    myconnect.location = "server01"
    myconnect.ConnectToServer()

    uo_custdata iuo_custdata

    myconnect.CreateInstance(iuo_custdata)
    custdata = iuo_custdata.retrieve_custlist()
    dw_1.object.data = custdata.customers

PBL Peeper PB Help PB History
& Future About Us Feedback Site Map

Google
 
Web www.techno-kitten.com
www.sybase.com