001: package com.sun.portal.netlet.client.jnlp.connect;
002:
003: import com.sun.portal.netlet.client.jnlp.ClientManager;
004: import com.sun.portal.log.common.PortalLogger;
005:
006: import java.net.*;
007: import java.io.IOException;
008:
009: public class ClientListener implements Runnable {
010:
011: private Socket inconnection;
012: private ServerSocket sconnection = null;
013: private Thread thread = null;
014: private boolean socketOpened;
015: private int srcPort;
016:
017: public ClientListener(int srcPort) {
018: this .srcPort = srcPort;
019: }
020:
021: public void start() {
022: if (sconnection == null) {
023: sconnection = makeServerSocket();
024: }
025: if (thread == null) {
026: thread = new Thread(this );
027: thread.start();
028: }
029: }
030:
031: public void run() {
032: System.out.println("ClientListener run");
033: while (socketOpened) {
034: // wait for incoming connection request
035: if (sconnection == null) {
036: socketOpened = false;
037: } else {
038: try {
039: inconnection = sconnection.accept();
040: } catch (IOException e) {
041: try {
042: sconnection.close();
043: Thread.sleep(5);
044: if (socketOpened == false) {
045: break;
046: }
047: // hack, if io error, retry?
048: } catch (IOException ee) {
049: System.out
050: .println("SClient serversocket accept problem close error:");
051: ee.printStackTrace();
052: } catch (InterruptedException ee) {
053: // do nothing
054: } catch (NullPointerException ee) {
055: // hack for netscape < 4.0.5
056: return;
057: }
058:
059: // now, reopen the connection and listen for requests...
060: if (socketOpened == true) {
061: sconnection = makeServerSocket();
062: continue;
063: }
064: }
065: try {
066: inconnection.setTcpNoDelay(true);
067: } catch (SocketException e) {
068: System.out
069: .println("SClient accept socket error setting tcpNoDelay");
070: e.printStackTrace();
071: socketOpened = false;
072: }
073:
074: if (inconnection != null) {
075: System.out
076: .println("Client Listener got connection on port: "
077: + inconnection.getLocalPort()
078: + " from port:"
079: + inconnection.getPort());
080: new ConnectionHandler(inconnection, this );
081: }
082: } // end else
083: } // end while
084: close();
085: }
086:
087: private ServerSocket makeServerSocket() {
088: ServerSocket sock = null;
089: boolean go = true;
090: int retries = 5; // Max 5 retries for port hunting.
091: String clientBindIP = ClientManager.getParam("clientBindIP");
092: while (go) {
093: try {
094: InetAddress bindAddr = null;
095: bindAddr = InetAddress.getByName(clientBindIP);
096: sock = new ServerSocket(srcPort, 10, bindAddr);
097: srcPort = sock.getLocalPort();
098: go = false;
099: socketOpened = true;
100: System.out
101: .println("ClientListener: server socket opened at port "
102: + srcPort + " and host " + clientBindIP);
103: } catch (UnknownHostException uhe) {
104: System.out
105: .println("ClientListener:Unable to make server socket at port "
106: + srcPort
107: + " and host "
108: + clientBindIP
109: + " " + uhe);
110: sock = null;
111: srcPort = 0;
112: go = false;
113: } catch (IOException ioe) {
114: System.out
115: .println("ClientListener: Unable to make server socket at port "
116: + srcPort
117: + " and host "
118: + clientBindIP
119: + " " + ioe);
120: sock = null;
121: srcPort = 0;
122: // Try binding to a freely availble port.
123: go = retries > 0 ? true : false;
124: --retries;
125: }
126: }
127: return sock;
128: }
129:
130: public int getClientListenerPort() {
131: return srcPort;
132: }
133:
134: public String getClientListenerURL() {
135: return "http://" + ClientManager.getParam("clientBindIP") + ":"
136: + srcPort;
137: }
138:
139: public void close() {
140: socketOpened = false;
141: try {
142: if (inconnection != null) {
143: inconnection.close();
144: inconnection = null;
145: }
146: if (sconnection != null) {
147: sconnection.close();
148: sconnection = null;
149: }
150: } catch (IOException e) {
151: System.out
152: .println("Unable to close the connection -> " + e);
153: }
154: }
155: }
|