Skip to content

Commit

Permalink
feat(agent): adding functionality to serve an agent's store directory
Browse files Browse the repository at this point in the history
  • Loading branch information
notthetup committed Oct 5, 2024
1 parent 3838dad commit c91f248
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
51 changes: 51 additions & 0 deletions src/main/java/org/arl/fjage/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

package org.arl.fjage;

import java.io.File;
import java.io.Serializable;
import java.util.*;
import java.util.TimerTask;
Expand All @@ -20,7 +21,10 @@
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.arl.fjage.connectors.WebServer;
import org.arl.fjage.persistence.Store;
import org.arl.fjage.remote.MasterContainer;
import org.arl.fjage.remote.SlaveContainer;

/**
Expand Down Expand Up @@ -696,6 +700,26 @@ public Store getStore() {
return Store.getInstance(this);
}

/**
* Serve the Agent's store over HTTP.
*
* @param enable true to enable, false to disable
* @return true if successful, false otherwise
*/
public boolean serveStore(boolean enable){
WebServer webServer = getWebServer();
if (webServer != null) {
String path = "store/" + this.getClass().getCanonicalName().replace(".", "/");
if (webServer.hasContext(path) == enable) return true;
if (enable) webServer.add("/"+path, new File(path+"/"));
else webServer.remove("/"+path);
return true;
}
log.warning("No web server found");
return false;
}


/**
* Deep clones an object. This is typically used to explicitly clone a message for
* modification when autocloning is not enabled.
Expand Down Expand Up @@ -845,6 +869,33 @@ public final void run() {
platform = null;
}

/**
* Finds the web server that provides the websocket connection for the agent's container
*
* @return the web server that provides the websocket connection for the container
*/
WebServer getWebServer() {
if (container instanceof MasterContainer) {
// look for connector that starts with ws:// and get it's port
// ((MasterContainer) container).getConnectors()
for (String connector : ((MasterContainer) container).getConnectors()) {
if (connector.startsWith("ws://")) {
// Parse a string like this and find the port "ws://127.0.0.1:8080/ws"
String[] parts = connector.split(":");
if (parts.length > 2) {
try {
int port = Integer.parseInt(parts[2].split("/")[0]);
return WebServer.getInstance(port);
} catch (NumberFormatException e) {
log.warning("Invalid port number in connector: "+connector);
}
}
}
}
}
return null;
}

private class InternalRequestSender
implements RequestSender {

Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/arl/fjage/shell/ShellAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,6 @@ public void onTick() {
}
}
});

}

@Override
Expand Down

0 comments on commit c91f248

Please sign in to comment.