📄 romulustestclient.java
字号:
/* * RomulusTestClient.java * * Created on 2004年1月11日, 下午8:46 */package romulus.NET;import java.net.*;import java.io.*;import java.util.*;import romulus.*;/** * * @author S */public class RomulusTestClient { //the communication socket private Socket socket = null; //the properties private Properties props = null; /** Creates a new instance of RomulusTestClient */ public RomulusTestClient() throws IOException{ try{ //get properties props = RomulusNetProtocol.getRomulusNetProtocolProperties(); String hostwelcome = props.getProperty(RomulusNetProtocol.AppServer, RomulusNetProtocol.AppServer); String hostname = props.getProperty(RomulusNetProtocol.AppServerAddr, "localhost"); String hostport = props.getProperty(RomulusNetProtocol.AppServerPort, "1228"); int port = Integer.parseInt(hostport); //construct and connect the socket socket = new Socket(hostname, port); socket.setSoTimeout(60000); //get input & output streams InputStream in = socket.getInputStream(); //get the message of the application server, check the server BufferedReader buffin = new BufferedReader(new InputStreamReader(in)); String welinfo = buffin.readLine(); if(welinfo.equals(hostwelcome)){ //for testing System.out.println("in: "+welinfo); } else{ throw new ConnectException("Can not connect a correct Romulus Application Server at "+hostname+":"+port); } } catch(IOException e){ try{ socket.close(); } catch(IOException e2){ } finally{ throw e; } } } //the method to check the user and test public boolean UserCheck(String checkinfo) throws IOException{ String outString = checkinfo; try{ //init OutputStream out = socket.getOutputStream(); InputStream in = socket.getInputStream(); //command PrintWriter printout = new PrintWriter(out); String UserCheckCommand = props.getProperty(RomulusNetProtocol.UserCheckCommands); if(UserCheckCommand == null){ throw new IOException("Can not get the correct property"); } System.out.println("out: "+UserCheckCommand); printout.println(UserCheckCommand); printout.flush(); //get response BufferedReader buffin = new BufferedReader(new InputStreamReader(in)); String res = buffin.readLine(); if(props.containsKey(RomulusNetProtocol.UC_ServerAsk)&&res.equals(props.getProperty(RomulusNetProtocol.UC_ServerAsk))){ System.out.println("in: "+props.getProperty(RomulusNetProtocol.UC_ServerAsk)); } else{ throw new IOException("Can not get the correct property"); } //test number System.out.println("out: "+outString); printout.println(outString); printout.flush(); //get the test back res = buffin.readLine(); if(props.containsKey(RomulusNetProtocol.UC_ServerRetPass)&&res.equals(props.getProperty(RomulusNetProtocol.UC_ServerRetPass))){ System.out.println("in: "+props.getProperty(RomulusNetProtocol.UC_ServerRetPass)); return true; } else if(props.containsKey(RomulusNetProtocol.UC_ServerRetFail)&&res.equals(props.getProperty(RomulusNetProtocol.UC_ServerRetFail))){ System.out.println("in: "+props.getProperty(RomulusNetProtocol.UC_ServerRetFail)); return false; } else{ throw new IOException("Can not get the correct property"); } } catch(IOException e){ try{ socket.close(); } catch(IOException e2){ } finally{ throw e; } } finally{ try{ socket.close(); } catch(Exception e){ e.printStackTrace(); } } } //the method to get the initial test public Test GetTest(String testid) throws IOException{ Test ret = null; try{ //init OutputStream out = socket.getOutputStream(); InputStream in = socket.getInputStream(); //command PrintWriter printout = new PrintWriter(out); String GetTestCommand = props.getProperty(RomulusNetProtocol.GetTestCommands); if(GetTestCommand == null){ throw new IOException("Can not get the correct property"); } System.out.println("out: "+GetTestCommand); printout.println(GetTestCommand); printout.flush(); //get response BufferedReader buffin = new BufferedReader(new InputStreamReader(in)); String res = buffin.readLine(); if(props.containsKey(RomulusNetProtocol.GT_ServerAsk)&&res.equals(props.getProperty(RomulusNetProtocol.GT_ServerAsk))){ System.out.println("in: "+props.getProperty(RomulusNetProtocol.GT_ServerAsk)); } else{ throw new IOException("Can not get the correct property"); } //test number System.out.println("out: "+testid); printout.println(testid); printout.flush(); //get the test back ObjectInputStream testin = new ObjectInputStream(in); try{ ret = (Test)(testin.readObject()); System.out.println("in: a Test"); } catch(Exception e){ throw new IOException(e.toString()); } } catch(IOException e){ try{ socket.close(); } catch(IOException e2){ } finally{ throw e; } } finally{ try{ socket.close(); } catch(Exception e){ e.printStackTrace(); } } return ret; } //the method to get the initial test public romulus.Manager.TestInfo GradeTest(romulus.Manager.TestInfo t) throws IOException{ romulus.Manager.TestInfo ret = null; try{ //init OutputStream out = socket.getOutputStream(); InputStream in = socket.getInputStream(); //command PrintWriter printout = new PrintWriter(out); String GradeTestCommand = props.getProperty(RomulusNetProtocol.GradeTestCommands); if(GradeTestCommand == null){ throw new IOException("Can not get the correct property"); } System.out.println("out: "+GradeTestCommand); printout.println(GradeTestCommand); printout.flush(); //get response BufferedReader buffin = new BufferedReader(new InputStreamReader(in)); String res = buffin.readLine(); if(props.containsKey(RomulusNetProtocol.GDT_ServerAsk)&&res.equals(props.getProperty(RomulusNetProtocol.GDT_ServerAsk))){ System.out.println("in: "+props.getProperty(RomulusNetProtocol.GDT_ServerAsk)); } else{ throw new IOException("Can not get the correct property"); } //test number ObjectOutputStream testout = new ObjectOutputStream(out); System.out.println("out: TestInfo"); testout.writeObject(t); testout.flush(); //get the test back ObjectInputStream testin = new ObjectInputStream(in); try{ ret = (romulus.Manager.TestInfo)(testin.readObject()); System.out.println("in: a Test"); } catch(Exception e){ throw new IOException(e.toString()); } } catch(IOException e){ try{ socket.close(); } catch(IOException e2){ } finally{ throw e; } } finally{ try{ socket.close(); } catch(Exception e){ e.printStackTrace(); } } return ret; } /** * @param args the command line arguments */ public static void main(String[] args) { try{// Properties props = RomulusNetProtocol.getRomulusNetProtocolProperties();// props.list(System.out); RomulusTestClient client = new RomulusTestClient();// client.GetTest(1); System.out.println(""+client.UserCheck("std1|std1|test")); System.out.println("Finish"); client = new RomulusTestClient(); Test t = client.GetTest("test"); QuestionItem qitem = null; for(Iterator i = t.questionIterator(); i.hasNext(); ){ qitem = ((Question)i.next()).getItem(); if(qitem instanceof FIB){ ((FIB)qitem).setAnswer("FIB"); } } client = new RomulusTestClient(); romulus.Manager.TestInfo tinfo = new romulus.Manager.TestInfo("Std1", t, 0.0, "info"); tinfo = client.GradeTest(tinfo); System.out.println(tinfo.getScore()+""); } catch(Exception e){ e.printStackTrace(); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -