Commits

Rhys ! committed f15399f Draft

made example/test apps public and documented; Main-Class now provides version information

Comments (0)

Files changed (4)

-<?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>
-
-	<parent>
-		<groupId>org.sonatype.oss</groupId>
-		<artifactId>oss-parent</artifactId>
-		<version>7</version>
-	</parent>
-
-	<groupId>org.lyrcl.app.pirate</groupId>
-	<artifactId>zpirate</artifactId>
-	<version>0.1.0-SNAPSHOT</version>
-	<packaging>jar</packaging>
-	<name>zpirate</name>
-
-	<description>
-	Implementation of the Paranoid Pirate Protocol for ZeroMQ 2.2.
-    </description>
-
-	<scm>
-		<connection>scm:hg:https://bitbucket.org/rimmington/zpirate</connection>
-		<url>https://bitbucket.org/rimmington/zpirate</url>
-	</scm>
-
-	<licenses>
-		<license>
-			<name>GNU GPL 3</name>
-			<url>http://www.gnu.org/licenses/gpl.txt</url>
-		</license>
-	</licenses>
-
-	<developers>
-		<developer>
-			<id>rimmington</id>
-			<name>Rhys !</name>
-			<email>rimmington@gmail.com</email>
-		</developer>
-	</developers>
-
-	<properties>
-		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
-		<maven.compiler.source>1.6</maven.compiler.source>
-		<maven.compiler.target>1.6</maven.compiler.target>
-
-		<jzmq.version>1.0.1-SNAPSHOT</jzmq.version>
-		<native.os>${os.name}</native.os>
-		<native.arch>${os.arch}</native.arch>
-	</properties>
-
-	<profiles>
-		<profile>
-			<id>Windows</id>
-			<activation>
-				<os>
-					<family>windows</family>
-				</os>
-			</activation>
-			<properties>
-				<native.os>Windows</native.os>
-			</properties>
-		</profile>
-	</profiles>
-
-	<dependencies>
-		<dependency>
-			<groupId>org.zeromq</groupId>
-			<artifactId>jzmq</artifactId>
-			<version>${jzmq.version}</version>
-			<scope>compile</scope>
-		</dependency>
-
-		<dependency>
-			<groupId>org.zeromq</groupId>
-			<artifactId>jzmq</artifactId>
-			<version>${jzmq.version}</version>
-			<classifier>native-${native.arch}-${native.os}</classifier>
-		</dependency>
-	</dependencies>
-
-	<repositories>
-		<repository>
-			<id>jzmq-mv</id>
-			<url>https://bitbucket.org/rimmington/jzmq-mv/raw/default/snapshots</url>
-		</repository>
-	</repositories>
-
-	<build>
-		<sourceDirectory>src</sourceDirectory>
-
-		<plugins>
-
-			<plugin>
-				<groupId>org.codehaus.mojo</groupId>
-				<artifactId>buildnumber-maven-plugin</artifactId>
-				<executions>
-					<execution>
-						<phase>validate</phase>
-						<goals>
-							<goal>create</goal>
-						</goals>
-					</execution>
-				</executions>
-				<configuration>
-					<doCheck>false</doCheck>
-					<doUpdate>false</doUpdate>
-					<timestampPropertyName>buildTimestamp</timestampPropertyName>
-					<timestampFormat>{0,date,yyyy-MM-dd HH:mm Z}</timestampFormat>
-				</configuration>
-			</plugin>
-      
-			<plugin>
-				<groupId>org.apache.maven.plugins</groupId>
-				<artifactId>maven-jar-plugin</artifactId>
-				<inherited>true</inherited>
-				<configuration>
-					<archive>                   
-						<manifest>
-							<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
-						</manifest>
-						<manifestEntries>
-							<Implementation-Version>${buildNumber} ${buildTimestamp}</Implementation-Version>
-						</manifestEntries>
-					</archive>
-				</configuration>
-			</plugin>
-
-			<plugin>
-				<groupId>org.apache.maven.plugins</groupId>
-				<artifactId>maven-shade-plugin</artifactId>
-				<executions>
-					<execution>
-						<phase>package</phase>
-						<goals>
-							<goal>shade</goal>
-						</goals>
-						<configuration>
-							<transformers>
-								<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
-									<manifestEntries>
-										<Main-Class>org.lyrcl.lib.pirate.Test</Main-Class>
-									</manifestEntries>
-								</transformer>
-							</transformers>
-						</configuration>
-					</execution>
-				</executions>
-			</plugin>
-
-      		<plugin>
-				<groupId>org.apache.maven.plugins</groupId>
-				<artifactId>maven-source-plugin</artifactId>
-				<executions>
-					<execution>
-						<id>attach-sources</id>
-						<goals>
-							<goal>jar</goal>
-						</goals>
-					</execution>
-				</executions>
-			</plugin>
-
-			<plugin>
-				<groupId>org.apache.maven.plugins</groupId>
-				<artifactId>maven-javadoc-plugin</artifactId>
-				<executions>
-					<execution>
-						<id>attach-javadoc</id>
-						<goals>
-							<goal>jar</goal>
-						</goals>
-					</execution>
-				</executions>
-			</plugin>
-
-		</plugins>
-
-	</build>
-
+<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/maven-v4_0_0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>org.sonatype.oss</groupId>
+        <artifactId>oss-parent</artifactId>
+        <version>7</version>
+    </parent>
+    <groupId>org.lyrcl.app.pirate</groupId>
+    <artifactId>zpirate</artifactId>
+    <version>0.1.0-SNAPSHOT</version>
+    <packaging>jar</packaging>
+    <name>zpirate</name>
+    <description>Implementation of the Paranoid Pirate Protocol for ZeroMQ 2.2.</description>
+    <scm>
+        <connection>scm:hg:https://bitbucket.org/rimmington/zpirate</connection>
+        <url>https://bitbucket.org/rimmington/zpirate</url>
+    </scm>
+    <licenses>
+        <license>
+            <name>GNU GPL 3</name>
+            <url>http://www.gnu.org/licenses/gpl.txt</url>
+        </license>
+    </licenses>
+    <developers>
+        <developer>
+            <id>rimmington</id>
+            <name>Rhys !</name>
+            <email>rimmington@gmail.com</email>
+        </developer>
+    </developers>
+    <properties>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <maven.compiler.source>1.6</maven.compiler.source>
+        <maven.compiler.target>1.6</maven.compiler.target>
+        <jzmq.version>1.1.0-SNAPSHOT</jzmq.version>
+        <native.os>${os.name}</native.os>
+        <native.arch>${os.arch}</native.arch>
+    </properties>
+    <profiles>
+        <profile>
+            <id>Windows</id>
+            <activation>
+                <os>
+                    <family>windows</family>
+                </os>
+            </activation>
+            <properties>
+                <native.os>Windows</native.os>
+            </properties>
+        </profile>
+    </profiles>
+    <dependencies>
+        <dependency>
+            <groupId>org.zeromq</groupId>
+            <artifactId>jzmq</artifactId>
+            <version>${jzmq.version}</version>
+            <scope>compile</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.zeromq</groupId>
+            <artifactId>jzmq</artifactId>
+            <version>${jzmq.version}</version>
+            <classifier>native-${native.arch}-${native.os}</classifier>
+        </dependency>
+    </dependencies>
+    <build>
+        <sourceDirectory>src</sourceDirectory>
+        <plugins>
+            <plugin>
+                <groupId>org.codehaus.mojo</groupId>
+                <artifactId>buildnumber-maven-plugin</artifactId>
+                <executions>
+                    <execution>
+                        <phase>validate</phase>
+                        <goals>
+                            <goal>create</goal>
+                        </goals>
+                    </execution>
+                </executions>
+                <configuration>
+                    <doCheck>false</doCheck>
+                    <doUpdate>false</doUpdate>
+                    <timestampPropertyName>buildTimestamp</timestampPropertyName>
+                    <timestampFormat>{0,date,yyyy-MM-dd HH:mm Z}</timestampFormat>
+                </configuration>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-jar-plugin</artifactId>
+                <inherited>true</inherited>
+                <configuration>
+                    <archive>
+                        <manifest>
+                            <mainClass>org.lyrcl.lib.pirate.App</mainClass>
+                            <addClasspath>true</addClasspath>
+                            <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
+                        </manifest>
+                        <manifestEntries>
+                            <Implementation-Version>${buildNumber} ${buildTimestamp}</Implementation-Version>
+                        </manifestEntries>
+                    </archive>
+                </configuration>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-source-plugin</artifactId>
+                <executions>
+                    <execution>
+                        <id>attach-sources</id>
+                        <goals>
+                            <goal>jar</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-javadoc-plugin</artifactId>
+                <executions>
+                    <execution>
+                        <id>attach-javadoc</id>
+                        <goals>
+                            <goal>jar</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
 </project>

src/org/lyrcl/lib/pirate/App.java

+package org.lyrcl.lib.pirate;
+
+/**
+ * App that displays version information, plus a few examples.
+ */
+public class App {
+    /**
+     * Runs a queue that listens for clients on port 5555 and workers on port
+     * 5556.
+     */
+    public static void queue() {
+        new PirateQueue("tcp://*:5555", "tcp://*:5556").run();
+    }
+
+    /**
+     * Runs a client that connects to tcp://localhost:5555 and expects the
+     * server to echo messages sent to it.
+     */
+    public static void client() {
+        System.out.println("Connecting to server…");
+        PirateClient client = new PirateClient("tcp://localhost:5555");
+
+        Integer sequence = 0;
+        while (!Thread.currentThread().isInterrupted()) {
+            // We send a request, then we work to get a reply
+            sequence++;
+            final byte[] request = sequence.toString().getBytes();
+            final byte[] reply = client.req(request);
+            if (reply == null) {
+                break;
+            }
+
+            final String replyString = new String(reply).trim();
+
+            int replySequence = -1;
+            try {
+                replySequence = Integer.parseInt(replyString);
+            } catch (Exception e) {
+                // Do nothing
+            }
+
+            if (replySequence == sequence) {
+                System.out.printf("I: server replied OK (%d)\n",
+                        replySequence);
+            } else {
+                System.out.printf("E: malformed reply from server: (%s)\n",
+                        replyString);
+            }
+        }
+
+        client.close();
+    }
+
+    /**
+     * Worker that connects to a queue at tcp://localhost:5556 and echos
+     * back integers and -1 for anything that does not parse to an integer,
+     */
+    public static void worker() {
+        PirateWorker worker = new PirateWorker("tcp://localhost:5556");
+        byte[] work = worker.recv(null);
+        while (work != null && !Thread.currentThread().isInterrupted()) {
+            Integer n;
+            try {
+                n = Integer.parseInt(new String(work));
+            } catch (NumberFormatException e) {
+                work = worker.recv("-1".getBytes());
+                continue;
+            }
+
+            work = worker.recv(n.toString().getBytes());
+        }
+    }
+
+    /**
+     * Print out version information.
+     *
+     * @param args Command line arguments (ignored).
+     * @throws Exception An exception occurred.
+     */
+    public static void main(String[] args) throws Exception {
+        final Package p = App.class.getPackage();
+        final String appName = p.getSpecificationTitle();
+        final String versionMaven = p.getSpecificationVersion();
+        String[] version = new String[] {"", ""};
+        if (p.getImplementationVersion() != null) {
+            version = p.getImplementationVersion().split(" ", 2);
+        }
+
+        final String fmt = "%-7.7s %-15.15s %s%n";
+
+        System.out.printf(fmt, appName, "version:", versionMaven);
+        System.out.printf(fmt, appName, "build time:", version[1]);
+        System.out.printf(fmt, appName, "build commit:", version[0]);
+
+        System.out.println();
+        org.zeromq.App.main(new String[] {});
+    }
+}

src/org/lyrcl/lib/pirate/PirateProtocol.java

 /**
  * Pirate protocol constants.
  */
-final class PirateProtocol {
+public final class PirateProtocol {
     /**
      * The bytes signifying a new worker is ready for work.
      */

src/org/lyrcl/lib/pirate/Test.java

-package org.lyrcl.lib.pirate;
-
-class Test {
-    public static void queue() {
-        new PirateQueue("tcp://*:5555", "tcp://*:5556").run();
-    }
-
-    public static void client() {
-        System.out.println("Connecting to server…");
-        PirateClient client = new PirateClient("tcp://localhost:5555");
-
-        Integer sequence = 0;
-        while (true) {
-            // We send a request, then we work to get a reply
-            sequence++;
-            final byte[] request = sequence.toString().getBytes();
-            final byte[] reply = client.req(request);
-            if (reply == null) {
-                break;
-            }
-
-            final String replyString = new String(reply).trim();
-
-            int replySequence = -1;
-            try {
-                replySequence = Integer.parseInt(replyString);
-            } catch (Exception e) {
-                // Do nothing
-            }
-
-            if (replySequence == sequence) {
-                System.out.printf("I: server replied OK (%d)\n",
-                        replySequence);
-            } else {
-                System.out.printf("E: malformed reply from server: (%s)\n",
-                        replyString);
-            }
-        }
-
-        client.close();
-    }
-
-    public static void worker() {
-        PirateWorker worker = new PirateWorker("tcp://localhost:5556");
-        byte[] work = worker.recv(null);
-        while (work != null) {
-            Integer n = Integer.parseInt(new String(work));
-            work = worker.recv(n.toString().getBytes());
-        }
-    }
-
-    public static void main(String[] args) {
-        Test.client();
-    }
-}