-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c8cb08a
commit 1bd9461
Showing
5 changed files
with
293 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package jlifx.boblightd; | ||
|
||
import io.netty.bootstrap.Bootstrap; | ||
import io.netty.bootstrap.ServerBootstrap; | ||
import io.netty.channel.ChannelFuture; | ||
import io.netty.channel.ChannelHandler; | ||
import io.netty.channel.ChannelHandlerAdapter; | ||
import io.netty.channel.ChannelInitializer; | ||
import io.netty.channel.ChannelOption; | ||
import io.netty.channel.EventLoopGroup; | ||
import io.netty.channel.nio.NioEventLoopGroup; | ||
import io.netty.channel.socket.SocketChannel; | ||
import io.netty.channel.socket.nio.NioDatagramChannel; | ||
import io.netty.channel.socket.nio.NioServerSocketChannel; | ||
|
||
public final class NetworkUtils { | ||
private NetworkUtils() {} | ||
|
||
public static void startTcpServer(int port, ChannelHandlerAdapter channelHandlerAdapter) | ||
throws InterruptedException { | ||
EventLoopGroup bossGroup = new NioEventLoopGroup(); | ||
EventLoopGroup workerGroup = new NioEventLoopGroup(); | ||
ServerBootstrap server = new ServerBootstrap(); | ||
server.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) | ||
.childHandler(new ChannelInitializer<SocketChannel>() { | ||
public void initChannel(SocketChannel ch) throws Exception { | ||
ch.pipeline().addLast(channelHandlerAdapter); | ||
}; | ||
}).option(ChannelOption.SO_BACKLOG, 100); | ||
ChannelFuture f = server.bind(port).sync(); | ||
f.channel().closeFuture().sync(); | ||
} | ||
|
||
public static void startUdpServer(int port, ChannelHandler channelHandler) throws InterruptedException { | ||
EventLoopGroup eventLoopGroup = new NioEventLoopGroup(); | ||
Bootstrap server = new Bootstrap(); | ||
server.group(eventLoopGroup).channel(NioDatagramChannel.class).handler(channelHandler) | ||
.option(ChannelOption.SO_BACKLOG, 100); | ||
ChannelFuture f = server.bind(port).sync(); | ||
f.channel().closeFuture().sync(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/test/java/jlifx/commandline/command/ColorCommandTest.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,40 @@ | ||
package jlifx.commandline.command; | ||
|
||
import static org.easymock.EasyMock.expectLastCall; | ||
|
||
import java.awt.Color; | ||
|
||
import jlifx.bulb.AbstractJLifxTestCase; | ||
import jlifx.bulb.IBulb; | ||
|
||
import org.junit.Test; | ||
|
||
public class ColorCommandTest extends AbstractJLifxTestCase { | ||
|
||
@Test | ||
public void testColorBulbDefaultBrightness() throws Exception { | ||
ColorCommand command = new ColorCommand(); | ||
IBulb bulb = getMockedBulb(); | ||
bulb.colorize(Color.GREEN, 3, 1.0F); | ||
expectLastCall().once(); | ||
replayAll(); | ||
|
||
executeCommand(command, bulb, "color", "green"); | ||
|
||
verifyAll(); | ||
} | ||
|
||
@Test | ||
public void testColorBulbDefaultLowBrightness() throws Exception { | ||
ColorCommand command = new ColorCommand(); | ||
IBulb bulb = getMockedBulb(); | ||
bulb.colorize(Color.GREEN, 3, 0.1F); | ||
expectLastCall().once(); | ||
replayAll(); | ||
|
||
executeCommand(command, bulb, "color", "green", "0.1"); | ||
|
||
verifyAll(); | ||
} | ||
|
||
} |
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,7 @@ | ||
package jlifx.commandline.command; | ||
|
||
import jlifx.bulb.AbstractJLifxTestCase; | ||
|
||
public class ScanCommandTest extends AbstractJLifxTestCase { | ||
|
||
} |
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,112 @@ | ||
package jlifx.packet; | ||
|
||
import static org.junit.Assert.assertArrayEquals; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
|
||
import org.junit.Test; | ||
|
||
import jlifx.bulb.AbstractJLifxTestCase; | ||
|
||
public class TcpPacketReaderTest extends AbstractJLifxTestCase { | ||
|
||
@Test | ||
public void testNoPacket() throws Exception { | ||
InputStream inputStream = new BufferedInputStream(new ByteArrayInputStream(new byte[] {})); | ||
|
||
TcpPacketReader packetReader = new TcpPacketReader(inputStream); | ||
packetReader.start(); | ||
Thread.sleep(500); | ||
List<Packet> receivedPackets = packetReader.getReceivedPackets(); | ||
packetReader.stopRunning(); | ||
|
||
assertTrue(receivedPackets.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void testOnePacket() throws Exception { | ||
PowerManagementPacket packet = new PowerManagementPacket(TEST_MAC_ADDRESS_1, true); | ||
byte[] bytes = packet.toByteArray(); | ||
InputStream inputStream = new BufferedInputStream(new ByteArrayInputStream(bytes)); | ||
|
||
TcpPacketReader packetReader = new TcpPacketReader(inputStream); | ||
packetReader.start(); | ||
Thread.sleep(500); | ||
List<Packet> receivedPackets = packetReader.getReceivedPackets(); | ||
packetReader.stopRunning(); | ||
|
||
assertFalse(receivedPackets.isEmpty()); | ||
assertEquals(1, receivedPackets.size()); | ||
|
||
PowerManagementPacket receivedPacket = new PowerManagementPacket(receivedPackets.get(0)); | ||
|
||
assertArrayEquals(TEST_MAC_ADDRESS_1, receivedPacket.getTargetMac()); | ||
assertTrue(receivedPacket.on()); | ||
} | ||
|
||
private final class InputStreamExtension extends InputStream { | ||
private int request = 0; | ||
|
||
@Override | ||
public int read() throws IOException { | ||
return 0; | ||
} | ||
|
||
private int copy(byte[] src, byte[] dst) { | ||
for (int i = 0; i < src.length; i++) { | ||
dst[i] = src[i]; | ||
} | ||
return src.length; | ||
} | ||
|
||
@Override | ||
public int read(byte[] b) throws IOException { | ||
if (request == 0) { | ||
request++; | ||
PowerManagementPacket packet = new PowerManagementPacket(TEST_MAC_ADDRESS_1, true); | ||
byte[] bytes = packet.toByteArray(); | ||
return copy(bytes, b); | ||
} else | ||
if (request == 1) { | ||
request++; | ||
PowerManagementPacket packet = new PowerManagementPacket(TEST_MAC_ADDRESS_2, false); | ||
byte[] bytes = packet.toByteArray(); | ||
return copy(bytes, b); | ||
} else { | ||
return -1; | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void testTwoPackets() throws Exception { | ||
InputStream inputStream = new InputStreamExtension(); | ||
|
||
TcpPacketReader packetReader = new TcpPacketReader(inputStream); | ||
packetReader.start(); | ||
Thread.sleep(500); | ||
List<Packet> receivedPackets = packetReader.getReceivedPackets(); | ||
packetReader.stopRunning(); | ||
|
||
assertFalse(receivedPackets.isEmpty()); | ||
assertEquals(2, receivedPackets.size()); | ||
|
||
PowerManagementPacket receivedPacket = new PowerManagementPacket(receivedPackets.get(0)); | ||
|
||
assertArrayEquals(TEST_MAC_ADDRESS_1, receivedPacket.getTargetMac()); | ||
assertTrue(receivedPacket.on()); | ||
|
||
receivedPacket = new PowerManagementPacket(receivedPackets.get(1)); | ||
|
||
assertArrayEquals(TEST_MAC_ADDRESS_2, receivedPacket.getTargetMac()); | ||
assertFalse(receivedPacket.on()); | ||
} | ||
|
||
} |
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 jlifx.packet; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.io.IOException; | ||
import java.net.DatagramPacket; | ||
import java.net.DatagramSocket; | ||
import java.net.SocketException; | ||
import java.net.SocketTimeoutException; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
import org.junit.Test; | ||
|
||
import jlifx.bulb.AbstractJLifxTestCase; | ||
|
||
public class UdpPacketReaderTest extends AbstractJLifxTestCase { | ||
|
||
private class DatagramSocketStub extends DatagramSocket { | ||
private final Iterator<Packet> packetIterator; | ||
|
||
public DatagramSocketStub(List<Packet> packets) throws SocketException { | ||
this.packetIterator = packets.iterator(); | ||
} | ||
|
||
@Override | ||
public synchronized void receive(DatagramPacket p) throws IOException { | ||
if (packetIterator.hasNext()) { | ||
p.setData(packetIterator.next().toByteArray()); | ||
} else { | ||
throw new SocketTimeoutException(); | ||
} | ||
} | ||
|
||
} | ||
|
||
@Test | ||
public void testNoPacket() throws Exception { | ||
UdpPacketReader packetReader = new UdpPacketReader(new DatagramSocketStub(new ArrayList<Packet>())); | ||
packetReader.start(); | ||
Thread.sleep(500); | ||
List<Packet> receivedPackets = packetReader.getReceivedPackets(); | ||
packetReader.stopRunning(); | ||
|
||
assertTrue(receivedPackets.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void testOnePacket() throws Exception { | ||
DatagramSocket datagramSocket = new DatagramSocketStub(Collections.singletonList(new StatusRequestPacket())); | ||
UdpPacketReader packetReader = new UdpPacketReader(datagramSocket); | ||
packetReader.start(); | ||
Thread.sleep(500); | ||
List<Packet> receivedPackets = packetReader.getReceivedPackets(); | ||
packetReader.stopRunning(); | ||
|
||
assertFalse(receivedPackets.isEmpty()); | ||
assertEquals(1, receivedPackets.size()); | ||
|
||
Packet receivedPacket = receivedPackets.get(0); | ||
|
||
assertEquals(StatusRequestPacket.TYPE, receivedPacket.getType()); | ||
} | ||
|
||
@Test | ||
public void testTwoPackets() throws Exception { | ||
List<Packet> packets = new ArrayList<Packet>(); | ||
packets.add(new StatusRequestPacket()); | ||
packets.add(new PowerManagementPacket(TEST_MAC_ADDRESS_1, true)); | ||
UdpPacketReader packetReader = new UdpPacketReader(new DatagramSocketStub(packets)); | ||
packetReader.start(); | ||
Thread.sleep(500); | ||
List<Packet> receivedPackets = packetReader.getReceivedPackets(); | ||
packetReader.stopRunning(); | ||
|
||
assertFalse(receivedPackets.isEmpty()); | ||
assertEquals(2, receivedPackets.size()); | ||
|
||
Packet receivedPacket = receivedPackets.get(0); | ||
|
||
assertEquals(StatusRequestPacket.TYPE, receivedPacket.getType()); | ||
|
||
receivedPacket = receivedPackets.get(1); | ||
|
||
assertEquals(PowerManagementPacket.TYPE, receivedPacket.getType()); | ||
} | ||
|
||
} |