<commit_before>package com.ctrip.xpipe.simpleserver;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

import org.slf4j.LoggerFactory;

import com.ctrip.xpipe.lifecycle.AbstractLifecycle;

import org.slf4j.Logger;

/**
 * @author wenchao.meng
 *
 * 2016年4月15日 下午3:01:03
 */
public class Server extends AbstractLifecycle{
	
	protected Logger logger = LoggerFactory.getLogger(getClass());

	private int port;
	private IoActionFactory ioActionFactory;
	private ExecutorService executors = Executors.newCachedThreadPool();
	private ServerSocket ss;
	private AtomicInteger connected = new AtomicInteger(0);
	private AtomicInteger totalConnected = new AtomicInteger(0);
	
	public Server(int port, IoActionFactory ioActionFactory){
		this.port = port;
		this.ioActionFactory = ioActionFactory;
	}
	
	public int getPort() {
		return port;
	}
	
	public int getConnected() {
		return connected.get();
	}
	
	public int getTotalConnected() {
		return totalConnected.get();
	}
	
	@Override
	protected void doStart() throws Exception {
		
		final CountDownLatch latch = new CountDownLatch(1);
		executors.execute(new Runnable() {
			
			@Override
			public void run() {
				
				try {
					ss = new ServerSocket(port);
					if(logger.isInfoEnabled()){
						logger.info("[run][listening]" + port);
					}
					latch.countDown();
					while(true){
						
						Socket socket = ss.accept();
						if(logger.isInfoEnabled()){
							logger.info("[run][new socket]" + socket);
						}
						connected.incrementAndGet();
						totalConnected.incrementAndGet();
						IoAction ioAction = ioActionFactory.createIoAction();
						if(ioAction instanceof SocketAware){
							((SocketAware) ioAction).setSocket(socket);
						}
						executors.execute(new Task(socket, ioAction));
					}
					
				} catch (IOException e) {
					logger.warn("[run]" + port + "," + e.getMessage());
				}
			}
		});
		latch.await();
	}

	@Override
	protected void doStop() throws Exception {
		if(ss != null){
			ss.close();
		}
	}
	
	
	public class Task implements Runnable{
		
		private Socket socket;
		private IoAction ioAction;<commit_msg>Log after count incr in test servers<commit_after>package com.ctrip.xpipe.simpleserver;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

import org.slf4j.LoggerFactory;

import com.ctrip.xpipe.lifecycle.AbstractLifecycle;

import org.slf4j.Logger;

/**
 * @author wenchao.meng
 *
 * 2016年4月15日 下午3:01:03
 */
public class Server extends AbstractLifecycle{
	
	protected Logger logger = LoggerFactory.getLogger(getClass());

	private int port;
	private IoActionFactory ioActionFactory;
	private ExecutorService executors = Executors.newCachedThreadPool();
	private ServerSocket ss;
	private AtomicInteger connected = new AtomicInteger(0);
	private AtomicInteger totalConnected = new AtomicInteger(0);
	
	public Server(int port, IoActionFactory ioActionFactory){
		this.port = port;
		this.ioActionFactory = ioActionFactory;
	}
	
	public int getPort() {
		return port;
	}
	
	public int getConnected() {
		return connected.get();
	}
	
	public int getTotalConnected() {
		return totalConnected.get();
	}
	
	@Override
	protected void doStart() throws Exception {
		
		final CountDownLatch latch = new CountDownLatch(1);
		executors.execute(new Runnable() {
			
			@Override
			public void run() {
				
				try {
					ss = new ServerSocket(port);
					if(logger.isInfoEnabled()){
						logger.info("[run][listening]" + port);
					}
					latch.countDown();
					while(true){
						
						Socket socket = ss.accept();
						connected.incrementAndGet();
						totalConnected.incrementAndGet();
						if(logger.isInfoEnabled()){
							logger.info("[run][new socket]" + socket);
						}
						IoAction ioAction = ioActionFactory.createIoAction();
						if(ioAction instanceof SocketAware){
							((SocketAware) ioAction).setSocket(socket);
						}
						executors.execute(new Task(socket, ioAction));
					}
					
				} catch (IOException e) {
					logger.warn("[run]" + port + "," + e.getMessage());
				}
			}
		});
		latch.await();
	}

	@Override
	protected void doStop() throws Exception {
		if(ss != null){
			ss.close();
		}
	}
	
	
	public class Task implements Runnable{
		
		private Socket socket;
		private IoAction ioAction;
{'fraction_non_alphanumeric': 0.11332633788037776, 'fraction_numerical': 0.007135362014690451, 'mean_word_length': 3.7422885572139304, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3433482', 'n_tokens_mistral': 1792, 'n_tokens_neox': 1446, 'n_words': 323}