author | nemo |
Tue, 25 Aug 2009 20:54:11 +0000 | |
changeset 2331 | e4941a7986d6 |
parent 2307 | b20830087ed1 |
child 2385 | 56b2e12b9eeb |
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 |
|
2296
19f2f76dc346
Patch for compiling with 6.10 (define NEW_EXCEPTIONS to do that)
unc0rr
parents:
2245
diff
changeset
|
11 |
#if defined(NEW_EXCEPTIONS) |
19f2f76dc346
Patch for compiling with 6.10 (define NEW_EXCEPTIONS to do that)
unc0rr
parents:
2245
diff
changeset
|
12 |
import qualified Control.OldException as Exception |
19f2f76dc346
Patch for compiling with 6.10 (define NEW_EXCEPTIONS to do that)
unc0rr
parents:
2245
diff
changeset
|
13 |
#else |
19f2f76dc346
Patch for compiling with 6.10 (define NEW_EXCEPTIONS to do that)
unc0rr
parents:
2245
diff
changeset
|
14 |
import qualified Control.Exception as Exception |
19f2f76dc346
Patch for compiling with 6.10 (define NEW_EXCEPTIONS to do that)
unc0rr
parents:
2245
diff
changeset
|
15 |
#endif |
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1979
diff
changeset
|
16 |
import Control.Monad |
2117
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
17 |
import qualified Data.Map as Map |
1833 | 18 |
import Monad |
1834 | 19 |
import Maybe |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
20 |
import System.Log.Logger |
2126 | 21 |
import Data.Time |
1833 | 22 |
------------------------ |
23 |
import CoreTypes |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1979
diff
changeset
|
24 |
import Utils |
1804 | 25 |
|
1921 | 26 |
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
|
27 |
|
1857
b835395659e2
Fake database connection with routine which marks all users as guests, when no database host was specified
unc0rr
parents:
1847
diff
changeset
|
28 |
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
|
29 |
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
|
30 |
case q of |
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1979
diff
changeset
|
31 |
CheckAccount clUid _ clHost -> do |
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1979
diff
changeset
|
32 |
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
|
33 |
if clHost `elem` localAddressList then Admin else Guest) |
2155
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2129
diff
changeset
|
34 |
ClearCache -> return () |
2245
c011aecc95e5
unc0rr's patch from issue #144 - prevent spectators from ruining the game
nemo
parents:
2184
diff
changeset
|
35 |
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
|
36 |
|
b835395659e2
Fake database connection with routine which marks all users as guests, when no database host was specified
unc0rr
parents:
1847
diff
changeset
|
37 |
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
|
38 |
|
b835395659e2
Fake database connection with routine which marks all users as guests, when no database host was specified
unc0rr
parents:
1847
diff
changeset
|
39 |
|
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) |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
41 |
------------------------------------------------------------------- |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
42 |
-- borrowed from base 4.0.0 --------------------------------------- |
2306 | 43 |
onException :: IO a -> IO b -> IO a |
2307 | 44 |
onException io what = io `Exception.catch` \e -> do |
45 |
what |
|
46 |
Exception.throw (e :: Exception.Exception) |
|
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
47 |
-- to be deleted -------------------------------------------------- |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
48 |
------------------------------------------------------------------- |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
49 |
|
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
50 |
|
2184 | 51 |
pipeDbConnectionLoop queries coreChan hIn hOut accountsCache = |
2296
19f2f76dc346
Patch for compiling with 6.10 (define NEW_EXCEPTIONS to do that)
unc0rr
parents:
2245
diff
changeset
|
52 |
Exception.handle (\e -> warningM "Database" (show e) >> return accountsCache) $ |
2184 | 53 |
do |
1833 | 54 |
q <- readChan queries |
2117
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
55 |
updatedCache <- case q of |
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
56 |
CheckAccount clUid clNick _ -> do |
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
57 |
let cacheEntry = clNick `Map.lookup` accountsCache |
2126 | 58 |
currentTime <- getCurrentTime |
59 |
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
|
60 |
do |
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
61 |
hPutStrLn hIn $ show q |
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
62 |
hFlush hIn |
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
63 |
|
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
64 |
(clId, accountInfo) <- hGetLine hOut >>= (maybeException . maybeRead) |
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
65 |
|
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
66 |
writeChan coreChan $ ClientAccountInfo (clId, accountInfo) |
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
67 |
|
2126 | 68 |
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
|
69 |
`onException` |
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
70 |
(unGetChan queries q) |
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
71 |
else |
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
72 |
do |
2126 | 73 |
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
|
74 |
return accountsCache |
2155
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2129
diff
changeset
|
75 |
|
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2129
diff
changeset
|
76 |
ClearCache -> return Map.empty |
2184 | 77 |
SendStats {} -> onException ( |
78 |
(hPutStrLn hIn $ show q) >> |
|
79 |
hFlush hIn >> |
|
80 |
return accountsCache) |
|
81 |
(unGetChan queries q) |
|
82 |
||
83 |
pipeDbConnectionLoop queries coreChan hIn hOut updatedCache |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1979
diff
changeset
|
84 |
where |
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1979
diff
changeset
|
85 |
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
|
86 |
maybeException Nothing = ioError (userError "Can't read") |
1804 | 87 |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1979
diff
changeset
|
88 |
|
2117
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
89 |
pipeDbConnection accountsCache serverInfo = do |
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
90 |
updatedCache <- |
2296
19f2f76dc346
Patch for compiling with 6.10 (define NEW_EXCEPTIONS to do that)
unc0rr
parents:
2245
diff
changeset
|
91 |
Exception.handle (\e -> warningM "Database" (show e) >> return accountsCache) $ do |
2184 | 92 |
(Just hIn, Just hOut, _, _) <- createProcess (proc "./OfficialServer/extdbinterface" []) |
93 |
{std_in = CreatePipe, |
|
94 |
std_out = CreatePipe} |
|
95 |
hSetBuffering hIn LineBuffering |
|
96 |
hSetBuffering hOut LineBuffering |
|
1804 | 97 |
|
2184 | 98 |
hPutStrLn hIn $ dbHost serverInfo |
99 |
hPutStrLn hIn $ dbLogin serverInfo |
|
100 |
hPutStrLn hIn $ dbPassword serverInfo |
|
101 |
pipeDbConnectionLoop (dbQueries serverInfo) (coreChan serverInfo) hIn hOut accountsCache |
|
102 |
||
103 |
threadDelay (3 * 10^6) |
|
2117
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
104 |
pipeDbConnection updatedCache serverInfo |
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1979
diff
changeset
|
105 |
|
2126 | 106 |
dbConnectionLoop serverInfo = |
2123 | 107 |
if (not . null $ dbHost serverInfo) then |
2126 | 108 |
pipeDbConnection Map.empty serverInfo |
2123 | 109 |
else |
2126 | 110 |
fakeDbConnection serverInfo |
1979 | 111 |
#else |
112 |
dbConnectionLoop = fakeDbConnection |
|
113 |
#endif |
|
1804 | 114 |
|
1833 | 115 |
startDBConnection serverInfo = |
2123 | 116 |
forkIO $ dbConnectionLoop serverInfo |