author | unc0rr |
Sun, 21 Jun 2009 15:36:13 +0000 | |
changeset 2178 | d8b3417b2704 |
parent 2172 | 80d34c0b9dfe |
child 2184 | f59f80e034b1 |
permissions | -rw-r--r-- |
1979 | 1 |
{-# LANGUAGE CPP #-} |
1804 | 2 |
module OfficialServer.DBInteraction |
3 |
( |
|
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
4 |
startDBConnection |
1804 | 5 |
) where |
6 |
||
1979 | 7 |
import Prelude hiding (catch); |
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1979
diff
changeset
|
8 |
import System.Process |
1804 | 9 |
import System.IO |
10 |
import Control.Concurrent |
|
11 |
import Control.Exception |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1979
diff
changeset
|
12 |
import Control.Monad |
2117
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
13 |
import qualified Data.Map as Map |
1833 | 14 |
import Monad |
1834 | 15 |
import Maybe |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
16 |
import System.Log.Logger |
2126 | 17 |
import Data.Time |
1833 | 18 |
------------------------ |
19 |
import CoreTypes |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1979
diff
changeset
|
20 |
import Utils |
1804 | 21 |
|
1921 | 22 |
localAddressList = ["127.0.0.1", "0:0:0:0:0:0:0:1", "0:0:0:0:0:ffff:7f00:1"] |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
23 |
|
1857
b835395659e2
Fake database connection with routine which marks all users as guests, when no database host was specified
unc0rr
parents:
1847
diff
changeset
|
24 |
fakeDbConnection serverInfo = do |
b835395659e2
Fake database connection with routine which marks all users as guests, when no database host was specified
unc0rr
parents:
1847
diff
changeset
|
25 |
q <- readChan $ dbQueries serverInfo |
b835395659e2
Fake database connection with routine which marks all users as guests, when no database host was specified
unc0rr
parents:
1847
diff
changeset
|
26 |
case q of |
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1979
diff
changeset
|
27 |
CheckAccount clUid _ clHost -> do |
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1979
diff
changeset
|
28 |
writeChan (coreChan serverInfo) $ ClientAccountInfo (clUid, |
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1979
diff
changeset
|
29 |
if clHost `elem` localAddressList then Admin else Guest) |
2155
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2129
diff
changeset
|
30 |
ClearCache -> return () |
1857
b835395659e2
Fake database connection with routine which marks all users as guests, when no database host was specified
unc0rr
parents:
1847
diff
changeset
|
31 |
|
b835395659e2
Fake database connection with routine which marks all users as guests, when no database host was specified
unc0rr
parents:
1847
diff
changeset
|
32 |
fakeDbConnection serverInfo |
b835395659e2
Fake database connection with routine which marks all users as guests, when no database host was specified
unc0rr
parents:
1847
diff
changeset
|
33 |
|
b835395659e2
Fake database connection with routine which marks all users as guests, when no database host was specified
unc0rr
parents:
1847
diff
changeset
|
34 |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1979
diff
changeset
|
35 |
#if defined(OFFICIAL_SERVER) |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
36 |
------------------------------------------------------------------- |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
37 |
-- borrowed from base 4.0.0 --------------------------------------- |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
38 |
onException :: IO a -> IO b -> IO a -- |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
39 |
onException io what = io `catch` \e -> do what -- |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
40 |
throw (e :: Exception) -- |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
41 |
-- to be deleted -------------------------------------------------- |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
42 |
------------------------------------------------------------------- |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
43 |
|
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
44 |
|
2117
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
45 |
pipeDbConnectionLoop queries coreChan hIn hOut accountsCache = do |
1833 | 46 |
q <- readChan queries |
2117
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
47 |
updatedCache <- case q of |
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
48 |
CheckAccount clUid clNick _ -> do |
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
49 |
let cacheEntry = clNick `Map.lookup` accountsCache |
2126 | 50 |
currentTime <- getCurrentTime |
51 |
if (isNothing cacheEntry) || (currentTime `diffUTCTime` (fst . fromJust) cacheEntry > 2 * 24 * 60 * 60) then |
|
2117
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
52 |
do |
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
53 |
hPutStrLn hIn $ show q |
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
54 |
hFlush hIn |
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
55 |
|
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
56 |
(clId, accountInfo) <- hGetLine hOut >>= (maybeException . maybeRead) |
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
57 |
|
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
58 |
writeChan coreChan $ ClientAccountInfo (clId, accountInfo) |
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
59 |
|
2126 | 60 |
return $ Map.insert clNick (currentTime, accountInfo) accountsCache |
2117
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
61 |
`onException` |
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
62 |
(unGetChan queries q) |
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
63 |
else |
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
64 |
do |
2126 | 65 |
writeChan coreChan $ ClientAccountInfo (clUid, snd $ fromJust cacheEntry) |
2117
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
66 |
return accountsCache |
2155
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2129
diff
changeset
|
67 |
|
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2129
diff
changeset
|
68 |
ClearCache -> return Map.empty |
2172 | 69 |
SendStats {} -> do |
70 |
hPutStrLn hIn $ show q |
|
71 |
hFlush hIn |
|
72 |
return accountsCache |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1979
diff
changeset
|
73 |
|
2117
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
74 |
return updatedCache |
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1979
diff
changeset
|
75 |
where |
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1979
diff
changeset
|
76 |
maybeException (Just a) = return a |
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1979
diff
changeset
|
77 |
maybeException Nothing = ioError (userError "Can't read") |
1804 | 78 |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1979
diff
changeset
|
79 |
|
2117
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
80 |
pipeDbConnection accountsCache serverInfo = do |
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
81 |
updatedCache <- |
2126 | 82 |
Control.Exception.handle (\e -> warningM "Database" (show e) >> return accountsCache) $ |
83 |
bracket |
|
84 |
(createProcess (proc "./OfficialServer/extdbinterface" []) {std_in = CreatePipe, std_out = CreatePipe}) |
|
2129
8664554d5547
Another approach to zombies problem (set SIGCHLD handler to SIG_IGN)
unc0rr
parents:
2126
diff
changeset
|
85 |
(\(_, _, _, processHandle) -> return accountsCache) |
2126 | 86 |
(\(Just hIn, Just hOut, _, _) -> do |
87 |
hSetBuffering hIn LineBuffering |
|
88 |
hSetBuffering hOut LineBuffering |
|
89 |
||
90 |
hPutStrLn hIn $ dbHost serverInfo |
|
91 |
hPutStrLn hIn $ dbLogin serverInfo |
|
92 |
hPutStrLn hIn $ dbPassword serverInfo |
|
93 |
pipeDbConnectionLoop (dbQueries serverInfo) (coreChan serverInfo) hIn hOut accountsCache |
|
94 |
) |
|
1804 | 95 |
|
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
96 |
threadDelay (5 * 10^6) |
2117
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
97 |
pipeDbConnection updatedCache serverInfo |
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1979
diff
changeset
|
98 |
|
2126 | 99 |
dbConnectionLoop serverInfo = |
2123 | 100 |
if (not . null $ dbHost serverInfo) then |
2126 | 101 |
pipeDbConnection Map.empty serverInfo |
2123 | 102 |
else |
2126 | 103 |
fakeDbConnection serverInfo |
1979 | 104 |
#else |
105 |
dbConnectionLoop = fakeDbConnection |
|
106 |
#endif |
|
1804 | 107 |
|
1833 | 108 |
startDBConnection serverInfo = |
2123 | 109 |
forkIO $ dbConnectionLoop serverInfo |