Skip to content

Commit

Permalink
Merge branch 'develop' into musicsearch
Browse files Browse the repository at this point in the history
  • Loading branch information
kwatters committed Jun 27, 2023
2 parents 1d7c498 + 1aa2004 commit 314a547
Show file tree
Hide file tree
Showing 60 changed files with 1,885 additions and 953 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# TODO clean this up !
/.project
/.classpath
/*.iml
/.settings
/src/main/resources/resource/WebGui/react
/src/main/resources/resource/Vertx/app
Expand Down Expand Up @@ -83,3 +82,4 @@
/lastRestart.py
/.factorypath
start.yml
*.iml
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,18 @@
<version>0.10.9.7</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>cpython-platform</artifactId>
<version>3.11.3-1.5.9</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>cpython</artifactId>
<version>3.11.3-1.5.9</version>
<scope>provided</scope>
</dependency>
<!-- Py4j end -->

<!-- Python begin -->
Expand Down
23 changes: 19 additions & 4 deletions src/main/java/org/myrobotlab/codec/CodecUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -469,8 +469,8 @@ static public byte[] getBytes(Object o) throws IOException {
* @return The simple name of the service. If null,
* will return null, and if already a simple name
* then will return name
*/
static public String shortName(String name) {
*/
static public String getShortName(String name) {
if (name == null) {
return null;
}
Expand All @@ -480,6 +480,7 @@ static public String shortName(String name) {
return name;
}
}


// TODO
// public static Object encode(Object, encoding) - dispatches appropriately
Expand Down Expand Up @@ -516,7 +517,7 @@ static public String getId(String name) {
* @param name The service name to normalize
* @return The normalized (full) name, or null if name is null
*/
public static String normalizeServiceName(String name) {
public static String getFullName(String name) {
if (name == null) {
return null;
}
Expand All @@ -537,7 +538,7 @@ public static String normalizeServiceName(String name) {
* @return Whether the two names are effectively equal
*/
public static boolean checkServiceNameEquality(String name1, String name2) {
return Objects.equals(normalizeServiceName(name1), normalizeServiceName(name2));
return Objects.equals(getFullName(name1), getFullName(name2));
}

/**
Expand Down Expand Up @@ -1470,6 +1471,20 @@ public static <T extends Object> T fromYaml(String data, Class<T> clazz) {
return (T) yaml.load(data);
}


/**
* Checks if the service name is local to the current process instance
* @param name The service name to be checked
* @return Whether the service name is local to the given ID
*/
public static boolean isLocal(String name) {
if (!name.contains("@")) {
return true;
}
return name.substring(name.indexOf("@") + 1).equals(Platform.getLocalInstance().getId());
}


/**
* Checks if the service name given by name is local,
* i.e. it has no remote ID (has no '@' symbol), or
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/myrobotlab/framework/Outbox.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public Set<String> getAttached(String publishingPoint, boolean localOnly) {
Set<String> unique = new TreeSet<>();
for (List<MRLListener> subcribers : notifyList.values()) {
for (MRLListener listener : subcribers) {
if (localOnly && listener.callbackName.contains("@")) {
if (localOnly && !CodecUtils.isLocal(listener.callbackName)) {
continue;
}
if (publishingPoint == null) {
Expand Down Expand Up @@ -337,7 +337,8 @@ public void reset() {
* the name of the listener to detach
*
*/
synchronized public void detach(String name) {
synchronized public void detach(String service) {
String name = CodecUtils.getFullName(service);
for (String topic : notifyList.keySet()) {
List<MRLListener> subscribers = notifyList.get(topic);
ArrayList<MRLListener> smallerList = new ArrayList<>();
Expand Down
56 changes: 45 additions & 11 deletions src/main/java/org/myrobotlab/framework/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,7 @@ public void addListener(String topicMethod, String callbackName) {
*/
@Override
public void addListener(String topicMethod, String callbackName, String callbackMethod) {
callbackName = CodecUtils.getFullName(callbackName);
MRLListener listener = new MRLListener(topicMethod, callbackName, callbackMethod);
if (outbox.notifyList.containsKey(listener.topicMethod)) {
// iterate through all looking for duplicate
Expand Down Expand Up @@ -1294,6 +1295,14 @@ final public Object invokeOn(boolean blockLocally, Object obj, String methodName
}
retobj = method.invoke(obj, params);
if (blockLocally) {
Outbox outbox = null;
if (obj instanceof ServiceInterface) {
outbox = ((ServiceInterface)obj).getOutbox();
} else {
return retobj;
}


List<MRLListener> subList = outbox.notifyList.get(methodName);
// correct? get local (default?) gateway
Runtime runtime = Runtime.getInstance();
Expand Down Expand Up @@ -1375,6 +1384,11 @@ public boolean isRunning() {
* Default load config method, subclasses should override this to support
* service specific configuration in the service yaml files.
*
* apply is the first function to be called after construction of a service,
* then startService will be called
*
* construct -&gt; apply -&gt; startService
*
*/
@Override
public ServiceConfig apply(ServiceConfig inConfig) {
Expand Down Expand Up @@ -1452,6 +1466,15 @@ public void setConfig(ServiceConfig config) {
this.config = config;
}

@Override
public void setConfigValue(String fieldname, Object value) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
log.info("setting field name fieldname {} to {}", fieldname, value);

Field field = config.getClass().getDeclaredField(fieldname);
// field.setAccessible(true); should not need this - it "should" be public
field.set(config, value);
}

@Override
@Deprecated /*
* this is being used wrongly - Runtime knows how to load services
Expand Down Expand Up @@ -1545,6 +1568,7 @@ public void removeListener(String topicMethod, String callbackName) {

@Override
public void removeListener(String outMethod, String serviceName, String inMethod) {
String fullName = CodecUtils.getFullName(serviceName);
if (outbox.notifyList.containsKey(outMethod)) {
List<MRLListener> nel = outbox.notifyList.get(outMethod);
nel.removeIf(listener -> {
Expand All @@ -1557,14 +1581,14 @@ public void removeListener(String outMethod, String serviceName, String inMethod
// subscriptions to the same topic (one to many mapping), the first in the list would be removed
// instead of the requested one.
if (listener.callbackMethod.equals(inMethod)
&& CodecUtils.checkServiceNameEquality(listener.callbackName, serviceName)) {
log.info("removeListener requested {}.{} to be removed", serviceName, outMethod);
&& CodecUtils.checkServiceNameEquality(listener.callbackName, fullName)) {
log.info("removeListener requested {}.{} to be removed", fullName, outMethod);
return true;
}
return false;
});
} else {
log.info("removeListener requested {}.{} to be removed - but does not exist", serviceName, outMethod);
log.info("removeListener requested {}.{} to be removed - but does not exist", fullName, outMethod);
}
}

Expand Down Expand Up @@ -1948,11 +1972,7 @@ synchronized public void startService() {
}
thisThread.start();
isRunning = true;
Runtime runtime = Runtime.getInstance();
if (runtime != null) {
runtime.invoke("started", getName()); // getFullName()); - removed
// fullname
}
send("runtime", "started", getName());

} else {
log.debug("startService request: service {} is already running", name);
Expand Down Expand Up @@ -1987,6 +2007,11 @@ public void subscribe(String topicName, String topicMethod) {
String callbackMethod = CodecUtils.getCallbackTopicName(topicMethod);
subscribe(topicName, topicMethod, getFullName(), callbackMethod);
}

@Override
public void subscribe(String service, String method, String callback) {
subscribe(service, method, getFullName(), callback);
}

public void subscribeTo(String service, String method) {
subscribe(service, method, getFullName(), CodecUtils.getCallbackTopicName(method));
Expand All @@ -2004,8 +2029,10 @@ public void unsubscribeToRuntime(String method) {
unsubscribe(Runtime.getInstance().getFullName(), method, getFullName(), CodecUtils.getCallbackTopicName(method));
}

@Override
// TODO make protected or private
public void subscribe(String topicName, String topicMethod, String callbackName, String callbackMethod) {
topicName = CodecUtils.getFullName(topicName);
callbackName = CodecUtils.getFullName(callbackName);
log.info("subscribe [{}/{} ---> {}/{}]", topicName, topicMethod, callbackName, callbackMethod);
// TODO - do regex matching
if (topicName.contains("*")) { // FIXME "any regex expression
Expand Down Expand Up @@ -2039,9 +2066,16 @@ public void unsubscribe(String topicName, String topicMethod) {
String callbackMethod = CodecUtils.getCallbackTopicName(topicMethod);
unsubscribe(topicName, topicMethod, getFullName(), callbackMethod);
}

@Override
public void unsubscribe(String topicName, String topicMethod, String callback) {
unsubscribe(topicName, topicMethod, getFullName(), callback);
}

// TODO make protected or private
public void unsubscribe(String topicName, String topicMethod, String callbackName, String callbackMethod) {
topicName = CodecUtils.getFullName(topicName);
callbackName = CodecUtils.getFullName(callbackName);
log.info("unsubscribe [{}/{} ---> {}/{}]", topicName, topicMethod, callbackName, callbackMethod);
send(Message.createMessage(getFullName(), topicName, "removeListener", new Object[] { topicMethod, callbackName, callbackMethod }));
}
Expand Down Expand Up @@ -2204,7 +2238,7 @@ public void attach(String serviceName) throws Exception {
*/
@Override
public boolean isAttached(String serviceName) {
return getAttached().contains(serviceName);
return getAttached().contains(CodecUtils.getFullName(serviceName));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,54 @@

public interface MessageSubscriber {

public void subscribe(NameProvider topicName, String topicKey);

public void subscribe(String topicName, String topicKey);

public void subscribe(String topicName, String topicMethod, String callbackName, String callbackMethod);

public void unsubscribe(NameProvider topicName, String topicKey);

public void unsubscribe(String topicName, String topicKey);

public void unsubscribe(String topicName, String topicMethod, String callbackName, String callbackMethod);
/**
* This will subscribe to a NameProviders method. The callback will be
* automatically generated. Rules are publish{Method} or get{Method} will
* callback with on{Method}.
*
* @param service
* @param method
*/
public void subscribe(NameProvider service, String method);

/**
* Service name is supplied and method to subscribe to. The callback will be
* automatically generated. Rules are publish{Method} or get{Method}
* will callback with on{Method}.
*
* @param service
* @param method
*/
public void subscribe(String service, String method);

/**
* Subscribe with callback. The callback is explicitly set.
* @param service
* @param method
* @param callback
*/
public void subscribe(String service, String method, String callback);

/***
* Unsubscribe from a NameProviders method.
* @param service
* @param method
*/
public void unsubscribe(NameProvider service, String method);

/**
* Unsubscribe from a service method.
* @param service
* @param method
*/
public void unsubscribe(String service, String method);

/**
* Unsubscribe from a service method with an explicit callback.
* @param service
* @param method
* @param callback
*/
public void unsubscribe(String service, String method, String callback);

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.myrobotlab.service.meta.abstracts.MetaData;
import org.slf4j.Logger;

public interface ServiceInterface extends ServiceQueue, LoggingSink, NameTypeProvider, MessageSubscriber, MessageSender, StateSaver, Invoker, StatePublisher, StatusPublisher,
public interface ServiceInterface extends ServiceQueue, LoggingSink, NameTypeProvider, MessageSubscriber, MessageSender, StateSaver, Invoker, StatePublisher, StatusPublisher,
ServiceStatus, TaskManager, Attachable, MessageInvoker, Comparable<ServiceInterface> {

// does this work ?
Expand Down Expand Up @@ -140,6 +140,15 @@ public interface ServiceInterface extends ServiceQueue, LoggingSink, NameTypePro
*/
void setConfig(ServiceConfig config);

/**
* reflectively sets a part of config
*
* @param fieldname - the name of the config field
* @param value - the value
*/
void setConfigValue(String fieldname, Object value) throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException;


/**
* Configure a service by merging in configuration
*
Expand Down Expand Up @@ -210,6 +219,7 @@ public interface ServiceInterface extends ServiceQueue, LoggingSink, NameTypePro

/**
* Get a clone of config that is filtered based on service preference
*
* @return
*/
ServiceConfig getFilteredConfig();
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/org/myrobotlab/io/FileIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -1574,6 +1574,37 @@ public static String getExt(final String filename) {
}
return null;
}

/**
* validate a directory exists
* @param dir
* @return
*/
public static boolean checkDir(String dir) {
try {
File check = new File(dir);
return check.exists() && check.isDirectory();
} catch (Exception e) {
log.error("checkDir threw", e);
}
return false;
}

/**
* validate a file exists
* @param filename
* @return
*/
public static boolean checkFile(String filename) {
try {
File check = new File(filename);
return check.exists() && !check.isDirectory();
} catch (Exception e) {
log.error("checkDir threw", e);
}
return false;
}


/**
* flips all \ to / or / to \ depending on OS
Expand All @@ -1594,4 +1625,5 @@ public static String normalize(String dirPath) {
}
}


}
Loading

0 comments on commit 314a547

Please sign in to comment.