Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## errors
- vnaweb:/var/www/server# javac -cp mysql-connector-java.jar:./: -d ./bin/ src/com/sgtserver/Application.java
- src/com/sgtserver/Application.java:6: cannot find symbol
- symbol : class Server
- location: class com.sgtserver.Application
- private Server server;
- ^
- src/com/sgtserver/Application.java:13: cannot find symbol
- symbol : class Server
- location: class com.sgtserver.Application
- server = new Server(Config.get().getInt("port"));
- ^
- src/com/sgtserver/Application.java:13: cannot find symbol
- symbol : variable Config
- location: class com.sgtserver.Application
- server = new Server(Config.get().getInt("port"));
- ^
- 3 errors
- vnaweb:/var/www/server#
- ## Application.java
- package com.sgtserver;
- public class Application {
- private Server server;
- public static void main(String[] args) {
- new Application();
- }
- public Application() {
- server = new Server(Config.get().getInt("port"));
- server.start();
- }
- }
- ## Config.java
- package com.sgtserver;
- import java.io.BufferedReader;
- import java.io.DataInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.InputStreamReader;
- import java.util.HashMap;
- public class Config {
- private static Config instance;
- private HashMap<String, String> options;
- private Config() {
- options = new HashMap<String, String>();
- parse();
- }
- private void parse() {
- File configFile = new File("config");
- try {
- FileInputStream fstream = new FileInputStream(configFile);
- DataInputStream in = new DataInputStream(fstream);
- BufferedReader br = new BufferedReader(new InputStreamReader(in));
- String strLine;
- while ((strLine = br.readLine()) != null) {
- String[] parts = strLine.split("\\:");
- String key = parts[0];
- String value = parts[1];
- options.put(key, value);
- }
- in.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static Config get() {
- if (instance == null) {
- instance = new Config();
- }
- return instance;
- }
- public int getInt(String key) {
- return Integer.parseInt(options.get(key));
- }
- public boolean getBoolean(String key) {
- return Boolean.parseBoolean(options.get(key));
- }
- public String getString(String key) {
- return options.get(key);
- }
- }
- ## Connection.java
- package com.sgtserver;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.net.SocketException;
- import java.util.Timer;
- import java.util.TimerTask;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class Connection implements Runnable {
- private Socket socket = null;
- private Server server;
- private PrintWriter out;
- private BufferedReader in;
- private int timeout;
- boolean canTimeout;
- public Connection(Server server, Socket socket, int timeout) {
- canTimeout = true;
- this.socket = socket;
- this.server = server;
- this.timeout = timeout;
- try {
- out = new PrintWriter(socket.getOutputStream(), true);
- in = new BufferedReader(new InputStreamReader(socket
- .getInputStream()));
- } catch (IOException e) {
- e.printStackTrace();
- }
- Timer timer = new Timer();
- timer.schedule(new Timeout(), timeout);
- }
- private class Timeout extends TimerTask {
- @Override
- public void run() {
- if (canTimeout) {
- System.out
- .println("Connection did not send a query within specified timeout. Closing connection.");
- close();
- }
- }
- }
- public void run() {
- try {
- String inputLine, outputLine;
- inputLine = in.readLine();
- canTimeout = false;
- parseInput(inputLine);
- close();
- } catch (SocketException se) {
- //
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private void parseInput(String input) {
- Pattern p = Pattern.compile("(.*?):([0-9]+)\\" + Config.get().getString("delimiter"));
- Matcher m = p.matcher(input);
- if(!m.find())
- {
- System.out.println("Invalid query sent. Closing connection.");
- close();
- } else {
- m.reset();
- while (m.find()) {
- String query = m.group(1);
- String millis = m.group(2);
- server.addQuery(query, Long.parseLong(millis));
- }
- }
- }
- public void close() {
- try {
- out.close();
- in.close();
- socket.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- ## Query.java
- package com.sgtserver;
- import java.sql.DriverManager;
- import java.sql.Statement;
- import java.util.TimerTask;
- public class Query extends TimerTask {
- private String query;
- public Query(String query) {
- this.query = query;
- }
- @Override
- public void run() {
- java.sql.Connection conn = null;
- try {
- String userName = Config.get().getString("mysql-user");
- String password = Config.get().getString("mysql-pass");
- String url = "jdbc:mysql://" + Config.get().getString("mysql-host")
- + "/" + Config.get().getString("mysql-db") + "?useUnicode=yes&characterEncoding=UTF-8";
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- conn = DriverManager.getConnection(url, userName, password);
- Statement s = conn.createStatement();
- s.executeUpdate(query);
- s.close();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (conn != null) {
- try {
- conn.close();
- } catch (Exception e) {
- }
- }
- }
- }
- }
- ## Server.java
- package com.sgtserver;
- import java.io.IOException;
- import java.net.ServerSocket;
- import java.util.ArrayList;
- import java.util.Timer;
- public class Server {
- private int port;
- private ServerSocket socket = null;
- private ArrayList<Query> queries;
- public Server(int port) {
- this.port = port;
- queries = new ArrayList<Query>();
- }
- public void addQuery(String query, long time) {
- Timer t = new Timer();
- String its = Config.get().getString("timestamp-input-type");
- if(its.equals("s")) {
- time = time * 1000;
- }
- long actualTime = time - System.currentTimeMillis();
- if (actualTime <= 0) {
- System.out.println("Invalid query time [" + actualTime +"]. Ignoring.");
- } else {
- Query q = new Query(query);
- t.schedule(q, actualTime);
- System.out.println("Performing new query ["+query+"] in " + actualTime + " milliseconds.");
- queries.add(q);
- }
- }
- public void start() {
- boolean listening = true;
- try {
- socket = new ServerSocket(port);
- } catch (IOException e) {
- e.printStackTrace();
- }
- while (listening) {
- try {
- new Thread(new Connection(this, socket.accept(), Config.get()
- .getInt("timeout"))).start();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- die();
- }
- public void die() {
- try {
- socket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment