/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 Bull S.A.
 * Contact: jonas-team@objectweb.org
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.stests.manac;

import java.rmi.RemoteException;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.rmi.PortableRemoteObject;


/**
 * Stress Test with read only operations
 * @author Philippe Durieux
 */
public class A_readthread extends Thread {
    String managerName;
    String name;
    int accounts;
    int loops;
    Context ictx; 

    public A_readthread(String mName, int num, Context ictx, int accounts, int loops) {
        managerName = mName;
        name = managerName + "_readac_" + num;
        setName(name);
        this.ictx = ictx;
        this.accounts = accounts;
        this.loops = loops;
    }

    /**
     * random returns an integer between 0 and max - 1
     */
    private int random(int max) throws RemoteException {
        double d = Math.random();
        int ret = (int) (max * d);
        return ret;
    }

    public void run() {

        // Create a session bean
        ManagerHome mgrHome = null;
        Manager mgr = null;
        try {
            mgrHome = (ManagerHome) PortableRemoteObject.narrow(ictx.lookup(managerName), ManagerHome.class);
            mgr = mgrHome.create(F_read.initialValue);
        } catch (Exception e) {
            System.out.println("Cannot Create Session:" + e);
            return;
        }

        try {

            // Choose the 4 accounts randomly
            int d1 = random(accounts);
            int d2 = random(accounts);
            int c1 = random(accounts);
            int c2 = random(accounts);
            // Set these accounts in session bean
            mgr.setAccounts(d1, d2, c1, c2);

            // main loop
            boolean ok = true;
            for (int i = 0; i < loops; i++) {
                int minval = mgr.readBalances();
                if (minval < 0) {
                    System.out.println("Bad read value " + minval);
                    F_read.threadfail = true;
                    break;
                }
            }
            // delete Session
            mgr.remove();
        } catch (RemoteException e) {
            System.out.println("Thread " + name + " : " + e);
            F_read.threadfail = true;
        } catch (RemoveException e) {
            System.out.println("Thread " + name + " Cannot remove session: " + e);
            F_read.threadfail = true;
        }
    }
}
