Skip to content

Commit

Permalink
Smart port reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
SamCarlberg committed Nov 9, 2017
1 parent d16e05d commit af7ebeb
Showing 1 changed file with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@
import org.opencv.core.Mat;

import java.lang.reflect.Field;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.bytedeco.javacpp.opencv_core.CV_8S;
import static org.bytedeco.javacpp.opencv_core.CV_8U;
Expand Down Expand Up @@ -57,7 +61,11 @@ public class PublishVideoOperation implements Operation {

@SuppressWarnings("PMD.AssignmentToNonFinalStatic")
private static int numSteps;
private static final int MAX_STEP_COUNT = 10;
private static final int MAX_STEP_COUNT = 10; // limit ports to 1180-1189
private static final Deque<Integer> availablePorts =
Stream.iterate(PORT, i -> i + 1)
.limit(MAX_STEP_COUNT)
.collect(Collectors.toCollection(LinkedList::new));

private final InputSocket<opencv_core.Mat> inputSocket;
private final InputSocket<Number> qualitySocket;
Expand All @@ -81,11 +89,13 @@ public PublishVideoOperation(InputSocket.Factory inputSocketFactory) {
this.qualitySocket = inputSocketFactory.create(SocketHints.Inputs
.createNumberSliderSocketHint("Quality", 80, 0, 100));

server = new MjpegServer("GRIP server " + numSteps, PORT + numSteps);
serverSource = new CvSource("GRIP CvSource" + numSteps, VideoMode.PixelFormat.kMJPEG, 0, 0, 0);
int ourPort = availablePorts.removeFirst();

server = new MjpegServer("GRIP video publishing server " + ourPort, ourPort);
serverSource = new CvSource("GRIP CvSource:" + ourPort, VideoMode.PixelFormat.kMJPEG, 0, 0, 0);
server.setSource(serverSource);
cameraPublisherTable.putStringArray("streams",
new String[]{CameraServerJNI.getHostname() + ":" + server.getPort()});
new String[]{CameraServerJNI.getHostname() + ":" + ourPort});

numSteps++;
}
Expand Down Expand Up @@ -125,6 +135,7 @@ public void perform() {
public synchronized void cleanUp() {
// Stop the video server if there are no Publish Video steps left
numSteps--;
availablePorts.addFirst(server.getPort());
}

private void copyJavaCvToOpenCvMat(opencv_core.Mat javaCvMat, Mat openCvMat) {
Expand Down

0 comments on commit af7ebeb

Please sign in to comment.