Skip to content

Commit

Permalink
Added my solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
viswans83 committed Sep 25, 2014
1 parent 8c49fe7 commit 9b7b63b
Show file tree
Hide file tree
Showing 68 changed files with 1,313 additions and 0 deletions.
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>TelemetrySystem</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
@@ -0,0 +1,5 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
org.eclipse.jdt.core.compiler.compliance=1.5
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.5
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
@@ -0,0 +1,15 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TelemetrySystem</groupId>
<artifactId>TelemetrySystem</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<type>jar</type>
<scope>test</scope>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,17 @@
package tddmicroexercises.telemetrysystem;

public interface ITelemetryClient {

public static final String DIAGNOSTIC_MESSAGE = "AT#UD";

boolean getOnlineStatus();

void connect(String telemetryServerConnectionString);

void disconnect();

void send(String message);

String receive();

}
@@ -0,0 +1,98 @@
package tddmicroexercises.telemetrysystem;

import java.util.Random;

public class TelemetryClient implements ITelemetryClient
{
//
// The communication with the server is simulated in this implementation.
// Because the focus of the exercise is on the other class.
//

private boolean onlineStatus;
private boolean diagnosticMessageJustSent = false;

private final Random connectionEventsSimulator = new Random();
private final Random randomMessageSimulator = new Random();

public boolean getOnlineStatus()
{
return onlineStatus;
}

public void connect(String telemetryServerConnectionString)
{
if (telemetryServerConnectionString == null || "".equals(telemetryServerConnectionString))
{
throw new IllegalArgumentException();
}

// Fake the connection with 20% chances of success
boolean success = connectionEventsSimulator.nextInt(10) <= 2;

onlineStatus = success;
}

public void disconnect()
{
onlineStatus = false;
}

public void send(String message)
{
if (message == null || "".equals(message))
{
throw new IllegalArgumentException();
}

// The simulation of Send() actually just remember if the last message sent was a diagnostic message.
// This information will be used to simulate the Receive(). Indeed there is no real server listening.
if (message == DIAGNOSTIC_MESSAGE)
{
diagnosticMessageJustSent = true;
}
else
{
diagnosticMessageJustSent = false;
}
}

public String receive()
{
String message;

if (diagnosticMessageJustSent)
{
// Simulate the reception of the diagnostic message
message = "LAST TX rate................ 100 MBPS\r\n"
+ "HIGHEST TX rate............. 100 MBPS\r\n"
+ "LAST RX rate................ 100 MBPS\r\n"
+ "HIGHEST RX rate............. 100 MBPS\r\n"
+ "BIT RATE.................... 100000000\r\n"
+ "WORD LEN.................... 16\r\n"
+ "WORD/FRAME.................. 511\r\n"
+ "BITS/FRAME.................. 8192\r\n"
+ "MODULATION TYPE............. PCM/FM\r\n"
+ "TX Digital Los.............. 0.75\r\n"
+ "RX Digital Los.............. 0.10\r\n"
+ "BEP Test.................... -5\r\n"
+ "Local Rtrn Count............ 00\r\n"
+ "Remote Rtrn Count........... 00";

diagnosticMessageJustSent = false;
}
else
{
// Simulate the reception of a response message returning a random message.
message = "";
int messageLength = randomMessageSimulator.nextInt(50) + 60;
for(int i = messageLength; i > 0; --i)
{
message += (char)randomMessageSimulator.nextInt(40) + 86;
}
}

return message;
}
}

@@ -0,0 +1,50 @@
package tddmicroexercises.telemetrysystem;

public class TelemetryDiagnosticControls
{
private final static String DIAGNOSTIC_CHANNEL_CONNECTION_STRING = "*111#";

private final ITelemetryClient telemetryClient;
private String diagnosticInfo = "";

public TelemetryDiagnosticControls()
{
telemetryClient = new TelemetryClient();
}

public TelemetryDiagnosticControls(ITelemetryClient telemetryClient) {
this.telemetryClient = telemetryClient;
}

public String getDiagnosticInfo()
{
return diagnosticInfo;
}

public void setDiagnosticInfo(String diagnosticInfo)
{
this.diagnosticInfo = diagnosticInfo;
}

public void checkTransmission() throws Exception
{
diagnosticInfo = "";

telemetryClient.disconnect();

int retryLeft = 3;
while (telemetryClient.getOnlineStatus() == false && retryLeft > 0)
{
telemetryClient.connect(DIAGNOSTIC_CHANNEL_CONNECTION_STRING);
retryLeft -= 1;
}

if(telemetryClient.getOnlineStatus() == false)
{
throw new Exception("Unable to connect.");
}

telemetryClient.send(TelemetryClient.DIAGNOSTIC_MESSAGE);
diagnosticInfo = telemetryClient.receive();
}
}

0 comments on commit 9b7b63b

Please sign in to comment.