DistSimHelloWorld

From
Revision as of 17:02, 16 February 2007 by Uhermann (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
//////////////////////////////////////////////////
// JIST (Java In Simulation Time) Project
//

// Copyright (C) 2004 by Cornell University
// All rights reserved.
// Refer to LICENSE for terms and conditions of use.

package jist.minisim;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Properties;
import java.util.Random;

import jist.runtime.JistAPI;
import or_mapper.util.DBSaver;

class Result {
	Result(int counter, String greeting) {
		this.counter = counter;
		this.greeting = greeting;
	}
	int counter;
	String greeting;
}

/**
 * Hello World with distsim.
 * 
 * @author Rimon Barr <barr+jist@cs.cornell.edu> and Ulf Hermann
 * @version $Id: hello.java,v 1.7 2004/06/09 18:54:16 barr Exp $
 * @since JIST1.0
 */
public class DistsimTest implements JistAPI.Entity {

	private Random random;

	private String greeting;

	private DBSaver saver;
	
	private int counter;

	/**
	 * Hello event.
	 */
	public void myEvent() {

		counter = counter + 1;
		
		
		try {
			if (counter < 2) {
				saver.save(new Result(counter, greeting), "distsim-test");
			} else
				saver.save(new Result(counter, greeting), "quicksim");
			int pos = random.nextInt(greeting.length());
			greeting = greeting.replace(greeting.charAt(pos), (char) random
					.nextInt(128));
			System.out.println("distsim mutters: " + counter);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (counter < 10)
			JistAPI.sleep(1);
		else {
			try {
				saver.finalize();
			} catch (Throwable e1) {
				e1.printStackTrace();
			}
			return;
		}
		myEvent();

		// delay execution

		/*try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
		}*/

	}

	/**
	 * Program entry point: show difference between Java and JiST execution
	 * models with a "hello world!" example.
	 * 
	 * @param args
	 *            command-line parameters
	 */
	public static void main(String[] args) {
		System.out.println("starting simulation.");
		DistsimTest t = new DistsimTest();
		t.myEvent();
	}

	public DistsimTest() {
		try {

			random = new Random();
			greeting = "Hello world!";
			Properties simulationProperties = new Properties();

			simulationProperties.load(System.in);
			saver = new DBSaver("jdbc:mysql://"
					+ simulationProperties.getProperty("results.host") + "/"
					+ simulationProperties.getProperty("results.database"),
					simulationProperties.getProperty("results.username"),
					simulationProperties.getProperty("results.password"),
					Integer.parseInt(simulationProperties
							.getProperty("simulationId")));
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

} // class: hello