1 package com.Yasna.forum.Tasks; 2 3 import com.Yasna.forum.ForumFactory; 4 import com.Yasna.forum.ForumMessage; 5 import com.Yasna.forum.SessionVO; 6 import com.Yasna.forum.database.DbConnectionManager; 7 8 import java.util.LinkedList ; 9 import java.util.Calendar ; 10 import java.sql.Connection ; 11 import java.sql.PreparedStatement ; 12 import java.sql.SQLException ; 13 import java.sql.ResultSet ; 14 15 67 68 public class SessionManager { 69 private LinkedList newSessions; 70 private Thread worker; 72 private static int counter=0; 73 public SessionManager(){ 74 if(counter==0){ 76 System.out.println("Starting Session Manager (you should only see this message once)"); 77 counter++; 78 newSessions = new LinkedList (); 79 worker = new Thread (new SessionWatchWorker(this)); 81 worker.setDaemon(true); 82 worker.start(); 83 } 84 } 85 public synchronized void addMessage(String sID,String IP,int uID){ 86 newSessions.addLast(new SessionVO(sID,IP,uID)); 87 notify(); 89 } 90 public synchronized SessionVO getNextMessage(){ 91 if(newSessions.isEmpty()){ 92 try{ 93 wait(); 95 }catch (InterruptedException ie) {} 97 } 98 return (SessionVO)newSessions.removeFirst(); 99 } 100 101 public class SessionWatchWorker implements Runnable { 102 private static final String GET_SESSION= 103 "select sessionID from yazdSessions where sessionID=?"; 104 private static final String UPDATE_SESSION="update yazdSessions set userID=?,IP=?,lasttime=? where sessionID=?"; 105 private static final String INSERT_SESSION="insert into yazdSessions (sessionID,userID,IP,lasttime,initime) values (?,?,?,?,?)"; 106 private static final String DELETE_SESION="delete from yazdSessions where lasttime < ?"; 107 private static final String GET_CURRENT_COUNT="select count(*) as cnt from yazdSessions"; 108 private static final String GET_STATS="select maxusercount from yazdUserStats where day_dt=?"; 109 private static final String INSERT_STATS="insert into yazdUserStats(day_dt,usercount,maxusercount,maxuserdt) values(?,?,?,?)"; 110 private static final String UPDATE_COUNT="update yazdUserStats set usercount=usercount+1 where day_dt=?"; 111 private static final String UPDATE_COUNT_MAX="update yazdUserStats set maxusercount=?,maxuserdt=? where day_dt=?"; 112 113 private SessionManager manager; 114 public SessionWatchWorker(SessionManager m){ 115 this.manager=m; 116 } 117 public void run(){ 118 while(true){ 119 updatesssions(manager.getNextMessage()); 120 } 121 } 122 private void updatesssions(SessionVO session){ 123 Calendar now = Calendar.getInstance(); 124 int now_in_minutes = (int)(now.getTimeInMillis()*1.0/(1000.0 * 60.0)); 125 int now_in_seconds = (int)(now.getTimeInMillis()*1.0/1000.0); 126 int now_today = (int)(now_in_minutes * 1.0/(60.0 * 24.0)); 127 Connection con = null; 128 PreparedStatement pstmt = null; 129 boolean newsession = false; 130 try { 131 con = DbConnectionManager.getConnection(); 132 pstmt = con.prepareStatement(GET_SESSION); 133 pstmt.setString(1,session.getSessionID()); 134 ResultSet rs = pstmt.executeQuery(); 135 if(rs.next()){ 136 pstmt = con.prepareStatement(UPDATE_SESSION); 137 pstmt.setInt(1,session.getUserID()); 138 pstmt.setString(2,session.getIP()); 139 pstmt.setInt(3,now_in_minutes); 140 pstmt.setString(4,session.getSessionID()); 141 pstmt.executeUpdate(); 142 }else{ 143 newsession=true; 144 pstmt = con.prepareStatement(INSERT_SESSION); 145 pstmt.setString(1,session.getSessionID()); 146 pstmt.setInt(2,session.getUserID()); 147 pstmt.setString(3,session.getIP()); 148 pstmt.setInt(4,now_in_minutes); 149 pstmt.setInt(5,now_in_seconds); 150 pstmt.executeUpdate(); 151 } 152 pstmt = con.prepareStatement(DELETE_SESION); 154 pstmt.setInt(1,now_in_minutes - 6); 155 pstmt.executeUpdate(); 156 pstmt = con.prepareStatement(GET_CURRENT_COUNT); 158 rs = pstmt.executeQuery(); 159 int sessioncount=0; 160 if(rs.next()){ 161 sessioncount=rs.getInt("cnt"); 162 } 163 int maxcount=0; 165 pstmt = con.prepareStatement(GET_STATS); 166 pstmt.setInt(1,now_today); 167 rs = pstmt.executeQuery(); 168 if(rs.next()){ 169 maxcount = rs.getInt("maxusercount"); 170 if(maxcount < sessioncount){ 171 pstmt = con.prepareStatement(UPDATE_COUNT_MAX); 173 pstmt.setInt(1,sessioncount); 174 pstmt.setString(2,Long.toString(now.getTimeInMillis())); 175 pstmt.setInt(3,now_today); 176 pstmt.executeUpdate(); 177 } 178 if(newsession){ 179 pstmt = con.prepareStatement(UPDATE_COUNT); 181 pstmt.setInt(1,now_today); 182 pstmt.executeUpdate(); 183 } 184 }else{ 185 pstmt = con.prepareStatement(INSERT_STATS); 187 pstmt.setInt(1,now_today); 188 pstmt.setInt(2,sessioncount); 189 pstmt.setInt(3,sessioncount); 190 pstmt.setString(4,Long.toString(now.getTimeInMillis())); 191 pstmt.executeUpdate(); 192 } 193 194 } 195 catch( SQLException sqle ) { 196 System.err.println("SessionManager (394) Exception:"+sqle.getMessage()); 197 sqle.printStackTrace(); 198 } 199 catch (Exception e) { 200 System.err.println("SessionManager (3847) Exception:"+e.getMessage()); 201 } 202 finally { 203 try { pstmt.close(); } 204 catch (Exception e) { e.printStackTrace(); } 205 try { con.close(); } 206 catch (Exception e) { e.printStackTrace(); } 207 } 208 } 209 } 210 211 } 212 | Popular Tags |