43 lines
1.4 KiB
Java
43 lines
1.4 KiB
Java
String rot13(String text) {
|
|
var result = new StringBuilder();
|
|
|
|
for (var character : text.toCharArray()) {
|
|
if (character >= 'a' && character <= 'z')
|
|
character = (char) ('a' + (character - 'a' + 13) % 26);
|
|
else if (character >= 'A' && character <= 'Z')
|
|
character = (char) ('A' + (character - 'A' + 13) % 26);
|
|
|
|
result.append(character);
|
|
}
|
|
|
|
return result.toString();
|
|
}
|
|
|
|
void main() throws Exception {
|
|
try (var subscriptions = new FileSubscriptions()) {
|
|
var controlMailbox = "rot13/inbox";
|
|
String textMailbox = null;
|
|
subscriptions.subscribe(controlMailbox);
|
|
|
|
while (true) {
|
|
var incoming = subscriptions.take();
|
|
var message = incoming.message();
|
|
|
|
if (incoming.mailbox().equals(controlMailbox)
|
|
&& message.type().equals("TOPIC")) {
|
|
if (textMailbox != null)
|
|
subscriptions.unsubscribe(textMailbox);
|
|
|
|
textMailbox = message.value();
|
|
subscriptions.subscribe(textMailbox);
|
|
IO.println("ROT13 now listens to " + textMailbox);
|
|
} else if (message.type().equals("TEXT")) {
|
|
FileMessageBoard.publish(
|
|
"rot13/outbox",
|
|
new Message("TEXT", rot13(message.value()))
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|