author | nemo |
Sun, 14 Aug 2011 17:42:26 -0400 | |
changeset 5563 | fc73b6e941f7 |
parent 4996 | 76ef3d8bd78e |
child 7331 | 0e50456d652c |
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 |
|
4989 | 30 |
fakeDbConnection :: forall b. ServerInfo -> IO b |
4943
21d6b2b79cfe
Allow users to join official server even when there's no db connection
unc0rr
parents:
4932
diff
changeset
|
31 |
fakeDbConnection si = forever $ do |
21d6b2b79cfe
Allow users to join official server even when there's no db connection
unc0rr
parents:
4932
diff
changeset
|
32 |
q <- readChan $ dbQueries si |
2869 | 33 |
case q of |
4932 | 34 |
CheckAccount clId clUid _ clHost -> |
4943
21d6b2b79cfe
Allow users to join official server even when there's no db connection
unc0rr
parents:
4932
diff
changeset
|
35 |
writeChan (coreChan si) $ 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 |
|
4996
76ef3d8bd78e
Fix crash (accessing already deleted client record) by reverting to old client removing handling + throwTo
unc0rr
parents:
4992
diff
changeset
|
39 |
dbConnectionLoop :: ServerInfo -> IO () |
76ef3d8bd78e
Fix crash (accessing already deleted client record) by reverting to old client removing handling + throwTo
unc0rr
parents:
4992
diff
changeset
|
40 |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1979
diff
changeset
|
41 |
#if defined(OFFICIAL_SERVER) |
4989 | 42 |
flushRequests :: ServerInfo -> IO () |
4943
21d6b2b79cfe
Allow users to join official server even when there's no db connection
unc0rr
parents:
4932
diff
changeset
|
43 |
flushRequests si = do |
21d6b2b79cfe
Allow users to join official server even when there's no db connection
unc0rr
parents:
4932
diff
changeset
|
44 |
e <- isEmptyChan $ dbQueries si |
21d6b2b79cfe
Allow users to join official server even when there's no db connection
unc0rr
parents:
4932
diff
changeset
|
45 |
unless e $ do |
21d6b2b79cfe
Allow users to join official server even when there's no db connection
unc0rr
parents:
4932
diff
changeset
|
46 |
q <- readChan $ dbQueries si |
21d6b2b79cfe
Allow users to join official server even when there's no db connection
unc0rr
parents:
4932
diff
changeset
|
47 |
case q of |
21d6b2b79cfe
Allow users to join official server even when there's no db connection
unc0rr
parents:
4932
diff
changeset
|
48 |
CheckAccount clId clUid _ clHost -> |
21d6b2b79cfe
Allow users to join official server even when there's no db connection
unc0rr
parents:
4932
diff
changeset
|
49 |
writeChan (coreChan si) $ ClientAccountInfo clId clUid (if clHost `L.elem` localAddressList then Admin else Guest) |
21d6b2b79cfe
Allow users to join official server even when there's no db connection
unc0rr
parents:
4932
diff
changeset
|
50 |
ClearCache -> return () |
21d6b2b79cfe
Allow users to join official server even when there's no db connection
unc0rr
parents:
4932
diff
changeset
|
51 |
SendStats {} -> return () |
21d6b2b79cfe
Allow users to join official server even when there's no db connection
unc0rr
parents:
4932
diff
changeset
|
52 |
flushRequests si |
21d6b2b79cfe
Allow users to join official server even when there's no db connection
unc0rr
parents:
4932
diff
changeset
|
53 |
|
4944 | 54 |
pipeDbConnectionLoop :: Chan DBQuery -> Chan CoreMessage -> Handle -> Handle -> Map.Map ByteString (UTCTime, AccountInfo) -> Int -> IO (Map.Map ByteString (UTCTime, AccountInfo), Int) |
55 |
pipeDbConnectionLoop queries cChan hIn hOut accountsCache req = |
|
56 |
Exception.handle (\(e :: Exception.IOException) -> warningM "Database" (show e) >> return (accountsCache, req)) $ |
|
2869 | 57 |
do |
58 |
q <- readChan queries |
|
4944 | 59 |
(updatedCache, newReq) <- case q of |
4921 | 60 |
CheckAccount clId clUid clNick _ -> do |
2869 | 61 |
let cacheEntry = clNick `Map.lookup` accountsCache |
62 |
currentTime <- getCurrentTime |
|
63 |
if (isNothing cacheEntry) || (currentTime `diffUTCTime` (fst . fromJust) cacheEntry > 2 * 24 * 60 * 60) then |
|
64 |
do |
|
4921 | 65 |
SIO.hPutStrLn hIn $ show q |
2869 | 66 |
hFlush hIn |
2117
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
67 |
|
4921 | 68 |
(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
|
69 |
|
4943
21d6b2b79cfe
Allow users to join official server even when there's no db connection
unc0rr
parents:
4932
diff
changeset
|
70 |
writeChan cChan $ ClientAccountInfo clId' clUid' accountInfo |
2117
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
71 |
|
4944 | 72 |
return $ (Map.insert clNick (currentTime, accountInfo) accountsCache, req + 1) |
2869 | 73 |
`Exception.onException` |
4944 | 74 |
(unGetChan queries q) |
2869 | 75 |
else |
76 |
do |
|
4943
21d6b2b79cfe
Allow users to join official server even when there's no db connection
unc0rr
parents:
4932
diff
changeset
|
77 |
writeChan cChan $ ClientAccountInfo clId clUid (snd $ fromJust cacheEntry) |
4944 | 78 |
return (accountsCache, req) |
2155
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2129
diff
changeset
|
79 |
|
4944 | 80 |
ClearCache -> return (Map.empty, req) |
2869 | 81 |
SendStats {} -> ( |
4921 | 82 |
(SIO.hPutStrLn hIn $ show q) >> |
2869 | 83 |
hFlush hIn >> |
4944 | 84 |
return (accountsCache, req)) |
2869 | 85 |
`Exception.onException` |
86 |
(unGetChan queries q) |
|
2184 | 87 |
|
4944 | 88 |
pipeDbConnectionLoop queries cChan hIn hOut updatedCache newReq |
2869 | 89 |
where |
90 |
maybeException (Just a) = return a |
|
91 |
maybeException Nothing = ioError (userError "Can't read") |
|
1804 | 92 |
|
4992 | 93 |
pipeDbConnection :: |
4982
3572eaf14340
Add dbName parameter to .ini file, fix some warnings
unc0rr
parents:
4975
diff
changeset
|
94 |
Map.Map ByteString (UTCTime, AccountInfo) |
4989 | 95 |
-> ServerInfo |
4992 | 96 |
-> Int |
97 |
-> IO () |
|
4982
3572eaf14340
Add dbName parameter to .ini file, fix some warnings
unc0rr
parents:
4975
diff
changeset
|
98 |
|
4943
21d6b2b79cfe
Allow users to join official server even when there's no db connection
unc0rr
parents:
4932
diff
changeset
|
99 |
pipeDbConnection accountsCache si errNum = do |
21d6b2b79cfe
Allow users to join official server even when there's no db connection
unc0rr
parents:
4932
diff
changeset
|
100 |
(updatedCache, newErrNum) <- |
21d6b2b79cfe
Allow users to join official server even when there's no db connection
unc0rr
parents:
4932
diff
changeset
|
101 |
Exception.handle (\(e :: Exception.IOException) -> warningM "Database" (show e) >> return (accountsCache, errNum + 1)) $ do |
2869 | 102 |
(Just hIn, Just hOut, _, _) <- createProcess (proc "./OfficialServer/extdbinterface" []) |
103 |
{std_in = CreatePipe, |
|
104 |
std_out = CreatePipe} |
|
105 |
hSetBuffering hIn LineBuffering |
|
106 |
hSetBuffering hOut LineBuffering |
|
1804 | 107 |
|
4921 | 108 |
B.hPutStrLn hIn $ dbHost si |
4982
3572eaf14340
Add dbName parameter to .ini file, fix some warnings
unc0rr
parents:
4975
diff
changeset
|
109 |
B.hPutStrLn hIn $ dbName si |
4921 | 110 |
B.hPutStrLn hIn $ dbLogin si |
111 |
B.hPutStrLn hIn $ dbPassword si |
|
4944 | 112 |
(c, r) <- pipeDbConnectionLoop (dbQueries si) (coreChan si) hIn hOut accountsCache 0 |
113 |
return (c, if r > 0 then 0 else errNum + 1) |
|
2184 | 114 |
|
4943
21d6b2b79cfe
Allow users to join official server even when there's no db connection
unc0rr
parents:
4932
diff
changeset
|
115 |
when (newErrNum > 1) $ flushRequests si |
21d6b2b79cfe
Allow users to join official server even when there's no db connection
unc0rr
parents:
4932
diff
changeset
|
116 |
threadDelay (3000000) |
21d6b2b79cfe
Allow users to join official server even when there's no db connection
unc0rr
parents:
4932
diff
changeset
|
117 |
pipeDbConnection updatedCache si newErrNum |
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1979
diff
changeset
|
118 |
|
4921 | 119 |
dbConnectionLoop si = |
120 |
if (not . B.null $ dbHost si) then |
|
4943
21d6b2b79cfe
Allow users to join official server even when there's no db connection
unc0rr
parents:
4932
diff
changeset
|
121 |
pipeDbConnection Map.empty si 0 |
2869 | 122 |
else |
4921 | 123 |
fakeDbConnection si |
1979 | 124 |
#else |
125 |
dbConnectionLoop = fakeDbConnection |
|
126 |
#endif |
|
1804 | 127 |
|
4989 | 128 |
startDBConnection :: ServerInfo -> IO () |
1833 | 129 |
startDBConnection serverInfo = |
4932 | 130 |
forkIO (dbConnectionLoop serverInfo) >> return () |