// File: TutorialDescriptionServer.java // // Author: Francois Coupleux (coupleux@eleve.emn.fr) // // Date: 7-8-1996 // // Description: Server which sends the description of the tutorial to the applet Visualizer. import java.net.*; import java.io.*; import java.util.*; // Class TutorialDescriptionServer // ------------------------------- // This class is the model for the server which will send information // concerning the tutorial to the applet. public class TutorialDescriptionServer { Vector windowNames , windowTypes , beginningsOfDescriptions; // main() // ------ public static void main(String[] args) { RandomAccessFile descriptionFile; Vector windowNames , windowTitles , windowTypes , beginningsOfDescriptions; ServerSocket serverSocket; Socket clientSocket; DataInputStream inputStream; PrintStream outputStream; String currentLine , keyword , queryKey , sectionDelimiter , windowType; StringTokenizer stringTokenizer; int i; boolean isTitleRequest; try { // Creates the file descriptionFile = new RandomAccessFile("tutorialdescription.dat" , "r"); // Creates the vectors which will be used to look for a specific window windowNames = new Vector(); windowTitles = new Vector(); windowTypes = new Vector(); beginningsOfDescriptions = new Vector(); // Puts elements in the vectors while((currentLine = descriptionFile.readLine()) != null) { stringTokenizer = new StringTokenizer(currentLine , " "); keyword = stringTokenizer.nextToken(); // Locates the beginning of default values section and main menu definition if(keyword.equals("DEFAULTVALUES") || keyword.equals("MAINMENU")) { windowNames.addElement(keyword); windowTitles.addElement(keyword); windowTypes.addElement(keyword); beginningsOfDescriptions.addElement(new Long(descriptionFile.getFilePointer())); } // If the part read in the file correspond to a menu, a plotting system or a window if(keyword.equals("MENU") || keyword.equals("PLOTTINGSYSTEM") || keyword.equals("WINDOW")) { // Sets the window type and the beginning of the description windowTypes.addElement(keyword); beginningsOfDescriptions.addElement(new Long(descriptionFile.getFilePointer())); } // If the keyword NAME is read if(keyword.equals("NAME")) { // Sets the name of the window windowNames.addElement(stringTokenizer.nextToken()); } // If the keyword WINDOWTITLE is read if(keyword.equals("WINDOWTITLE")) { // Sets the title of the window windowTitles.addElement(currentLine.substring(12)); } } // Creates a server socket on the port 8000 serverSocket = new ServerSocket(8000); while(true) { // Accepts a client clientSocket = serverSocket.accept(); // Opens input and output streams inputStream = new DataInputStream(clientSocket.getInputStream()); outputStream = new PrintStream(clientSocket.getOutputStream()); while ((queryKey = inputStream.readLine()) == null) { ; } // By default the request doesn't concern a title isTitleRequest = false; // If the query key begins with "TITLE" if(queryKey.startsWith("TITLE")) { // Gets the name of the window stringTokenizer = new StringTokenizer(queryKey , " "); stringTokenizer.nextToken(); queryKey = stringTokenizer.nextToken(); // This is a request about a title isTitleRequest = true; } i = 0; while(! ((String)windowNames.elementAt(i)).equals(queryKey)) { i++; } // If the section corresponding to the request is not found if(i == windowNames.size()) { outputStream.println("Not Found"); } // If the section is found else { // If the request is about a title if(isTitleRequest) { outputStream.println((String)windowTitles.elementAt(i)); } // If the request is about a window description else { // Places the file pointer at the right position descriptionFile.seek(((Long)beginningsOfDescriptions.elementAt(i)).longValue()); sectionDelimiter = "END" + queryKey; // If the request concerns a window if(! (queryKey.equals("DEFAULTVALUES") || queryKey.equals("MAINMENU"))) { windowType = (String)windowTypes.elementAt(i); sectionDelimiter = "END" + windowType; // Sends the type of the window to the applet outputStream.println(windowType); } // Sends description while(! (currentLine = descriptionFile.readLine()).equals(sectionDelimiter)) { outputStream.println(currentLine); } } } // Closes streams outputStream.close(); inputStream.close(); // Closes client socket clientSocket.close(); } } // If a problem occured catch (IOException e) { System.out.println("Error: Impossible to run the server."); } } }