Advertisement
Guest User

Untitled

a guest
Jun 21st, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.96 KB | None | 0 0
  1.  
  2.  
  3. import java.net.ServerSocket;
  4. import java.net.Socket;
  5. import java.util.concurrent.*;
  6. import java.io.*;
  7.  
  8. class Worker implements Runnable {
  9.     Socket sock = null;
  10.     Proxy proxy = null;
  11.  
  12.  
  13.     public Worker (Socket socket, Proxy proxy) {
  14.         proxy = proxy;
  15.         sock = socket;
  16.     }
  17.  
  18.     public void run () {
  19. /*
  20.         try {
  21.             OutputStream outputStream = sock.getOutputStream();
  22.             InputStream inputStream = sock.getInputStream ();
  23.  
  24.             BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));
  25.             BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
  26.  
  27.             while (inputStream.available () != 0) {
  28.                 String line = reader.readLine ();
  29.                 System.out.println (line);
  30.             }
  31.  
  32.             reader.close();
  33.             writer.close();
  34.         } catch (IOException err) {
  35.  
  36.         }
  37. */
  38.     }
  39. }
  40.  
  41.  
  42.  
  43. public class Proxy {
  44.     // Settings:
  45.     private int fileserverTimeout;
  46.     private int checkPeriod;
  47.     private int tcpPort;
  48.     private int udpPort;
  49.  
  50.     // Connection:
  51.     ServerSocket serverSocket;
  52.     ThreadPoolExecutor pool;
  53.  
  54.     // Rest:
  55.     boolean shutdown = false;
  56.  
  57.     // Proxy - Server Connection:
  58.  
  59.     // Proxy - User Connection:
  60.  
  61.     public Proxy (int fileserverTimeout, int checkPeriod, int tcpPort, int udpPort) throws IOException {
  62.         this.fileserverTimeout = fileserverTimeout;
  63.         this.checkPeriod = checkPeriod;
  64.         this.tcpPort = tcpPort;
  65.         this.udpPort = udpPort;
  66.  
  67.         try {
  68.             pool = new ThreadPoolExecutor(4, 10, 20, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable> (20));
  69.             serverSocket = new ServerSocket (tcpPort);
  70.         } catch (IOException err) {
  71.             throw err;
  72.         } finally {
  73.             pool.shutdownNow();
  74.         }
  75.     }
  76.  
  77.     public void listen () {
  78.         while (true) {
  79.             System.out.println ("----");
  80.             try {
  81.                 pool.execute(new Worker(serverSocket.accept(), this));
  82.             } catch (RejectedExecutionException err) {
  83.                 System.out.println (err.getMessage ());
  84.             } catch (IOException err) {
  85.             }
  86.         }
  87.     }
  88.  
  89.     // debug only:
  90.     void shutdown() {
  91.         this.shutdown = true;
  92.         pool.shutdownNow();
  93.  
  94.         try {
  95.             // dummy request - don't blame me for this.
  96.             Socket clientSocket = new Socket("localhost", tcpPort);
  97.             clientSocket.close();
  98.         } catch (Exception e) {
  99.         }
  100.     }
  101.  
  102.     // Proxy creation:
  103.  
  104.     private void parseProperties () {
  105.         try {
  106.             java.io.InputStream in = ClassLoader.getSystemResourceAsStream("user.properties");
  107.             java.util.Properties users = new java.util.Properties();
  108.             users.load(in);
  109.             java.util.Set<String> usernames = users.stringPropertyNames();
  110.             for (String username : usernames) {
  111.                 String password = users.getProperty(username);
  112.             }
  113.         } catch (IOException e) {
  114.         }
  115.     }
  116.  
  117.     private static void usage () {
  118.         System.out.println ("Usage:");
  119.  
  120.         System.out.println (" tcpPort:           the port to be used for instantiating a");
  121.         System.out.println ("                    java.net.ServerSocket (handling TCP connection requests");
  122.         System.out.println ("                    from clients).");
  123.  
  124.         System.out.println (" udpPort:           the port to be used for instantiating a");
  125.         System.out.println ("                    java.net.DatagramSocket (handling UDP requests from");
  126.         System.out.println ("                    fileservers).");
  127.  
  128.         System.out.println (" fileserverTimeout: the period in milliseconds each fileserver has to send");
  129.         System.out.println ("                    an isAlive packet (only containing the fileserver's TCP");
  130.         System.out.println ("                    port). If no such packet is received within this time,");
  131.         System.out.println ("                    the fileserver is assumed to be offline and is no longer");
  132.         System.out.println ("                    available for handling requests.");
  133.  
  134.         System.out.println (" checkPeriod:       specifies that the test whether a fileserver has");
  135.         System.out.println ("                    timed-out or not (see fileserverTimeout) is repeated every");
  136.         System.out.println ("                    checkPeriod milliseconds.");
  137.     }
  138.  
  139.     public static void main (String[] args) {
  140.         // Parameter handling:
  141.         int fileserverTimeout = -1;
  142.         int checkPeriod = -1;
  143.         int tcpPort = -1;
  144.         int udpPort = -1;
  145.  
  146.         for (int i = 0; i < args.length; i++) {
  147.             try {
  148.                 if (args[i].equals ("tcpPort")) {
  149.                     i++;
  150.                     tcpPort = Integer.valueOf (args[i]).intValue ();
  151.                 } else if (args[i].equals ("udpPort")) {
  152.                     i++;
  153.                     udpPort = Integer.valueOf (args[i]).intValue ();
  154.                 } else if (args[i].equals ("fileserverTimeout")) {
  155.                     i++;
  156.                     fileserverTimeout = Integer.valueOf (args[i]).intValue ();
  157.                 } else if (args[i].equals ("checkPeriod")) {
  158.                     i++;
  159.                     checkPeriod = Integer.valueOf (args[i]).intValue ();
  160.                 } else {
  161.                     usage ();
  162.                     return ;
  163.                 }
  164.             } catch (ArrayIndexOutOfBoundsException e) {
  165.                 usage ();
  166.                 return ;
  167.             } catch (NumberFormatException e) {
  168.                 usage ();
  169.                 return ;
  170.             }
  171.         }
  172.  
  173.         //TODO: Check parameters
  174.  
  175.         try {
  176.             Proxy proxy = new Proxy (fileserverTimeout, checkPeriod, tcpPort, udpPort);
  177.             proxy.listen ();
  178.         } catch (IOException e) {
  179.         }
  180.     }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement