Last active
July 3, 2020 16:40
-
-
Save junkdog/8453832 to your computer and use it in GitHub Desktop.
AtomicQueue example.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.junkdog.hallucinolog.component; | |
import lombok.ToString; | |
import org.apache.commons.io.input.Tailer; | |
import com.artemis.Component; | |
import com.badlogic.gdx.utils.AtomicQueue; | |
@ToString | |
public class Tail extends Component | |
{ | |
public String path; | |
public transient Tailer tailer; | |
// from an entity system, poll() until messages returns null. | |
public transient AtomicQueue<String> messages = new AtomicQueue<String>(1024); | |
public transient int consecutiveRuns; | |
public transient int maxEmissionsPerTick = 1; | |
public Tail() {} | |
public Tail(String path) | |
{ | |
this.path = path; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.junkdog.hallucinolog.manager; | |
import java.io.File; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import lombok.AllArgsConstructor; | |
import lombok.ArtemisManager; | |
import org.apache.commons.io.input.Tailer; | |
import org.apache.commons.io.input.TailerListener; | |
import com.artemis.Entity; | |
import com.artemis.Manager; | |
import com.badlogic.gdx.utils.Array; | |
import com.github.junkdog.hallucinolog.component.Tail; | |
@ArtemisManager( | |
requires=Tail.class) | |
public class TailManager extends Manager | |
{ | |
private Array<Entity> active; | |
private ExecutorService pool; | |
@Override | |
protected void initialize() | |
{ | |
active = new Array<Entity>(); | |
pool = Executors.newCachedThreadPool(); | |
} | |
@Override | |
public void added(Entity e) | |
{ | |
Tail tail = tailMapper.get(e); | |
int delayMillis = 250; | |
Tailer tailer = new Tailer(new File(tail.path), new TailListener(tail), delayMillis, true); | |
tail.tailer = tailer; | |
active.add(e); | |
pool.submit(tailer); | |
} | |
@Override | |
public void deleted(Entity e) | |
{ | |
Tail tail = tailMapper.get(e); | |
tail.tailer.stop(); | |
active.removeValue(e, true); | |
} | |
@AllArgsConstructor | |
private static class TailListener implements TailerListener | |
{ | |
private final Tail tail; | |
@Override public void init(Tailer tailer) {} | |
@Override public void fileRotated() {} | |
@Override | |
public void fileNotFound() | |
{ | |
System.err.println("missing file: " + tail.path); | |
} | |
@Override | |
public void handle(String line) | |
{ | |
tail.messages.put(line); | |
} | |
@Override | |
public void handle(Exception ex) | |
{ | |
ex.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment