/*
 * 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
 * @author Philippe Durieux
 */
public class A_thread extends Thread {
    String managerName;
    String name;
    int accounts;
    int delay;
    int loops;
    int amount;
    Context ictx; 
    int checklevel;

    public A_thread(String mName, int num, Context ictx, int accounts, int delay, int loops, int amount, int checklevel) {
        managerName = mName;
        name = managerName + "_manac_" + num;
        setName(name);
        this.ictx = ictx;
        this.accounts = accounts;
        this.delay = delay;
        this.loops = loops;
        this.amount = amount;
        this.checklevel = checklevel;
    }

    /**
     * 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(A_manac.initialValue);
        } catch (Exception e) {
            System.out.println("Cannot Create Session:" + e);
            return;
        }

        try {
            // Set delay and value to move.
            if (delay > 0) {
                mgr.setDelay(delay);
            }
            if (amount > 0) {
                mgr.setValue(amount);
            }

            // main loop
            boolean ok = true;
            for (int i = 0; i < loops; i++) {
                // Choose the 4 accounts randomly
                int d1 = random(accounts);
                int d2 = random(accounts);
                int c1 = random(accounts);
                int c2 = random(accounts);
                if (amount < 0) {
                    // make a simple withdraw.
                    try {
                        mgr.withdraw(d1, c1, -amount);
                    } catch (RemoteException r) {
                        System.out.println("Exception (ignored) raised on withdraw:" + r);
                    }
                } else {
                    // Set these accounts in session bean
                    try {
                        mgr.setAccounts(d1, d2, c1, c2);
                    } catch (RemoteException r) {
                        System.out.println("Bad Account Setting:" + r);
                        A_manac.threadfail = true;
                        break;
                    }
                    // make the transfert
                    try {
                        mgr.movement();
                    } catch (RemoteException r) {
                        // movement raised exception. ignoring...
                        System.out.println("Exception (ignored) raised on movement:" + r);
                    }
                }
                if (checklevel >= 2) {
                    // Check accounts that were debited
                    if (! mgr.checkAccount(d1)) {
                        System.out.println("Bad Account after move on account " + d1);
                        ok = false;
                    }
                    if (! mgr.checkAccount(d2)) {
                        System.out.println("Bad Account after move on account " + d2);
                        ok = false;
                    }
                    if (!ok) {
                        System.out.println("Stopping this session because some accounts are not ok");
                        A_manac.threadfail = true;
                        break;
                    }
                }
            }
            // delete Session
            mgr.remove();
        } catch (RemoteException e) {
            System.out.println("Thread " + name + " : " + e);
            A_manac.threadfail = true;
        } catch (RemoveException e) {
            System.out.println("Thread " + name + " Cannot remove session: " + e);
            A_manac.threadfail = true;
        }
    }
}
