author | unc0rr |
Sun, 06 Feb 2011 21:50:29 +0300 | |
changeset 4932 | f11d80bac7ed |
parent 4921 | 2efad3acbb74 |
child 4943 | 21d6b2b79cfe |
permissions | -rw-r--r-- |
4906 | 1 |
{-# LANGUAGE CPP, ScopedTypeVariables, OverloadedStrings #-} |
1804 | 2 |
module OfficialServer.DBInteraction |
3 |
( |
|
2869 | 4 |
startDBConnection |
1804 | 5 |
) where |
6 |
||
1979 | 7 |
import Prelude hiding (catch); |
4932 | 8 |
import Control.Concurrent |
9 |
import Control.Monad |
|
10 |
import Data.List as L |
|
11 |
import Data.ByteString.Char8 as B |
|
12 |
#if defined(OFFICIAL_SERVER) |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1979
diff
changeset
|
13 |
import System.Process |
4921 | 14 |
import System.IO as SIO |
2296
19f2f76dc346
Patch for compiling with 6.10 (define NEW_EXCEPTIONS to do that)
unc0rr
parents:
2245
diff
changeset
|
15 |
import qualified Control.Exception as Exception |
2117
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
16 |
import qualified Data.Map as Map |
4295
1f5604cd99be
This revision should, in theory, correctly merge 0.9.14 and tip, so that future merges of 0.9.14 should work properly
nemo
parents:
4242
diff
changeset
|
17 |
import Data.Maybe |
4932 | 18 |
import Data.Time |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
19 |
import System.Log.Logger |
4932 | 20 |
#endif |
1833 | 21 |
------------------------ |
22 |
import CoreTypes |
|
4932 | 23 |
#if defined(OFFICIAL_SERVER) |
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1979
diff
changeset
|
24 |
import Utils |
4932 | 25 |
#endif |
1804 | 26 |
|
4932 | 27 |
localAddressList :: [B.ByteString] |
1921 | 28 |
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
|
29 |
|
4932 | 30 |
fakeDbConnection :: forall b. ServerInfo -> IO b |
4906 | 31 |
fakeDbConnection serverInfo = forever $ do |
2869 | 32 |
q <- readChan $ dbQueries serverInfo |
33 |
case q of |
|
4932 | 34 |
CheckAccount clId clUid _ clHost -> |
4921 | 35 |
writeChan (coreChan serverInfo) $ ClientAccountInfo clId clUid (if clHost `L.elem` localAddressList then Admin else Guest) |
2869 | 36 |
ClearCache -> return () |
37 |
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
|
38 |
|
4932 | 39 |
dbConnectionLoop :: forall b. ServerInfo -> IO b |
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1979
diff
changeset
|
40 |
#if defined(OFFICIAL_SERVER) |
2184 | 41 |
pipeDbConnectionLoop queries coreChan hIn hOut accountsCache = |
2869 | 42 |
Exception.handle (\(e :: Exception.IOException) -> warningM "Database" (show e) >> return accountsCache) $ |
43 |
do |
|
44 |
q <- readChan queries |
|
45 |
updatedCache <- case q of |
|
4921 | 46 |
CheckAccount clId clUid clNick _ -> do |
2869 | 47 |
let cacheEntry = clNick `Map.lookup` accountsCache |
48 |
currentTime <- getCurrentTime |
|
49 |
if (isNothing cacheEntry) || (currentTime `diffUTCTime` (fst . fromJust) cacheEntry > 2 * 24 * 60 * 60) then |
|
50 |
do |
|
4921 | 51 |
SIO.hPutStrLn hIn $ show q |
2869 | 52 |
hFlush hIn |
2117
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
53 |
|
4921 | 54 |
(clId', clUid', accountInfo) <- SIO.hGetLine hOut >>= (maybeException . maybeRead) |
2117
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
55 |
|
4921 | 56 |
writeChan coreChan $ ClientAccountInfo clId' clUid' accountInfo |
2117
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
57 |
|
2869 | 58 |
return $ Map.insert clNick (currentTime, accountInfo) accountsCache |
59 |
`Exception.onException` |
|
60 |
(unGetChan queries q) |
|
61 |
else |
|
62 |
do |
|
4921 | 63 |
writeChan coreChan $ ClientAccountInfo clId clUid (snd $ fromJust cacheEntry) |
2869 | 64 |
return accountsCache |
2155
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2129
diff
changeset
|
65 |
|
2869 | 66 |
ClearCache -> return Map.empty |
67 |
SendStats {} -> ( |
|
4921 | 68 |
(SIO.hPutStrLn hIn $ show q) >> |
2869 | 69 |
hFlush hIn >> |
70 |
return accountsCache) |
|
71 |
`Exception.onException` |
|
72 |
(unGetChan queries q) |
|
2184 | 73 |
|
2869 | 74 |
pipeDbConnectionLoop queries coreChan hIn hOut updatedCache |
75 |
where |
|
76 |
maybeException (Just a) = return a |
|
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 |
|
4921 | 80 |
pipeDbConnection accountsCache si = do |
2869 | 81 |
updatedCache <- |
82 |
Exception.handle (\(e :: Exception.IOException) -> warningM "Database" (show e) >> return accountsCache) $ do |
|
83 |
(Just hIn, Just hOut, _, _) <- createProcess (proc "./OfficialServer/extdbinterface" []) |
|
84 |
{std_in = CreatePipe, |
|
85 |
std_out = CreatePipe} |
|
86 |
hSetBuffering hIn LineBuffering |
|
87 |
hSetBuffering hOut LineBuffering |
|
1804 | 88 |
|
4921 | 89 |
B.hPutStrLn hIn $ dbHost si |
90 |
B.hPutStrLn hIn $ dbLogin si |
|
91 |
B.hPutStrLn hIn $ dbPassword si |
|
92 |
pipeDbConnectionLoop (dbQueries si) (coreChan si) hIn hOut accountsCache |
|
2184 | 93 |
|
2869 | 94 |
threadDelay (3 * 10^6) |
4921 | 95 |
pipeDbConnection updatedCache si |
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1979
diff
changeset
|
96 |
|
4921 | 97 |
dbConnectionLoop si = |
98 |
if (not . B.null $ dbHost si) then |
|
99 |
pipeDbConnection Map.empty si |
|
2869 | 100 |
else |
4921 | 101 |
fakeDbConnection si |
1979 | 102 |
#else |
103 |
dbConnectionLoop = fakeDbConnection |
|
104 |
#endif |
|
1804 | 105 |
|
4932 | 106 |
startDBConnection :: ServerInfo -> IO () |
1833 | 107 |
startDBConnection serverInfo = |
4932 | 108 |
forkIO (dbConnectionLoop serverInfo) >> return () |