messageType -> protocolId

This commit is contained in:
gmega 2023-08-23 08:27:53 -03:00
parent 53009b1391
commit cc2f854bb1
6 changed files with 12 additions and 9 deletions

View File

@ -37,7 +37,7 @@ type
let RandomShuffler = proc (arr: var seq[PeerDescriptor]) = let RandomShuffler = proc (arr: var seq[PeerDescriptor]) =
discard arr.nextPermutation() discard arr.nextPermutation()
proc messageType*(T: type DHTTracker): string = "DHTTracker" proc protocolId*(T: type DHTTracker): string = "DHTTracker"
proc defaultExpiry*(T: type DHTTracker): Duration = 15.dminutes proc defaultExpiry*(T: type DHTTracker): Duration = 15.dminutes
@ -53,7 +53,7 @@ proc new*(
maxPeers: maxPeers, maxPeers: maxPeers,
shuffler: shuffler, shuffler: shuffler,
peers: initOrderedTable[int, PeerDescriptor](), peers: initOrderedTable[int, PeerDescriptor](),
messageType: DHTTracker.messageType protocolId: DHTTracker.protocolId
) )
proc peers*(self: DHTTracker): seq[PeerDescriptor] = self.peers.values.toSeq() proc peers*(self: DHTTracker): seq[PeerDescriptor] = self.peers.values.toSeq()

View File

@ -16,6 +16,9 @@ type
network: Network network: Network
message: Message message: Message
# TODO: use distributions (or trace resampling) instead of a constant for link delay
# TODO: model link capacity and implement downloads
proc new*( proc new*(
T: type Network, T: type Network,
engine: EventDrivenEngine, engine: EventDrivenEngine,

View File

@ -21,13 +21,13 @@ proc getProtocol*(self: Peer, protocolId: string): Option[Protocol] =
proc deliver*(self: Peer, message: Message, engine: EventDrivenEngine, proc deliver*(self: Peer, message: Message, engine: EventDrivenEngine,
network: Network): void = network: Network): void =
self.getProtocol(message.messageType).map( self.getProtocol(message.protocolId).map(
proc (p: Protocol): void = p.deliver(message, engine, network)) proc (p: Protocol): void = p.deliver(message, engine, network))
proc initPeer(self: Peer, protocols: seq[Protocol]): Peer = proc initPeer(self: Peer, protocols: seq[Protocol]): Peer =
# XXX integer indexes or an enum would be better, but this is easier # XXX integer indexes or an enum would be better, but this is easier
for protocol in protocols: for protocol in protocols:
self.protocols[protocol.messageType] = protocol self.protocols[protocol.protocolId] = protocol
self self

View File

@ -15,5 +15,5 @@ method uncheckedDeliver(
proc deliver*(self: Protocol, message: Message, engine: EventDrivenEngine, proc deliver*(self: Protocol, message: Message, engine: EventDrivenEngine,
network: Network): void = network: Network): void =
assert(self.messageType == message.messageType) assert(self.protocolId == message.protocolId)
self.uncheckedDeliver(message, engine, network) self.uncheckedDeliver(message, engine, network)

View File

@ -30,7 +30,7 @@ type
## A `Protocol` defines a P2P protocol. It handles messages meant for it, ## A `Protocol` defines a P2P protocol. It handles messages meant for it,
## keeps internal state, and may expose services to other `Protocol`s within ## keeps internal state, and may expose services to other `Protocol`s within
## the same `Peer`. ## the same `Peer`.
messageType*: string ## "Type" of the message accepted by this protocol. protocolId*: string
type type
Peer* = ref object of RootObj Peer* = ref object of RootObj
@ -41,7 +41,7 @@ type
Message* = ref object of RootObj Message* = ref object of RootObj
## A `Message` is a piece of data that is sent over the network. Its meaning ## A `Message` is a piece of data that is sent over the network. Its meaning
## is typically protocol-specific. ## is typically protocol-specific.
messageType*: string protocolId*: string
sender*: Option[Peer] = none(Peer) sender*: Option[Peer] = none(Peer)
receiver*: Peer receiver*: Peer

View File

@ -13,7 +13,7 @@ import pkg/swarmsim/timeutils
proc getPeerArray(tracker: Peer): seq[PeerDescriptor] = proc getPeerArray(tracker: Peer): seq[PeerDescriptor] =
DHTTracker( DHTTracker(
tracker.getProtocol(DHTTracker.messageType).get()).peers tracker.getProtocol(DHTTracker.protocolId).get()).peers
proc getPeerIdArray(tracker: Peer): seq[int] = proc getPeerIdArray(tracker: Peer): seq[int] =
getPeerArray(tracker).map(p => p.peerId) getPeerArray(tracker).map(p => p.peerId)
@ -22,7 +22,7 @@ proc announcePeer(network: Network, tracker: Peer, peerId: int,
delay: uint64 = 0) = delay: uint64 = 0) =
network.send( network.send(
PeerAnnouncement(receiver: tracker, PeerAnnouncement(receiver: tracker,
messageType: DHTTracker.messageType, peerId: peerId), protocolId: DHTTracker.protocolId, peerId: peerId),
delay.some).doAwait() delay.some).doAwait()
suite "tracker node": suite "tracker node":