* INITIAL POPULATIONS OF THIS REPO
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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>
|
||||
<groupId>com.fourelementscapital</groupId>
|
||||
<artifactId>lib-socket</artifactId>
|
||||
<version>2.0</version>
|
||||
<licenses>
|
||||
<license>
|
||||
<name>The software is only allowed to be used in Four Elements - no license is given to any other parties</name>
|
||||
</license>
|
||||
</licenses>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-api</artifactId>
|
||||
<version>2.8.1</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-core</artifactId>
|
||||
<version>2.8.1</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
<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.test</groupId>
|
||||
<artifactId>testSocketClient</artifactId>
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>testSocketClient</name>
|
||||
<url>http://www.fourelementscapital.com</url>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<!-- build an executable JAR -->
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>3.0.2</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
<classpathPrefix>lib/</classpathPrefix>
|
||||
<mainClass>com.test.App</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<version>2.6</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
<mainClass>com.test.App</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
<descriptorRefs>
|
||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||
</descriptorRefs>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>make-assembly</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fourelementscapital</groupId>
|
||||
<artifactId>lib-socket</artifactId>
|
||||
<version>1.0</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,36 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
|
||||
* All rights reserved.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
package com.test;
|
||||
|
||||
import com.fourelementscapital.socket.SocketClient;
|
||||
|
||||
/**
|
||||
* A com.fourelementscapital Class
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main( String[] args )
|
||||
{
|
||||
try {
|
||||
|
||||
String message = "theme$#=itools~theme$#=computing~subject$#=test~body$#=-iMonitor~recipients$#=ari@4ecap.com~sayIt$#=FALSE~emailIt$#=TRUE~phoneCall$#=FALSE~ym$#=FALSE~sms$#=FALSE<EOF>";
|
||||
|
||||
for (int i=0; i<5; i++) {
|
||||
SocketClient.sendMessage(message);
|
||||
}
|
||||
|
||||
System.out.println("Message sent ");
|
||||
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
mail.smtp.auth=true
|
||||
mail.smtp.starttls.enable=true
|
||||
mail.smtp.host=smtp.gmail.com
|
||||
mail.smtp.port=587
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
socket_server_address=localhost
|
||||
socket_server_port_number=1777
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration status="WARN">
|
||||
<Appenders>
|
||||
<Console name="CONSOLE" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="%4p %d{HH:mm:ss,SSS} %C - %m%n" />
|
||||
</Console>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Root level="ERROR">
|
||||
<AppenderRef ref="CONSOLE"/>
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
|
||||
* All rights reserved.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
package com.test;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
/**
|
||||
* Unit test for App
|
||||
*/
|
||||
public class AppTest extends TestCase {
|
||||
|
||||
/**
|
||||
* Create the test case
|
||||
*
|
||||
* @param testName name of the test case
|
||||
*/
|
||||
public AppTest( String testName )
|
||||
{
|
||||
super( testName );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the suite of tests being tested
|
||||
*/
|
||||
public static Test suite()
|
||||
{
|
||||
return new TestSuite( AppTest.class );
|
||||
}
|
||||
|
||||
/**
|
||||
* Empty Test
|
||||
*/
|
||||
public void testApp()
|
||||
{
|
||||
assertTrue( true );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
<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.test</groupId>
|
||||
<artifactId>testSocketServer</artifactId>
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>testSocketServer</name>
|
||||
<url>http://www.fourelementscapital.com</url>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<!-- build an executable JAR -->
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>3.0.2</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
<classpathPrefix>lib/</classpathPrefix>
|
||||
<mainClass>com.test.App</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<version>2.6</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
<mainClass>com.test.App</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
<descriptorRefs>
|
||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||
</descriptorRefs>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>make-assembly</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fourelementscapital</groupId>
|
||||
<artifactId>lib-imonitor</artifactId>
|
||||
<version>1.0</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,32 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
|
||||
* All rights reserved.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
package com.test;
|
||||
|
||||
import com.fourelementscapital.imonitor.IMonitorSocketProcess;
|
||||
import com.fourelementscapital.socket.SocketServer;
|
||||
|
||||
/**
|
||||
* A com.fourelementscapital Class
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main( String[] args )
|
||||
{
|
||||
try {
|
||||
|
||||
// Create StackAlarmSocketProcess object and injected to SocketServer so it will serve as StackAlarm listener :
|
||||
IMonitorSocketProcess iMonitorSocketProcess = new IMonitorSocketProcess();
|
||||
SocketServer.listen(iMonitorSocketProcess);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
mail.smtp.auth=true
|
||||
mail.smtp.starttls.enable=true
|
||||
mail.smtp.host=smtp.gmail.com
|
||||
mail.smtp.port=587
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
socket_server_address=localhost
|
||||
socket_server_port_number=1777
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration status="WARN">
|
||||
<Appenders>
|
||||
<Console name="CONSOLE" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="%4p %d{HH:mm:ss,SSS} %C - %m%n" />
|
||||
</Console>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Root level="ERROR">
|
||||
<AppenderRef ref="CONSOLE"/>
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
|
||||
* All rights reserved.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
package com.test;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
/**
|
||||
* Unit test for App
|
||||
*/
|
||||
public class AppTest extends TestCase {
|
||||
|
||||
/**
|
||||
* Create the test case
|
||||
*
|
||||
* @param testName name of the test case
|
||||
*/
|
||||
public AppTest( String testName )
|
||||
{
|
||||
super( testName );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the suite of tests being tested
|
||||
*/
|
||||
public static Test suite()
|
||||
{
|
||||
return new TestSuite( AppTest.class );
|
||||
}
|
||||
|
||||
/**
|
||||
* Empty Test
|
||||
*/
|
||||
public void testApp()
|
||||
{
|
||||
assertTrue( true );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Executable
+21
@@ -0,0 +1,21 @@
|
||||
<?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>
|
||||
<groupId>com.fourelementscapital</groupId>
|
||||
<artifactId>lib</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<groupId>com.fourelementscapital</groupId>
|
||||
<artifactId>lib-socket</artifactId>
|
||||
<name>lib-socket</name>
|
||||
<description>a Java library containing socket server and client</description>
|
||||
<url>http://www.fourelementscapital.com</url>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.logging.log4j</groupId>
|
||||
<artifactId>log4j-api</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,47 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
|
||||
* All rights reserved.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
package com.fourelementscapital.socket;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* A class to read config file
|
||||
*/
|
||||
class Config {
|
||||
|
||||
/**
|
||||
* Get the value of a property defined in config file
|
||||
* @param propertyName - the name or key of the property you want to retrieve
|
||||
* @return the string value
|
||||
* @throws IOException
|
||||
*/
|
||||
protected static String getConfigValue(String propertyName) throws IOException
|
||||
{
|
||||
Properties prop = new Properties();
|
||||
String propFileName = "config_socket.properties";
|
||||
|
||||
InputStream inputStream = Config.class.getClassLoader().getResourceAsStream(propFileName);
|
||||
|
||||
if (inputStream != null) {
|
||||
prop.load(inputStream);
|
||||
} else {
|
||||
throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
|
||||
}
|
||||
|
||||
// get the property value and print it out
|
||||
String value = prop.getProperty(propertyName);
|
||||
return value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
|
||||
* All rights reserved.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
package com.fourelementscapital.socket;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.SocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.AsynchronousSocketChannel;
|
||||
import java.nio.channels.CompletionHandler;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
/**
|
||||
* Socket Client to send message to Socket Server
|
||||
*/
|
||||
public class SocketClient {
|
||||
|
||||
private static final Logger log = LogManager.getLogger(SocketClient.class.getName());
|
||||
|
||||
/**
|
||||
* Send message to listening socket server
|
||||
* @param message Message being sent
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void sendMessage(String message) throws Exception {
|
||||
|
||||
AsynchronousSocketChannel channel = AsynchronousSocketChannel.open();
|
||||
String serverAddress = Config.getConfigValue("socket_server_address");
|
||||
int port = Integer.parseInt(Config.getConfigValue("socket_server_port_number"));
|
||||
SocketAddress serverAddr = new InetSocketAddress(serverAddress, port);
|
||||
Future<Void> result = channel.connect(serverAddr);
|
||||
result.get();
|
||||
log.debug("Connected");
|
||||
AttachmentClient attach = new AttachmentClient();
|
||||
attach.channel = channel;
|
||||
attach.buffer = ByteBuffer.allocate(2048);
|
||||
|
||||
Charset cs = Charset.forName("UTF-8");
|
||||
byte[] data = message.getBytes(cs);
|
||||
attach.buffer.put(data);
|
||||
attach.buffer.flip();
|
||||
|
||||
ReadWriteHandlerClient readWriteHandler = new ReadWriteHandlerClient();
|
||||
channel.write(attach.buffer, attach, readWriteHandler);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Class as value container to be passed among methods
|
||||
*/
|
||||
class AttachmentClient {
|
||||
AsynchronousSocketChannel channel;
|
||||
ByteBuffer buffer;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Class to read / write process
|
||||
*/
|
||||
class ReadWriteHandlerClient implements CompletionHandler<Integer, AttachmentClient> {
|
||||
|
||||
private static final Logger log = LogManager.getLogger(ReadWriteHandlerClient.class.getName());
|
||||
|
||||
/**
|
||||
* An override method to be processed after operation has completed
|
||||
* @param result Result
|
||||
* @param attach AttachmentClient object
|
||||
*/
|
||||
@Override
|
||||
public void completed(Integer result, AttachmentClient attach) {
|
||||
// do process here
|
||||
}
|
||||
|
||||
/**
|
||||
* An override method to be processed when operation fails
|
||||
* @param e Throwable object
|
||||
* @param attach AttachmentClient object
|
||||
*/
|
||||
@Override
|
||||
public void failed(Throwable e, AttachmentClient attach) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
|
||||
* All rights reserved.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
package com.fourelementscapital.socket;
|
||||
|
||||
/**
|
||||
* An interface to be implemented to do a process then being injected to SocketServer.
|
||||
*/
|
||||
public interface SocketProcess {
|
||||
|
||||
/**
|
||||
* Run process
|
||||
* @param strReadLine Message received by SocketServer to be processed further.
|
||||
*/
|
||||
void run(String strReadLine);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
|
||||
* All rights reserved.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
package com.fourelementscapital.socket;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.SocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.AsynchronousServerSocketChannel;
|
||||
import java.nio.channels.AsynchronousSocketChannel;
|
||||
import java.nio.channels.CompletionHandler;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
/**
|
||||
* Socket Server to listen & process client's message after receive it
|
||||
*/
|
||||
public class SocketServer {
|
||||
|
||||
private static final Logger log = LogManager.getLogger(SocketServer.class.getName());
|
||||
|
||||
/**
|
||||
* Listening message and process it. Use SocketProcess as dependency injection.
|
||||
* @param socketProcess SocketProcess as dependency injection to process message
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void listen(SocketProcess socketProcess) throws Exception {
|
||||
AsynchronousServerSocketChannel server = AsynchronousServerSocketChannel.open();
|
||||
String host = "0.0.0.0"; // all IPs available on this host
|
||||
int port = Integer.parseInt(Config.getConfigValue("socket_server_port_number"));
|
||||
InetSocketAddress sAddr = new InetSocketAddress(host, port);
|
||||
server.bind(sAddr);
|
||||
System.out.print(String.format("Server is listening at %s%n", sAddr)); // sysout, not log
|
||||
AttachmentServer attach = new AttachmentServer();
|
||||
attach.server = server;
|
||||
attach.socketProcess = socketProcess;
|
||||
server.accept(attach, new ConnectionHandlerServer());
|
||||
Thread.currentThread().join();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Class as value container to be passed among methods
|
||||
*/
|
||||
class AttachmentServer {
|
||||
AsynchronousServerSocketChannel server;
|
||||
AsynchronousSocketChannel client;
|
||||
ByteBuffer buffer;
|
||||
SocketAddress clientAddr;
|
||||
SocketProcess socketProcess;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Server connection handler
|
||||
*/
|
||||
class ConnectionHandlerServer implements CompletionHandler<AsynchronousSocketChannel, AttachmentServer> {
|
||||
|
||||
private static final Logger log = LogManager.getLogger(ConnectionHandlerServer.class.getName());
|
||||
|
||||
private static final int BUFFER_ALLOCATED_IN_BYTE = 10000; // how many chars allowed in 1 socket message;
|
||||
|
||||
/**
|
||||
* An override method to be processed after operation has completed
|
||||
* @param client AsynchronousSocketChannel object
|
||||
* @param attach AttachmentServer object
|
||||
*/
|
||||
@Override
|
||||
public void completed(AsynchronousSocketChannel client, AttachmentServer attach) {
|
||||
try {
|
||||
SocketAddress clientAddr = client.getRemoteAddress();
|
||||
System.out.print(String.format("Accepted connection from %s%n", clientAddr)); // sysout, not log
|
||||
attach.server.accept(attach, this);
|
||||
ReadWriteHandlerServer rwHandler = new ReadWriteHandlerServer();
|
||||
AttachmentServer newAttach = new AttachmentServer();
|
||||
newAttach.server = attach.server;
|
||||
newAttach.client = client;
|
||||
//newAttach.buffer = ByteBuffer.allocate(2048);
|
||||
newAttach.buffer = ByteBuffer.allocate(BUFFER_ALLOCATED_IN_BYTE);
|
||||
newAttach.clientAddr = clientAddr;
|
||||
newAttach.socketProcess = attach.socketProcess;
|
||||
client.read(newAttach.buffer, newAttach, rwHandler);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* An override method to be processed when operation fails
|
||||
* @param e Throwable object
|
||||
* @param attach AttachmentServer object
|
||||
*/
|
||||
@Override
|
||||
public void failed(Throwable e, AttachmentServer attach) {
|
||||
log.error("Failed to accept a connection.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Class to read / write process
|
||||
*/
|
||||
class ReadWriteHandlerServer implements CompletionHandler<Integer, AttachmentServer> {
|
||||
|
||||
private static final Logger log = LogManager.getLogger(ReadWriteHandlerServer.class.getName());
|
||||
|
||||
/**
|
||||
* An override method to be processed after operation has completed
|
||||
* @param result Result
|
||||
* @param attach AttachmentServer object
|
||||
*/
|
||||
@Override
|
||||
public void completed(Integer result, AttachmentServer attach) {
|
||||
|
||||
attach.buffer.flip();
|
||||
int limits = attach.buffer.limit();
|
||||
byte bytes[] = new byte[limits];
|
||||
attach.buffer.get(bytes, 0, limits);
|
||||
Charset cs = Charset.forName("UTF-8");
|
||||
String msg = new String(bytes, cs);
|
||||
log.debug("SocketProcess.run() message : " + msg);
|
||||
attach.socketProcess.run(msg); // run socket process
|
||||
|
||||
attach.buffer.rewind();
|
||||
}
|
||||
|
||||
/**
|
||||
* An override method to be processed when operation fails
|
||||
* @param e Throwable object
|
||||
* @param attach AttachmentServer object
|
||||
*/
|
||||
@Override
|
||||
public void failed(Throwable e, AttachmentServer attach) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
socket_server_address=localhost
|
||||
socket_server_port_number=1777
|
||||
|
||||
|
||||
|
||||
|
||||
Executable
+13
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration status="WARN">
|
||||
<Appenders>
|
||||
<Console name="CONSOLE" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="%4p %d{HH:mm:ss,SSS} %C - %m%n" />
|
||||
</Console>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Root level="ERROR">
|
||||
<AppenderRef ref="CONSOLE"/>
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
||||
@@ -0,0 +1,81 @@
|
||||
/******************************************************************************
|
||||
*
|
||||
* Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore.
|
||||
* All rights reserved.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
package com.fourelementscapital.socket;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
/**
|
||||
* Unit test for App
|
||||
*/
|
||||
public class AppTest extends TestCase {
|
||||
|
||||
/**
|
||||
* Create the test case
|
||||
*
|
||||
* @param testName name of the test case
|
||||
*/
|
||||
public AppTest( String testName )
|
||||
{
|
||||
super( testName );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the suite of tests being tested
|
||||
*/
|
||||
public static Test suite()
|
||||
{
|
||||
return new TestSuite( AppTest.class );
|
||||
}
|
||||
|
||||
/**
|
||||
* Empty Test
|
||||
*/
|
||||
public void testApp()
|
||||
{
|
||||
assertTrue( true );
|
||||
}
|
||||
|
||||
/**
|
||||
* This is to show how to run socket server.
|
||||
* The code is commented out because it will listening forever until we stop it using Ctrl-C.
|
||||
*/
|
||||
public void testSocketServerListen()
|
||||
{
|
||||
try {
|
||||
// To test, provide socketProcess object first :
|
||||
// SocketProcess socketProcess = ...;
|
||||
//SocketServer.listen(socketProcess);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
assertTrue( true );
|
||||
}
|
||||
|
||||
/**
|
||||
* This is to show how to send message to socket server.
|
||||
* The code is commented out because it will throw error if server is not running.
|
||||
*/
|
||||
public void testSocketClientSendMessage()
|
||||
{
|
||||
try {
|
||||
// Remove the comment tag to test. The code are commented out to prevent sending message to socket server (set socket server & edit properties file first) :
|
||||
//String message = "theme$#=itools~theme$#=computing~subject$#=test~body$#=-iMonitor~recipients$#=Manas@4ecap.com~sayIt$#=FALSE~emailIt$#=TRUE~phoneCall$#=FALSE~ym$#=FALSE~sms$#=FALSE<EOF>";
|
||||
//SocketClient.sendMessage(message);
|
||||
}
|
||||
catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
assertTrue( true );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user