Skip to content

Commit

Permalink
demonized
Browse files Browse the repository at this point in the history
  • Loading branch information
YaroslavGaponov committed Nov 3, 2012
1 parent 857f504 commit 02ef063
Show file tree
Hide file tree
Showing 43 changed files with 132 additions and 110 deletions.
2 changes: 1 addition & 1 deletion .classpath
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.launching.macosx.MacOSXType/Java SE 7 (MacOS X Default)"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Binary file modified bin/com/gap/wallet/Copyright.class
Binary file not shown.
Binary file modified bin/com/gap/wallet/Wallet.class
Binary file not shown.
Binary file modified bin/com/gap/wallet/core/WalletSocketServer.class
Binary file not shown.
Binary file removed bin/com/gap/wallet/deamon/Daemon.class
Binary file not shown.
Binary file added bin/com/gap/wallet/deamon/Daemonizer$1.class
Binary file not shown.
Binary file added bin/com/gap/wallet/deamon/Daemonizer.class
Binary file not shown.
Binary file added bin/com/gap/wallet/deamon/Service.class
Binary file not shown.
Binary file modified bin/com/gap/wallet/logger/Log.class
Binary file not shown.
Binary file modified bin/com/gap/wallet/net/SocketServer.class
Binary file not shown.
Binary file modified bin/com/gap/wallet/storage/Session.class
Binary file not shown.
Binary file modified bin/com/gap/wallet/storage/driver/Driver.class
Binary file not shown.
Binary file modified bin/com/gap/wallet/storage/driver/FileHeader.class
Binary file not shown.
Binary file modified bin/com/gap/wallet/storage/driver/FileSpace.class
Binary file not shown.
8 changes: 8 additions & 0 deletions bin/com/gap/wallet/wallet.xml
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>deamon settings</comment>
<entry key="port">12345</entry>
<entry key="path">d:/data</entry>
<entry key="shutdown">12346</entry>
</properties>
Binary file modified nodejs/bin/build.jar
Binary file not shown.
Binary file modified nodejs/bin/rebuild.jar
Binary file not shown.
Binary file modified nodejs/bin/wallet.jar
Binary file not shown.
Binary file modified nodejs/data/test
Binary file not shown.
Binary file modified nodejs/data/test1
Binary file not shown.
Binary file modified nodejs/data/test2
Binary file not shown.
Binary file modified nodejs/data/test3
Binary file not shown.
Binary file modified nodejs/data/test4
Binary file not shown.
Binary file modified nodejs/data/test5
Binary file not shown.
Empty file modified nodejs/demo.cmd 100644 → 100755
Empty file.
Empty file modified nodejs/demo.sh 100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion nodejs/env.cmd 100644 → 100755
@@ -1 +1 @@
SET WALLET=C:\Users\yaroslav.gaponov\My Documents\GitHub\wallet\nodejs
SET WALLET=/Users/yaroslav/projects/wallet/nodejs
3 changes: 3 additions & 0 deletions nodejs/env.sh
@@ -0,0 +1,3 @@
#!/bin/sh

export WALLET=/Users/yaroslav/projects/wallet/nodejs
2 changes: 1 addition & 1 deletion nodejs/etc/wallet.xml
Expand Up @@ -3,6 +3,6 @@
<properties>
<comment>deamon settings</comment>
<entry key="port">12345</entry>
<entry key="path">C:\Users\yaroslav.gaponov\My Documents\GitHub\wallet\nodejs\data\</entry>
<entry key="path">/Users/yaroslav/projects/wallet/nodejs/data</entry>
<entry key="shutdown">12346</entry>
</properties>
2 changes: 1 addition & 1 deletion nodejs/lib/wallet.js
Expand Up @@ -209,7 +209,7 @@ Wallet.prototype.onerror = function(callback) {

var Frame = module.exports.Frame = function(command) {
if (! Protocol[command]) {
throw new Error('This command is not supported.');
throw new Error('This command ' + command + ' is not supported.');
}
if (this instanceof Frame) {
this.command = command;
Expand Down
2 changes: 1 addition & 1 deletion nodejs/package.json
@@ -1,6 +1,6 @@
{
"name": "wallet",
"version": "0.0.1",
"version": "0.0.2",
"description": "Wallet - nosql database server in java and client in nodejs",
"main": "./lib/wallet.js",
"directories": {
Expand Down
1 change: 0 additions & 1 deletion nodejs/start.cmd
@@ -1,2 +1 @@
call env
java -jar ./bin/wallet.jar start
2 changes: 1 addition & 1 deletion nodejs/start.sh 100644 → 100755
@@ -1,3 +1,3 @@
#!/bin/bash

java -jar ./bin/wallet.jar 12345 ./data
java -jar ./bin/wallet.jar start
1 change: 0 additions & 1 deletion nodejs/stop.cmd
@@ -1,2 +1 @@
call env
java -jar ./bin/wallet.jar stop
3 changes: 3 additions & 0 deletions nodejs/stop.sh
@@ -0,0 +1,3 @@
#!/bin/bash

java -jar ./bin/wallet.jar stop
2 changes: 1 addition & 1 deletion readme.md
@@ -1 +1 @@
Wallet is high performance key-value database server in Java and client library in Node.JS========# Demo## Create demo databases`demo.sh` or `demo.cmd`## Run database server`start.sh` or `start.cmd`## Run examples`test.js` or `node test.js``test2.js` or `node test2.js``test3.js` or `node test3.js``test4.js` or `node test4.js`# Depending`java version 1.7`# Author`Yaroslav Gaponov`
Wallet is high performance key-value database server in Java and client library in Node.JS========# Demo## Create demo databases`demo.sh` or `demo.cmd`## Set environment variable`Update and run env.sh or env.cmd script` ## Start database server`start.sh` or `start.cmd`## Run examples`test.js` or `node test.js``test2.js` or `node test2.js``test3.js` or `node test3.js``test4.js` or `node test4.js`## Stop database server`stop.sh` or `stop.cmd`# Depending`java version 1.7`# Author`Yaroslav Gaponov`
Expand Down
74 changes: 42 additions & 32 deletions src/main/java/com/gap/wallet/Wallet.java
Expand Up @@ -2,54 +2,64 @@

import java.io.FileInputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

import com.gap.wallet.deamon.Daemon;
import com.gap.wallet.core.WalletSocketServer;
import com.gap.wallet.deamon.Daemonizer;


public class Wallet {

public static void main(String[] args) throws IOException, URISyntaxException {
public static void main(String[] args) throws InvalidPropertiesFormatException, IOException {
System.out.println(Copyright.info);
System.out.println("wallet - demon");

String solutionPath = System.getenv("WALLET");

if (solutionPath == null) {
halt("Please set environment variable WALLET");
}
System.out.println("wallet - demon");

FileInputStream fileConfig = new FileInputStream(solutionPath + "/etc/wallet.xml");
Properties config = new Properties();
config.loadFromXML(fileConfig);

config.loadFromXML(new FileInputStream(System.getenv("WALLET") + "/etc/wallet.xml"));
if (args.length > 0) {
switch (args[0]) {
case "run":
int port = Integer.parseInt(config.getProperty("port"));
String path = config.getProperty("path");
int shutdown = Integer.parseInt(config.getProperty("shutdown"));
Daemon daemon = new Daemon(port, path, shutdown);
daemon.start();
case "start":
String java = "java -jar";
String jar = solutionPath + "/bin/wallet.jar";
String command = "run";
System.out.println(String.format("%s %s %s", java, jar, command));
Runtime.getRuntime().exec(String.format("%s \"%s\" %s", java, jar, command));
break;
case "stop":
Daemon.stopDaemon(Integer.parseInt(config.getProperty("shutdown")));
break;
case "start":
start();
break;

case "stop":
stop
(
Integer.parseInt(config.getProperty("shutdown"))
);
break;

default:
help();
}
}
else {
halt("help: wallet [start|stop|run]");
run
(
Integer.parseInt(config.getProperty("port")),
config.getProperty("path"),
Integer.parseInt(config.getProperty("shutdown"))
);
}
}

private static void halt(String message) {
System.out.println(message);
private static void start() throws IOException {
Runtime.getRuntime().exec("java -jar " + System.getenv("WALLET") + "/bin/wallet.jar");
}

private static void run(int port,String path,int shutdown) throws IOException {
Daemonizer.start(new WalletSocketServer(port, path), shutdown);
}

private static void stop(int shutdown) {
Daemonizer.stop(shutdown);
}


private static void help() {
System.out.println("help: wallet [start|stop]");
System.exit(-1);
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/gap/wallet/core/WalletSocketServer.java
Expand Up @@ -10,9 +10,10 @@
import com.gap.wallet.net.Frame;
import com.gap.wallet.net.SocketServer;
import com.gap.wallet.storage.WalletClient;
import com.gap.wallet.deamon.Service;
import com.gap.wallet.exception.WalletException;

public class WalletSocketServer extends SocketServer {
public class WalletSocketServer extends SocketServer implements Service {

private final static int DEFAULT_PORT = 12345;
private final Map<String, WalletClient> sessions = new HashMap<String, WalletClient>();
Expand Down
62 changes: 0 additions & 62 deletions src/main/java/com/gap/wallet/deamon/Daemon.java

This file was deleted.

53 changes: 53 additions & 0 deletions src/main/java/com/gap/wallet/deamon/Daemonizer.java
@@ -0,0 +1,53 @@
package com.gap.wallet.deamon;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.OutputStream;

public class Daemonizer {

public static void start(final Service service, int shutdown) {
// start service in separate thread
Thread threadService = new Thread(new Runnable() {
public void run() {
service.start();
}
});
threadService.start();

// start simple socket server for wait shutdown signal
ServerSocket serverSocket;
Socket clientSocket;
byte[] buf = new byte[8];
String cmd;
try {
serverSocket = new ServerSocket(shutdown);
do {
clientSocket = serverSocket.accept();
InputStream in = clientSocket.getInputStream();
in.read(buf);
in.close();
cmd = new String(buf);
} while (!cmd.equals("shutdown"));
service.stop();
clientSocket.close();
} catch (IOException e) {
}
}

public static void stop(int port) {
Socket clientSocket;
try {
clientSocket = new Socket("localhost", port);
OutputStream out = clientSocket.getOutputStream();
out.write(new String("shutdown").getBytes());
out.close();
clientSocket.close();
} catch (IOException e) {
}

}

}
6 changes: 6 additions & 0 deletions src/main/java/com/gap/wallet/deamon/Service.java
@@ -0,0 +1,6 @@
package com.gap.wallet.deamon;

public interface Service {
public void start();
public void stop();
}
12 changes: 7 additions & 5 deletions src/main/java/com/gap/wallet/net/SocketServer.java
Expand Up @@ -5,13 +5,15 @@
import java.nio.channels.*;
import java.util.*;

import com.gap.wallet.deamon.Service;
import com.gap.wallet.logger.Log;

public abstract class SocketServer {
public abstract class SocketServer implements Service {
private final static int DEFAULT_BUFFER_SIZE = 16384; // max frame size!!!
private final Selector selector;
private final ServerSocketChannel ssc;
private final int buffersize;
private static volatile boolean running = true;

public SocketServer(int port) throws IOException {
this(port, DEFAULT_BUFFER_SIZE);
Expand All @@ -28,12 +30,12 @@ public SocketServer(int port, int buffersize) throws IOException {
this.buffersize = buffersize;
}

public void start() throws IOException {
public void start() {
Log.logger.info("socket server is started");

try {
while (true) {
int changes = selector.select();
while (running) {
int changes = selector.select(1000);

if (changes == 0) {
continue;
Expand Down Expand Up @@ -120,12 +122,12 @@ public void start() throws IOException {
}

} catch (IOException e) {
e.printStackTrace();
}
}

public void stop() {
Log.logger.info("socket server is stopping");
running = false;

Set<SelectionKey> keys = selector.selectedKeys();
Iterator<SelectionKey> it = keys.iterator();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/gap/wallet/storage/Session.java
Expand Up @@ -14,7 +14,7 @@ class Session {
private final static Map<String, Integer> clients = new HashMap<String, Integer>();

// blocking or nonblocking mode
private boolean blocking = false;
private boolean blocking = true;

// session variables
private final String storagefile;
Expand Down

0 comments on commit 02ef063

Please sign in to comment.