Saturday, December 24, 2011

QTPGridServer.java.txt

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class QTPGridServer{
    ServerSocket providerSocket;
    Socket connection = null;
    ObjectOutputStream out;
    ObjectInputStream in;
    String message;
    QTPGridServer(){}
    void run()
    {
        try{
            //1. creating a server socket
            providerSocket = new ServerSocket(2004, 10);
            //2. Wait for connection
            System.out.println("Waiting for connection");
            connection = providerSocket.accept();
            System.out.println("Connection received from " + connection.getInetAddress().getHostName());
            //3. get Input and Output streams
            out = new ObjectOutputStream(connection.getOutputStream());
            out.flush();
            in = new ObjectInputStream(connection.getInputStream());
            sendMessage("Connection successful");
            //4. The two parts communicate via the input and output streams
            do{
                try{
                    message = (String)in.readObject();
                    System.out.println("client>" + message);
                    if (message.toLowerCase().equals("ping")){
                        sendMessage("Reply: Server running fine");
                    }else if (message.toLowerCase().equals("kill")){
                        sendMessage("Kill Request Recived.!! Shutting down server. You will not be able to connect to this server anymore.");
                        System.exit(0);
                    }else if (message.equals("Terminate Connection")){
                        //Do Nothing
                    }else{
                        sendMessage("Launching QTPTrigger.vbs with provided parameters: " + message);
                        String CurrentDirectory = System.getProperty("user.dir");
                        System.out.println(CurrentDirectory);
                        String cmd = "cmd.exe /c start \"\" \"" + CurrentDirectory + "\\QTPTrigger.vbs\" \"" + message + "\"";
                        System.out.println(cmd);
                        Runtime.getRuntime().exec(cmd);
                    }
                      
                }catch(ClassNotFoundException classnot){
                    System.err.println("Data received in unknown format");
                }
            }while(!message.equals("Terminate Connection"));
        }
        catch(IOException ioException){
            ioException.printStackTrace();
        }
        finally{
            //4: Closing connection
            try{
                in.close();
                out.close();
                providerSocket.close();
            }
            catch(IOException ioException){
                ioException.printStackTrace();
            }
        }
    }
    void sendMessage(String msg)
    {
        try{
            out.writeObject(msg);
            out.flush();
            System.out.println("server>" + msg);
        }
        catch(IOException ioException){
            ioException.printStackTrace();
        }
    }
    public static void main(String args[])
    {
        QTPGridServer server = new QTPGridServer();
        while(true){
            server.run();
        }
    }
}

No comments:

Post a Comment