cleanup and organize exports

This commit is contained in:
gmega 2023-08-17 16:50:41 -03:00
parent 7dde9f3f7f
commit f4f265a90e
8 changed files with 34 additions and 37 deletions

View File

@ -4,8 +4,11 @@ import std/strformat
import ./types
import ./schedulableevent
export options
export EventDrivenEngine
type
AwaitableHandle = object of RootObj
AwaitableHandle* = object of RootObj
schedulable: SchedulableEvent
engine: EventDrivenEngine
@ -44,7 +47,3 @@ proc run*(self: EventDrivenEngine): void =
proc doAwait*(self: AwaitableHandle): void =
self.engine.runUntil(self.schedulable.time)
export EventDrivenEngine
export AwaitableHandle
export options

View File

@ -2,8 +2,10 @@ import std/options
import ./types
export options
export Message
proc new*(T: type Message, sender: Option[Peer] = none(Peer), receiver: Peer, messageType: string): Message =
Message(sender: sender, receiver: receiver, messageType: messageType)
export Message
export options

View File

@ -5,6 +5,12 @@ import ./types
import ./peer
import ./eventdrivenengine
export options
export sets
export peer
export eventdrivenengine
export Network
type
ScheduledMessage = ref object of SchedulableEvent
message: Message
@ -42,9 +48,3 @@ proc send*(self: Network, message: Message,
method atScheduledTime*(self: ScheduledMessage, engine: EventDrivenEngine) =
self.message.receiver.deliver(self.message)
export Network
export peer
export eventdrivenengine
export options
export sets

View File

@ -5,12 +5,10 @@ import std/random
import ./types
import ./protocol
## TODO a "readonly" pragma could probably be my first macro
proc `peerId=`*(self: Peer, id: int): void {.error: "Cannot assign to `peerId` property of `Peer`.".}
proc `protocols=`*(self: Peer, value: Table[string, Protocol]): void {.error: "Cannot assign to `protocols` property of `Peer`.".}
proc `protocols`*(self: Peer): Table[string, Protocol] {.error: "Cannot read from `protocols` property of `Peer`.".}
export options
export tables
export protocol
export Peer
proc getProtocol*(self: Peer, protocolId: string): Option[Protocol] =
if self.protocols.hasKey(protocolId):
@ -34,13 +32,9 @@ proc hash*(self: Peer): int = self.peerId
proc new*(
T: type Peer,
protocols: seq[Protocol],
peerId: Option[int] = none(int)
peerId: Option[int] = none(int),
): Peer =
# XXX I can't have put this in the init proc as that would mean allowing public
# write access to every field in Peer. Not sure how to solve this in nim.
let peerId = peerId.get(rand(high(int)))
initPeer(Peer(protocols: initTable[string, Protocol](), peerId: peerId), protocols)
export Peer
export options
export tables

View File

@ -1,4 +1,8 @@
import ./types
import ./message
export message
export Protocol
method uncheckedDeliver(self: Protocol, message: Message): void {.base.} =
raise newException(CatchableError, "Method without implementation override")
@ -6,5 +10,3 @@ method uncheckedDeliver(self: Protocol, message: Message): void {.base.} =
proc deliver*(self: Protocol, message: Message): void =
assert(self.messageType == message.messageType)
self.uncheckedDeliver(message)
export Protocol

View File

@ -1,5 +1,7 @@
import ./types
export SchedulableEvent
func `<`*(self: SchedulableEvent, other: SchedulableEvent): bool =
return self.time < other.time
@ -10,5 +12,3 @@ method atScheduledTime*(self: SchedulableEvent, engine: EventDrivenEngine): void
## default, it does nothing.
##
discard
export SchedulableEvent

View File

@ -3,6 +3,9 @@ import std/tables
import std/sets
import std/options
export heapqueue
export option
type
SchedulableEvent* = ref object of RootObj
## A `SchedulableEvent` is an event that can be scheduled for execution in an `EventDrivenEngine`
@ -44,6 +47,3 @@ type
engine*: EventDrivenEngine
defaultLinkDelay*: uint64
peers*: HashSet[Peer] # TODO: use an array
export heapqueue
export option

View File

@ -22,12 +22,12 @@ suite "network":
test "should dispatch message to the correct protocol within a peer":
let engine = EventDrivenEngine()
var protocols: seq[Protocol] = newSeq[Protocol]()
protocols.add(FakeProtocol(messageType: "protocol1", received: false))
protocols.add(FakeProtocol(messageType: "protocol2", received: false))
let peer = Peer.new(protocols = protocols)
let peer = Peer.new(
protocols = @[
Protocol FakeProtocol(messageType: "protocol1", received: false),
FakeProtocol(messageType: "protocol2", received: false)
]
)
let network = Network.new(engine = engine, defaultLinkDelay = 20)
network.add(peer)