Skip to content

Commit

Permalink
Do not dump exception in WebcamStreamer on connection close
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Nov 10, 2017
1 parent facdf3b commit 3876d3c
Showing 1 changed file with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
import org.slf4j.LoggerFactory;


/**
* This is very simple class which allows video from webcam to be exposed as MJPEG stream on a given
* port. The mapping between webcam and port is one-to-one, which means that a single port need to
* be allocated for every webcam you want to stream from.
*
* @author Bartoisz Firyn (sarxos)
*/
public class WebcamStreamer implements ThreadFactory, WebcamListener {

private static final Logger LOG = LoggerFactory.getLogger(WebcamStreamer.class);
Expand All @@ -33,12 +40,9 @@ private class Acceptor implements Runnable {

@Override
public void run() {
try {
ServerSocket server = new ServerSocket(port);
try (ServerSocket server = new ServerSocket(port)) {
while (started.get()) {
Socket socket = server.accept();
LOG.info("New connection from {}", socket.getRemoteSocketAddress());
executor.execute(new Connection(socket));
executor.execute(new Connection(server.accept()));
}
} catch (Exception e) {
LOG.error("Cannot accept socket connection", e);
Expand All @@ -57,6 +61,8 @@ public Connection(Socket socket) {
@Override
public void run() {

LOG.info("New connection from {}", socket.getRemoteSocketAddress());

BufferedReader br = null;
BufferedOutputStream bos = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Expand Down Expand Up @@ -135,9 +141,19 @@ public void run() {
bos.write(CRLF.getBytes());
bos.flush();
} catch (SocketException e) {
LOG.error("Socket exception from " + socket.getRemoteSocketAddress(), e);

if (!socket.isConnected()) {
LOG.debug("Connection to client has been lost");
}
if (socket.isClosed()) {
LOG.debug("Connection to client is closed");
}

br.close();
bos.close();

LOG.debug("Socket exception from " + socket.getRemoteSocketAddress(), e);

return;
}

Expand Down

0 comments on commit 3876d3c

Please sign in to comment.