Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package engine;
- import webserver.OnHandleRequestResult;
- import facebooklikershelper.FacebookLikersHelper;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.concurrent.ConcurrentHashMap;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import webserver.WebServer;
- /**
- *
- * @author Happy
- */
- public class CommunicatingWebServer extends WebServer {
- private static Object syncPosts = new Object();
- private static Object syncLikers = new Object();
- private static Object syncGroups = new Object();
- private static long reparseGroupsInterval = 1 * 24 * 60 * 60 * 1000L;
- private static long deleteLikersInterval = 7 * 24 * 60 * 60 * 1000L;
- private static long lastTimeLikersUpdated = 0L;
- private static long updateLikersInterval = 3 * 60 * 60 * 1000L;
- public static ConcurrentHashMap<String, Long> likers = new ConcurrentHashMap<String, Long>();
- private synchronized void init(Object[] params, Connection connection, Statement st, ResultSet rs) {
- if (params[0].equals(true)) {
- try {
- dbExecuteUpdate(connection, "UPDATE Posts SET InUse = 0;\n"
- + "UPDATE Groups SET InUse = 0;");
- } catch (SQLException ex) {
- Logger.getLogger(CommunicatingWebServer.class.getName()).log(Level.SEVERE, null, ex);
- }
- params[0] = false;
- }
- if (likers.isEmpty()){
- try {
- rs = dbExecuteQuery(connection, "SELECT * FROM Likers ;");
- while (rs.next()){
- likers.put(rs.getString("LikerAddFriendUrlPart"), rs.getLong("AddedTime"));
- }
- } catch (SQLException ex) {
- Logger.getLogger(CommunicatingWebServer.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- private synchronized void updateLikers(Connection connection, Statement st, ResultSet rs) {
- if ((System.currentTimeMillis() - lastTimeLikersUpdated) >= updateLikersInterval) {
- /*String[] likersKeySet = likers.keySet().toArray(new String[0]);
- for (String liker : likersKeySet) {
- if (likers.keySet().contains(liker) &&
- (likers.get(liker) <= (System.currentTimeMillis() - deleteLikersInterval)))
- likers.remove(liker);
- }*/
- try {
- dbExecuteUpdate(connection, "DELETE FROM Likers WHERE AddedTime <= "
- + (System.currentTimeMillis() - deleteLikersInterval) + ";");
- } catch (SQLException ex) {
- Logger.getLogger(CommunicatingWebServer.class.getName()).log(Level.SEVERE, null, ex);
- }
- likers.clear();
- init(new Object[] {false}, connection, st, rs);
- lastTimeLikersUpdated = System.currentTimeMillis();
- }
- }
- private ResultSet dbExecuteQuery(Connection connection, String sql) throws SQLException{
- connection.setAutoCommit(false);
- Statement st = connection.createStatement();
- ResultSet rs = st.executeQuery(sql);
- connection.commit();
- return rs;
- }
- private int dbExecuteUpdate(Connection connection, String sql) throws SQLException{
- connection.setAutoCommit(false);
- Statement st = connection.createStatement();
- int rs = st.executeUpdate(sql);
- connection.commit();
- return rs;
- }
- @Override
- protected OnHandleRequestResult onHandleRequest(String request, Object[] params) {
- request = request.replace("null", "");
- Connection connection = null;
- Statement st = null;
- ResultSet rs = null;
- Matcher m;
- String log = new SimpleDateFormat("[dd.MM.yyyy HH:mm:ss]").format(Calendar.getInstance().getTime())
- + ": " + request;
- try {
- try {
- connection = DriverManager.getConnection("jdbc:mariadb://localhost:3306/posts_likers?user=root&password=password&allowMultiQueries=true&characterEncoding=UTF-8");
- } catch (SQLException ex) {
- Logger.getLogger(FacebookLikersHelper.class.getName()).log(Level.SEVERE, null, ex);
- }
- log += "db connection ok\r\n";
- init(params, connection, st, rs);
- log += "init ok\r\n";
- updateLikers (connection, st, rs);
- log += "update likers ok\r\n";
- if (request.split("[ \t]")[0].equals("POST")) {
- if (request.contains("do=take")) {
- if (request.contains("what=post")) {
- m = Pattern.compile("(?<=group_post: )[^ \\r\\n\\s$]+").matcher(request);
- while (m.find()) {
- synchronized (syncPosts) {
- try {
- rs = dbExecuteQuery(connection, "SELECT @gr_id := MAX(GroupID) FROM Groups;"
- + "INSERT INTO Groups (GroupID, GroupUrl, LastParsedTime, InUse)\n"
- + "VALUES (@gr_id + 1, '" + m.group().split("\\|")[0] + "', '"
- + System.currentTimeMillis() + "', 1)\n"
- + "ON DUPLICATE KEY UPDATE\n"
- + "PostsNum = PostsNum + 1, LastParsedTime = '"
- + System.currentTimeMillis() + "';");
- rs = dbExecuteQuery(connection, "SELECT GroupID FROM Groups WHERE GroupUrl = '"
- + m.group().split("\\|")[0] + "';");
- rs.first();
- int currentGroupID = rs.getInt("GroupID");
- rs = dbExecuteQuery(connection, "SELECT PostUrl FROM Posts WHERE PostUrl = '"
- + m.group().split("\\|")[1] + "';");
- if (rs.isBeforeFirst()) {
- rs.first();
- String post = rs.getString("PostUrl");
- if ((post != null) && (!post.equals("null")) && (!post.equals("NULL"))) {
- dbExecuteUpdate(connection, "UPDATE Groups\n"
- + "SET PostsNum = PostsNum - 1\n"
- + "WHERE GroupID = '" + currentGroupID + "';");
- log += "do=take&what=post - DUPLICATE POST!\r\n";
- return new OnHandleRequestResult(false, "error");
- }
- }
- dbExecuteUpdate(connection, "INSERT INTO Posts (PostUrl, GroupID)\n"
- + "VALUES ('" + m.group().split("\\|")[1] + "', '"
- + currentGroupID + "');");
- } catch (SQLException ex) {
- Logger.getLogger(FacebookLikersHelper.class.getName()).log(Level.SEVERE, null, ex);
- log += "do=take&what=post exception\r\n";
- return new OnHandleRequestResult(false, "error");
- }
- }
- }
- return new OnHandleRequestResult(true, "OK");
- } else if (request.contains("what=liker")) {
- m = Pattern.compile("(?<=liker: )[^ \\r\\n\\s$]+").matcher(request);
- while (m.find()) {
- synchronized (syncLikers) {
- likers.put(m.group().trim(), System.currentTimeMillis());
- try {
- /*rs = st.executeQuery("SELECT PostID FROM Posts WHERE PostUrl = '"
- + m.group().split("\\|")[1] + "';");
- int currentPostID = rs.getInt("PostID");*/
- dbExecuteUpdate(connection, "INSERT INTO Likers (LikerAddFriendUrlPart, AddedTime)\n"
- + "VALUES ('" + m.group() + "', '"
- + System.currentTimeMillis() + "');");
- } catch (SQLException ex) {
- Logger.getLogger(FacebookLikersHelper.class.getName()).log(Level.SEVERE, null, ex);
- log += "do=take&what=liker exception\r\n";
- return new OnHandleRequestResult(false, "error");
- }
- }
- return new OnHandleRequestResult(true, "OK");
- }
- }
- } else if (request.contains("do=release")) {
- if (request.contains("what=post")) {
- m = Pattern.compile("(?<=postid: )[^ \\r\\n\\s$]+").matcher(request);
- while (m.find()) {
- synchronized (syncPosts) {
- try {
- while (dbExecuteUpdate(connection, "UPDATE Posts SET InUse = 0 WHERE PostID = "
- + m.group() + ";") == 0){;};
- } catch (SQLException ex) {
- Logger.getLogger(FacebookLikersHelper.class.getName()).log(Level.SEVERE, null, ex);
- log += "do=release&what=post exception\r\n";
- return new OnHandleRequestResult(false, "error");
- }
- }
- }
- return new OnHandleRequestResult(true, "OK");
- } else if (request.contains("what=group")) {
- m = Pattern.compile("(?<=groupid: )[^ \\r\\n\\s$]+").matcher(request);
- while (m.find()) {
- synchronized (syncGroups) {
- try {
- while (dbExecuteUpdate(connection, "UPDATE Groups SET InUse = 0, "
- + "LastParsedTime = " + System.currentTimeMillis()
- + " WHERE GroupID = "
- + m.group() + ";") == 0){;};
- } catch (SQLException ex) {
- Logger.getLogger(FacebookLikersHelper.class.getName()).log(Level.SEVERE, null, ex);
- log += "do=release&what=group exception\r\n";
- return new OnHandleRequestResult(false, "error");
- }
- }
- }
- return new OnHandleRequestResult(true, "OK");
- }
- }
- else if (request.contains("do=delete")) {
- if (request.contains("what=post")) {
- m = Pattern.compile("(?<=postid: )[^ \\r\\n\\s$]+").matcher(request);
- while (m.find()) {
- synchronized (syncPosts) {
- try {
- dbExecuteUpdate(connection, "DELETE FROM Posts WHERE PostID = "
- + m.group() + ";");
- } catch (SQLException ex) {
- Logger.getLogger(CommunicatingWebServer.class.getName()).log(Level.SEVERE, null, ex);
- log += "do=delete&what=post exception\r\n";
- return new OnHandleRequestResult(false, "error");
- }
- }
- }
- return new OnHandleRequestResult(true, "OK");
- }
- else if (request.contains("what=group")) {
- m = Pattern.compile("(?<=groupid: )[^ \\r\\n\\s$]+").matcher(request);
- while (m.find()) {
- synchronized (syncGroups) {
- try {
- dbExecuteUpdate(connection, "DELETE FROM Groups WHERE GroupID = "
- + m.group() + ";");
- } catch (SQLException ex) {
- Logger.getLogger(CommunicatingWebServer.class.getName()).log(Level.SEVERE, null, ex);
- log += "do=delete&what=group exception\r\n";
- return new OnHandleRequestResult(false, "error");
- }
- }
- }
- return new OnHandleRequestResult(true, "OK");
- }
- }
- } else if (request.split("[ \t]")[0].equals("GET")) {
- if (request.contains("do=give")) {
- String response = "";
- if (request.contains("what=post")) {
- try {
- synchronized (syncPosts) {
- String post = "";
- int postID = 0;
- String group = "";
- int groupID = 0;
- rs = dbExecuteQuery(connection, "SELECT PostID, PostUrl, GroupID FROM Posts WHERE InUse = 0 LIMIT 1;");
- if (!rs.isBeforeFirst()) {
- log += "do=give&what=post no available posts\r\n";
- return new OnHandleRequestResult(false, "error");
- }
- rs.first();
- post = rs.getString("PostUrl");
- postID = rs.getInt("PostID");
- groupID = rs.getInt("GroupID");
- if ((post == null) || post.equals("null") || post.equals("NULL")){
- log += "do=give&what=post no available posts\r\n";
- return new OnHandleRequestResult(false, "error");
- }
- rs = dbExecuteQuery(connection, "SELECT GroupUrl FROM Groups WHERE GroupID = " + groupID + ";");
- rs.first();
- group = rs.getString("GroupUrl");
- response = "post_postid_group: " + post + "|" + postID + "|" + group;
- dbExecuteUpdate(connection, "UPDATE Posts SET InUse = 1 WHERE PostID = " + postID + ";");
- }
- /* st.execute("START TRANSACTION;\n"
- + "SELECT @post_id := PostID, PostUrl FROM Posts WHERE InUse = 0 LIMIT 1;\n"
- + "UPDATE Posts SET InUse = 1 WHERE PostID = @post_id;\n"
- + "COMMIT;");*/
- } catch (SQLException ex) {
- Logger.getLogger(FacebookLikersHelper.class.getName()).log(Level.SEVERE, null, ex);
- log += "do=give&what=post exception\r\n";
- return new OnHandleRequestResult(false, "error");
- }
- return new OnHandleRequestResult(true, response);
- } else if (request.contains("what=group")) {
- try {
- synchronized (syncGroups) {
- rs = dbExecuteQuery(connection, "SELECT GroupID, GroupUrl, MAX(PostsNum) FROM Groups\n"
- + "WHERE GroupID = ANY (SELECT GroupID FROM Groups\n"
- + "WHERE InUse = 0 AND LastParsedTime <= " + (System.currentTimeMillis() - reparseGroupsInterval) + ")\n"
- + "LIMIT 1;");
- if (!rs.isBeforeFirst()) {
- log += "do=give&what=group no available groups\r\n";
- return new OnHandleRequestResult(false, "error");
- }
- rs.first();
- if ((rs.getString("GroupUrl") == null) || rs.getString("GroupUrl").equals("null")
- || rs.getString("GroupUrl").equals("NULL")){
- log += "do=give&what=group no available groups\r\n";
- return new OnHandleRequestResult(false, "error");
- }
- response = "group_groupid: " + rs.getString("GroupUrl") + "|" + rs.getInt("GroupID");
- dbExecuteUpdate(connection, "UPDATE Groups SET InUse = 1 WHERE GroupID = " + rs.getInt("GroupID") + ";");
- }
- } catch (SQLException ex) {
- Logger.getLogger(FacebookLikersHelper.class.getName()).log(Level.SEVERE, null, ex);
- log += "do=give&what=group exception\r\n";
- return new OnHandleRequestResult(false, "error");
- }
- return new OnHandleRequestResult(true, response);
- }
- }
- else if (request.contains("do=check")) {
- String response = "";
- if (request.contains("what=liker")) {
- m = Pattern.compile("(?<=likerurlpart: )[^ \\r\\n\\s$]+").matcher(request);
- while (m.find()) {
- synchronized (syncLikers) {
- boolean addedLiker = false;
- response += "liker_ifadded: " + m.group() + "|";
- for (String liker : likers.keySet()) {
- if (liker.equals(m.group().trim())) {
- response += "true";
- addedLiker = true;
- break;
- }
- }
- if (!addedLiker) {
- response += "false";
- }
- response += "\r\n";
- }
- }
- return new OnHandleRequestResult(true, response);
- }
- }
- }
- return new OnHandleRequestResult(false, "error");
- } finally {
- log += "likers count: " + likers.size();
- if (connection != null)
- try {
- connection.close();
- } catch (SQLException ex) {
- Logger.getLogger(CommunicatingWebServer.class.getName()).log(Level.SEVERE, null, ex);
- }
- addToLog(log);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement