/* * RemoteControl.java * Created on August 27, 2007, 8:21 PM * */ /** * * @author Mohammed Mohammed */ import java.net.*; import java.io.*; import java.util.*; public class RemoteControl { public static void main (String args []) { if (args.length <= 1) { System.out.println("Too few arguments."); System.out.println("Run it like this: java RemoteControl NameServer NS_Port"); System.out.println("Exiting.."); System.exit(0); } String host = args[0]; try { Socket s = new Socket (host, Integer.parseInt(args[1])); try { InputStream inStream = s.getInputStream(); OutputStream outStream = s.getOutputStream(); PrintWriter pr = new PrintWriter (outStream, true); BufferedReader br = new BufferedReader(new InputStreamReader(inStream)); String msg_received = br.readLine(); StringTokenizer strTok = new StringTokenizer(msg_received, "$$$"); String head = "", token = ""; head = strTok.nextToken(); int countTokens = strTok.countTokens(); for (int i = 0; i < countTokens ; i++) { token = strTok.nextToken(); } //send login information if (Integer.parseInt(token) == 0) { pr.println(head + "$$$" + "1" + "$$$" + "test1" + "$$$" + "test1" + "$$$" + "1234"); System.out.println(head + "$$$" + "1" + "$$$" + "test1" + "$$$" + "test1" + "$$$" + "1234"); } //receive authentication result msg_received = br.readLine(); strTok = new StringTokenizer(msg_received, "$$$"); countTokens = strTok.countTokens(); for (int i = 0; i < countTokens ; i++) { token = strTok.nextToken(); } if (token.equals("test")) { System.out.println("Connected to NameServer.."); } //select course pr.println(head + "$$$" + token); System.out.println(head + "$$$" + token); //receive VCServer info msg_received = br.readLine(); strTok = new StringTokenizer(msg_received, "$$$"); String VCip = "", VCport = ""; countTokens = strTok.countTokens(); for (int i = 0; i < countTokens ; i++) { token = strTok.nextToken(); if (i == 2) VCip = token; if (i == 3) VCport = token; } //confirm pr.println(head); System.out.println(head); //receive students list msg_received = br.readLine(); strTok = new StringTokenizer(msg_received, "$$$"); while (strTok.hasMoreTokens()) { token = strTok.nextToken(); } //connect to VCServer s = new Socket (VCip, Integer.parseInt(VCport)); inStream = s.getInputStream(); outStream = s.getOutputStream(); pr = new PrintWriter (outStream, true); br = new BufferedReader(new InputStreamReader(inStream)); //receive initial msg msg_received = br.readLine(); //send username and class pr.println(head + "$$$" + "1" + "$$$" + "test1" + "$$$" + "test1" + "$$$" + "test"); System.out.println(head + "$$$" + "1" + "$$$" + "test1" + "$$$" + "test1" + "$$$" + "test"); msg_received = br.readLine(); //broadcast a msg pr.println(head + "$$$" + "2" + "$$$" + "128" + "$$$" + "0" + "$$$" + "Hello All!!"); System.out.println(head + "$$$" + "2" + "$$$" + "128" + "$$$" + "0" + "$$$" + "Hello All!!"); inStream.close(); outStream.close(); } catch (IOException ex) { System.out.println("Error creating file streams"); } } catch (UnknownHostException ex) { System.out.println("Unable to connect to " + host); } catch (IOException ex) { System.out.println("Error creating socket"); } } }