Advertisement
Guest User

FixedMessageSequenceClient

a guest
Mar 12th, 2016
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.89 KB | None | 0 0
  1. import org.apache.commons.io.input.ReversedLinesFileReader;
  2.  
  3. import java.io.IOException;
  4. import java.util.*;
  5.  
  6. import java.net.*;
  7. import java.io.*;
  8. import javax.swing.Timer;
  9. import java.awt.event.ActionListener;
  10. import java.awt.event.ActionEvent;
  11.  
  12. public class FixedMessageSequenceClient {
  13.  
  14.  
  15.   static String KKJokes[] =  { "Permission granted." , "What is Alabama population", "y",    //00
  16.                                "Permission granted." , "What is Alaska population", "y",      //01
  17.                                "Permission granted." , "What is Arizona population", "y",     //02
  18.                                "Permission granted." , "What is Arkansas population", "y",     //03
  19.                                "Permission granted." , "What is California population", "y",    //04
  20.                                "Permission granted." , "What is Colorado population", "y",      //05
  21.                                "Permission granted." , "What is Connecticutt population", "y",      //06
  22.                                "Permission granted." , "What is Delaware population", "y", //  7
  23.                                "Permission granted." , "What is Florida population", "y", //   8
  24.                                "Permission granted." , "What is Georgia population", "y", //  9
  25.                                "Permission granted." , "What is Hawaii population", "y",  // 10
  26.                                "Permission granted." , "What is Idaho population", "y",   // 11
  27.                                "Permission granted." , "What is Illinois population", "y",  // 12
  28.                                "Permission granted." , "What is Indiana population", "y",   // 13
  29.                                "Permission granted." , "What is Iowa population", "y",      // 14
  30.                                "Permission granted." , "What is Kansas population", "y",    // 15
  31.                                "Permission granted." , "What is Kentucky population", "y",  //16
  32.                                "Permission granted." , "What is Louisiana population", "y",  //17
  33.                                "Permission granted." , "What is Maine population", "y",     //18
  34.                                "Permission granted." , "What is Maryland population", "y",   //19
  35.                                "Permission granted." , "What is Massachusetts population", "y", //20
  36.                                "Permission granted." , "What is Michigan population", "y",      //21    
  37.                                "Permission granted." , "What is Minnesota population", "y", //22
  38.                                "Permission granted." , "What is Mississippi population", "y",   //23
  39.                                "Permission granted." , "What is Missouri population", "y",     //24
  40.                                "Permission granted." , "What is Montana population", "y",      //25
  41.                                "Permission granted." , "What is Nebraska population", "y",     //26
  42.                                "Permission granted." , "What is Nevada population", "y",        //27
  43.                                "Permission granted." , "What is New Hampshire population", "y",  //28
  44.                                "Permission granted." , "What is New Jersey population", "y",     //29
  45.                                "Permission granted." , "What is New Mexico population", "y",   //30
  46.                                "Permission granted." , "What is New York population", "y",     //31
  47.                                "Permission granted." , "What is North Carolina population", "y",  //32
  48.                                "Permission granted." , "What is North Dakota population", "y",    //33
  49.                                "Permission granted." , "What is Ohio population", "y",            //34
  50.                                "Permission granted." , "What is Oklahoma population", "y",        //35
  51.                                "Permission granted." , "What is Oregon population", "y",          //36
  52.                                "Permission granted." , "What is Pennsylvania population", "y",    //37
  53.                                "Permission granted." , "What is Rhode Island population", "y",    //38
  54.                                "Permission granted." , "What is South Carolina population", "y",  //39
  55.                                "Permission granted." , "What is South Dakota population", "y",    //40
  56.                                "Permission granted." , "What is Tennessee population", "y",       //41
  57.                                "Permission granted." , "What is Texas population", "y",           //42
  58.                                "Permission granted." , "What is Utah population", "y",            //43
  59.                                "Permission granted." , "What is Vermont population", "y",         //44
  60.                                "Permission granted." , "What is Virginia population", "y",        //45
  61.                                "Permission granted." , "What is Washington population", "y",      //46
  62.                                "Permission granted." , "What is West Virginia population", "y",   //47
  63.                                "Permission granted." , "What is Wisconsin population", "y",       //48
  64.                                "Permission granted." , "What is Wyoming population", "y"   /*  49  */
  65.                              };  
  66.  
  67.   public static void main(String[] args ) throws IOException {
  68.    
  69.    String lastNumString  = returnLastNumber();
  70.    int lastNum =  Integer.parseInt(lastNumString );    
  71.  
  72.    int modlastNum = lastNum ;
  73.  
  74.    if (modlastNum != 0) {
  75.      modlastNum += 1; // starting  to start at next state
  76.      modlastNum*=3;  //because there are 3 phrases per state
  77.    }
  78.  
  79.    System.out.println( " Last position in file is " + lastNum + ". AND kkjokes.length " + KKJokes.length  
  80.                        + "\n and modlastNum is " + modlastNum);
  81.  
  82.     Socket kkSocket = null;
  83.     PrintWriter out = null;
  84.     BufferedReader br = null;
  85.      
  86.      
  87.     try {    
  88.       kkSocket = new Socket("localhost", 4444);
  89.       out = new PrintWriter(kkSocket.getOutputStream(), true);
  90.       br = new BufferedReader( new InputStreamReader ( kkSocket.getInputStream()  ));
  91.  
  92.                
  93.     for (int i = modlastNum; i < KKJokes.length ; i++){    
  94.         String fromServer = br.readLine();        
  95.         out.println(KKJokes[ i  % KKJokes.length ]);
  96.      
  97.         if ( (i % 3 )==2){
  98.            try {
  99.             Thread.sleep(11);
  100.             } catch (InterruptedException ie) {}
  101.         }  
  102.  
  103.  
  104.       // fromServer = br.readLine();   System.out.println ( fromServer ); fromServer = br.readLine();   System.out.println ( fromServer ); //    String fromServer3 = br.readLine();      System.out.println ( fromServer3 )  ; System.out.println ( fromServer )  ;
  105.      
  106.  
  107.  
  108.  
  109.     if(fromServer.equals("inputOfYes")){                  
  110.         while (!(fromServer.equals("nowlogged"))) {
  111.           fromServer = br.readLine();
  112.         }
  113.                
  114.     }  
  115.        
  116.      System.out.println ( fromServer )  ;
  117.  
  118.      } // end-forLoop starting at Ln. 93
  119.      
  120.       out.println("Line 138 Client- Bye");
  121.       System.out.println ("Line 139 Client - Bye" );
  122.  
  123.       out.close();
  124.       kkSocket.close();  
  125.     } catch ( IOException io ){
  126.         System.err.println("Calling IO . toString " + io.toString());  
  127.         System.exit(1);
  128.     }
  129.        
  130.     }
  131.  
  132.     public static String returnLastNumber() {
  133.      
  134.      String lastLine = null;
  135.  
  136.        try {        
  137.          File someFile = new File("C:\\\\Java_Scratch3_new_cleaned_Mar12\\\\FixedMessageLog.txt");
  138.          ReversedLinesFileReader reader = new ReversedLinesFileReader( someFile );
  139.          lastLine = reader.readLine();
  140.      
  141.          if (lastLine == null){
  142.              lastLine = "0";
  143.          }
  144.  
  145.        }catch ( IOException ee) {  }
  146.  
  147.      return lastLine;
  148.  
  149.     } //end function returnLastNumber()
  150.  
  151.  
  152. }// end class FixedMessageSequenceClient
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement