All pastes #216652 Raw Edit

Untitled

public java v1 · immutable
#216652 ·published 2006-10-23 06:58 UTC
rendered paste body
package edu.ncsu.ftp;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintStream;import java.net.ServerSocket;import java.net.Socket;import java.net.SocketException;/** * Created by IntelliJ IDEA. * User: BC * Date: Oct 2, 2005 * Time: 12:59:40 AM * To change this template use File | Settings | File Templates. */public class Ident extends Thread {    private static String userid;    private static int expectedConns;    private static boolean enabled;    private static ServerSocket ss;    private static boolean running = false;    public static void main(String[] args) throws IOException {        Ident i = new Ident("pyrr");        i.start();        Thread.yield();        System.out.println("IDENT SERVER STARTED");        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));        String buff = null;        while ((buff = in.readLine()) != null) {            System.out.println("Got: " + buff);            if (buff.equals("inc")) {                Ident.incExpected();                System.out.println("Expected Conns: " + expectedConns);            } else if (buff.equals("dec")) {                Ident.decExpected();                System.out.println("Expected Conns: " + expectedConns);            }        }    }    //TODO Make some spiffy expection... I guess    public Ident(String id) throws Exception {        if(running)            throw new Exception("Already running");        setUserId(id);        expectedConns = 0;    }    public void setUserId(String id) {        if (id == null) {            enabled = false;        } else {            userid = id;            enabled = true;        }    }    public static void incExpected() {        if (!enabled) {            return;        }        expectedConns++;        System.out.println("IDENT: Expecting " + expectedConns + " requests");    }    public static void decExpected() {        if (enabled && expectedConns > 0)            expectedConns--;        if((ss != null || !ss.isClosed()) && expectedConns <= 0) {            try {                ss.close();            } catch (IOException e) {            }        }    }    public static void die() {        running = false;        expectedConns = 0;        decExpected();    }    public void run() {        running = true;        while (running) {            try {                while (expectedConns <= 0) {                    try {                        Thread.sleep(500);                    } catch (InterruptedException e) {                    }                    continue;                }                try {                    ss = new ServerSocket(113);                } catch (Exception e) {                    e.printStackTrace();                    System.out.println("Failed to listen on port 113");                    expectedConns = 0;                    continue;                }                //ss.setSoTimeout(30 * 1000);                while (expectedConns > 0) {                    IdentConn ic = new IdentConn(ss.accept());                    ic.start();                    Thread.yield();                    System.err.println("IDENT: Received IDENT request.");//  Expecting " + expectedConns + " more");                }                ss.close();            } catch (IOException e) {                //e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.            }            ss = null;        }        running = false;    }    class IdentConn extends Thread {        private Socket sock;        public IdentConn(Socket s) throws SocketException {            sock = s;            sock.setSoTimeout(30 * 1000);        }        public void run() {            try {                BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));                PrintStream out = new PrintStream(sock.getOutputStream());                String buff = in.readLine();                //System.out.println("buff: " + buff);                if (buff != null) {                    out.print(buff + ":USERID:UNIX:" + userid + "\r\n");                    //System.out.println(buff + ":USERID:UNIX:" + userid + "\r\n");                }                //while((buff = in.readLine()) != null) {                //System.out.println(buff);                //}                in.close();                out.close();                sock.close();            } catch (IOException e) {                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.            }        }    }}