author | koda |
Sat, 01 May 2010 19:40:45 +0000 | |
changeset 3389 | 9b97b9240c22 |
parent 2869 | 93cc73dcc421 |
child 3425 | ead2ed20dfd4 |
permissions | -rw-r--r-- |
2386 | 1 |
{-# LANGUAGE CPP, ScopedTypeVariables #-} |
1804 | 2 |
module OfficialServer.DBInteraction |
3 |
( |
|
2869 | 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 |
|
2296
19f2f76dc346
Patch for compiling with 6.10 (define NEW_EXCEPTIONS to do that)
unc0rr
parents:
2245
diff
changeset
|
11 |
import qualified Control.Exception as 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 |
2869 | 25 |
q <- readChan $ dbQueries serverInfo |
26 |
case q of |
|
27 |
CheckAccount clUid _ clHost -> do |
|
28 |
writeChan (coreChan serverInfo) $ ClientAccountInfo (clUid, |
|
29 |
if clHost `elem` localAddressList then Admin else Guest) |
|
30 |
ClearCache -> return () |
|
31 |
SendStats {} -> 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
|
32 |
|
2869 | 33 |
fakeDbConnection serverInfo |
1857
b835395659e2
Fake database connection with routine which marks all users as guests, when no database host was specified
unc0rr
parents:
1847
diff
changeset
|
34 |
|
b835395659e2
Fake database connection with routine which marks all users as guests, when no database host was specified
unc0rr
parents:
1847
diff
changeset
|
35 |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1979
diff
changeset
|
36 |
#if defined(OFFICIAL_SERVER) |
2184 | 37 |
pipeDbConnectionLoop queries coreChan hIn hOut accountsCache = |
2869 | 38 |
Exception.handle (\(e :: Exception.IOException) -> warningM "Database" (show e) >> return accountsCache) $ |
39 |
do |
|
40 |
q <- readChan queries |
|
41 |
updatedCache <- case q of |
|
42 |
CheckAccount clUid clNick _ -> do |
|
43 |
let cacheEntry = clNick `Map.lookup` accountsCache |
|
44 |
currentTime <- getCurrentTime |
|
45 |
if (isNothing cacheEntry) || (currentTime `diffUTCTime` (fst . fromJust) cacheEntry > 2 * 24 * 60 * 60) then |
|
46 |
do |
|
47 |
hPutStrLn hIn $ show q |
|
48 |
hFlush hIn |
|
2117
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
49 |
|
2869 | 50 |
(clId, accountInfo) <- hGetLine hOut >>= (maybeException . maybeRead) |
2117
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
51 |
|
2869 | 52 |
writeChan coreChan $ ClientAccountInfo (clId, accountInfo) |
2117
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
53 |
|
2869 | 54 |
return $ Map.insert clNick (currentTime, accountInfo) accountsCache |
55 |
`Exception.onException` |
|
56 |
(unGetChan queries q) |
|
57 |
else |
|
58 |
do |
|
59 |
writeChan coreChan $ ClientAccountInfo (clUid, snd $ fromJust cacheEntry) |
|
60 |
return accountsCache |
|
2155
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2129
diff
changeset
|
61 |
|
2869 | 62 |
ClearCache -> return Map.empty |
63 |
SendStats {} -> ( |
|
64 |
(hPutStrLn hIn $ show q) >> |
|
65 |
hFlush hIn >> |
|
66 |
return accountsCache) |
|
67 |
`Exception.onException` |
|
68 |
(unGetChan queries q) |
|
2184 | 69 |
|
2869 | 70 |
pipeDbConnectionLoop queries coreChan hIn hOut updatedCache |
71 |
where |
|
72 |
maybeException (Just a) = return a |
|
73 |
maybeException Nothing = ioError (userError "Can't read") |
|
1804 | 74 |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1979
diff
changeset
|
75 |
|
2117
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
76 |
pipeDbConnection accountsCache serverInfo = do |
2869 | 77 |
updatedCache <- |
78 |
Exception.handle (\(e :: Exception.IOException) -> warningM "Database" (show e) >> return accountsCache) $ do |
|
79 |
(Just hIn, Just hOut, _, _) <- createProcess (proc "./OfficialServer/extdbinterface" []) |
|
80 |
{std_in = CreatePipe, |
|
81 |
std_out = CreatePipe} |
|
82 |
hSetBuffering hIn LineBuffering |
|
83 |
hSetBuffering hOut LineBuffering |
|
1804 | 84 |
|
2869 | 85 |
hPutStrLn hIn $ dbHost serverInfo |
86 |
hPutStrLn hIn $ dbLogin serverInfo |
|
87 |
hPutStrLn hIn $ dbPassword serverInfo |
|
88 |
pipeDbConnectionLoop (dbQueries serverInfo) (coreChan serverInfo) hIn hOut accountsCache |
|
2184 | 89 |
|
2869 | 90 |
threadDelay (3 * 10^6) |
91 |
pipeDbConnection updatedCache serverInfo |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1979
diff
changeset
|
92 |
|
2126 | 93 |
dbConnectionLoop serverInfo = |
2869 | 94 |
if (not . null $ dbHost serverInfo) then |
95 |
pipeDbConnection Map.empty serverInfo |
|
96 |
else |
|
97 |
fakeDbConnection serverInfo |
|
1979 | 98 |
#else |
99 |
dbConnectionLoop = fakeDbConnection |
|
100 |
#endif |
|
1804 | 101 |
|
1833 | 102 |
startDBConnection serverInfo = |
2869 | 103 |
forkIO $ dbConnectionLoop serverInfo |