-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Concurrency Improvements, new Delegates and general cleanup
- Loading branch information
1 parent
5b44e1a
commit 0959a78
Showing
10 changed files
with
236 additions
and
19 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
toast.version=1.1.0 | ||
toast.version=1.2.0 |
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
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
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
47 changes: 47 additions & 0 deletions
47
src/main/java/jaci/openrio/toast/core/network/CommandDelegate.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package jaci.openrio.toast.core.network; | ||
|
||
import jaci.openrio.delegate.BoundDelegate; | ||
import jaci.openrio.toast.core.Toast; | ||
import jaci.openrio.toast.core.command.CommandBus; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.net.Socket; | ||
|
||
/** | ||
* A one-way delegate for the Command Line. This Delegate will only receive messages and will interpret them as commands. | ||
* No data will be sent to the client from this delegate. To read the output log, it's recommended to use the {@link jaci.openrio.toast.core.network.LoggerDelegate} | ||
* | ||
* DelegateID: "TOAST_command" | ||
* | ||
* @author Jaci | ||
*/ | ||
public class CommandDelegate implements BoundDelegate.ConnectionCallback { | ||
|
||
static BoundDelegate server; | ||
|
||
public static void init() { | ||
server = SocketManager.register("TOAST_command"); | ||
CommandDelegate instance = new CommandDelegate(); | ||
server.callback(instance); | ||
} | ||
|
||
@Override | ||
public void onClientConnect(Socket clientSocket, BoundDelegate delegate) { | ||
new Thread() { | ||
public void run() { | ||
this.setName("CommandDelegate"); | ||
try { | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); | ||
while (true) | ||
CommandBus.parseMessage(reader.readLine()); | ||
} catch (IOException e) { | ||
Toast.log().error("Could not read Command Client: "); | ||
Toast.log().exception(e); | ||
} | ||
} | ||
}.start(); | ||
} | ||
|
||
} |
92 changes: 92 additions & 0 deletions
92
src/main/java/jaci/openrio/toast/core/network/LoggerDelegate.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package jaci.openrio.toast.core.network; | ||
|
||
import jaci.openrio.delegate.BoundDelegate; | ||
import jaci.openrio.toast.core.Toast; | ||
import jaci.openrio.toast.lib.log.LogHandler; | ||
import jaci.openrio.toast.lib.log.Logger; | ||
import jaci.openrio.toast.lib.log.SysLogProxy; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.DataOutputStream; | ||
import java.io.FileReader; | ||
import java.net.Socket; | ||
import java.util.Iterator; | ||
import java.util.Vector; | ||
|
||
/** | ||
* A one-way delegate for the Command Line. This Delegate will only send messages that are a direct output from the Logger | ||
* No data will be received from the client. Commands should be interpreted through the {@link jaci.openrio.toast.core.network.CommandDelegate} | ||
* | ||
* DelegateID: "TOAST_logger" | ||
* | ||
* @author Jaci | ||
*/ | ||
public class LoggerDelegate implements BoundDelegate.ConnectionCallback, LogHandler { | ||
|
||
static BoundDelegate server; | ||
static Vector<Client> clients; | ||
|
||
public static void init() { | ||
server = SocketManager.register("TOAST_logger"); | ||
clients = new Vector<>(); | ||
LoggerDelegate instance = new LoggerDelegate(); | ||
server.callback(instance); | ||
Logger.addHandler(instance); | ||
} | ||
|
||
/** | ||
* Broadcast a message to all clients | ||
*/ | ||
public static void broadcast(String message) { | ||
Iterator<Client> it = clients.iterator(); | ||
while (it.hasNext()) { | ||
Client client = it.next(); | ||
try { | ||
if (!client.client.isClosed()) { | ||
client.output.writeBytes(message + "\n"); | ||
continue; | ||
} | ||
} catch (Exception e) {} | ||
it.remove(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onClientConnect(Socket clientSocket, BoundDelegate delegate) { | ||
new Thread() { | ||
public void run() { | ||
this.setName("LoggerBacklog"); | ||
try { | ||
BufferedReader file_reader = new BufferedReader(new FileReader(SysLogProxy.recentOut)); | ||
DataOutputStream socket_out = new DataOutputStream(clientSocket.getOutputStream()); | ||
String ln; | ||
socket_out.writeBytes("***** BEGIN BACKLOG *****\n"); | ||
while ((ln = file_reader.readLine()) != null) | ||
socket_out.writeBytes(ln + "\n"); | ||
socket_out.writeBytes("***** END BACKLOG *****\n"); | ||
file_reader.close(); | ||
|
||
Client client = new Client(); | ||
client.client = clientSocket; | ||
client.output = socket_out; | ||
clients.add(client); | ||
} catch (java.io.IOException e) { | ||
Toast.log().error("Could not connect Logger Client: "); | ||
Toast.log().exception(e); | ||
} | ||
} | ||
}.start(); | ||
} | ||
|
||
@Override | ||
public void onLog(String level, String message, String formatted, Logger logger) { | ||
broadcast(formatted); | ||
} | ||
|
||
public static class Client { | ||
|
||
public DataOutputStream output; | ||
public Socket client; | ||
|
||
} | ||
} |
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
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
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
42 changes: 42 additions & 0 deletions
42
src/main/java/jaci/openrio/toast/lib/state/ConcurrentVector.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package jaci.openrio.toast.lib.state; | ||
|
||
import java.util.Iterator; | ||
import java.util.Vector; | ||
|
||
/** | ||
* Why the extra Join and Remove Queues you ask? Well, if a module that is currently in a ticking or transition state wants | ||
* to add or remove a listener from the list, we run into some errors. Without 'synchronized', we run into a Concurrent | ||
* Modification, which causes big issues. With 'synchronized', the Thread will be forever blocked because it's trying to access | ||
* a synchronized lock from INSIDE the synchronized method. I hate iteration iteration iteration iteration iteration iteration | ||
* iteration iteration iteration iteration iteration iteration iteration.... | ||
* | ||
* @author Jaci | ||
*/ | ||
public class ConcurrentVector<E> extends Vector<E> { | ||
|
||
Vector<E> joinQueue = new Vector<E>(); | ||
Vector<E> removeQueue = new Vector<E>(); | ||
|
||
public void addConcurrent(E element) { | ||
joinQueue.add(element); | ||
} | ||
|
||
public void removeConcurrent(E element) { | ||
removeQueue.add(element); | ||
} | ||
|
||
public synchronized void tick() { | ||
Iterator<E> joinIt = joinQueue.iterator(); | ||
while (joinIt.hasNext()) { | ||
this.add(joinIt.next()); | ||
joinIt.remove(); | ||
} | ||
|
||
Iterator<E> removeIt = removeQueue.iterator(); | ||
while (removeIt.hasNext()) { | ||
this.remove(removeIt.next()); | ||
removeIt.remove(); | ||
} | ||
} | ||
|
||
} |