1 6 7 package org.jfox.ioc.connector; 8 9 import java.io.BufferedOutputStream ; 10 import java.io.BufferedReader ; 11 import java.io.IOException ; 12 import java.io.InputStreamReader ; 13 import java.io.ObjectOutputStream ; 14 import java.net.ServerSocket ; 15 import java.net.Socket ; 16 import java.net.SocketException ; 17 import java.net.InetAddress ; 18 import java.rmi.MarshalledObject ; 19 import java.util.List ; 20 import java.util.ArrayList ; 21 import java.util.concurrent.ExecutorService ; 22 import java.util.concurrent.Executors ; 23 24 import org.jfox.ioc.common.AbstractService; 25 import org.jfox.ioc.ext.ActiveComponent; 26 import org.jfox.ioc.ext.SingletonComponent; 27 import org.jfox.ioc.util.Marshaller; 28 29 33 34 public class ConnectorServer extends AbstractService implements ActiveComponent, SingletonComponent { 35 38 protected static int DEFAULT_PORT = 1099; 39 40 protected ServerSocket ssocket = null; 41 42 45 public static final ServerNode theNode = ServerNode.THE_NODE; 46 47 50 protected static ClusterServer clusterServer = null; 51 52 private ExecutorService pool = Executors.newCachedThreadPool(); 53 54 public ConnectorServer() throws Exception { 55 this(DEFAULT_PORT); 56 } 57 58 public ConnectorServer(int port) throws Exception { 59 InetAddress address = InetAddress.getLocalHost(); 60 String ip = address.getHostAddress(); 61 String hostName = address.getCanonicalHostName(); 62 theNode.setIp(ip); 63 theNode.setPort(port); 64 theNode.setHostName(hostName); 65 } 66 67 public int getPort() { 68 return theNode.getPort(); 69 } 70 71 public void setPort(int port) { 72 theNode.setPort(port); 73 } 74 75 protected void doStart() throws Exception { 76 if(context.getRegistry().isRegisteredImplemetation(ClusterServer.class)) { 77 try { 79 clusterServer = (ClusterServer) context.getRegistry().getComponentInstance(ClusterServer.class); 80 } 81 catch(Exception e) { 82 e.printStackTrace(); 83 } 84 } 85 Thread t = new Thread (this, this.getClass().getName()); 87 t.start(); 88 Thread.sleep(50); 89 } 91 92 public static ClusterServer getClusterServer() { 93 return clusterServer; 94 } 95 96 protected void doStop() throws Exception { 97 ssocket.close(); 98 } 99 100 protected void doInit() throws Exception { 101 ssocket = new ServerSocket (theNode.getPort()); 102 ssocket.setReuseAddress(true); 103 } 104 105 protected void doDestroy() throws Exception { 106 ssocket = null; 107 } 108 109 public void run() { 110 while(isStarted()) { 111 Socket socket = null; 112 try { 113 socket = ssocket.accept(); 114 socket.setReuseAddress(true); 115 } 116 catch(SocketException e) { 117 } 119 catch(IOException e){ 120 e.printStackTrace(); 121 } 122 if(socket != null) { 123 pool.execute(new Sender(socket)); 124 } 125 } 126 } 127 128 protected ConnectorRemote getConnectorRemote(String protocol){ 129 ConnectorRemote remote = theNode.getConnector(protocol); 130 if(clusterServer != null){ 131 try { 133 List <ServerNode> nodes = clusterServer.getClusterNodes(); 134 List <ConnectorRemote> remotes = new ArrayList <ConnectorRemote>(nodes.size()); 135 for(ServerNode node : nodes ){ 136 remotes.add(node.getConnector(protocol)); 137 } 138 remote = new ClusterableConnectorRemote(remote,remotes.toArray(new ConnectorRemote[remotes.size()])); 139 } 140 catch(Exception e){ 141 e.printStackTrace(); 142 } 143 } 144 return remote; 145 } 146 147 private class Sender implements Runnable { 148 private Socket socket; 149 150 public Sender(Socket socket) { 151 this.socket = socket; 152 } 153 154 public void run() { 155 try { 156 BufferedReader in = new BufferedReader (new InputStreamReader ((socket.getInputStream()))); 157 String protocol = in.readLine(); 158 ConnectorRemote remote = getConnectorRemote(protocol); 159 MarshalledObject mobj = Marshaller.marshall(remote); 160 ObjectOutputStream out = new ObjectOutputStream (new BufferedOutputStream (socket.getOutputStream())); 161 out.writeObject(mobj); 162 out.flush(); 163 socket.close(); 164 } 165 catch(Exception e) { 166 logger.warn(e.getMessage(), e); 167 } 168 } 169 } 170 171 public static void main(String [] args) { 172 173 } 174 } 175 | Popular Tags |