diff options
author | joe <joe@jerkface.net> | 2013-06-16 00:03:33 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2013-06-16 00:03:33 -0400 |
commit | eda067284b31189d198e5d94a969c9a8ba6b77a7 (patch) | |
tree | cf49ac2971123fd1a858c50220ef852da322432d /Presence | |
parent | 42eb97f5574844d9899a42cf4b7c458c4a1b950e (diff) |
graceful quit from accept loop
Diffstat (limited to 'Presence')
-rw-r--r-- | Presence/Server.hs | 20 | ||||
-rw-r--r-- | Presence/main.hs | 26 |
2 files changed, 42 insertions, 4 deletions
diff --git a/Presence/Server.hs b/Presence/Server.hs index 5dce323c..feefea2b 100644 --- a/Presence/Server.hs +++ b/Presence/Server.hs | |||
@@ -1,5 +1,6 @@ | |||
1 | {-# LANGUAGE TypeFamilies #-} | 1 | {-# LANGUAGE TypeFamilies #-} |
2 | {-# LANGUAGE TypeOperators #-} | 2 | {-# LANGUAGE TypeOperators #-} |
3 | {-# LANGUAGE OverloadedStrings #-} | ||
3 | module Server where | 4 | module Server where |
4 | 5 | ||
5 | import Network.Socket | 6 | import Network.Socket |
@@ -73,10 +74,21 @@ doServer port g startCon = runServer2 port (runConn2 g) | |||
73 | setSocketOption sock ReuseAddr 1 | 74 | setSocketOption sock ReuseAddr 1 |
74 | bindSocket sock (SockAddrInet port iNADDR_ANY) | 75 | bindSocket sock (SockAddrInet port iNADDR_ANY) |
75 | listen sock 2 | 76 | listen sock 2 |
76 | mainLoop sock (ConnId 0) go | 77 | forkIO $ do |
78 | mainLoop sock (ConnId 0) go | ||
79 | L.putStrLn $ "quit accept loop" | ||
80 | return sock | ||
77 | where | 81 | where |
78 | mainLoop sock idnum@(ConnId n) go = do | 82 | mainLoop sock idnum@(ConnId n) go = do |
79 | con <- accept sock | 83 | let doException (SomeException e) = do |
80 | forkIO $ go (idnum .*. st) con | 84 | Prelude.putStrLn ("\n\naccept-loop exception: " ++ show e ++ "\n\n") |
81 | mainLoop sock (ConnId (n+1)) go | 85 | return Nothing |
86 | mcon <- handle doException $ fix $ \loop -> do | ||
87 | con <- accept sock | ||
88 | return $ Just con | ||
89 | case mcon of | ||
90 | Just con -> do | ||
91 | forkIO $ go (idnum .*. st) con | ||
92 | mainLoop sock (ConnId (n+1)) go | ||
93 | Nothing -> return () | ||
82 | 94 | ||
diff --git a/Presence/main.hs b/Presence/main.hs index 1eb0c0ed..b0f73a9e 100644 --- a/Presence/main.hs +++ b/Presence/main.hs | |||
@@ -1,3 +1,4 @@ | |||
1 | {-# LANGUAGE CPP #-} | ||
1 | 2 | ||
2 | import System.Directory | 3 | import System.Directory |
3 | import System.IO | 4 | import System.IO |
@@ -10,9 +11,15 @@ import System.Posix.Process | |||
10 | import Data.Maybe | 11 | import Data.Maybe |
11 | 12 | ||
12 | import System.INotify | 13 | import System.INotify |
14 | #ifndef NOUTMP | ||
13 | import UTmp | 15 | import UTmp |
16 | #endif | ||
14 | import FGConsole | 17 | import FGConsole |
15 | import XMPPServer | 18 | import XMPPServer |
19 | import Data.HList | ||
20 | import Network.Socket (sClose) | ||
21 | import Control.Concurrent (threadDelay) | ||
22 | import Control.Exception (handle,SomeException(..)) | ||
16 | 23 | ||
17 | jid user host rsrc = user ++ "@" ++ host ++ "/" ++ rsrc | 24 | jid user host rsrc = user ++ "@" ++ host ++ "/" ++ rsrc |
18 | 25 | ||
@@ -22,6 +29,7 @@ toJabberId host (user,tty,_) = | |||
22 | then Just (jid user host tty) | 29 | then Just (jid user host tty) |
23 | else Nothing | 30 | else Nothing |
24 | 31 | ||
32 | #ifndef NOUTMP | ||
25 | utmp_event e = do | 33 | utmp_event e = do |
26 | -- print e | 34 | -- print e |
27 | putStrLn "---" | 35 | putStrLn "---" |
@@ -29,26 +37,44 @@ utmp_event e = do | |||
29 | let ids = mapMaybe (toJabberId "localhost") us | 37 | let ids = mapMaybe (toJabberId "localhost") us |
30 | ids :: [String] | 38 | ids :: [String] |
31 | forM_ ids putStrLn | 39 | forM_ ids putStrLn |
40 | #endif | ||
32 | 41 | ||
33 | on_chvt vtnum = do | 42 | on_chvt vtnum = do |
34 | putStrLn $ "changed vt to "++ show vtnum | 43 | putStrLn $ "changed vt to "++ show vtnum |
35 | 44 | ||
36 | start :: IO () | 45 | start :: IO () |
37 | start = do | 46 | start = do |
47 | #ifndef NOUTMP | ||
38 | installHandler sigUSR1 (Catch (utmp_event (userError "signaled"))) Nothing | 48 | installHandler sigUSR1 (Catch (utmp_event (userError "signaled"))) Nothing |
49 | #endif | ||
50 | -- installHandler sigTERM (CatchOnce (utmp_event (userError "term signaled"))) Nothing | ||
39 | inotify <- initINotify | 51 | inotify <- initINotify |
40 | print inotify | 52 | print inotify |
53 | #ifndef NOUTMP | ||
41 | wd <- addWatch | 54 | wd <- addWatch |
42 | inotify | 55 | inotify |
43 | [CloseWrite] -- [Open,Close,Access,Modify,Move] | 56 | [CloseWrite] -- [Open,Close,Access,Modify,Move] |
44 | utmp_file | 57 | utmp_file |
45 | utmp_event | 58 | utmp_event |
46 | print wd | 59 | print wd |
60 | #endif | ||
47 | mtty <- monitorTTY on_chvt | 61 | mtty <- monitorTTY on_chvt |
62 | sock <- listenForXmppClients 5222 HNil | ||
48 | putStrLn "Hit enter to terminate..." | 63 | putStrLn "Hit enter to terminate..." |
49 | getLine | 64 | getLine |
65 | {- | ||
66 | let doException (SomeException e) = Prelude.putStrLn ("\n\nexception: " ++ show e ++ "\n\n") | ||
67 | handle doException $ do | ||
68 | -} | ||
69 | sClose sock | ||
70 | -- threadDelay 1000 | ||
71 | putStrLn "closed listener." | ||
50 | unmonitorTTY mtty | 72 | unmonitorTTY mtty |
73 | putStrLn "unhooked tty monitor." | ||
74 | #ifndef NOUTMP | ||
51 | removeWatch wd | 75 | removeWatch wd |
76 | #endif | ||
77 | putStrLn "Normal termination." | ||
52 | 78 | ||
53 | sendUSR1 pid = do | 79 | sendUSR1 pid = do |
54 | signalProcess sigUSR1 pid | 80 | signalProcess sigUSR1 pid |