-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PM-3129: Add LocalConnectionManager on top of the remote one. (#50)
- Loading branch information
Showing
2 changed files
with
58 additions
and
2 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
metronome/networking/src/io/iohk/metronome/networking/LocalConnectionManager.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package io.iohk.metronome.networking | ||
|
||
import cats.implicits._ | ||
import cats.effect.{Concurrent, Timer, Resource, ContextShift} | ||
import java.net.InetSocketAddress | ||
import monix.eval.{TaskLift, TaskLike} | ||
import monix.tail.Iterant | ||
import scodec.Codec | ||
|
||
trait LocalConnectionManager[F[_], K, M] { | ||
def isConnected: F[Boolean] | ||
def incomingMessages: Iterant[F, M] | ||
def sendMessage( | ||
message: M | ||
): F[Either[ConnectionHandler.ConnectionAlreadyClosedException[K], Unit]] | ||
} | ||
|
||
/** Connect to a single local process and keep the connection alive. */ | ||
object LocalConnectionManager { | ||
|
||
def apply[ | ||
F[_]: Concurrent: TaskLift: TaskLike: Timer: ContextShift, | ||
K: Codec, | ||
M: Codec | ||
]( | ||
encryptedConnectionsProvider: EncryptedConnectionProvider[F, K, M], | ||
targetKey: K, | ||
targetAddress: InetSocketAddress, | ||
retryConfig: RemoteConnectionManager.RetryConfig | ||
)(implicit | ||
tracers: NetworkTracers[F, K, M] | ||
): Resource[F, LocalConnectionManager[F, K, M]] = { | ||
for { | ||
remoteConnectionManager <- RemoteConnectionManager[F, K, M]( | ||
encryptedConnectionsProvider, | ||
RemoteConnectionManager.ClusterConfig[K]( | ||
Set(targetKey -> targetAddress) | ||
), | ||
retryConfig | ||
) | ||
localConnectionManager = new LocalConnectionManager[F, K, M] { | ||
override def isConnected = | ||
remoteConnectionManager.getAcquiredConnections.map( | ||
_.contains(targetKey) | ||
) | ||
|
||
override def incomingMessages = | ||
remoteConnectionManager.incomingMessages.map { | ||
case ConnectionHandler.MessageReceived(_, m) => m | ||
} | ||
|
||
override def sendMessage(message: M) = | ||
remoteConnectionManager.sendMessage(targetKey, message) | ||
} | ||
} yield localConnectionManager | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters