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 ./types
import ./schedulableevent import ./schedulableevent
export options
export EventDrivenEngine
type type
AwaitableHandle = object of RootObj AwaitableHandle* = object of RootObj
schedulable: SchedulableEvent schedulable: SchedulableEvent
engine: EventDrivenEngine engine: EventDrivenEngine
@ -44,7 +47,3 @@ proc run*(self: EventDrivenEngine): void =
proc doAwait*(self: AwaitableHandle): void = proc doAwait*(self: AwaitableHandle): void =
self.engine.runUntil(self.schedulable.time) self.engine.runUntil(self.schedulable.time)
export EventDrivenEngine
export AwaitableHandle
export options

View File

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

View File

@ -5,6 +5,12 @@ import ./types
import ./peer import ./peer
import ./eventdrivenengine import ./eventdrivenengine
export options
export sets
export peer
export eventdrivenengine
export Network
type type
ScheduledMessage = ref object of SchedulableEvent ScheduledMessage = ref object of SchedulableEvent
message: Message message: Message
@ -42,9 +48,3 @@ proc send*(self: Network, message: Message,
method atScheduledTime*(self: ScheduledMessage, engine: EventDrivenEngine) = method atScheduledTime*(self: ScheduledMessage, engine: EventDrivenEngine) =
self.message.receiver.deliver(self.message) 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 ./types
import ./protocol import ./protocol
## TODO a "readonly" pragma could probably be my first macro export options
proc `peerId=`*(self: Peer, id: int): void {.error: "Cannot assign to `peerId` property of `Peer`.".} export tables
export protocol
proc `protocols=`*(self: Peer, value: Table[string, Protocol]): void {.error: "Cannot assign to `protocols` property of `Peer`.".} export Peer
proc `protocols`*(self: Peer): Table[string, Protocol] {.error: "Cannot read from `protocols` property of `Peer`.".}
proc getProtocol*(self: Peer, protocolId: string): Option[Protocol] = proc getProtocol*(self: Peer, protocolId: string): Option[Protocol] =
if self.protocols.hasKey(protocolId): if self.protocols.hasKey(protocolId):
@ -34,13 +32,9 @@ proc hash*(self: Peer): int = self.peerId
proc new*( proc new*(
T: type Peer, T: type Peer,
protocols: seq[Protocol], protocols: seq[Protocol],
peerId: Option[int] = none(int) peerId: Option[int] = none(int),
): Peer = ): Peer =
# XXX I can't have put this in the init proc as that would mean allowing public # 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. # write access to every field in Peer. Not sure how to solve this in nim.
let peerId = peerId.get(rand(high(int))) let peerId = peerId.get(rand(high(int)))
initPeer(Peer(protocols: initTable[string, Protocol](), peerId: peerId), protocols) initPeer(Peer(protocols: initTable[string, Protocol](), peerId: peerId), protocols)
export Peer
export options
export tables

View File

@ -1,4 +1,8 @@
import ./types import ./types
import ./message
export message
export Protocol
method uncheckedDeliver(self: Protocol, message: Message): void {.base.} = method uncheckedDeliver(self: Protocol, message: Message): void {.base.} =
raise newException(CatchableError, "Method without implementation override") 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 = proc deliver*(self: Protocol, message: Message): void =
assert(self.messageType == message.messageType) assert(self.messageType == message.messageType)
self.uncheckedDeliver(message) self.uncheckedDeliver(message)
export Protocol

View File

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

View File

@ -3,6 +3,9 @@ import std/tables
import std/sets import std/sets
import std/options import std/options
export heapqueue
export option
type type
SchedulableEvent* = ref object of RootObj SchedulableEvent* = ref object of RootObj
## A `SchedulableEvent` is an event that can be scheduled for execution in an `EventDrivenEngine` ## A `SchedulableEvent` is an event that can be scheduled for execution in an `EventDrivenEngine`
@ -44,6 +47,3 @@ type
engine*: EventDrivenEngine engine*: EventDrivenEngine
defaultLinkDelay*: uint64 defaultLinkDelay*: uint64
peers*: HashSet[Peer] # TODO: use an array 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": test "should dispatch message to the correct protocol within a peer":
let engine = EventDrivenEngine() let engine = EventDrivenEngine()
var protocols: seq[Protocol] = newSeq[Protocol]() let peer = Peer.new(
protocols = @[
protocols.add(FakeProtocol(messageType: "protocol1", received: false)) Protocol FakeProtocol(messageType: "protocol1", received: false),
protocols.add(FakeProtocol(messageType: "protocol2", received: false)) FakeProtocol(messageType: "protocol2", received: false)
]
let peer = Peer.new(protocols = protocols) )
let network = Network.new(engine = engine, defaultLinkDelay = 20) let network = Network.new(engine = engine, defaultLinkDelay = 20)
network.add(peer) network.add(peer)