Skip to content

Commit

Permalink
Clent changes.
Browse files Browse the repository at this point in the history
  - Add timeout handling.
  - Change default remote.
  • Loading branch information
jiyuuchc authored and Nick-Kuang committed May 20, 2024
1 parent 4235c13 commit 6324f67
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class Constants {
public static final String DEFAULT_LACSS_CUSTOM_MODEL_FILEPATH = "";

public static final String KEY_LACSS_REMOTE_SERVER = "LACSS_REMOTE_SERVER";
public static final String DEFAULT_LACSS_REMOTE_SERVER = "localhost:50051";
public static final String DEFAULT_LACSS_REMOTE_SERVER = "localhost:7051";

public static final String KEY_LACSS_REMOTE_SERVER_TOKEN = "LACSS_REMOTE_SERVER_TOKEN";
public static final String DEFAULT_LACSS_REMOTE_SERVER_TOKEN = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,28 @@ public String getModelPath() {
}

LacssClient(String host, String token) {
if (token != null && token.trim().length() == 0) {
token = null;
}
this.token = token;
this.channel = Grpc.newChannelBuilder(host, InsecureChannelCredentials.create()).build();

this.channel = Grpc.newChannelBuilder(host, InsecureChannelCredentials.create())
.build();
this.blockingStub = LacssGrpc.newBlockingStub(channel);

this.localProcess = null;
this.modelPath = null;
}

LacssClient(String modelPath) throws IOException {
ProcessBuilder pb = new ProcessBuilder("python", "-m", "lacss.deploy.remote_server", "--local", modelPath);
pb.inheritIO().redirectErrorStream();
localProcess = pb.start();
this.localProcess = pb.start();

String target = "localhost:50051";
channel = Grpc.newChannelBuilder(target, InsecureChannelCredentials.create())
this.channel = Grpc.newChannelBuilder(target, InsecureChannelCredentials.create())
.build();
blockingStub = LacssGrpc.newBlockingStub(channel)
this.blockingStub = LacssGrpc.newBlockingStub(channel)
.withWaitForReady();

this.token = null;
Expand All @@ -52,14 +58,14 @@ public void shutdownLocalProcess() {
}

public LacssMsg.PolygonResult runDetection(LacssMsg.Input inputs) {
LacssBlockingStub stub = blockingStub.withDeadlineAfter(90, TimeUnit.SECONDS);

if (token != null) {
LacssTokenCredentials callCredentials = new LacssTokenCredentials(token);
return blockingStub
.withCallCredentials(callCredentials)
.runDetection(inputs);
} else {
return blockingStub.runDetection(inputs);
stub = stub.withCallCredentials(callCredentials);
}

return stub.runDetection(inputs);
}

@Override
Expand All @@ -68,4 +74,13 @@ public void finalize() {
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {}
}

@Override
public String toString() {
if (modelPath != null) {
return "Local: " + getModelPath();
} else {
return "Remote: " + blockingStub.getChannel() + ":" + token;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public LacssDetector(
final ImgPlus<T> img,
final Interval interval,
final Map< String, Object > settings,
final LacssClient client) {
final LacssClient client )
{
this.img = img;
this.interval = interval;
this.settings = settings;
Expand All @@ -58,7 +59,8 @@ public LacssDetector(
this.client = client;
}

private boolean getDetections(RandomAccessibleInterval<T> crop, LacssMsg.Settings settings) {
private boolean getDetections(RandomAccessibleInterval<T> crop, LacssMsg.Settings settings)
{
long[] dims = crop.dimensionsAsLongArray();
long n_ch = 1;
int ch_c = img.dimensionIndex(Axes.CHANNEL);
Expand Down Expand Up @@ -97,12 +99,16 @@ private boolean getDetections(RandomAccessibleInterval<T> crop, LacssMsg.Setting

LacssMsg.PolygonResult msg;
try {

// Logger.IJ_LOGGER.log("Connecting to server" + client.toString());

msg = client.runDetection(inputs);

} catch (StatusRuntimeException e) {
// logger.error(BASE_ERROR_MESSAGE + "Unable to communicate with the server.");
logger.error(BASE_ERROR_MESSAGE + e.getLocalizedMessage());
return false;
}
}

spots = new ArrayList<>( msg.getPolygonsCount() );
for ( LacssMsg.Polygon polygon : msg.getPolygonsList()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,13 @@ public LacssClient getClient() throws IOException
{
if (settings.get(Constants.KEY_LACSS_MODEL) == PretrainedModel.Remote) {

if (remoteClient == null) {
String host = (String) settings.get(Constants.KEY_LACSS_REMOTE_SERVER);
String token = (String) settings.get(Constants.KEY_LACSS_REMOTE_SERVER_TOKEN);
String host = (String) settings.get(Constants.KEY_LACSS_REMOTE_SERVER);
String token = (String) settings.get(Constants.KEY_LACSS_REMOTE_SERVER_TOKEN);

remoteClient = new LacssClient(host, token);
}
remoteClient = new LacssClient(host, token);

// Logger.IJ_LOGGER.log("Trying connecting to: " + host);
// Logger.IJ_LOGGER.log("Using client object: " + remoteClient.toString());

return remoteClient;

Expand Down Expand Up @@ -221,7 +222,7 @@ public SpotDetector< T > getDetector( final Interval interval, final int frame )

String errMsg = "Unable to start the python backend. " + e.getLocalizedMessage();

Logger.DEFAULT_LOGGER.error(errMsg);
Logger.IJ_LOGGER.error(errMsg);

return null;
}
Expand Down

0 comments on commit 6324f67

Please sign in to comment.