Guest User

Untitled

a guest
Jul 23rd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.90 KB | None | 0 0
  1. ## errors
  2.  
  3. vnaweb:/var/www/server# javac -cp mysql-connector-java.jar:./: -d ./bin/ src/com/sgtserver/Application.java
  4. src/com/sgtserver/Application.java:6: cannot find symbol
  5. symbol : class Server
  6. location: class com.sgtserver.Application
  7. private Server server;
  8. ^
  9. src/com/sgtserver/Application.java:13: cannot find symbol
  10. symbol : class Server
  11. location: class com.sgtserver.Application
  12. server = new Server(Config.get().getInt("port"));
  13. ^
  14. src/com/sgtserver/Application.java:13: cannot find symbol
  15. symbol : variable Config
  16. location: class com.sgtserver.Application
  17. server = new Server(Config.get().getInt("port"));
  18. ^
  19. 3 errors
  20. vnaweb:/var/www/server#
  21.  
  22. ## Application.java
  23.  
  24. package com.sgtserver;
  25.  
  26. public class Application {
  27.  
  28.  
  29. private Server server;
  30.  
  31. public static void main(String[] args) {
  32. new Application();
  33. }
  34.  
  35. public Application() {
  36. server = new Server(Config.get().getInt("port"));
  37. server.start();
  38. }
  39. }
  40.  
  41. ## Config.java
  42.  
  43. package com.sgtserver;
  44.  
  45. import java.io.BufferedReader;
  46. import java.io.DataInputStream;
  47. import java.io.File;
  48. import java.io.FileInputStream;
  49. import java.io.InputStreamReader;
  50. import java.util.HashMap;
  51.  
  52. public class Config {
  53.  
  54. private static Config instance;
  55. private HashMap<String, String> options;
  56.  
  57. private Config() {
  58. options = new HashMap<String, String>();
  59. parse();
  60. }
  61.  
  62. private void parse() {
  63. File configFile = new File("config");
  64. try {
  65. FileInputStream fstream = new FileInputStream(configFile);
  66. DataInputStream in = new DataInputStream(fstream);
  67. BufferedReader br = new BufferedReader(new InputStreamReader(in));
  68. String strLine;
  69. while ((strLine = br.readLine()) != null) {
  70. String[] parts = strLine.split("\\:");
  71. String key = parts[0];
  72. String value = parts[1];
  73. options.put(key, value);
  74. }
  75. in.close();
  76. } catch (Exception e) {
  77. e.printStackTrace();
  78. }
  79.  
  80. }
  81.  
  82. public static Config get() {
  83. if (instance == null) {
  84. instance = new Config();
  85. }
  86. return instance;
  87. }
  88.  
  89. public int getInt(String key) {
  90. return Integer.parseInt(options.get(key));
  91. }
  92.  
  93. public boolean getBoolean(String key) {
  94. return Boolean.parseBoolean(options.get(key));
  95. }
  96.  
  97. public String getString(String key) {
  98. return options.get(key);
  99. }
  100.  
  101. }
  102.  
  103. ## Connection.java
  104.  
  105. package com.sgtserver;
  106.  
  107. import java.io.BufferedReader;
  108. import java.io.IOException;
  109. import java.io.InputStreamReader;
  110. import java.io.PrintWriter;
  111. import java.net.Socket;
  112. import java.net.SocketException;
  113. import java.util.Timer;
  114. import java.util.TimerTask;
  115. import java.util.regex.Matcher;
  116. import java.util.regex.Pattern;
  117.  
  118. public class Connection implements Runnable {
  119. private Socket socket = null;
  120. private Server server;
  121. private PrintWriter out;
  122. private BufferedReader in;
  123. private int timeout;
  124. boolean canTimeout;
  125.  
  126. public Connection(Server server, Socket socket, int timeout) {
  127. canTimeout = true;
  128. this.socket = socket;
  129. this.server = server;
  130. this.timeout = timeout;
  131. try {
  132. out = new PrintWriter(socket.getOutputStream(), true);
  133.  
  134. in = new BufferedReader(new InputStreamReader(socket
  135. .getInputStream()));
  136. } catch (IOException e) {
  137. e.printStackTrace();
  138. }
  139. Timer timer = new Timer();
  140. timer.schedule(new Timeout(), timeout);
  141. }
  142.  
  143. private class Timeout extends TimerTask {
  144.  
  145. @Override
  146. public void run() {
  147. if (canTimeout) {
  148. System.out
  149. .println("Connection did not send a query within specified timeout. Closing connection.");
  150. close();
  151. }
  152. }
  153.  
  154. }
  155.  
  156. public void run() {
  157.  
  158. try {
  159.  
  160. String inputLine, outputLine;
  161. inputLine = in.readLine();
  162. canTimeout = false;
  163. parseInput(inputLine);
  164. close();
  165.  
  166. } catch (SocketException se) {
  167. //
  168. } catch (IOException e) {
  169. e.printStackTrace();
  170. }
  171. }
  172.  
  173. private void parseInput(String input) {
  174. Pattern p = Pattern.compile("(.*?):([0-9]+)\\" + Config.get().getString("delimiter"));
  175. Matcher m = p.matcher(input);
  176. if(!m.find())
  177. {
  178. System.out.println("Invalid query sent. Closing connection.");
  179. close();
  180. } else {
  181. m.reset();
  182. while (m.find()) {
  183.  
  184. String query = m.group(1);
  185. String millis = m.group(2);
  186. server.addQuery(query, Long.parseLong(millis));
  187. }
  188. }
  189. }
  190.  
  191. public void close() {
  192. try {
  193. out.close();
  194. in.close();
  195. socket.close();
  196. } catch (IOException e) {
  197. // TODO Auto-generated catch block
  198. e.printStackTrace();
  199. }
  200.  
  201. }
  202.  
  203. }
  204.  
  205. ## Query.java
  206.  
  207. package com.sgtserver;
  208.  
  209. import java.sql.DriverManager;
  210. import java.sql.Statement;
  211. import java.util.TimerTask;
  212.  
  213. public class Query extends TimerTask {
  214.  
  215. private String query;
  216.  
  217. public Query(String query) {
  218. this.query = query;
  219. }
  220.  
  221. @Override
  222. public void run() {
  223. java.sql.Connection conn = null;
  224.  
  225. try {
  226. String userName = Config.get().getString("mysql-user");
  227. String password = Config.get().getString("mysql-pass");
  228. String url = "jdbc:mysql://" + Config.get().getString("mysql-host")
  229. + "/" + Config.get().getString("mysql-db") + "?useUnicode=yes&characterEncoding=UTF-8";
  230. Class.forName("com.mysql.jdbc.Driver").newInstance();
  231. conn = DriverManager.getConnection(url, userName, password);
  232. Statement s = conn.createStatement();
  233. s.executeUpdate(query);
  234. s.close();
  235. } catch (Exception e) {
  236. e.printStackTrace();
  237. } finally {
  238. if (conn != null) {
  239. try {
  240. conn.close();
  241. } catch (Exception e) {
  242. }
  243. }
  244. }
  245.  
  246. }
  247.  
  248. }
  249.  
  250. ## Server.java
  251.  
  252. package com.sgtserver;
  253.  
  254. import java.io.IOException;
  255. import java.net.ServerSocket;
  256. import java.util.ArrayList;
  257. import java.util.Timer;
  258.  
  259. public class Server {
  260.  
  261. private int port;
  262. private ServerSocket socket = null;
  263. private ArrayList<Query> queries;
  264.  
  265. public Server(int port) {
  266. this.port = port;
  267. queries = new ArrayList<Query>();
  268. }
  269.  
  270. public void addQuery(String query, long time) {
  271. Timer t = new Timer();
  272. String its = Config.get().getString("timestamp-input-type");
  273. if(its.equals("s")) {
  274. time = time * 1000;
  275. }
  276. long actualTime = time - System.currentTimeMillis();
  277. if (actualTime <= 0) {
  278. System.out.println("Invalid query time [" + actualTime +"]. Ignoring.");
  279. } else {
  280. Query q = new Query(query);
  281. t.schedule(q, actualTime);
  282. System.out.println("Performing new query ["+query+"] in " + actualTime + " milliseconds.");
  283. queries.add(q);
  284. }
  285. }
  286.  
  287. public void start() {
  288.  
  289. boolean listening = true;
  290.  
  291. try {
  292. socket = new ServerSocket(port);
  293. } catch (IOException e) {
  294. e.printStackTrace();
  295. }
  296.  
  297. while (listening) {
  298. try {
  299. new Thread(new Connection(this, socket.accept(), Config.get()
  300. .getInt("timeout"))).start();
  301. } catch (IOException e) {
  302. e.printStackTrace();
  303. }
  304. }
  305.  
  306. die();
  307. }
  308.  
  309. public void die() {
  310. try {
  311. socket.close();
  312. } catch (IOException e) {
  313. e.printStackTrace();
  314. }
  315. }
  316.  
  317. }
Add Comment
Please, Sign In to add comment