Skip to content

Commit

Permalink
Initial upload of finished project.
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanFleck committed Mar 20, 2020
1 parent 9663e80 commit 54c62a7
Show file tree
Hide file tree
Showing 12 changed files with 825 additions and 2 deletions.
29 changes: 29 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,34 @@
*.tar.gz
*.rar

# Eclipse
*target*
*.jar
*.war
*.ear
*.class

# eclipse specific git ignore
*.pydevproject
.project
.metadata
bin/**
tmp/**
tmp/**/*
*.tmp
*.bak
*.swp
*~.nib
local.properties
.classpath
.settings/
.loadpath

# External tool builders
.externalToolBuilders/

# Locally stored "Eclipse launch configurations"
*.launch

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# HDB3
HDB3 encoder/decoder library, including REPL and socket-based client and server toys for sample transmission.
# HDB3 App

HDB3 encoder/decoder application, including REPL and socket-based client and server toys for sample transmission.
Binary file added docs/system_demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
91 changes: 91 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>

<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>ca.rcf</groupId>
<artifactId>hdb3</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>

<name>hdb3</name>
<url>https://ryanfleck.ca</url>

<build>
<finalName>hdb3</finalName>

<!-- Keep Java Source Code in Folder -->
<resources>
<resource>
<directory>src/main/java</directory>
<filtering>false</filtering>
</resource>
</resources>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>libs/</classpathPrefix>
<mainClass>ca.rcf.hdb3.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/libs
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

</plugins>
</build>


<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/com.beust/jcommander -->
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>1.78</version>
</dependency>

</dependencies>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<javac.target.version>1.8</javac.target.version>
<javac.src.version>1.8</javac.src.version>
</properties>
</project>
122 changes: 122 additions & 0 deletions src/main/java/ca/rcf/hdb3/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/**
*
*/
package ca.rcf.hdb3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;

import ca.rcf.hdb3.conversion.Binary;
import ca.rcf.hdb3.conversion.HDB3;
import ca.rcf.hdb3.networking.Client;
import ca.rcf.hdb3.networking.Server;

/**
* @author Ryan Fleck
*
*/
public class App {

// @Parameter(description = "Files to process.")
// private List<String> parameters = new ArrayList<>();

@Parameter(order = 3, names = { "-v", "--verbose" }, description = "Enable debugging output")
private boolean VERBOSE = false;

@Parameter(order = 0, names = { "-r", "--repl" }, description = "Enter a REPL instead of using a file input")
private boolean STARTCLI = false;

@Parameter(order = 1, names = { "-c", "--client" }, description = "Boot to CLIENT mode")
private boolean STARTCLIENT = false;

@Parameter(order = 2, names = { "-s", "--server" }, description = "Boot to SERVER mode")
private boolean STARTSERVER = false;

public static boolean debug = false; // false;
private static JCommander j;

public static void main(String[] args) {
System.out.println("[rcf] Ryan Fleck - HDB3 Encoder");

App app = new App();

j = JCommander.newBuilder().addObject(app).build();
j.setProgramName("java -jar hdb3.jar");
j.parse(args);

app.run();

}

public void run() {

if (!(VERBOSE || STARTCLI || STARTCLIENT || STARTSERVER)) {
j.usage();
System.exit(0);
}

if (VERBOSE) {
System.out.println("[dbg] Using verbose logging.");
debug = true;
}

if (STARTCLIENT || STARTSERVER) {
System.out.println("Starting remote system.");

if (STARTCLIENT) {
Client.run();
} else {
Server.run();
}

} else if (STARTCLI) {
System.out.println("Starting REPL.");
repl();
}
}

public static void repl() {
Client.printUsage();
while (true) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = "";
try {
System.out.print("\nCONVERT > ");
s = br.readLine();
if (s.startsWith("quit") || s.startsWith("exit") || s.startsWith("halt")) {
System.out.println("\nParting is such sweet sorrow!\n");
System.exit(0);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

if (isBinary(s)) {
System.out.println("HDB-3 " + HDB3.binaryHDB3encode(s));
} else {
System.out.println("BINARY " + Binary.encode(s));
System.out.println("HDB-3 " + HDB3.encode(s));
}
}
}

public static void dbg(String s) {
if (debug)
System.out.println(s);
}

public static boolean isBinary(String s) {
char[] ca = s.toCharArray();
for (char c : ca) {
if (c != '1' && c != '0')
return false;
}
return true;
}

}
72 changes: 72 additions & 0 deletions src/main/java/ca/rcf/hdb3/conversion/Binary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
*
*/
package ca.rcf.hdb3.conversion;

import ca.rcf.hdb3.App;

/**
* @author Ryan Fleck
*
*/
public class Binary {

public static String encode(String s) {
StringBuilder sb = new StringBuilder();
App.dbg("\n\n[dbg] Converting '" + s + "' to binary.\nBytes:");

byte[] bytes = s.getBytes();
for (byte b : bytes) {
sb.append("");
String a = String.format("%8s", Integer.toBinaryString(b & 0xFF)).replace(' ', '0');
App.dbg(b + " -> " + a);
sb.append(a);
}

String res = sb.toString();
System.out.println("Text -> Binary : "+res);
return res;
}

public static String decode(String s) {
App.dbg("\n\n[dbg] Converting " + s + " to UTF-8 string.");
StringBuilder sb = new StringBuilder();
char[] chars = s.toCharArray();

int sum = 0;
int bitcount = 0;
for (int i = 0; i < chars.length; i++) {

// Append every four bits.
if (chars[i] == '1') {
sum += 1 << (7 - bitcount);
}

bitcount++;
App.dbg("Bit " + bitcount + ": " + chars[i]);

if (bitcount == 8) {
char[] x = Character.toChars(sum);

// Overkill for printing one char, revise:
if (App.debug) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Got character ");
stringBuilder.append(sum);
stringBuilder.append(": ");
stringBuilder.append(x);
App.dbg(stringBuilder.toString());
App.dbg(String.format("%8s", Integer.toBinaryString(sum & 0xFF)).replace(' ', '0'));
}

sb.append(x);
sum = 0;
bitcount = 0;
}
}

String res = sb.toString();
System.out.println("Binary -> Text : "+res);
return res;
}
}
Loading

0 comments on commit 54c62a7

Please sign in to comment.