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]) =
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
@ -53,7 +53,7 @@ proc new*(
maxPeers: maxPeers,
shuffler: shuffler,
peers: initOrderedTable[int, PeerDescriptor](),
messageType: DHTTracker.messageType
protocolId: DHTTracker.protocolId
)
proc peers*(self: DHTTracker): seq[PeerDescriptor] = self.peers.values.toSeq()

View File

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

View File

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

View File

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

View File

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

View File

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