Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 5 commits
  • 30 files changed
  • 0 commit comments
  • 1 contributor
Showing with 297 additions and 10 deletions.
  1. +9 −0 .gitignore
  2. +2 −1 powerflare-server/.gitignore
  3. BIN powerflare-server/out/artifacts/powerflare_server_jar/RXTXcomm.jar
  4. BIN powerflare-server/out/artifacts/powerflare_server_jar/powerflare-server.jar
  5. BIN powerflare-server/out/artifacts/powerflare_server_war_exploded/RXTXcomm.jar
  6. BIN ...ts/powerflare_server_war_exploded/WEB-INF/classes/de/paluch/powerflare/HttpControlInterface.class
  7. BIN ...t/artifacts/powerflare_server_war_exploded/WEB-INF/classes/de/paluch/powerflare/Multiplexer.class
  8. BIN ...er/out/artifacts/powerflare_server_war_exploded/WEB-INF/classes/de/paluch/powerflare/Server.class
  9. BIN ...rver_war_exploded/WEB-INF/classes/de/paluch/powerflare/channel/AbstractCommunicationChannel.class
  10. BIN ...owerflare_server_war_exploded/WEB-INF/classes/de/paluch/powerflare/channel/DummyCommChannel.class
  11. BIN ...lare_server_war_exploded/WEB-INF/classes/de/paluch/powerflare/channel/ICommunicationChannel.class
  12. BIN ...er_war_exploded/WEB-INF/classes/de/paluch/powerflare/channel/SerialPortCommunicationChannel.class
  13. BIN .../powerflare_server_war_exploded/WEB-INF/classes/de/paluch/powerflare/command/ConnectCommand.class
  14. BIN ...werflare_server_war_exploded/WEB-INF/classes/de/paluch/powerflare/command/DisconnectCommand.class
  15. BIN ...ifacts/powerflare_server_war_exploded/WEB-INF/classes/de/paluch/powerflare/command/ICommand.class
  16. BIN ...s/powerflare_server_war_exploded/WEB-INF/classes/de/paluch/powerflare/command/RelayCommands.class
  17. BIN ...owerflare_server_war_exploded/WEB-INF/classes/de/paluch/powerflare/command/SendDataCallable.class
  18. BIN ...owerflare_server_war_exploded/WEB-INF/classes/de/paluch/powerflare/command/SwitchOffCommand.class
  19. BIN ...powerflare_server_war_exploded/WEB-INF/classes/de/paluch/powerflare/command/SwitchOnCommand.class
  20. BIN powerflare-server/out/artifacts/powerflare_server_war_exploded/WEB-INF/lib/RXTXcomm.jar
  21. +25 −0 powerflare-server/pom.xml
  22. +26 −5 powerflare-server/src/main/java/de/paluch/powerflare/HttpControlInterface.java
  23. +2 −2 powerflare-server/src/main/java/de/paluch/powerflare/Server.java
  24. +12 −0 powerflare-server/src/main/java/de/paluch/powerflare/channel/DummyCommChannel.java
  25. +23 −0 powerflare-server/src/main/java/de/paluch/powerflare/channel/ICommunicationChannel.java
  26. +2 −0 powerflare-server/src/main/java/de/paluch/powerflare/channel/SerialPortCommunicationChannel.java
  27. +1 −1 powerflare-server/src/main/java/de/paluch/powerflare/command/ConnectDisconnectCommand.java
  28. +1 −1 powerflare-server/src/main/java/de/paluch/powerflare/locking/LockManager.java
  29. +109 −0 powerflare-server/src/test/java/de/paluch/powerflare/ConcurrencyTest.java
  30. +85 −0 powerflare-server/src/test/java/de/paluch/powerflare/command/PowerflareSwitchOnCommandTest.java
View
9 .gitignore
@@ -3,3 +3,12 @@
.project
.settings
target
+powerflare-server/out/*
+powerflare-server/out/
+.idea
+
+burndown-chart/burndown-chart.iml
+
+richfaces-long-running-task/richfaces-long-running-task (1).iml
+
+safety-facade/safety-facade (1).iml
View
3 powerflare-server/.gitignore
@@ -1 +1,2 @@
-.idea
+.idea
+out
View
BIN powerflare-server/out/artifacts/powerflare_server_jar/RXTXcomm.jar
Binary file not shown.
View
BIN powerflare-server/out/artifacts/powerflare_server_jar/powerflare-server.jar
Binary file not shown.
View
BIN powerflare-server/out/artifacts/powerflare_server_war_exploded/RXTXcomm.jar
Binary file not shown.
View
BIN ...flare_server_war_exploded/WEB-INF/classes/de/paluch/powerflare/HttpControlInterface.class
Binary file not shown.
View
BIN ...cts/powerflare_server_war_exploded/WEB-INF/classes/de/paluch/powerflare/Multiplexer.class
Binary file not shown.
View
BIN ...rtifacts/powerflare_server_war_exploded/WEB-INF/classes/de/paluch/powerflare/Server.class
Binary file not shown.
View
BIN ..._exploded/WEB-INF/classes/de/paluch/powerflare/channel/AbstractCommunicationChannel.class
Binary file not shown.
View
BIN ...e_server_war_exploded/WEB-INF/classes/de/paluch/powerflare/channel/DummyCommChannel.class
Binary file not shown.
View
BIN ...ver_war_exploded/WEB-INF/classes/de/paluch/powerflare/channel/ICommunicationChannel.class
Binary file not shown.
View
BIN ...xploded/WEB-INF/classes/de/paluch/powerflare/channel/SerialPortCommunicationChannel.class
Binary file not shown.
View
BIN ...are_server_war_exploded/WEB-INF/classes/de/paluch/powerflare/command/ConnectCommand.class
Binary file not shown.
View
BIN ..._server_war_exploded/WEB-INF/classes/de/paluch/powerflare/command/DisconnectCommand.class
Binary file not shown.
View
BIN ...owerflare_server_war_exploded/WEB-INF/classes/de/paluch/powerflare/command/ICommand.class
Binary file not shown.
View
BIN ...lare_server_war_exploded/WEB-INF/classes/de/paluch/powerflare/command/RelayCommands.class
Binary file not shown.
View
BIN ...e_server_war_exploded/WEB-INF/classes/de/paluch/powerflare/command/SendDataCallable.class
Binary file not shown.
View
BIN ...e_server_war_exploded/WEB-INF/classes/de/paluch/powerflare/command/SwitchOffCommand.class
Binary file not shown.
View
BIN ...re_server_war_exploded/WEB-INF/classes/de/paluch/powerflare/command/SwitchOnCommand.class
Binary file not shown.
View
BIN powerflare-server/out/artifacts/powerflare_server_war_exploded/WEB-INF/lib/RXTXcomm.jar
Binary file not shown.
View
25 powerflare-server/pom.xml
@@ -55,6 +55,15 @@
<build>
<plugins>
+
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <source>1.6</source>
+ <target>1.6</target>
+ <encoding>UTF-8</encoding>
+ </configuration>
+ </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
@@ -81,6 +90,22 @@
</descriptorRefs>
</configuration>
</plugin>
+ <plugin>
+ <artifactId>maven-dependency-plugin</artifactId>
+ <configuration>
+ <outputDirectory>target</outputDirectory>
+ </configuration>
+ <executions>
+ <execution>
+ <id>copy-dependencies</id>
+ <phase>package</phase>
+ <goals>
+ <goal>copy-dependencies</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+
</plugins>
</build>
View
31 powerflare-server/src/main/java/de/paluch/powerflare/HttpControlInterface.java
@@ -8,6 +8,7 @@
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
+import java.lang.annotation.Documented;
/**
* Created with IntelliJ IDEA. User: mark Date: 25.04.12 Time: 08:02 To change this template use File | Settings | File
@@ -16,6 +17,15 @@
@Path("/")
public class HttpControlInterface {
+ public static final String RESULT_FAIL = "FAIL";
+ public static final String RESULT_OK = "OK";
+
+ /**
+ * Switch Power-Flares.
+ * @param port
+ * @param state
+ * @return OK/FAIL
+ */
@Path("powerflare/{port:[0-8]}/{state:(ON|OFF)}")
@GET
@Produces(MediaType.TEXT_PLAIN)
@@ -37,11 +47,16 @@ public String controlPowerflare(@PathParam("port") byte port, @PathParam("state"
if (command != null) {
command.execute(Multiplexer.getInstance().getChannel(), Multiplexer.getInstance().getExec());
- return "OK";
+ return RESULT_OK;
}
- return "FAIL";
+ return RESULT_FAIL;
}
+ /**
+ * Connect-Disconnect Port with 100ms Connect Delay.
+ * @param port
+ * @return OK
+ */
@Path("switch/{port:[0-8]}")
@GET
@Produces(MediaType.TEXT_PLAIN)
@@ -51,9 +66,15 @@ public String controlSwitch(@PathParam("port") byte port) {
AbstractCommunicationCommand command = new ConnectDisconnectCommand(port);
command.execute(Multiplexer.getInstance().getChannel(), Multiplexer.getInstance().getExec());
- return "OK";
+ return RESULT_OK;
}
+ /**
+ * Connect or Disconnect Port.
+ * @param port
+ * @param state
+ * @return OK/FAIL
+ */
@Path("relay/{port:[0-8]}/{state:(CONNECT|DISCONNECT)}")
@GET
@Produces(MediaType.TEXT_PLAIN)
@@ -75,9 +96,9 @@ public String controlPort(@PathParam("port") byte port, @PathParam("state") Stri
if (command != null) {
command.execute(Multiplexer.getInstance().getChannel(), Multiplexer.getInstance().getExec());
- return "OK";
+ return RESULT_OK;
}
- return "FAIL";
+ return RESULT_FAIL;
}
}
View
4 powerflare-server/src/main/java/de/paluch/powerflare/Server.java
@@ -6,6 +6,7 @@
import com.sun.net.httpserver.HttpServer;
import de.paluch.powerflare.channel.DummyCommChannel;
import de.paluch.powerflare.channel.ICommunicationChannel;
+import de.paluch.powerflare.channel.SerialPortCommunicationChannel;
/**
* Created with IntelliJ IDEA. User: mark Date: 25.04.12 Time: 08:31 To change this template use File | Settings | File
@@ -55,8 +56,7 @@ private void close() {
}
private Server(String port) throws Exception {
- //channel = new SerialPortCommunicationChannel(port, 19200);
- channel = new DummyCommChannel();
+ channel = new SerialPortCommunicationChannel(port, 19200);
Multiplexer.getInstance().setChannel(channel);
}
View
12 powerflare-server/src/main/java/de/paluch/powerflare/channel/DummyCommChannel.java
@@ -1,16 +1,28 @@
package de.paluch.powerflare.channel;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
/**
* Created with IntelliJ IDEA. User: mark Date: 27.04.12 Time: 08:29 To change this template use File | Settings | File
* Templates.
*/
public class DummyCommChannel extends AbstractCommunicationChannel implements ICommunicationChannel {
+ private List<byte[]> history = Collections.synchronizedList(new ArrayList<byte[]>());
+
@Override
public void sendData(byte[] data) {
System.out.println("Sending: " + new String(data));
+ history.add(data);
}
@Override
public void close() {
}
+
+ public List<byte[]> getHistory() {
+ return history;
+ }
}
View
23 powerflare-server/src/main/java/de/paluch/powerflare/channel/ICommunicationChannel.java
@@ -6,13 +6,36 @@
*/
public interface ICommunicationChannel {
+ /**
+ * Send Data to Channel.
+ * @param data
+ */
void sendData(byte[] data);
+ /**
+ * Close Channel.
+ */
void close();
+ /**
+ * Lock port of Channel.
+ * @param port
+ * @param owner Lock-Owner
+ */
void lock(int port, Object owner);
+
+ /**
+ * Unlock port of Channel.
+ * @param port
+ * @param owner Lock-Owner
+ */
void unlock(int port, Object owner);
+ /**
+ * Wait until channel is free to send.
+ * @param port
+ * @param owner Lock-Owner
+ */
public void waitForFreeChannel(int port, Object owner);
}
View
2 ...are-server/src/main/java/de/paluch/powerflare/channel/SerialPortCommunicationChannel.java
@@ -47,6 +47,7 @@ public void close() {
try {
in.close();
} catch (IOException e) {
+ // nothing to do.
}
}
@@ -54,6 +55,7 @@ public void close() {
try {
out.close();
} catch (IOException e) {
+ // nothing to do.
}
}
View
2 powerflare-server/src/main/java/de/paluch/powerflare/command/ConnectDisconnectCommand.java
@@ -10,7 +10,7 @@
public class ConnectDisconnectCommand extends AbstractCommunicationCommand {
private final static int RELAY_CONNECT_DELAY = 0;
- private final static int RELAY_DISCONNECT_DELAY = 200;
+ private final static int RELAY_DISCONNECT_DELAY = 100;
public ConnectDisconnectCommand(byte port) {
View
2 powerflare-server/src/main/java/de/paluch/powerflare/locking/LockManager.java
@@ -50,7 +50,7 @@ public Lock tryLock(T resource, Lock.LockLevel level, Object owner) {
public Lock lock(T resource, Lock.LockLevel level, Object owner) {
- Lock lock = null;
+ Lock lock;
while ((lock = tryLock(resource, level, owner)) == null) {
try {
View
109 powerflare-server/src/test/java/de/paluch/powerflare/ConcurrencyTest.java
@@ -0,0 +1,109 @@
+package de.paluch.powerflare;
+
+import de.paluch.powerflare.channel.DummyCommChannel;
+import de.paluch.powerflare.command.ConnectDisconnectCommand;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.runners.MockitoJUnitRunner;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static junit.framework.Assert.*;
+
+/**
+ * Created with IntelliJ IDEA. User: mark Date: 03.05.12 Time: 17:12 To change this template use File | Settings | File
+ * Templates.
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class ConcurrencyTest {
+
+ private DummyCommChannel channel = new DummyCommChannel();
+
+ @Before
+ public void before() {
+ Multiplexer.getInstance().setChannel(channel);
+ }
+
+ @Test(timeout = 20000)
+ public void testSendData() throws Exception {
+
+ List<Thread> threads = new ArrayList<Thread>();
+ for (int i = 0; i < 30; i++) {
+
+ Thread thread = new Thread() {
+ @Override
+ public void run() {
+ try {
+
+ long randomSleep = (long) (Math.random() * 300);
+ Thread.sleep(randomSleep);
+
+ ConnectDisconnectCommand cd = new ConnectDisconnectCommand(Byte.valueOf("2"));
+ ConnectDisconnectCommand cd2 = new ConnectDisconnectCommand(Byte.valueOf("0"));
+ cd.execute(channel, Multiplexer.getInstance().getExec());
+ cd2.execute(channel, Multiplexer.getInstance().getExec());
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ };
+
+ threads.add(thread);
+ }
+
+ for (Thread t : threads) {
+ t.start();
+ }
+ Thread.sleep(100);
+
+ for (Thread t : threads) {
+ if (t.isAlive()) {
+ t.join();
+ }
+ }
+
+
+ Multiplexer.getInstance().getExec().shutdown();
+ while (!Multiplexer.getInstance().getExec().isTerminated()) {
+ Thread.sleep(100);
+ }
+
+
+ List<byte[]> history = channel.getHistory();
+
+ boolean wasConnect = false;
+ boolean wasDisconnect = false;
+
+ for (byte[] data : history) {
+
+ assertEquals(1, data.length);
+
+ assertTrue(data[0] >= 100 && data[0] <= 120);
+
+
+ if (data[0] >= 100 && data[0] <= 109) {
+
+ if (wasConnect) {
+ fail("Two following Connects");
+ }
+
+ wasConnect = true;
+ wasDisconnect = false;
+ }
+
+ if (data[0] >= 110 && data[0] <= 119) {
+
+ if (wasDisconnect) {
+ fail("Two following Disconnects");
+ }
+ wasConnect = false;
+ wasDisconnect = true;
+ }
+
+ }
+
+
+ }
+}
View
85 ...lare-server/src/test/java/de/paluch/powerflare/command/PowerflareSwitchOnCommandTest.java
@@ -0,0 +1,85 @@
+package de.paluch.powerflare.command;
+
+import de.paluch.powerflare.channel.ICommunicationChannel;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+
+import java.util.List;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertNotNull;
+import static org.mockito.Mockito.*;
+
+/**
+ * Created with IntelliJ IDEA. User: mark Date: 03.05.12 Time: 08:18 To change this template use File | Settings | File
+ * Templates.
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class PowerflareSwitchOnCommandTest {
+ private PowerflareSwitchOnCommand sut = new PowerflareSwitchOnCommand(Byte.valueOf("2"));
+
+ @Mock
+ private ICommunicationChannel channel;
+
+ @Mock
+ private ScheduledExecutorService executor;
+
+
+ @Test
+ public void testGetCommunicationCommands() throws Exception {
+ List<? extends RelayCommunicationCallable> result = sut.getCommunicationCommands();
+ assertEquals(2, result.size());
+
+ RelayCommunicationCallable on = result.get(0);
+ RelayCommunicationCallable off = result.get(1);
+
+
+ assertEquals(0, on.getDelay());
+ assertEquals(200, off.getDelay());
+ }
+
+
+ @Test
+ public void testSend() throws Exception {
+ List<? extends RelayCommunicationCallable> result = sut.getCommunicationCommands();
+ assertEquals(2, result.size());
+
+ RelayCommunicationCallable on = result.get(0);
+ RelayCommunicationCallable off = result.get(1);
+
+ on.sendData(channel);
+ ArgumentCaptor<byte[]> captor = ArgumentCaptor.forClass(byte[].class);
+
+ verify(channel).sendData(captor.capture());
+
+ byte[] data = captor.getValue();
+
+ assertNotNull(data);
+ assertEquals(1, data.length);
+ assertEquals(102, data[0]);
+
+ }
+
+ @Test
+ public void testExecute() throws Exception {
+ sut.execute(channel, executor);
+
+ verify(channel).waitForFreeChannel(Byte.valueOf("2"), sut);
+ verify(channel, times(3)).lock(Byte.valueOf("2"), sut);
+
+ verify(executor).schedule(any(Callable.class), eq(0L), eq(TimeUnit.MILLISECONDS));
+ verify(executor).schedule(any(Callable.class), eq(200L), eq(TimeUnit.MILLISECONDS));
+
+ }
+
+ @Test
+ public void testGetPort() throws Exception {
+ assertEquals(2, sut.getPort());
+ }
+}

No commit comments for this range

Something went wrong with that request. Please try again.