/*
* Hasan Eskandari - 9330430
* 1393/12/09
* Grid Computing - Exercise #1
* Dar in barname information static va dynamic system b shekle local
* daryaft va darone databse MySQL zakhire mishavad
* information dar 2 table TBL_NODE va TBL_DYNAMIC_INFORMATION zakhire mishavad
*/
package es.GIS.server;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.rmi.RMISecurityManager;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.sql.*;
import java.util.*;
import org.hyperic.sigar.*;
import es.GIS.inter.Constant;
public class Server_JOB extends TimerTask {
static Timer timer1 = new Timer(); //define timer to do the task every time intervals seconds
private static int count = 0;
static int simtime,timeintervals; //define variables
static DBHandler dbh=new DBHandler();
static Useful_Functions func=new Useful_Functions();
static Sigar sigar=new Sigar();
public static void main(String args[]) throws Exception {
func.clear_scr();
func.print_student_info();
/*String host=InetAddress.getLocalHost().toString();
String hostname=InetAddress.getByName(host).getHostAddress();
System.setProperty("java.rmi.server.hostname", host);*/
RemoteIMPL impl = new RemoteIMPL();
System.setProperty("java.security.policy", "client.policy");
if (System.getSecurityManager()==null)
System.setSecurityManager(new RMISecurityManager());
Registry registry = LocateRegistry.createRegistry(Constant.PORT_NUM);
registry.bind("rmi://"+InetAddress.getLocalHost().getHostAddress().toString()+":1099/GISInterface",impl);
System.out.println("Server starts ...");
Scanner reader = new Scanner(System.in); //variable to get input
func.prt("Enter simtime in minutes: ");
simtime=reader.nextInt(); //get simulation time from user
func.prt("Enter time intervals in seconds: ");
timeintervals=reader.nextInt(); //get time intervals from user
RemoteIMPL.Interval=timeintervals;
RemoteIMPL.SimTime=simtime;
DBHandler.create_db();
DBHandler.create_tbl();
DBHandler.node_register(InetAddress.getLocalHost().getHostAddress().toString(), InetAddress.getLocalHost().getHostName(), System.getProperty("os.name"),
System.getProperty("os.version"), System.getProperty("os.arch"), Runtime.getRuntime().availableProcessors(),
Runtime.getRuntime().maxMemory(), Runtime.getRuntime().totalMemory(), Runtime.getRuntime().freeMemory());
timer1.schedule(new Server_JOB(), 0, timeintervals*1000);
int choice = 1;
int mem_cpu_choice = 1;
int min_max_avg_choice = 1;
int k_minutes = 1;
String ip_addr;
while(choice != 2)
{
func.clear_scr();
func.prt("\nSelect one of below choices :\n1. Submit a query\n2.Exit\n ");
choice=reader.nextInt(); //get next choice
if (choice == 2)
{
func.clear_scr();
System.exit(0);
}
else if (choice == 1)
{
func.clear_scr();
func.prt("\n\tEnter IP Address : ");
ip_addr=reader.next(); //get next choice for cpu or memory queries
while(mem_cpu_choice != 3)
{
func.clear_scr();
func.prt("\n\tAvailable queries, select one from the list: \n\t1.Cpu queries\n\t2.Memory queries\n\t3.Exit");
mem_cpu_choice=reader.nextInt(); //get next choice for cpu or memory queries
if (mem_cpu_choice == 1) //selecting cpu queries
{
while(min_max_avg_choice != 4)
{
func.clear_scr();
func.prt("\n\t\tAvailable queries, select one from the list: \n\t\t1.Max\n\t\t2.Min\n\t\t3.AVG\n\t\t4.Exit");
min_max_avg_choice=reader.nextInt(); //get next choice for min,max or avg queries
if (min_max_avg_choice == 1) //select max of cpu
{
func.clear_scr();
func.prt("\n\t\t\tEnter time (minute) to calculate the query : ");
k_minutes=reader.nextInt(); //get k (minute)
func.prt("Your answer is : " + DBHandler.query_answer("cpu", "max", k_minutes, ip_addr));
}
else if (min_max_avg_choice == 2) //select min of cpu
{
func.clear_scr();
func.prt("\n\t\t\tEnter time (minute) to calculate the query : ");
k_minutes=reader.nextInt(); //get k (minute)
func.prt("Your answer is : " + DBHandler.query_answer("cpu", "min", k_minutes, ip_addr));
}
else if (min_max_avg_choice == 3) //select avg of cpu
{
func.clear_scr();
func.prt("\n\t\t\tEnter time (minute) to calculate the query : ");
k_minutes=reader.nextInt(); //get k (minute)
func.prt("Your answer is : " + DBHandler.query_answer("cpu", "avg", k_minutes, ip_addr));
}
else if (min_max_avg_choice == 4) //exit to cpu and memory selection
{
break;
}
else
{
func.prt("\nInvalid selection ...\n");
}
}
}
else if (mem_cpu_choice == 2) //selecting memory queries
{
while(min_max_avg_choice != 4)
{
func.clear_scr();
func.prt("\n\t\tAvailable queries, select one from the list: \n\t\t1.Max\n\t\t2.Min\n\t\t3.AVG\n\t\t4.Exit");
min_max_avg_choice=reader.nextInt(); //get next choice for min,max or avg queries
if (min_max_avg_choice == 1) //select max of memory
{
func.clear_scr();
func.prt("\n\t\t\tEnter time (minute) to calculate the query : ");
k_minutes=reader.nextInt(); //get k (minute)
func.prt("Your answer is : " + DBHandler.query_answer("mem", "max", k_minutes, ip_addr));
}
else if (min_max_avg_choice == 2) //select min of memory
{
func.clear_scr();
func.prt("\n\t\t\tEnter time (minute) to calculate the query : ");
k_minutes=reader.nextInt(); //get k (minute)
func.prt("Your answer is : " + DBHandler.query_answer("mem", "min", k_minutes, ip_addr));
}
else if (min_max_avg_choice == 3) //select avg of memory
{
func.clear_scr();
func.prt("\n\t\t\tEnter time (minute) to calculate the query : ");
k_minutes=reader.nextInt(); //get k (minute)
func.prt("Your answer is : " + DBHandler.query_answer("mem", "avg", k_minutes, ip_addr));
}
else if (min_max_avg_choice == 4) //exit to cpu and memory selection
{
break;
}
else
{
func.prt("\nInvalid selection ...\n");
}
}
}
else if (mem_cpu_choice == 3)
{
break;
}
else
{
func.prt("\nInvalid selection ...\n");
}
}
}
else
{
func.prt("\nInvalid selection ...\n");
}
}
// end main
}
public void run() {
// function for timer
count++;
if (count > (simtime*60)/timeintervals) { //check for end timer's function
timer1.cancel();
timer1.purge();
return;
}
try {
DBHandler.dynamic_info(InetAddress.getLocalHost().getHostAddress().toString(), sigar.getCpuPerc().getUser(), sigar.getMem().getUsedPercent()); //save dynamic info periodically
}
catch (SigarException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
评论0