001: package vicazh.hyperpool.stream.net;
002:
003: import java.io.*;
004: import java.net.*;
005: import java.net.Socket;
006: import java.util.logging.*;
007: import vicazh.hyperpool.*;
008: import vicazh.hyperpool.Start;
009: import vicazh.hyperpool.stream.Element;
010: import vicazh.hyperpool.stream.*;
011:
012: /**
013: * The in service
014: *
015: * @author Victor Zhigunov
016: * @version 0.4.0
017: */
018: public class InService extends Service implements Runnable,
019: InServiceMBean {
020: public InService() {
021: }
022:
023: private String msg;
024:
025: public int timeout;
026:
027: /**
028: * @param msg
029: * port use message
030: * @param timeout
031: * socket timeout
032: */
033: public InService(String msg, int timeout) {
034: this .timeout = timeout;
035: this .msg = msg;
036: }
037:
038: private ServerSocket serversocket;
039:
040: private int port;
041:
042: public void setAttribute(String name, Object value)
043: throws Exception {
044: if (name.equals(ElementMBean.INIT))
045: super .setAttribute(name, InetAddress.getLocalHost()
046: .getHostAddress());
047: else {
048: setPort((Integer) value);
049: super .setAttribute(name, value);
050: if (serversocket == null)
051: throw (new IOException());
052: }
053: }
054:
055: public void setPort(int port) throws IOException {
056: this .port = port;
057: if (msg != null) {
058: if (serversocket != null) {
059: try {
060: serversocket.close();
061: } catch (Exception e) {
062: }
063: serversocket = null;
064: try {
065: while (getConnections().size() > 0)
066: getConnections().get(0).close();
067: } catch (Exception e) {
068: }
069: }
070: try {
071: serversocket = new ServerSocket(port);
072: new Thread(this ).start();
073: } catch (IOException e) {
074: System.out.println(msg);
075: }
076: }
077: }
078:
079: public int getPort() {
080: return port;
081: }
082:
083: public void run() {
084: try {
085: while (!serversocket.isClosed())
086: new Trans(serversocket.accept()).start();
087: } catch (Exception e) {
088: }
089: }
090:
091: private class Trans extends Thread {
092: private Socket socket;
093:
094: public Trans(Socket socket) {
095: this .socket = socket;
096: }
097:
098: public void run() {
099: Connection connection = getConnection(socket);
100: try {
101: socket.setSoTimeout(timeout);
102: connection.setServer(new BufferedOutputStream(socket
103: .getOutputStream()));
104: connection.setClient(((Element) getElement())
105: .get(connection.getServer()));
106: add(connection);
107: Transfer.run(new BufferedInputStream(socket
108: .getInputStream()), connection.getClient());
109: } catch (BreakException e) {
110: } catch (Exception e) {
111: Start.logger.log(Level.SEVERE, e.getMessage(), e);
112: } finally {
113: try {
114: socket.close();
115: } catch (Exception ex) {
116: }
117: }
118: }
119: }
120:
121: protected Connection getConnection(Socket socket) {
122: return new InConnection(this , socket);
123: }
124:
125: public void stop() throws Exception {
126: try {
127: serversocket.close();
128: } catch (Exception e) {
129: }
130: try {
131: while (getConnections().size() > 0)
132: getConnections().get(0).close();
133: } catch (Exception e) {
134: }
135: super.stop();
136: }
137: }
|