Skip to content

Commit

Permalink
Small fix
Browse files Browse the repository at this point in the history
  • Loading branch information
bolt12 committed Mar 30, 2023
1 parent 6c81552 commit a516d53
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE ScopedTypeVariables #-}


-- | Intended to be imported qualified.
--
module Ouroboros.Network.InboundGovernor.ControlChannel
( NewConnection (..)
, ControlChannel (..)
, ServerControlChannel
, GovernorControlChannel
, newControlChannel
) where

import Control.Concurrent.Class.MonadSTM.Strict

import Data.Functor (($>))
import GHC.Natural (Natural)

import Network.Mux.Types (MuxMode)

import Ouroboros.Network.ConnectionHandler
import Ouroboros.Network.ConnectionId (ConnectionId (..))
import Ouroboros.Network.ConnectionManager.Types
import Ouroboros.Network.PeerSelection.PeerSharing (PeerSharing)


-- | Announcement message for a new connection.
--
data NewConnection peerAddr handle

-- | Announce a new connection. /Inbound protocol governor/ will start
-- responder protocols using 'StartOnDemand' strategy and monitor remote
-- transitions: @PromotedToWarm^{Duplex}_{Remote}@ and
-- @DemotedToCold^{dataFlow}_{Remote}@.
= NewConnection
!Provenance
!(ConnectionId peerAddr)
!DataFlow
!handle

instance Show peerAddr
=> Show (NewConnection peerAddr handle) where
show (NewConnection provenance connId dataFlow _) =
concat [ "NewConnection "
, show provenance
, " "
, show connId
, " "
, show dataFlow
]



-- | A Server control channel which instantiates to 'NewConnection' and 'Handle'.
--
-- It allows to pass 'STM' transactions which will resolve to 'NewConnection'.
-- Server's monitoring thread is the consumer of these messages; there are two
-- producers: accept loop and connection handler for outbound connections.
--
type ServerControlChannel (muxMode :: MuxMode) peerAddr versionData bytes m a b =
ControlChannel (NewConnection peerAddr (Handle muxMode peerAddr versionData bytes m a b)) m

-- | Control Channel between Server and Outbound Governor.
--
-- Control channel that is meant to share inbound connections with the Peer
-- Selection Governor. So the consumer is the Governor and Producer is the
-- Server.
--
type GovernorControlChannel peerAddr m =
ControlChannel (peerAddr, PeerSharing) m

-- | Control channel.
--
data ControlChannel a m =
ControlChannel {
-- | Read a single value from the channel.
--
readMessage :: STM m a,

-- | Write a value to the channel.
--
writeMessage :: a -> STM m ()
}


newControlChannel :: forall a m.
MonadLabelledSTM m
=> m (ControlChannel a m)
newControlChannel = do
channel <-
atomically $
newTBQueue cc_QUEUE_BOUND
>>= \q -> labelTBQueue q "server-cc" $> q
pure $ ControlChannel {
readMessage = readTBQueue channel,
writeMessage = writeTBQueue channel
}


-- | The 'ControlChannel's 'TBQueue' depth.
--
cc_QUEUE_BOUND :: Natural
cc_QUEUE_BOUND = 10
3 changes: 1 addition & 2 deletions ouroboros-network/test/Test/Ouroboros/Network/Testnet.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2628,9 +2628,8 @@ prop_never_connects_to_self absBearerInfo diffScript =
let sim :: forall s . IOSim s Void
sim = diffusionSimulation (toBearerInfo absBearerInfo)
diffScript
tracersExtraWithTimeName
iosimTracer
tracerDiffusionSimWithTimeName
nullTracer

events :: [Trace () DiffusionTestTrace]
events = fmap ( Trace.fromList ()
Expand Down

0 comments on commit a516d53

Please sign in to comment.