summaryrefslogtreecommitdiff
path: root/Remote/Helper/P2P/IO.hs
blob: 6908fd68c6c96e7d3ed1691d0653ed21d8b5f8f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
{- P2P protocol, partial IO implementation
 -
 - Copyright 2016 Joey Hess <id@joeyh.name>
 -
 - Licensed under the GNU GPL version 3 or higher.
 -}

{-# LANGUAGE RankNTypes #-}

module Remote.Helper.P2P.IO
	( RunProto
	, runNetProtoHandle
	) where

import Remote.Helper.P2P
import Utility.Process
import Types.UUID
import Git
import Git.Command
import Utility.SafeCommand
import Utility.SimpleProtocol

import Control.Monad
import Control.Monad.Free
import Control.Monad.IO.Class
import Data.Maybe
import System.Exit (ExitCode(..))
import System.IO
import Control.Concurrent
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as L

type RunProto = forall a m. MonadIO m => Proto a -> m a

data S = S
	{ repo :: Repo
	, ihdl :: Handle
	, ohdl :: Handle
	}

-- Implementation of the protocol, communicating with a peer
-- over a Handle. No Local actions will be run.
runNetProtoHandle :: MonadIO m => Handle -> Handle -> Repo -> Proto a -> m a
runNetProtoHandle i o r = go
  where
	go :: RunProto
	go (Pure a) = pure a
	go (Free (Net n)) = runNetHandle (S r i o) go n
	go (Free (Local _)) = error "local actions not allowed"

runNetHandle :: MonadIO m => S -> RunProto -> NetF (Proto a) -> m a
runNetHandle s runner f = case f of
	SendMessage m next -> do
		liftIO $ do
			hPutStrLn (ohdl s) (unwords (formatMessage m))
			hFlush (ohdl s)
		runner next
	ReceiveMessage next -> do
		l <- liftIO $ hGetLine (ihdl s)
		case parseMessage l of
			Just m -> runner (next m)
			Nothing -> runner $ do
				let e = ERROR "protocol parse error"
				net $ sendMessage e
				next e
	SendBytes _len b next -> do
		liftIO $ do
			L.hPut (ohdl s) b
			hFlush (ohdl s)
		runner next
	ReceiveBytes (Len n) next -> do
		b <- liftIO $ L.hGet (ihdl s) (fromIntegral n)
		runner (next b)
	CheckAuthToken u t next -> do
		authed <- return True -- TODO XXX FIXME really check
		runner (next authed)
	Relay hout callback next ->
		runRelay runner hout callback >>= runner . next
	RelayService service callback next ->
		runRelayService s runner service callback >>= runner . next
	WriteRelay (RelayHandle h) b next -> do
		liftIO $ do
			L.hPut h b
			hFlush h
		runner next

runRelay
	:: MonadIO m
	=> RunProto
	-> RelayHandle
	-> (RelayData -> Net (Maybe ExitCode))
	-> m ExitCode
runRelay runner (RelayHandle hout) callback = do
	v <- liftIO newEmptyMVar
	_ <- liftIO $ forkIO $ readout v
	feeder <- liftIO $ forkIO $ feedin v
	exitcode <- liftIO $ drain v
	liftIO $ killThread feeder
	return exitcode
  where
	feedin v = forever $ do
		m <- runner $ net receiveMessage
		putMVar v $ RelayMessage m
	
	readout v = do
		b <- B.hGetSome hout 65536
		if B.null b
			then hClose hout
			else do
				putMVar v $ RelayData (L.fromChunks [b])
				readout v

	drain v = do
		d <- takeMVar v
		r <- runner $ net $ callback d
		case r of
			Nothing -> drain v
			Just exitcode -> return exitcode

runRelayService
	:: MonadIO m
	=> S
	-> RunProto
	-> Service
	-> (RelayHandle -> RelayData -> Net (Maybe ExitCode))
	-> m ExitCode
runRelayService s runner service callback = do
	v <- liftIO newEmptyMVar
	(Just hin, Just hout, _, pid) <- liftIO $ createProcess serviceproc
		{ std_out = CreatePipe
		, std_in = CreatePipe
		}
	_ <- liftIO $ forkIO $ readout v hout
	feeder <- liftIO $ forkIO $ feedin v
	_ <- liftIO $ forkIO $ putMVar v . Left =<< waitForProcess pid
	exitcode <- liftIO $ drain v hin
	liftIO $ killThread feeder
	return exitcode
  where
	cmd = case service of
		UploadPack -> "upload-pack"
		ReceivePack -> "receive-pack"
	serviceproc = gitCreateProcess [Param cmd, File (repoPath (repo s))] (repo s)

	drain v hin = do
		d <- takeMVar v
		case d of
			Left exitcode -> do
				hClose hin
				return exitcode
			Right relaydata -> do
				_ <- runner $ net $
					callback (RelayHandle hin) relaydata
				drain v hin
	
	readout v hout = do
		b <- B.hGetSome hout 65536
		if B.null b
			then hClose hout
			else do
				putMVar v $ Right $ 
					RelayData (L.fromChunks [b])
				readout v hout

	feedin v = forever $ do
		m <- runner $ net receiveMessage
		putMVar v $ Right $ RelayMessage m