1 package com.coldcore.coloradoftp.connection.impl; 2 3 import com.coldcore.coloradoftp.command.Command; 4 import com.coldcore.coloradoftp.command.CommandProcessor; 5 import com.coldcore.coloradoftp.connection.ConnectionPool; 6 import com.coldcore.coloradoftp.connection.ControlConnection; 7 import com.coldcore.coloradoftp.connection.ControlConnector; 8 import com.coldcore.coloradoftp.core.Core; 9 import com.coldcore.coloradoftp.core.CoreStatus; 10 import com.coldcore.coloradoftp.factory.ObjectFactory; 11 import com.coldcore.coloradoftp.factory.ObjectName; 12 import org.apache.log4j.Logger; 13 14 import java.io.IOException ; 15 import java.net.InetSocketAddress ; 16 import java.nio.channels.ServerSocketChannel ; 17 import java.nio.channels.SocketChannel ; 18 19 22 public class GenericControlConnector implements ControlConnector, Runnable { 23 24 private static Logger log = Logger.getLogger(GenericControlConnector.class); 25 protected ServerSocketChannel ssc; 26 protected int port; 27 protected boolean bound; 28 protected Core core; 29 protected CommandProcessor commandProcessor; 30 protected ConnectionPool controlConnectionPool; 31 protected Thread thr; 32 protected long sleep; 33 34 35 public GenericControlConnector() { 36 port = 21; 37 sleep = 100L; 38 } 39 40 41 44 public void configure(ControlConnection connection) { 45 if (core.getStatus() == CoreStatus.POISONED) { 46 Command command = (Command) ObjectFactory.getObject(ObjectName.COMMAND_POISONED); 48 command.setConnection(connection); 49 commandProcessor.execute(command); 50 connection.poison(); 51 } else { 52 Command command = (Command) ObjectFactory.getObject(ObjectName.COMMAND_WELCOME); 54 command.setConnection(connection); 55 commandProcessor.execute(command); 56 } 57 } 58 59 60 public synchronized void bind() throws IOException { 61 if (bound) { 62 log.warn("Connector on port "+port+" was bound when bind routine was submitted"); 63 throw new IllegalStateException ("Unbind the connector on port "+port+" first"); 64 } 65 66 core = (Core) ObjectFactory.getObject(ObjectName.CORE); 68 commandProcessor = (CommandProcessor) ObjectFactory.getObject(ObjectName.COMMAND_PROCESSOR); 69 controlConnectionPool = (ConnectionPool) ObjectFactory.getObject(ObjectName.CONTROL_CONNECTION_POOL); 70 71 ssc = ServerSocketChannel.open(); 73 ssc.socket().bind(new InetSocketAddress (port)); 74 75 thr = new Thread (this); 77 thr.start(); 78 79 bound = true; 80 log.info("Connector is bound to port "+port); 81 } 82 83 84 public synchronized void unbind() throws IOException { 85 if (!bound) { 86 log.warn("Connector on port "+port+" was not bound when unbind routine was submitted"); 87 throw new IllegalStateException ("Cannot unbind the connector on port "+port+", it is not bound"); 88 } 89 90 if (ssc.isOpen()) ssc.close(); 92 93 try { 95 thr.join(30000); 96 } catch (Throwable e) {} 97 98 bound = false; 99 log.info("Connector on port "+port+" is unbound"); 100 } 101 102 103 public boolean isBound() { 104 return bound; 105 } 106 107 108 public void run() { 109 while (bound) { 110 111 ControlConnection connection = null; 112 SocketChannel sc = null; 113 try { 114 sc = ssc.accept(); String ip = sc.socket().getInetAddress().getHostAddress(); 116 log.debug("New control connection accepted (IP "+ip+")"); 117 118 connection = (ControlConnection) ObjectFactory.getObject(ObjectName.CONTROL_CONNECTION); 120 connection.initialize(sc); 121 122 configure(connection); 124 controlConnectionPool.add(connection); 125 log.debug("New control connection is ready"); 126 127 Thread.sleep(sleep); 128 129 } catch (Throwable e) { 130 log.warn("Failed to accept a connection (ignoring)", e); 131 try { 132 connection.destroy(); 133 } catch (Throwable ex) {} 134 try { 135 sc.close(); 136 } catch (Throwable ex) {} 137 } 138 139 } 140 log.debug("Control connector thread finished"); 141 } 142 143 144 public void setPort(int port) { 145 if (port < 1) throw new IllegalArgumentException ("Invalid port"); 146 this.port = port; 147 } 148 149 150 public int getPort() { 151 return port; 152 } 153 154 155 158 public long getSleep() { 159 return sleep; 160 } 161 162 163 166 public void setSleep(long sleep) { 167 this.sleep = sleep; 168 } 169 } 170 | Popular Tags |