Skip to content

Commit

Permalink
- implemented retained messages for OOCSI channels
Browse files Browse the repository at this point in the history
  • Loading branch information
matsfunk committed Sep 20, 2021
1 parent 5f09ea0 commit 95b07c1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
Binary file modified server/dist/OOCSI_server.jar
Binary file not shown.
30 changes: 29 additions & 1 deletion server/src/nl/tue/id/oocsi/server/model/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
*/
public class Channel {

private static final String RETAIN_MESSAGE = "_RETAIN";

protected final Date created = new Date();
protected final ChangeListener presence;
protected final Map<String, Channel> subChannels = new ConcurrentHashMap<String, Channel>();

protected String token;
protected Message retainedMessage;

public Channel(String token, ChangeListener changeListener) {
this.token = token;
Expand Down Expand Up @@ -95,11 +98,25 @@ public void send(Message message) {
if (!subChannel.isPrivate()) {
if (!message.recipient.equals(subChannel.getName())) {
OOCSIServer.logEvent(message.sender, message.recipient, subChannel.getName(), message.data,
message.timestamp);
message.timestamp);
}
}
}

// new message erases always retained message
retainedMessage = null;

// check for retained message flag and store message
if (message.data.containsKey(RETAIN_MESSAGE)) {
Object retainTimeoutRaw = message.data.getOrDefault(RETAIN_MESSAGE, "0");
try {
long timeout = Long.parseLong(retainTimeoutRaw.toString());
message.validUntil = new Date(System.currentTimeMillis() + timeout);
retainedMessage = message;
} catch (Exception e) {
// do nothing
}
}
}

/**
Expand Down Expand Up @@ -156,10 +173,21 @@ public String getChannelList() {
public void addChannel(Channel channel) {
if (!getName().equals(channel.getName()) && !subChannels.containsKey(channel.getName())) {
subChannels.put(channel.getName(), channel);

// update presence information
if (!channel.isPrivate()) {
presence.join(this, channel);
OOCSIServer.logConnection(getName(), channel.getName(), "added channel", new Date());
}

// send out the retained message to new client
final Message retainedMessageCopy = retainedMessage;
if (retainedMessageCopy != null && retainedMessageCopy.isValid()) {
channel.send(retainedMessageCopy);
} else {
// clear invalid or null message
retainedMessage = null;
}
}
}

Expand Down
15 changes: 14 additions & 1 deletion server/src/nl/tue/id/oocsi/server/protocol/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public class Message implements Serializable {
* data payload of message
*/
public Map<String, Object> data;
/**
* until when the message is valid (used for retained messages)
*/
public Date validUntil;

/**
* create message from sender and recipient
Expand Down Expand Up @@ -86,14 +90,23 @@ public Message addData(String key, Object value) {
return this;
}

/**
* check whether the message is still valid
*
* @return
*/
public boolean isValid() {
return validUntil == null || validUntil.after(new Date());
}

/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
public String toString() {
return "{sender: " + sender + ", recipient: " + recipient + ", timestamp: " + timestamp + ", data: " + data
+ "}";
+ "}";
}

}

0 comments on commit 95b07c1

Please sign in to comment.