📄 romulustestserverthread.java
字号:
/* * RomulusTestServerThread.java * * Created on 2004年1月11日, 下午9:13 */package romulus.NET;import java.net.*;import java.io.*;import java.util.*;import java.sql.*;import romulus.*;import romulus.Manager.*;/** * * @author S */public class RomulusTestServerThread extends Thread { //the communication socket private Socket socket = null; //the toolset private RomulusToolSet romulustool = null; private ManagerToolSet managertool = null; //the properties private Properties props = null; /** Creates a new instance of RomulusTestServerThread */ public RomulusTestServerThread(Socket socket, Connection rtsCon, Connection mtsCon) throws ClassNotFoundException, SQLException{ super("Thread of RomulusTestServer"); System.out.println("New RomulusTestServerThread"); this.romulustool = RomulusToolSet.getInstance(rtsCon); this.managertool = ManagerToolSet.getInstance(mtsCon); this.socket = socket; } public void finalize(){ try{ romulustool.close(); managertool.close(); socket.close(); } catch(Exception e ){ } } public void run() { try{ System.out.println("in running"); //set the socket props this.socket.setSoTimeout(60000); //get the properties props = RomulusNetProtocol.getRomulusNetProtocolProperties(); //get tht input & output streams OutputStream out = socket.getOutputStream(); InputStream in = socket.getInputStream(); //out the welcome info PrintWriter printout = new PrintWriter(out); String welinfo = props.getProperty(RomulusNetProtocol.AppServer, RomulusNetProtocol.AppServer); System.out.println("out: "+welinfo); printout.println(welinfo); printout.flush(); //get the initial command BufferedReader buffin = new BufferedReader(new InputStreamReader(in)); String command = buffin.readLine(); //Check the user if(props.containsKey(RomulusNetProtocol.UserCheckCommands)&&command.equals(props.getProperty(RomulusNetProtocol.UserCheckCommands))){ System.out.println("in: "+props.getProperty(RomulusNetProtocol.UserCheckCommands)); this.UserCheck(in, out); } //Get initial test else if(props.containsKey(RomulusNetProtocol.GetTestCommands)&&command.equals(props.getProperty(RomulusNetProtocol.GetTestCommands))){ System.out.println("in: "+props.getProperty(RomulusNetProtocol.GetTestCommands)); this.GetTest(in, out); } //get full test else if(props.containsKey(RomulusNetProtocol.GetFullTestCommands)&&command.equals(props.getProperty(RomulusNetProtocol.GetFullTestCommands))){ System.out.println("in: "+props.getProperty(RomulusNetProtocol.GetFullTestCommands)); this.GetFullTest(in, out); } //grade test else if(props.containsKey(RomulusNetProtocol.GradeTestCommands)&&command.equals(props.getProperty(RomulusNetProtocol.GradeTestCommands))){ System.out.println("in: "+props.getProperty(RomulusNetProtocol.GradeTestCommands)); this.GradeTest(in, out); } //wrong command else{ printout.println("Your command is wrong!"); } } catch(Exception e){ e.printStackTrace(); } finally{ try{ romulustool.close(); managertool.close(); socket.close(); } catch(Exception e){ e.printStackTrace(); } } } //Check the Username PassWord and TestNumber private void UserCheck(InputStream in, OutputStream out) throws IOException{ //initial String serverask = props.getProperty(RomulusNetProtocol.UC_ServerAsk); String serverret_fail = props.getProperty(RomulusNetProtocol.UC_ServerRetFail); String serverret_pass = props.getProperty(RomulusNetProtocol.UC_ServerRetPass); if(serverask == null || serverret_fail == null || serverret_pass == null){ throw new IOException("Can not get the correct property"); } //server ask PrintWriter printout = new PrintWriter(out); printout.println(serverask); printout.flush(); //response BufferedReader buffin = new BufferedReader(new InputStreamReader(in)); String[] info = RomulusNetProtocol.getInfo(buffin.readLine()); String name = info[0]; String pass = info[1]; String test = info[2]; try{ //Check The Infomation if(managertool.CheckTest(name, pass, test)){ System.out.println(serverret_pass); printout.println(serverret_pass); printout.flush(); } else{ System.out.println(serverret_fail); printout.println(serverret_fail); printout.flush(); } } catch(java.sql.SQLException e){ e.printStackTrace(); printout.println(serverret_fail); printout.flush(); } } //Get Test Protocol private void GetTest(InputStream in, OutputStream out) throws IOException{ //initial PrintWriter printout = new PrintWriter(out); String serverask = props.getProperty(RomulusNetProtocol.GT_ServerAsk); if(serverask == null){ throw new IOException("Can not get the correct property"); } printout.println(serverask); printout.flush(); //get response BufferedReader buffin = new BufferedReader(new InputStreamReader(in)); String str_testident = buffin.readLine(); //Get and pass the test Test test = null; try{ test = romulustool.getInitTest(str_testident); } catch(Exception e){ throw new IOException(e.toString()); } ObjectOutputStream testout = new ObjectOutputStream(out); testout.writeObject(test); testout.flush(); //close the section printout.close(); testout.close(); out.close(); buffin.close(); in.close(); } //Get Full Test Protocol private void GetFullTest(InputStream in, OutputStream out) throws IOException{ } //Grade Test Protocol private void GradeTest(InputStream in, OutputStream out) throws IOException{ //initial PrintWriter printout = new PrintWriter(out); String serverask = props.getProperty(RomulusNetProtocol.GDT_ServerAsk); if(serverask == null){ throw new IOException("Can not get the correct property"); } printout.println(serverask); printout.flush(); //get response ObjectInputStream objin = new ObjectInputStream(in); romulus.Manager.TestInfo initTest = null; try{ initTest = (romulus.Manager.TestInfo)(objin.readObject()); } catch(Exception e){ throw new IOException(e.toString()); } //Grade and pass the test double testscore = 0; try{ testscore = romulustool.gradeTest(initTest.getTest()); initTest.setScore(testscore); managertool.StoreResult(initTest.getName(), initTest.getTest().getIdent(), initTest.getScore(), initTest.getInfo()); } catch(Exception e){ e.printStackTrace(); throw new IOException(e.toString()); } ObjectOutputStream testout = new ObjectOutputStream(out); testout.writeObject(initTest); testout.flush(); //close the section printout.close(); testout.close(); out.close(); objin.close(); in.close(); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -