author | nemo |
Sun, 14 Nov 2010 15:06:02 -0500 | |
changeset 4337 | 85e02b1a8e8f |
parent 4295 | 1f5604cd99be |
parent 4334 | 82cfbbab73da |
child 4568 | f85243bf890e |
permissions | -rw-r--r-- |
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
|
1 |
{-# LANGUAGE CPP, ScopedTypeVariables, OverloadedStrings #-} |
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 |
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
|
14 |
import Data.Maybe |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
15 |
import System.Log.Logger |
2126 | 16 |
import Data.Time |
1833 | 17 |
------------------------ |
18 |
import CoreTypes |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1979
diff
changeset
|
19 |
import Utils |
1804 | 20 |
|
1921 | 21 |
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
|
22 |
|
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
|
23 |
fakeDbConnection serverInfo = forever $ do |
2869 | 24 |
q <- readChan $ dbQueries serverInfo |
25 |
case q of |
|
26 |
CheckAccount clUid _ clHost -> do |
|
27 |
writeChan (coreChan serverInfo) $ ClientAccountInfo (clUid, |
|
28 |
if clHost `elem` localAddressList then Admin else Guest) |
|
29 |
ClearCache -> return () |
|
30 |
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
|
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 |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1979
diff
changeset
|
33 |
#if defined(OFFICIAL_SERVER) |
2184 | 34 |
pipeDbConnectionLoop queries coreChan hIn hOut accountsCache = |
2869 | 35 |
Exception.handle (\(e :: Exception.IOException) -> warningM "Database" (show e) >> return accountsCache) $ |
36 |
do |
|
37 |
q <- readChan queries |
|
38 |
updatedCache <- case q of |
|
39 |
CheckAccount clUid clNick _ -> do |
|
40 |
let cacheEntry = clNick `Map.lookup` accountsCache |
|
41 |
currentTime <- getCurrentTime |
|
42 |
if (isNothing cacheEntry) || (currentTime `diffUTCTime` (fst . fromJust) cacheEntry > 2 * 24 * 60 * 60) then |
|
43 |
do |
|
44 |
hPutStrLn hIn $ show q |
|
45 |
hFlush hIn |
|
2117
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
46 |
|
2869 | 47 |
(clId, accountInfo) <- hGetLine hOut >>= (maybeException . maybeRead) |
2117
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
48 |
|
2869 | 49 |
writeChan coreChan $ ClientAccountInfo (clId, accountInfo) |
2117
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
50 |
|
2869 | 51 |
return $ Map.insert clNick (currentTime, accountInfo) accountsCache |
52 |
`Exception.onException` |
|
53 |
(unGetChan queries q) |
|
54 |
else |
|
55 |
do |
|
56 |
writeChan coreChan $ ClientAccountInfo (clUid, snd $ fromJust cacheEntry) |
|
57 |
return accountsCache |
|
2155
d897222d3339
Implement ability for server admin to clear accounts cache
unc0rr
parents:
2129
diff
changeset
|
58 |
|
2869 | 59 |
ClearCache -> return Map.empty |
60 |
SendStats {} -> ( |
|
61 |
(hPutStrLn hIn $ show q) >> |
|
62 |
hFlush hIn >> |
|
63 |
return accountsCache) |
|
64 |
`Exception.onException` |
|
65 |
(unGetChan queries q) |
|
2184 | 66 |
|
2869 | 67 |
pipeDbConnectionLoop queries coreChan hIn hOut updatedCache |
68 |
where |
|
69 |
maybeException (Just a) = return a |
|
70 |
maybeException Nothing = ioError (userError "Can't read") |
|
1804 | 71 |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1979
diff
changeset
|
72 |
|
2117
1ac0e10e546f
Add caching for accounts information (entries are stored in memory forever)
unc0rr
parents:
2116
diff
changeset
|
73 |
pipeDbConnection accountsCache serverInfo = do |
2869 | 74 |
updatedCache <- |
75 |
Exception.handle (\(e :: Exception.IOException) -> warningM "Database" (show e) >> return accountsCache) $ do |
|
76 |
(Just hIn, Just hOut, _, _) <- createProcess (proc "./OfficialServer/extdbinterface" []) |
|
77 |
{std_in = CreatePipe, |
|
78 |
std_out = CreatePipe} |
|
79 |
hSetBuffering hIn LineBuffering |
|
80 |
hSetBuffering hOut LineBuffering |
|
1804 | 81 |
|
2869 | 82 |
hPutStrLn hIn $ dbHost serverInfo |
83 |
hPutStrLn hIn $ dbLogin serverInfo |
|
84 |
hPutStrLn hIn $ dbPassword serverInfo |
|
85 |
pipeDbConnectionLoop (dbQueries serverInfo) (coreChan serverInfo) hIn hOut accountsCache |
|
2184 | 86 |
|
2869 | 87 |
threadDelay (3 * 10^6) |
88 |
pipeDbConnection updatedCache serverInfo |
|
2116
dec7ead2d178
Bring back authentication to official server, now using separate process to perform database interaction
unc0rr
parents:
1979
diff
changeset
|
89 |
|
2126 | 90 |
dbConnectionLoop serverInfo = |
2869 | 91 |
if (not . null $ dbHost serverInfo) then |
92 |
pipeDbConnection Map.empty serverInfo |
|
93 |
else |
|
94 |
fakeDbConnection serverInfo |
|
1979 | 95 |
#else |
96 |
dbConnectionLoop = fakeDbConnection |
|
97 |
#endif |
|
1804 | 98 |
|
1833 | 99 |
startDBConnection serverInfo = |
2869 | 100 |
forkIO $ dbConnectionLoop serverInfo |