gameServer/OfficialServer/DBInteraction.hs
author unc0rr
Wed, 18 Feb 2009 15:04:40 +0000
changeset 1804 4e78ad846fb6
child 1833 e901ec5644b4
permissions -rw-r--r--
New game server: - Incomplete implementation - More robust, no memory leaks, better architecture for easy features addition - Incompatible with current client
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1804
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     1
module OfficialServer.DBInteraction
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     2
(
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     3
	startDBConnection,
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     4
	DBQuery(HasRegistered, CheckPassword)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     5
) where
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     6
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     7
import Database.HDBC
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     8
import Database.HDBC.MySQL
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
     9
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    10
import System.IO
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    11
import Control.Concurrent
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    12
import Control.Concurrent.STM
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    13
import Control.Exception
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    14
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    15
data DBQuery =
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    16
	HasRegistered String
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    17
	| CheckPassword String
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    18
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    19
dbInteractionLoop queries dbConn = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    20
	q <- atomically $ readTChan queries
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    21
	case q of
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    22
		HasRegistered queryStr -> putStrLn queryStr
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    23
		CheckPassword queryStr -> putStrLn queryStr
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    24
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    25
	dbInteractionLoop queries dbConn
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    26
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    27
dbConnectionLoop queries = do
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    28
	Control.Exception.handle (\e -> print e) $ handleSqlError $
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    29
		bracket
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    30
			(connectMySQL defaultMySQLConnectInfo { mysqlHost = "192.168.50.5", mysqlDatabase = "glpi" })
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    31
			(disconnect)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    32
			(dbInteractionLoop queries)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    33
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    34
	threadDelay (15 * 10^6)
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    35
	dbConnectionLoop queries
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    36
4e78ad846fb6 New game server:
unc0rr
parents:
diff changeset
    37
startDBConnection queries = forkIO $ dbConnectionLoop queries