Skip to content

edolganov/demos

Repository files navigation

Demo projects

Build and test

  • Clone in Desktop
  • Open in IDE (contains Eclipse project files)
  • Every project has test-src folder with JUnit tests

demo

Demo dir contains examples of work with all projects. Run them in IDE.

socket-server

SocketServer can be used for tcp socket communications:

int port = 11002;
int maxThreads = 10;
SocketServer server = new SocketServer(port, maxThreads, new SocketWriterHander());
server.runAsync();

SocketWriterHander processes a connection and releases it for disconnecting:

public static class HttpEchoHandler extends SocketWriterHander {
	
	@Override
	protected void process(Socket openedSocket, BufferedReader socketReader, PrintWriter socketWriter, SocketServer owner) throws Throwable {
		
		String echo = "";
		
		String line = socketReader.readLine();
		while( hasText(line)){
			echo += line + "\n";
			line = socketReader.readLine();
		}
		
		socketWriter.println("SocketSever. Echo example:\n"+echo);
		socketWriter.flush();
	}
}

See full example.

socket-client-pool

SocketsPool creates pooled tcp connections and can be used like socket's client:

String host = "localhost";
int port = 11002;
SocketsPool pool = new SocketsPool(host, port);
pool.setPoolMaximumActiveConnections(10);
pool.setPoolMaximumIdleConnections(5);

SocketConnHandler processes a connection and returns it to the pool for other handlers:

String answer = pool.invoke(new SocketConnHandler<String>() {
	@Override
	public String handle(SocketConn c) throws IOException {
		
		PrintWriter writer = c.getWriter();
		writer.println(msg);
		writer.flush();
		
		BufferedReader reader = c.getReader();
		return reader.readLine();
	}
});

See full example.

socket-json-server-client

JsonSocketServer is extension of SocketServer with JSON protocol for java objects serialization and with AES128 encryption:

int port = 11001;
int maxThreads = 10;
String secureKey = "3rw!!esafd";

JsonSocketServer server = new JsonSocketServer(port, maxThreads);
server.setSecureKey(secureKey);
server.runAsync();

server.putController(Req.class, new ReqController<Req, Resp>() {
	@Override
	public Resp processReq(Req data, SocketAddress remoteAddress) throws Exception {
		
		System.out.println("Client req: " + data);
		
		Resp resp = new Resp("echo: "+data.in);
		return resp;
	}
});

JsonSocketClient uses SocketsPool and has easy 'request-response' api:

String host = "localhost";
int port = 11001;
int maxConnections = 10;
int idleConnections = 5;
String secureKey = "3rw!!esafd";

JsonSocketClient client = new JsonSocketClient(host, port, maxConnections, idleConnections);
client.setSecureKey(secureKey);

//request - response
Future<Object> futureResult = client.invokeAsync(new Req("hello"));
Resp resp = (Resp)futureResult.get();

See full example.

nosql-comments-db

NoSql database for storing a comments in JSON format and fast read them.

Capabilities:

  • Creating 10.000 urls with 500 comments (5.000.000 total comments count)
  • 40MB RAM
  • 3,2GB HDD

See the test.

This database was created and used in freecom.me project.

See example of work.

About

demo projects, prototypes

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages