Skip to content

Commit

Permalink
Bring in Messenger
Browse files Browse the repository at this point in the history
  • Loading branch information
rmheuer committed Aug 30, 2023
1 parent 11694b1 commit f92e0aa
Show file tree
Hide file tree
Showing 13 changed files with 2,202 additions and 0 deletions.
538 changes: 538 additions & 0 deletions Messenger/MessengerClient/MessengerClient-Python/messenger.py

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions Messenger/MessengerClient/MessengerClient-Python/messenger_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from messenger import *
from time import sleep

host = 'localhost'
port = 5805


def main():
client = MessengerClient(host, port, 'test') # Start up a connections
while not client.is_connected(): # Wait until it connects
time.sleep(0.1)

client.add_handler('Test', handler)

# Add 5 numbers to the messenger
for i in range(5):
client.prepare('Test')\
.add_int(i)\
.send()
sleep_with_read()

client.disconnect()
print("Done")

# Just print out what is going on
def handler(type, reader):
print(type, reader.read_int())

def sleep_with_read(client):
for _ in range(10):
client.read_messages()
time.sleep(0.1)

if __name__ == '__main__':
main()
11 changes: 11 additions & 0 deletions Messenger/MessengerClient/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
plugins {
id 'java'
}

group 'com.swrobotics'
version '2023'

compileJava {
sourceCompatibility = '11'
targetCompatibility = '11'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
package com.swrobotics.messenger.client;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
* Allows easy storage of data into a message.
*
* @author rmheuer
*/
public final class MessageBuilder {
private final MessengerClient client;
private final String type;
private final ByteArrayOutputStream b;
private final DataOutputStream out;

public MessageBuilder(MessengerClient client, String type) {
this.client = client;
this.type = type;
b = new ByteArrayOutputStream();
out = new DataOutputStream(b);
}

/** Sends the message with the type and data. */
public void send() {
client.sendMessage(type, b.toByteArray());
}

/**
* Adds a {@code boolean} to this message.
*
* @param b boolean to add
* @return this
*/
public MessageBuilder addBoolean(boolean b) {
try {
out.writeBoolean(b);
} catch (IOException e) {
throw new RuntimeException("Failed to write boolean", e);
}
return this;
}

/**
* Adds a {@code String} to this message.
*
* @param s String to add
* @return this
*/
public MessageBuilder addString(String s) {
try {
MessengerClient.writeStringUtf8(out, s);
} catch (IOException e) {
throw new RuntimeException("Failed to write String", e);
}
return this;
}

/**
* Adds a {@code char} to this message.
*
* @param c char to add
* @return this
*/
public MessageBuilder addChar(char c) {
try {
out.writeChar(c);
} catch (IOException e) {
throw new RuntimeException("Failed to write char", e);
}
return this;
}

/**
* Adds a {@code byte} to this message.
*
* @param b byte to add
* @return this
*/
public MessageBuilder addByte(byte b) {
try {
out.writeByte(b);
} catch (IOException e) {
throw new RuntimeException("Failed to write byte", e);
}
return this;
}

/**
* Adds a {@code short} to this message.
*
* @param s short to add
* @return this
*/
public MessageBuilder addShort(short s) {
try {
out.writeShort(s);
} catch (IOException e) {
throw new RuntimeException("Failed to write short", e);
}
return this;
}

/**
* Adds an {@code int} to this message.
*
* @param i int to add
* @return this
*/
public MessageBuilder addInt(int i) {
try {
out.writeInt(i);
} catch (IOException e) {
throw new RuntimeException("Failed to write int", e);
}
return this;
}

/**
* Adds a {@code long} to this message.
*
* @param l long to add
* @return this
*/
public MessageBuilder addLong(long l) {
try {
out.writeLong(l);
} catch (IOException e) {
throw new RuntimeException("Failed to write long", e);
}
return this;
}

/**
* Adds a {@code float} to this message.
*
* @param f float to add
* @return this
*/
public MessageBuilder addFloat(float f) {
try {
out.writeFloat(f);
} catch (IOException e) {
throw new RuntimeException("Failed to write float", e);
}
return this;
}

/**
* Adds a {@code double} to this message.
*
* @param d double to add
* @return this
*/
public MessageBuilder addDouble(double d) {
try {
out.writeDouble(d);
} catch (IOException e) {
throw new RuntimeException("Failed to write double", e);
}
return this;
}

/**
* Adds raw data to this message.
*
* @param b data to add
* @return self
*/
public MessageBuilder addRaw(byte[] b) {
try {
out.write(b);
} catch (IOException e) {
throw new RuntimeException("Failed to write raw data", e);
}
return this;
}

public byte[] getData() {
return b.toByteArray();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.swrobotics.messenger.client;

/**
* Represents a function called when a message is received.
*
* @author rmheuer
*/
@FunctionalInterface
public interface MessageHandler {
/**
* Called when a matching message is received.
*
* @param type message type
* @param reader message data reader
*/
void handle(String type, MessageReader reader);
}
Loading

0 comments on commit f92e0aa

Please sign in to comment.