Browse Source

Initial commit of LibAPIs.

Matt Clark 2 years ago
parent
commit
a311e7df55

+ 1 - 0
.gitignore

@@ -1,3 +1,4 @@
+.classpath
 .project
 .settings
 bin

+ 61 - 70
pom.xml

@@ -1,70 +1,61 @@
-<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>
-
-	<organization>
-		<name>MClarkDev.com</name>
-		<url>https://tools.mclarkdev.com/tools</url>
-	</organization>
-
-	<groupId>com.mclarkdev.tools</groupId>
-	<artifactId>libmetrics</artifactId>
-	<version>PRERELEASE</version>
-	<packaging>jar</packaging>
-
-	<name>libmetrics</name>
-	<url>https://www.mclarkdev.com/tools/libmetrics</url>
-	<description>An generic metrics cache for collecting various system counters.</description>
-
-	<scm>
-		<url>https://git.mclarkdev.com/MClarkDev.com--Public/LibMetrics.git</url>
-	</scm>
-
-	<properties>
-		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
-		<maven.compiler.target>1.8</maven.compiler.target>
-		<maven.compiler.source>1.8</maven.compiler.source>
-	</properties>
-
-	<build>
-		<sourceDirectory>src/main/java</sourceDirectory>
-		<plugins>
-			<plugin>
-				<groupId>org.apache.maven.plugins</groupId>
-				<artifactId>maven-source-plugin</artifactId>
-				<version>3.1.0</version>
-				<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>
-				<version>3.1.1</version>
-				<executions>
-					<execution>
-						<id>attach-javadocs</id>
-						<goals>
-							<goal>jar</goal>
-						</goals>
-					</execution>
-				</executions>
-			</plugin>
-		</plugins>
-	</build>
-
-	<dependencies>
-		<dependency>
-			<groupId>org.json</groupId>
-			<artifactId>json</artifactId>
-			<version>20160212</version>
-			<scope>compile</scope>
-		</dependency>
-	</dependencies>
-</project>
+<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>
+
+	<organization>
+		<name>MClarkDev.com</name>
+		<url>https://tools.mclarkdev.com/tools</url>
+	</organization>
+
+	<groupId>com.mclarkdev.tools</groupId>
+	<artifactId>libsockets</artifactId>
+	<version>PRERELEASE</version>
+	<packaging>jar</packaging>
+
+	<name>libsockets</name>
+	<url>https://www.mclarkdev.com/tools/libsockets</url>
+	<description>A generic socket manager.</description>
+
+	<scm>
+		<url>https://git.mclarkdev.com/MClarkDev.com--Public/libsockets.git</url>
+	</scm>
+
+	<properties>
+		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+		<maven.compiler.target>1.8</maven.compiler.target>
+		<maven.compiler.source>1.8</maven.compiler.source>
+	</properties>
+
+	<build>
+		<sourceDirectory>src/main/java</sourceDirectory>
+		<plugins>
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-source-plugin</artifactId>
+				<version>3.1.0</version>
+				<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>
+				<version>3.1.1</version>
+				<executions>
+					<execution>
+						<id>attach-javadocs</id>
+						<goals>
+							<goal>jar</goal>
+						</goals>
+					</execution>
+				</executions>
+			</plugin>
+		</plugins>
+	</build>
+</project>

+ 0 - 123
src/main/java/com/mclarkdev/tools/libmetrics/LibMetrics.java

@@ -1,123 +0,0 @@
-package com.mclarkdev.tools.libmetrics;
-
-import org.json.JSONObject;;
-
-public class LibMetrics {
-
-	private static LibMetrics statsCollector;
-
-	private final JSONObject counterCache = new JSONObject();
-
-	private LibMetrics() {
-	}
-
-	/**
-	 * Increment the value of a single counter by one.
-	 * 
-	 * @param name
-	 */
-	public void hitCounter(String... name) {
-		hitCounter(1, name);
-	}
-
-	/**
-	 * Increment the value of a single counter by the specified value.
-	 * 
-	 * @param name
-	 */
-	public void hitCounter(double count, String... name) {
-
-		synchronized (counterCache) {
-
-			JSONObject outer = counterCache;
-			for (int x = 0; x < name.length - 1; x++) {
-
-				JSONObject inner = outer.optJSONObject(name[x]);
-				if (inner == null) {
-
-					inner = new JSONObject();
-					outer.put(name[x], inner);
-				}
-				outer = inner;
-			}
-
-			String key = name[name.length - 1];
-			double current = outer.optDouble(key, 0);
-			outer.put(key, current + count);
-		}
-	}
-
-	/**
-	 * Get a single counter by name.
-	 * 
-	 * @param name
-	 * @return
-	 */
-	public double getCounter(String... name) {
-
-		JSONObject outer = counterCache;
-		for (String key : name) {
-
-			JSONObject inner = outer.optJSONObject(key);
-			if (inner == null) {
-
-				inner = new JSONObject();
-				outer.put(key, inner);
-			}
-			outer = inner;
-		}
-
-		return outer.getDouble("value");
-	}
-
-	/**
-	 * Set the value for a counter.
-	 * 
-	 * @param name
-	 * @param value
-	 */
-	public void setValue(Object value, String... name) {
-
-		synchronized (counterCache) {
-
-			JSONObject outer = counterCache;
-			for (int x = 0; x < name.length - 1; x++) {
-
-				JSONObject inner = outer.optJSONObject(name[x]);
-				if (inner == null) {
-
-					inner = new JSONObject();
-					outer.put(name[x], inner);
-				}
-				outer = inner;
-			}
-
-			String key = name[name.length - 1];
-			outer.put(key, value);
-		}
-	}
-
-	/**
-	 * Get the details of the counter cache.
-	 * 
-	 * @return
-	 */
-	public JSONObject getDetails() {
-
-		return counterCache;
-	}
-
-	/**
-	 * Get a reference to the shared statistics collector.
-	 * 
-	 * @return
-	 */
-	public static synchronized LibMetrics instance() {
-
-		if (statsCollector == null) {
-
-			statsCollector = new LibMetrics();
-		}
-		return statsCollector;
-	}
-}

+ 52 - 0
src/main/java/com/mclarkdev/tools/libsockets/LibSocketClient.java

@@ -0,0 +1,52 @@
+package com.mclarkdev.tools.libsockets;
+
+import java.io.IOException;
+import java.net.UnknownHostException;
+
+public class LibSocketClient implements LibSocketListener {
+
+	private final String connHost;
+	private final int connPort;
+	private LibSocketConnection client;
+
+	private boolean reconnect = true;
+	private LibSocketListener listener = null;
+
+	public LibSocketClient(LibSocketListener listener, String host, int port) {
+		this.listener = listener;
+		this.connHost = host;
+		this.connPort = port;
+	}
+
+	public void setAutoReconnect(boolean reconnect) {
+		this.reconnect = reconnect;
+	}
+
+	public void connect() throws UnknownHostException, IOException {
+		client = LibSocketConnection.createConnection(this, connHost, connPort);
+	}
+
+	@Override
+	public void onConnect() {
+		listener.onConnect();
+	}
+
+	@Override
+	public void onMessage(LibSocketConnection connection, String message) {
+		listener.onMessage(connection, message);
+	}
+
+	@Override
+	public void onDiconnect(IOException e) {
+		listener.onDiconnect(e);
+		client.shutdown();
+		client = null;
+		if (reconnect) {
+			try {
+				connect();
+			} catch (IOException ex) {
+				e.printStackTrace();
+			}
+		}
+	}
+}

+ 51 - 0
src/main/java/com/mclarkdev/tools/libsockets/LibSocketConnection.java

@@ -0,0 +1,51 @@
+package com.mclarkdev.tools.libsockets;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PrintWriter;
+import java.net.Socket;
+import java.net.UnknownHostException;
+
+public class LibSocketConnection {
+
+	private final LibSocketListener listener;
+
+	private final Socket socket;
+	private final BufferedReader reader;
+	private final PrintWriter writer;
+
+	private LibSocketConnection(LibSocketListener listener, Socket socket) throws IOException {
+
+		this.listener = listener;
+		this.socket = socket;
+
+		this.reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
+		this.writer = new PrintWriter(socket.getOutputStream(), true);
+
+		new Thread() {
+			public void run() {
+				try {
+					String line;
+					while ((line = reader.readLine()) != null) {
+						listener.onMessage(LibSocketConnection.this, line);
+					}
+				} catch (IOException e) {
+					listener.onDiconnect(e);
+				}
+			}
+		}.start();
+		this.listener.onConnect();
+	}
+
+	public void shutdown() {
+	}
+
+	public static LibSocketConnection createConnection(LibSocketListener listener, String host, int port)
+			throws UnknownHostException, IOException {
+
+		Socket s = new Socket(host, port);
+		return new LibSocketConnection(listener, s);
+	}
+
+}

+ 12 - 0
src/main/java/com/mclarkdev/tools/libsockets/LibSocketListener.java

@@ -0,0 +1,12 @@
+package com.mclarkdev.tools.libsockets;
+
+import java.io.IOException;
+
+public interface LibSocketListener {
+
+	public void onConnect();
+
+	public void onMessage(LibSocketConnection connection, String message);
+
+	public void onDiconnect(IOException e);
+}

+ 14 - 0
src/main/java/com/mclarkdev/tools/libsockets/LibSocketServer.java

@@ -0,0 +1,14 @@
+package com.mclarkdev.tools.libsockets;
+
+import java.io.IOException;
+import java.net.ServerSocket;
+
+public class LibSocketServer {
+
+	private final ServerSocket server;
+
+	public LibSocketServer(int port) throws IOException {
+
+		server = new ServerSocket(port);
+	}
+}