Attached the server code. at line 154, "while (reader.read()) {" it will 
throw

java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(SocketInputStream.java:196)
        at java.net.SocketInputStream.read(SocketInputStream.java:122)
        at java.net.SocketInputStream.read(SocketInputStream.java:210)
        at java.io.DataInputStream.readByte(DataInputStream.java:265)
        at 
org.jacoco.core.data.ExecutionDataReader.read(ExecutionDataReader.java:82)
        at 
ExecutionDataServerWithDumpTimer$Handler.run(ExecutionDataServerWithDumpTimer.java:152)
        at java.lang.Thread.run(Thread.java:812)

as you can see, the reader keep the connection, always read the data from 
jacoco agent. my question is if reconnection, will the bytes that already 
read into the the server lost? since these data did not flush into the 
local file, also for the data not sent successfully will still store on the 
agent? 



在 2016年10月21日星期五 UTC+8下午2:29:23,Marc R. Hoffmann写道:
>
> Hi,
>
> to get an better picture what actually happens: Can you please provide the 
> Java exceptions that JaCoCo prints to syserr when the connection gets 
> disconnected?
>
> So what behavior do you expect from JaCoCo in tcpclientmode? Reconnect 
> after failure?
>
> Regards,
> -marc 
>
> On 21.10.16 07:27, van basten wrote:
>
> The problem for our environment is the network very unstable, alwasy lost 
> connection after one hour.
>
> As you know, for tcpclient mode, the socket between jacoco agent and the 
> server is always connected.
>
> Why we do not use the tcpserver mode? since we do not know how many 
> machines installed jacoco agent, we need use the tcpclient mode to 
> automatically connect to the server, then we can know.
>
> any unclear, please let me know.
> -- 
> You received this message because you are subscribed to the Google Groups 
> "JaCoCo and EclEmma Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected] <javascript:>.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/jacoco/36132c30-7f92-40fe-8cdf-0237286a148e%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/jacoco/36132c30-7f92-40fe-8cdf-0237286a148e%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"JaCoCo and EclEmma Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jacoco/ba5a196d-61fb-4975-9d91-db6fba481375%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.jacoco.core.data.ExecutionData;
import org.jacoco.core.data.IExecutionDataVisitor;
import org.jacoco.core.data.ISessionInfoVisitor;
import org.jacoco.core.data.SessionInfo;
import org.jacoco.core.runtime.RemoteControlReader;
import org.jacoco.core.runtime.RemoteControlWriter;
/**
 * This example starts a socket server to collect coverage from agents that run
 * in output mode <code>tcpclient</code>. The collected data is dumped to a
 * local file.
 * 
 * The TCP server will collect data from its client at every specified interval.
 */
public class ExecutionDataServerWithDumpTimer {
    private static String jacocoData = "jacoco.exec";
    private static String filePath = "/export/jacoco/exec/";
    private static final String ADDRESS = "192.168.1.102";
    private static final String configPath = "/export/home/jacoco/jacococonfig/config.prop";
    private static final int PORT = 6300;
    private static final int INTERVAL_DUMP_SEC = 14400;
    private static String currentDate;
//    private static RemoteControlWriter fileWriter;
    private static List<Handler> listHandler;
    
    /**
     * Start the server as a standalone program.
     * 
     * @param args
     * @throws IOException
     * @throws ParseException 
     */
	public static void main(final String[] args) throws IOException, ParseException {
		
		final ConfigurationHandler configHandler = new  ConfigurationHandler();
		
		System.out.println("Starting TCP server on: " + ADDRESS + ":" + PORT);
		final ServerSocket server = new ServerSocket(PORT, 0,InetAddress.getByName(ADDRESS));
		listHandler = new ArrayList<Handler>();
		
		// thread to collect dump data at specified INTERVAL_DUMP_SEC
		Runnable runDumpTimer = new Runnable() {
			@Override
			public void run() {
				while (true) {
					try {
						int interval = configHandler.getData();
						if (interval != -1) {
							Thread.sleep(interval * 1000);
						}
						else {
							Thread.sleep(INTERVAL_DUMP_SEC * 1000);
						}
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					Iterator<Handler> it = listHandler.iterator();
					while (it.hasNext()) {
						ExecutionDataServerWithDumpTimer.Handler handler = (ExecutionDataServerWithDumpTimer.Handler) it.next();
						boolean toRemove = false;
						String id = handler.getId();
						try {
							if (!handler.isAlive()) {
								System.out.println("Socket closed, removing handler: "+ id);
								toRemove = true;
							} else {
								handler.captureDump(true, true);
							}
						} catch (IOException e) {
							System.out.println("Socket error: "+ e.getMessage() + ", removing handler: "+ id);
							toRemove = true;
						} finally {
							if (toRemove) {
								it.remove();
							}
						}
					}
				}
			}
		};
		
		Thread threadDumpTimer = new Thread(runDumpTimer, "threadDumpTimer");
		threadDumpTimer.start();
		
		while (true) {
			System.out.println("waiting for connection from client");
			Socket socket = server.accept();
			System.out.println("Remote connection detected, openning socket on local port: "+ socket.getLocalPort());
			System.out.println("Remote Socket Address : " + socket.getRemoteSocketAddress().toString());
			
			currentDate = getCurrentDateFormatted("yyyyMMdd_hh_mm_ss_");
			
			// String a = "/10.129.126.230:45386";
			String remoteSocketAddress = socket.getRemoteSocketAddress().toString().replace('/', ' ').trim();
			int index = remoteSocketAddress.indexOf(":");
			remoteSocketAddress = remoteSocketAddress.substring(0,index); // 10.129.126.230
			
			String jacocoDataFilePath = filePath + currentDate + remoteSocketAddress + "_" + jacocoData;
			
			System.out.println("Output file is: "+ new File(jacocoDataFilePath).getAbsolutePath());
			
			RemoteControlWriter fileWriter = new RemoteControlWriter(new FileOutputStream(jacocoDataFilePath));
			
			final Handler handler = new Handler(socket, fileWriter);
			listHandler.add(handler);
			new Thread(handler).start();
		}
	}

	private static class Handler implements Runnable, ISessionInfoVisitor,
			IExecutionDataVisitor {
		private final Socket socket;
		private final RemoteControlReader reader;
		private RemoteControlWriter fileWriter;
		private final RemoteControlWriter remoteWriter;
		private String id;
	    
		public String getId() {
			return id;
		}

		Handler(final Socket socket, final RemoteControlWriter fileWriter) throws IOException
				 {
			this.socket = socket;
			this.fileWriter = fileWriter;
			// Just send a valid header:
			remoteWriter = new RemoteControlWriter(socket.getOutputStream());
			reader = new RemoteControlReader(socket.getInputStream());
			reader.setSessionInfoVisitor(this);
			reader.setExecutionDataVisitor(this);
			
		}

		public void run() {
			try {
				
				while (reader.read()) {
					
				}
				
				socket.close();
				
				synchronized (fileWriter) {
					fileWriter.flush();
				}
			} catch (final IOException e) {
				e.printStackTrace();
			} 
		}

		
		public void visitSessionInfo(final SessionInfo info) {
			id = info.getId();
			System.out.printf("Retrieving execution Data for session: %s%n",
					info.getId());
			synchronized (fileWriter) {
				fileWriter.visitSessionInfo(info);
			}
		}

		public void visitClassExecution(final ExecutionData data) {
			synchronized (fileWriter) {
				fileWriter.visitClassExecution(data);
			}
		}
		
		public void captureDump(boolean dump, boolean reset) throws IOException {
			remoteWriter.visitDumpCommand(dump, reset);
		}

		public boolean isAlive() {
			if (socket != null && socket.isConnected()) {
				return true;
			}
			return false;
		}
		
	}
	
	public static String getCurrentDateFormatted(String dateFormat)
    {
        SimpleDateFormat dateFormatter;
        dateFormatter = new SimpleDateFormat(dateFormat);
        Date currentDate = new Date();
        return dateFormatter.format(currentDate);
    }
	
	
	private static final class ConfigurationHandler {
//		private File config = new File("/export/home/jacoco/config.prop");
		//configPath
		private File config = new File(configPath);
		
		private ConfigurationHandler(){
			
		}
		
		private int getData(){
			try {
				BufferedReader br = new BufferedReader (new FileReader(config));
				String data = br.readLine();
				int retVal = -1;
				if (data != null) {
					retVal = Integer.parseInt(data);
					System.out.println("Time interval is set to : " + retVal);
				}else {
					throw new RuntimeException("the configuration file data doesn't exist!");
				}
				br.close();
				return retVal;
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				throw new RuntimeException("the configuration file  doesn't exist!");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				throw new RuntimeException("the configuration file cannot be read!");
			}
		}
	}
}

Reply via email to