Skip to content

Commit

Permalink
Misc-updates (#1394)
Browse files Browse the repository at this point in the history
  • Loading branch information
supertick authored Jan 20, 2024
1 parent 226af58 commit 0731f65
Show file tree
Hide file tree
Showing 16 changed files with 181 additions and 723 deletions.
32 changes: 0 additions & 32 deletions src/main/java/org/myrobotlab/framework/CmdConfig.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/java/org/myrobotlab/framework/CmdOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static boolean contains(List<String> l, String flag) {

// launcher
@Option(names = { "-c",
"--config" }, description = "Specify a configuration set to start. The config set is a directory which has all the necessary configuration files. It loads runtime.yml first, and subsequent service configuration files will then load. \n example: --config data/config/my-config-dir")
"--config" }, fallbackValue="default", description = "Specify a configuration set to start. The config set is a directory which has all the necessary configuration files. It loads runtime.yml first, and subsequent service configuration files will then load. \n example: --config data/config/my-config-dir")
public String config = null;

@Option(names = {
Expand Down
30 changes: 17 additions & 13 deletions src/main/java/org/myrobotlab/framework/Outbox.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

import org.myrobotlab.codec.CodecUtils;
Expand All @@ -43,7 +44,6 @@
import org.myrobotlab.service.interfaces.Gateway;
import org.slf4j.Logger;


/*
* Outbox is a message based thread which sends messages based on addListener lists and current
* queue status. It is only aware of the Service directory, addListener lists, and operators.
Expand All @@ -63,14 +63,20 @@ public class Outbox implements Runnable, Serializable {
static public final String PROCESSANDBROADCAST = "PROCESSANDBROADCAST";

protected String name = null;
private transient LinkedList<Message> msgBox = new LinkedList<Message>();

private transient LinkedList<Message> msgBox = new LinkedList<Message>();

private boolean isRunning = false;

private boolean blocking = false;

int maxQueue = 1024;

int initialThreadCount = 1;

transient ArrayList<Thread> outboxThreadPool = new ArrayList<Thread>();

protected Map<String, FilterInterface> filters = new HashMap<>();
protected Map<String, FilterInterface> filters = new TreeMap<>();

public interface FilterInterface {
public boolean filter(Message msg);
Expand All @@ -79,7 +85,7 @@ public interface FilterInterface {
/**
* pub/sub listeners - HashMap &lt; {topic}, List {listeners} &gt;
*/
protected Map<String, List<MRLListener>> notifyList = new HashMap<String, List<MRLListener>>();
protected Map<String, List<MRLListener>> notifyList = new TreeMap<String, List<MRLListener>>();

List<MessageListener> listeners = new ArrayList<MessageListener>();

Expand Down Expand Up @@ -216,10 +222,10 @@ public void run() {
MRLListener listener = subList.get(i);
msg.setName(listener.callbackName);
msg.method = listener.callbackMethod;

if (!isFiltered(msg)) {
send(msg);
}
send(msg);
}

// must make new for internal queues
// otherwise you'll change the name on
Expand All @@ -234,15 +240,15 @@ public void run() {
}
} // while (isRunning)
}

public FilterInterface addFilter(String name, String method, FilterInterface filter) {
return filters.put(String.format("%s.%s", CodecUtils.getFullName(name), method), filter);
}

public FilterInterface removeFilter(String name, String method) {
return filters.remove(String.format("%s.%s", CodecUtils.getFullName(name), method));
}

public boolean isFiltered(Message msg) {
String fullname = CodecUtils.getFullName(msg.name);
if (filters.size() == 0 || !filters.containsKey(String.format("%s.%s", fullname, msg.method))) {
Expand All @@ -258,7 +264,7 @@ public int size() {

public void start() {
for (int i = outboxThreadPool.size(); i < initialThreadCount; ++i) {
Thread t = new Thread(this, name + "_outbox_" + i);
Thread t = new Thread(this, CodecUtils.getShortName(name) + "_outbox_" + i);
outboxThreadPool.add(t);
t.start();
}
Expand Down Expand Up @@ -380,6 +386,4 @@ public Map<String, List<MRLListener>> getNotifyList() {
return notifyList;
}



}
18 changes: 1 addition & 17 deletions src/main/java/org/myrobotlab/framework/Plan.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void clear() {
*/
public ServiceConfig remove(String service) {
RuntimeConfig rtConfig = (RuntimeConfig) config.get("runtime");
rtConfig.registry.remove(service);
rtConfig.remove(service);
return config.remove(service);
}

Expand Down Expand Up @@ -142,21 +142,5 @@ public void addRegistry(String service) {
runtime.add(service);
}

/**
* good to prune trees of peers from starting - expecially if the peers
* require re-configuring
*
* @param startsWith
* - removes RuntimeConfig.registry all services that start with
* input
*/
public void removeStartsWith(String startsWith) {
RuntimeConfig runtime = (RuntimeConfig) config.get("runtime");
if (runtime == null) {
log.error("removeRegistry - runtime null !");
return;
}
runtime.removeStartsWith(startsWith);
}

}
94 changes: 78 additions & 16 deletions src/main/java/org/myrobotlab/service/Emoji.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package org.myrobotlab.service;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.myrobotlab.codec.CodecUtils;
import org.myrobotlab.framework.Service;
import org.myrobotlab.io.FileIO;
import org.myrobotlab.logging.Level;
Expand All @@ -22,7 +24,6 @@
import org.myrobotlab.service.interfaces.TextPublisher;
import org.slf4j.Logger;


// emotionListener
// Links
// - http://googleemotionalindex.com/
Expand All @@ -32,8 +33,6 @@ public class Emoji extends Service<EmojiConfig> implements TextListener, StateCh

public final static Logger log = LoggerFactory.getLogger(Emoji.class);

// transient ImageDisplay display = null;

transient HttpClient http = null;

String lastState = null;
Expand Down Expand Up @@ -85,29 +84,64 @@ public void addEmojiMap() {

}

/**
* Map a reference of some text like "grinning face" to a unicode reference.
*
* @param keyword - words desired
* @param unicode - emoji reference
*/
public void addEmoji(String keyword, String unicode) {
log.info("emoji {}:{}", keyword, unicode);
((EmojiConfig) config).map.put(keyword, unicode);
}

/**
* clears all mappings
*/
public void clearEmojis() {
((EmojiConfig) config).map.clear();
}

@Override
public void startService() {
super.startService();

// FIXME - send default fullscreen always on top minAutoSize to display ?
// display = (ImageDisplay) startPeer("display");
http = (HttpClient) startPeer("http");

addEmojiMap();
}

public void display(String source) {
/**
* Returns a base 64 string representation of the emoji image
*
* @param source - text or unicode refrence
* @return - base64 png
* @throws IOException
*/
public String getBase64Image(String source) throws IOException {
ImageData img = getImageData(source);
String ret = CodecUtils.toBase64(FileIO.toByteArray(new File(img.src)));
return ret;
}

/**
* Returns filename and other info
*
* @param source - keyword or unicode reference
* @return ImageData
*/
public ImageData getImageData(String source) {

try {

if (source == null) {
error("emoji source cannot be null");
return null;
}

boolean isUnicode = source.toUpperCase().startsWith("U+");
if (isUnicode) {
source = source.toUpperCase();
}

String cacheDir = getEmojiCacheDir();

log.info("display source {} fullscreen {}", source);
Expand All @@ -121,12 +155,23 @@ public void display(String source) {
Map<String, String> map = ((EmojiConfig) config).map;

// check for keyword
if (map.containsKey(source)) {
String unicodeFileName = cacheDir + File.separator + map.get(source) + ".png";
if (map.containsKey(source) || isUnicode) {
String unicodeFileName = null;
if (isUnicode) {
unicodeFileName = cacheDir + File.separator + source + ".png";
} else {
unicodeFileName = cacheDir + File.separator + map.get(source) + ".png";
}
if (!new File(unicodeFileName).exists()) {
try {
String fetchCode = map.get(source).replace("+", "").toLowerCase();
String url = ((EmojiConfig) config).emojiSourceUrlTemplate.replace("{size}", "" + getSize()).replace("{code}", fetchCode).replace("{CODE}", source);
String fetchCode = null;
if (isUnicode) {
fetchCode = source.replace("+", "").toLowerCase();
} else {
fetchCode = map.get(source).replace("+", "").toLowerCase();
}
String url = ((EmojiConfig) config).emojiSourceUrlTemplate.replace("{size}", "" + getSize())
.replace("{code}", fetchCode).replace("{CODE}", source);
byte[] bytes = http.getBytes(url);
FileIO.toFile(unicodeFileName, bytes);
} catch (Exception e) {
Expand Down Expand Up @@ -162,11 +207,18 @@ public void display(String source) {
img.src = filename;
img.source = getName();

invoke("publishImage", img);
return img;

} catch (Exception e) {
log.error("displayFullScreen threw", e);
}
return null;
}

public void display(String source) {
ImageData img = getImageData(source);
invoke("publishImage", img);
invoke("publishDisplay", img);
}

public int getSize() {
Expand All @@ -193,10 +245,10 @@ public void onText(String text) {
public void onStateChange(StateChange event) {
log.info("handleEvent {}", event);
EmojiData emoji = new EmojiData();
emoji.name = event.current;
emoji.name = event.state;
emoji.unicode = ((EmojiConfig) config).map.get(emoji.name);
invoke("publishEmoji", emoji);
display(event.current);
display(event.state);
}

public void publishEmoji(EmojiData emoji) {
Expand All @@ -219,12 +271,17 @@ public static void main(String[] args) {
// scan text for emotional words - addEmotionWordPair(happy 1f609) ...
LoggingFactory.init(Level.WARN);

Runtime.startConfig("emoji-display-2");
// Runtime.startConfig("emoji-display-2");

// Runtime.startConfig("emoji-display-1");
// Runtime.saveConfig("emoji-display-2");

Emoji emoji = (Emoji) Runtime.start("emoji", "Emoji");

String imgTag = String.format("<img src=\"data:img/png;base64,%s\" />", emoji.getBase64Image("U+1F98D"));

FileIO.toFile("emoji.html", String.format("<html><body>%s</body></html>", imgTag));

ImageDisplay display = (ImageDisplay) Runtime.start("display", "ImageDisplay");
emoji.attachImageListener(display);

Expand Down Expand Up @@ -357,6 +414,11 @@ public static void main(String[] args) {
}
}

public String getImageFile(String emoji) {
ImageData data = getImageData(emoji);
return data.src;
}

@Override
public void attachTextPublisher(TextPublisher service) {
if (service == null) {
Expand Down
Loading

0 comments on commit 0731f65

Please sign in to comment.