Thursday, 29 May 2014

Finding CPU load : Client-server architecture

Task :
Design a distributed application using Socket. Application consists of a server which requests CPU loads to n clients and finds the maximum and minimum loaded systems.

To find CPU load of a single local machine, see this previous post .


Server.java :


public class Server extends Thread
{
   private ServerSocket serverSocket;
  
   public Server(int port) throws IOException
   {
      serverSocket = new ServerSocket(port);
      serverSocket.setSoTimeout(30000);
   }

   public void run()
   {
     int p1=0,p2=0;
     float max,min;
     int j=0,i;
     float a[]=new float [10];
     String clients[]=new String [10];
      while(true)
      {
         try
         {
           clients[j]="";
           String cmd="";
           float y;
            System.out.println("Port ready to connect: " + serverSocket.getLocalPort());
            Socket server = serverSocket.accept();
            System.out.println("Connected");
            DataInputStream in = new DataInputStream(server.getInputStream());
            DataOutputStream out = new DataOutputStream(server.getOutputStream());
            out.writeUTF("mpstat");
            cmd=in.readUTF();
            y=Float.parseFloat(cmd);
            y=100-y;
            clients[j]=""+server.getRemoteSocketAddress();
            a[j]=y;
            j++;
            System.out.println("CPU load:"+y);
            server.close();
         }catch(SocketTimeoutException s)
         {
            System.out.println("Socket timed out!");
            break;
         }catch(IOException e)
         {
            System.out.println(e);
            break;
         }
      }
        max=a[0];
        min=a[0];
        for(i=1; i         {
          if(max < a[i]){
            max=a[i];
            p1=i;
          }
          else if(min > a[i]){
            min=a[i];
            p2=i;
          }

       
        System.out.println();
        System.out.println("\nMax Load: "+max+" from "+clients[p1]);
        System.out.println("Min Load: "+min+" from "+clients[p2]);
      }
   }
   public static void main(String [] args)
   {
      int port = 5555;
      try
      {
         Thread t = new Server(port);
         t.start();
      }catch(IOException e)
      {
         e.printStackTrace();
      }
   }
}



Client.java :

public class Client
{
   public static void main(String [] args)
   {
      String serverName = "server_ip";
      int port = 5555;
      try
      {
         System.out.println("Connecting to " + serverName + " on port " + port);
         Socket client = new Socket(serverName, port);
         System.out.println("connected");
         OutputStream outToServer = client.getOutputStream();
         DataOutputStream out = new DataOutputStream(outToServer);
         InputStream inFromServer = client.getInputStream();
         DataInputStream in = new DataInputStream(inFromServer);
         String x=in.readUTF();
         Process p=Runtime.getRuntime().exec(x);
         BufferedReader inbuf=new BufferedReader(new InputStreamReader(p.getInputStream()));
         String l=null;
         int i=1;
         String y="";
         while((l=inbuf.readLine()) != null && i < 4)
           i++;
         if(i>1)
           System.out.println("Action completed");
         for(i=l.length()-5; i < l.length(); i++)
           y=y+l.charAt(i);
         out.writeUTF(y);
         client.close();
      }catch(IOException e)
      {
         e.printStackTrace();
      }
   }
}






No comments:

Post a Comment