if (log.isInfoEnabled())
log.infof("Local socket is up at :%d with %d action ready", port, actions.size());
// 循环监听的主程序
final SocketLock lock = new SocketLock();
ExecutorService execs = Executors.newCachedThreadPool();
SocketMain main = null;
Mirror mirror = Mirror.me(klass);
Borning<SocketMain> borning = null;
List<SocketAtom> atoms = new LinkedList<SocketAtom>();
while (!lock.isStop()) {
if (log.isDebugEnabled())
log.debug("create new main thread to wait...");
if(borning == null)
borning = mirror.getBorning(atoms, lock, server, service, saTable);
main = borning.born(new Object[]{atoms, lock, server, service, saTable});
if (log.isDebugEnabled())
log.debug("Ready for listen");
execs.execute(main);
if (log.isDebugEnabled())
log.debug("wait for accept ...");
// 如果没有接受套接字,那么自旋判断是不是有一个连接提示要关闭整个监听
while (!main.isAccepted()) {
// System.out.print(".");
// if(++i%80==0)
// System.out.println();
if (log.isDebugEnabled())
log.debug("wait lock ...");
synchronized (lock) {
try {
lock.wait();
}
catch (InterruptedException e) {
throw Lang.wrapThrow(e);
}
}
if (log.isDebugEnabled())
log.debugf( "check main accept [%s], lock [%s]",
main.isAccepted(),
lock.isStop());
if (lock.isStop())
break;
}
if (log.isDebugEnabled())
log.debug("Created a socket");
}
// 关闭所有的监听,退出程序
if (null != main && !main.isAccepted()) {
if (log.isInfoEnabled())
log.info("Notify waiting threads...");
try {
Socket ss = new Socket("127.0.0.1", port);
OutputStream sOut = ss.getOutputStream();
sOut.write("V~~".getBytes());
sOut.flush();
sOut.close();
ss.close();
}
catch (Exception e) {}
}
if (log.isInfoEnabled())
log.info("Stop connected threads...");
while (!execs.isTerminated())
execs.shutdown();
if (log.isInfoEnabled())
log.info("Close all sockets..");
try {