Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.concurrent.*;
- import java.io.*;
- class Worker implements Runnable {
- Socket sock = null;
- Proxy proxy = null;
- public Worker (Socket socket, Proxy proxy) {
- proxy = proxy;
- sock = socket;
- }
- public void run () {
- /*
- try {
- OutputStream outputStream = sock.getOutputStream();
- InputStream inputStream = sock.getInputStream ();
- BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));
- BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
- while (inputStream.available () != 0) {
- String line = reader.readLine ();
- System.out.println (line);
- }
- reader.close();
- writer.close();
- } catch (IOException err) {
- }
- */
- }
- }
- public class Proxy {
- // Settings:
- private int fileserverTimeout;
- private int checkPeriod;
- private int tcpPort;
- private int udpPort;
- // Connection:
- ServerSocket serverSocket;
- ThreadPoolExecutor pool;
- // Rest:
- boolean shutdown = false;
- // Proxy - Server Connection:
- // Proxy - User Connection:
- public Proxy (int fileserverTimeout, int checkPeriod, int tcpPort, int udpPort) throws IOException {
- this.fileserverTimeout = fileserverTimeout;
- this.checkPeriod = checkPeriod;
- this.tcpPort = tcpPort;
- this.udpPort = udpPort;
- try {
- pool = new ThreadPoolExecutor(4, 10, 20, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable> (20));
- serverSocket = new ServerSocket (tcpPort);
- } catch (IOException err) {
- throw err;
- } finally {
- pool.shutdownNow();
- }
- }
- public void listen () {
- while (true) {
- System.out.println ("----");
- try {
- pool.execute(new Worker(serverSocket.accept(), this));
- } catch (RejectedExecutionException err) {
- System.out.println (err.getMessage ());
- } catch (IOException err) {
- }
- }
- }
- // debug only:
- void shutdown() {
- this.shutdown = true;
- pool.shutdownNow();
- try {
- // dummy request - don't blame me for this.
- Socket clientSocket = new Socket("localhost", tcpPort);
- clientSocket.close();
- } catch (Exception e) {
- }
- }
- // Proxy creation:
- private void parseProperties () {
- try {
- java.io.InputStream in = ClassLoader.getSystemResourceAsStream("user.properties");
- java.util.Properties users = new java.util.Properties();
- users.load(in);
- java.util.Set<String> usernames = users.stringPropertyNames();
- for (String username : usernames) {
- String password = users.getProperty(username);
- }
- } catch (IOException e) {
- }
- }
- private static void usage () {
- System.out.println ("Usage:");
- System.out.println (" tcpPort: the port to be used for instantiating a");
- System.out.println (" java.net.ServerSocket (handling TCP connection requests");
- System.out.println (" from clients).");
- System.out.println (" udpPort: the port to be used for instantiating a");
- System.out.println (" java.net.DatagramSocket (handling UDP requests from");
- System.out.println (" fileservers).");
- System.out.println (" fileserverTimeout: the period in milliseconds each fileserver has to send");
- System.out.println (" an isAlive packet (only containing the fileserver's TCP");
- System.out.println (" port). If no such packet is received within this time,");
- System.out.println (" the fileserver is assumed to be offline and is no longer");
- System.out.println (" available for handling requests.");
- System.out.println (" checkPeriod: specifies that the test whether a fileserver has");
- System.out.println (" timed-out or not (see fileserverTimeout) is repeated every");
- System.out.println (" checkPeriod milliseconds.");
- }
- public static void main (String[] args) {
- // Parameter handling:
- int fileserverTimeout = -1;
- int checkPeriod = -1;
- int tcpPort = -1;
- int udpPort = -1;
- for (int i = 0; i < args.length; i++) {
- try {
- if (args[i].equals ("tcpPort")) {
- i++;
- tcpPort = Integer.valueOf (args[i]).intValue ();
- } else if (args[i].equals ("udpPort")) {
- i++;
- udpPort = Integer.valueOf (args[i]).intValue ();
- } else if (args[i].equals ("fileserverTimeout")) {
- i++;
- fileserverTimeout = Integer.valueOf (args[i]).intValue ();
- } else if (args[i].equals ("checkPeriod")) {
- i++;
- checkPeriod = Integer.valueOf (args[i]).intValue ();
- } else {
- usage ();
- return ;
- }
- } catch (ArrayIndexOutOfBoundsException e) {
- usage ();
- return ;
- } catch (NumberFormatException e) {
- usage ();
- return ;
- }
- }
- //TODO: Check parameters
- try {
- Proxy proxy = new Proxy (fileserverTimeout, checkPeriod, tcpPort, udpPort);
- proxy.listen ();
- } catch (IOException e) {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement