Skip to content

Commit

Permalink
Added test project for Netty client.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbinTheander committed Mar 10, 2011
1 parent 41db027 commit 2d29bff
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 4 deletions.
7 changes: 6 additions & 1 deletion mqtt-library/src/main/java/com/albin/mqtt/NettyClient.java
Expand Up @@ -22,6 +22,11 @@ public class NettyClient {

private Channel channel;
private ClientBootstrap bootstrap;
private final String id;

public NettyClient(String id) {
this.id = id;
}

public void connect() {
bootstrap = new ClientBootstrap(
Expand Down Expand Up @@ -50,7 +55,7 @@ public ChannelPipeline getPipeline() throws Exception {
return;
}

channel.write(new ConnectMessage("Albintest", true, 30));
channel.write(new ConnectMessage(id, true, 30));
}

public void disconnect() {
Expand Down
69 changes: 69 additions & 0 deletions netty-test-client/pom.xml
@@ -0,0 +1,69 @@
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>root</artifactId>
<groupId>com.albin.mqtt</groupId>
<version>0.1-SNAPSHOT</version>
</parent>
<groupId>com.albin.mqtt</groupId>
<artifactId>netty-test-client</artifactId>
<version>0.1-SNAPSHOT</version>
<name>netty-test-client</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.albin.NettyMain</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.albin.NettyMain</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.albin.mqtt</groupId>
<artifactId>mqtt-library</artifactId>
<version>0.1-SNAPSHOT</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
20 changes: 20 additions & 0 deletions netty-test-client/src/main/java/com/albin/Main.java
@@ -0,0 +1,20 @@
package com.albin;

import java.io.IOException;
import java.net.UnknownHostException;

import com.albin.mqtt.MqttClient;

public class Main {

public static void main(String[] args) throws UnknownHostException, IOException, InterruptedException {
MqttClient client = new MqttClient();
client.connect("localhost", 1883, "Albin");
Thread.sleep(3000);
// client.subscribe("$SYS/#");
Thread.sleep(1000);
Thread.sleep(30000);
client.disconnect();
}

}
51 changes: 51 additions & 0 deletions netty-test-client/src/main/java/com/albin/NettyMain.java
@@ -0,0 +1,51 @@
package com.albin;

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

import com.albin.mqtt.NettyClient;

public class NettyMain {

private static NettyClient client;
private static String topic;

public static void main(String[] args) throws InterruptedException, IOException {
client = new NettyClient(args[0]);
client.connect();
beInteractive();
client.disconnect();
}

private static void beInteractive() throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line;
do {
line = in.readLine();
if (line.startsWith("pub"))
publish(line.substring(4));
if (line.startsWith("sub"))
subscribe(line.substring(4));
if (line.startsWith("unsub"))
unsubscribe(line.substring(6));
if (line.startsWith("topic")) {
topic = line.substring(6);
}
} while(!"bye".equals(line));

}

private static void unsubscribe(String topic) {
client.unsubscribe(topic);
}

private static void subscribe(String topic) {
client.subscribe(topic);
}

private static void publish(String msg) {
client.publish(topic, msg);
}

}
7 changes: 4 additions & 3 deletions pom.xml
@@ -1,5 +1,5 @@
<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">
<?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>com.albin.mqtt</groupId>
Expand All @@ -16,6 +16,7 @@

<modules>
<module>mqtt-library</module>
<module>netty-test-client</module>
</modules>

</project>
</project>

0 comments on commit 2d29bff

Please sign in to comment.