Skip to content

Commit

Permalink
Merge pull request mrniko#678 from Skipua/bugfix/cross-namespace-broa…
Browse files Browse the repository at this point in the history
…dcast

Fix cross-namespace broadcast happens on single ns
  • Loading branch information
mrniko authored Sep 4, 2019
2 parents ea1787f + 75d08d4 commit 50b6b49
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/main/java/com/corundumstudio/socketio/protocol/Packet.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,28 @@ public <T> T getData() {
return (T)data;
}

/**
* Creates a copy of #{@link Packet} with new namespace set
* if it differs from current namespace.
* Otherwise, returns original object unchanged
*/
public Packet withNsp(String namespace) {
if (this.nsp.equalsIgnoreCase(namespace)) {
return this;
} else {
Packet newPacket = new Packet(this.type);
newPacket.setAckId(this.ackId);
newPacket.setData(this.data);
newPacket.setDataSource(this.dataSource);
newPacket.setName(this.name);
newPacket.setSubType(this.subType);
newPacket.setNsp(namespace);
newPacket.attachments = this.attachments;
newPacket.attachmentsCount = this.attachmentsCount;
return newPacket;
}
}

public void setNsp(String endpoint) {
this.nsp = endpoint;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ public void send(Packet packet) {
if (!isConnected()) {
return;
}
packet.setNsp(namespace.getName());
baseClient.send(packet);

baseClient.send(packet.withNsp(namespace.getName()));
}

public void onDisconnect() {
Expand Down
61 changes: 61 additions & 0 deletions src/test/java/com/corundumstudio/socketio/protocol/PacketTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.corundumstudio.socketio.protocol;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;

import io.netty.buffer.Unpooled;
import org.junit.Test;

public class PacketTest {

@Test
public void packetCopyIsCreatedWhenNamespaceDiffers() {
Packet oldPacket = createPacket();

String newNs = "new";
Packet newPacket = oldPacket.withNsp(newNs);
assertEquals(newNs, newPacket.getNsp());
assertPacketCopied(oldPacket, newPacket);
}

@Test
public void packetCopyIsCreatedWhenNewNamespaceDiffersAndIsNull() {
Packet packet = createPacket();
Packet newPacket = packet.withNsp(null);
assertNull(newPacket.getNsp());
assertPacketCopied(packet, newPacket);
}

@Test
public void originalPacketReturnedIfNamespaceIsTheSame() {
Packet packet = new Packet(PacketType.MESSAGE);
assertSame(packet, packet.withNsp(""));
}

private void assertPacketCopied(Packet oldPacket, Packet newPacket) {
assertNotSame(newPacket, oldPacket);
assertEquals(oldPacket.getName(), newPacket.getName());
assertEquals(oldPacket.getType(), newPacket.getType());
assertEquals(oldPacket.getSubType(), newPacket.getSubType());
assertEquals(oldPacket.getAckId(), newPacket.getAckId());
assertEquals(oldPacket.getAttachments().size(), newPacket.getAttachments().size());
assertSame(oldPacket.getAttachments(), newPacket.getAttachments());
assertEquals(oldPacket.getData(), newPacket.getData());
assertSame(oldPacket.getDataSource(), newPacket.getDataSource());
}

private Packet createPacket() {
Packet packet = new Packet(PacketType.MESSAGE);
packet.setSubType(PacketType.EVENT);
packet.setName("packetName");
packet.setData("data");
packet.setAckId(1L);
packet.setNsp("old");
packet.setDataSource(Unpooled.wrappedBuffer(new byte[]{10}));
packet.initAttachments(1);
packet.addAttachment(Unpooled.wrappedBuffer(new byte[]{20}));
return packet;
}
}

0 comments on commit 50b6b49

Please sign in to comment.