Guest User

FixedMessageSequenceServer

a guest
Mar 12th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.71 KB | None | 0 0
  1. import org.apache.commons.io.input.ReversedLinesFileReader;
  2. import java.net.*;
  3. import java.io.*;
  4.  
  5. public class FixedMessageSequenceServer {
  6.  
  7.     public static String possiblyEmpty = returnLastNumber();
  8.     public  static  int newPosition  = Integer.parseInt( possiblyEmpty );
  9.  
  10.  
  11.     public static void main(String[] args) throws IOException  , InterruptedException {
  12.  
  13.  
  14.         ServerSocket serverSocket = null;
  15.         try {
  16.             serverSocket = new ServerSocket(4444);
  17.         } catch (IOException e) {
  18.             System.err.println("Could not listen on port: 4444.");
  19.             System.exit(1);
  20.         }
  21.  
  22.         Socket clientSocket = null;
  23.         try {
  24.             clientSocket = serverSocket.accept();
  25.         } catch (IOException e) {
  26.             System.err.println("Accept failed.");
  27.             System.exit(1);
  28.         }
  29.  
  30.         PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
  31.         BufferedReader in = new BufferedReader(
  32.                                         new InputStreamReader(
  33.                                         clientSocket.getInputStream()));
  34.         String inputLine, outputLine;
  35.  
  36.         String lastLineFromLogFileString = returnLastNumber();  
  37.      
  38.         int lastLineFromLogFile = Integer.parseInt(returnLastNumber());  
  39.         lastLineFromLogFile += 1; ///we want to start at the Next State, hence +1
  40.         FixedMessageSequenceProtocol fmsp = new FixedMessageSequenceProtocol(lastLineFromLogFile  );
  41.         outputLine = fmsp.processInput(null );  // passing null means we start with state=waiting and it should send    "Give me prmission to send state"
  42.         out.println(outputLine);
  43.        
  44.        
  45.  
  46.      try {
  47.         while ((inputLine = in.readLine()) != null) {
  48.           System.out.println(inputLine);  //  it starts off with "Permission granted."
  49.           if (inputLine.equals("y")){
  50.               out.println("inputOfYes");
  51.               newPosition++;
  52.               boolean boolLog = false;
  53.  
  54.               boolLog = logPosition();
  55.  
  56.           if (boolLog){
  57.               out.println("nowlogged");
  58.           }
  59.           else { System.out.print("not logged");
  60.           }
  61.  
  62.           }//end-IF  
  63.  
  64.          
  65.         outputLine = fmsp.processInput(inputLine);
  66.    
  67.          
  68.         try {     //Sleep to prevent synchronization issues  ??
  69.           Thread.sleep( 10  );    
  70.           out.println(outputLine);          
  71.         }
  72.         catch (InterruptedException ie) {}  
  73.    
  74.  
  75.           if (outputLine.equals("Bye."))
  76.                 break;
  77.           }
  78.  
  79.  
  80.       } catch(SocketException so) {    } /*END LARGE TRY-CATCH*/
  81.        
  82.         out.close();
  83.         in.close();
  84.         clientSocket.close();
  85.         serverSocket.close();
  86.     }
  87.  
  88.  
  89.     public static boolean logPosition(){
  90.       try {
  91.              File file = new File("C:\\\\Java_Scratch3_new_cleaned_Mar12\\\\FixedMessageLog.txt");  
  92.              PrintStream ps = null;
  93.              ps = new PrintStream( new FileOutputStream(file, true) );            
  94.              ps.println(newPosition);
  95.           }
  96.       catch(FileNotFoundException fe){
  97.        
  98.       }
  99.  
  100.       return true;
  101.     }
  102.  
  103.  
  104.     public static String returnLastNumber() {
  105.      
  106.      String lastLine = null;
  107.  
  108.        try {        
  109.          File someFile = new File("C:\\\\Java_Scratch3_new_cleaned_Mar12\\\\FixedMessageLog.txt");
  110.          ReversedLinesFileReader reader = new ReversedLinesFileReader( someFile );
  111.          lastLine = reader.readLine();
  112.          
  113.          if (lastLine == null){
  114.              lastLine = "0";
  115.          }
  116.    
  117.        } catch ( IOException ee) {
  118.  
  119.        }
  120.  
  121.      return lastLine;
  122.  
  123.     }
  124.  
  125. }// end class FixedMessageSequenceServer
Add Comment
Please, Sign In to add comment