Move membranes to seperate module
This commit is contained in:
parent
d18508371f
commit
d1e196f6f0
|
@ -153,7 +153,7 @@ proc attenuate(r: Ref; a: Attenuation): Ref =
|
|||
proc hash*(facet): Hash =
|
||||
facet.id.hash
|
||||
|
||||
proc hash*(r: Ref): Hash =
|
||||
proc hash(r: Ref): Hash =
|
||||
!$(r.relay.hash !&
|
||||
r.target.unsafeAddr.hash) # !&
|
||||
# r.attenuation.toPreserve.hash)
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
# SPDX-FileCopyrightText: ☭ 2022 Emery Hemingway
|
||||
# SPDX-License-Identifier: Unlicense
|
||||
|
||||
import std/[hashes, options, tables]
|
||||
|
||||
from ./actors import Ref, hash
|
||||
from ./protocols/sturdy import Oid
|
||||
|
||||
proc hash(r: Ref): Hash = !$(r.relay.hash !& r.target.unsafeAddr.hash)
|
||||
|
||||
type
|
||||
Membrane* = object
|
||||
## Bidirectional mapping between `Oid` and `Ref` values.
|
||||
byOid: Table[Oid, WireSymbol]
|
||||
byRef: Table[Ref, WireSymbol]
|
||||
|
||||
WireSymbol* = ref object
|
||||
oid: Oid
|
||||
`ref`: Ref
|
||||
count: int
|
||||
when not defined(release):
|
||||
mem: Membrane
|
||||
|
||||
proc oid*(sym: WireSymbol): Oid = sym.oid
|
||||
proc `ref`*(sym: WireSymbol): Ref = sym.ref
|
||||
|
||||
proc grab*(mem: Membrane; key: Oid|Ref): WireSymbol =
|
||||
## Grab a `WireSymbol` from a `Membrane`.
|
||||
result =
|
||||
when key is Oid: mem.byOid.getOrDefault(key)
|
||||
elif key is Ref: mem.byRef.getOrDefault(key)
|
||||
else: {.error.}
|
||||
if not result.isNil: inc result.count
|
||||
|
||||
proc drop*(mem: var Membrane; sym: WireSymbol) =
|
||||
## Drop a `WireSymbol` from a `Membrane`.
|
||||
when not defined(release): assert sym.mem == mem
|
||||
assert sym.count > 0
|
||||
dec sym.count
|
||||
if sym.count < 1:
|
||||
mem.byOid.del sym.oid
|
||||
mem.byRef.del sym.`ref`
|
||||
|
||||
proc newWireSymbol*(mem: var Membrane; o: Oid; r: Ref): WireSymbol =
|
||||
## Allocate a `WireSymbol` at a `Membrane`.
|
||||
result = WireSymbol(oid: o, `ref`: r)
|
||||
when not defined(release): result.mem = mem
|
||||
mem.byOid[result.oid] = result
|
||||
mem.byRef[result.`ref`] = result
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
import std/[asyncdispatch, options, tables]
|
||||
import preserves, preserves/parse
|
||||
import ./actors, ./dataspaces, ./protocols/[protocol, sturdy]
|
||||
import ./actors, ./dataspaces, ./membranes, ./protocols/[protocol, sturdy]
|
||||
|
||||
when defined(traceSyndicate):
|
||||
template trace(args: varargs[untyped]): untyped = echo(args)
|
||||
|
@ -22,38 +22,6 @@ type
|
|||
|
||||
Turn = actors.Turn
|
||||
|
||||
WireSymbol = ref object
|
||||
oid: Oid
|
||||
`ref`: Ref
|
||||
membrane: Membrane
|
||||
count: int
|
||||
|
||||
Membrane = object
|
||||
byOid: Table[Oid, WireSymbol]
|
||||
byRef: Table[Ref, WireSymbol]
|
||||
|
||||
#[
|
||||
proc `$`(ws: WireSymbol): string =
|
||||
"<ws:" & $ws.oid & "/" & $ws.count & "/" & $ws.`ref` &>"
|
||||
]#
|
||||
|
||||
proc grab(mb: var Membrane; key: Oid|Ref; transient: bool; alloc: proc(): WireSymbol {.gcsafe.}): WireSymbol {.deprecated: "not idomatic Nim".} =
|
||||
when key is Oid:
|
||||
result = mb.byOid.getOrDefault(key)
|
||||
elif key is ref:
|
||||
result = mb.byRef.getOrDefault(key)
|
||||
if result.isNil:
|
||||
result = alloc()
|
||||
mb.byOid[result.oid] = result
|
||||
mb.byRef[result.`ref`] = result
|
||||
if not transient: inc result.count
|
||||
|
||||
proc drop(mb: var Membrane; ws: WireSymbol) =
|
||||
dec ws.count
|
||||
if ws.count < 1:
|
||||
mb.byOid.del ws.oid
|
||||
mb.byRef.del ws.`ref`
|
||||
|
||||
type
|
||||
PacketWriter = proc (bs: seq[byte]): Future[void] {.gcsafe.}
|
||||
RelaySetup = proc (turn: var Turn; relay: Relay) {.gcsafe.}
|
||||
|
@ -112,22 +80,21 @@ proc newSyncPeerEntity(r: Relay; p: Ref): SyncPeerEntity =
|
|||
syncPeerSync)
|
||||
|
||||
proc rewriteRefOut(relay: Relay; `ref`: Ref; transient: bool; exported: var seq[WireSymbol]): WireRef =
|
||||
#[
|
||||
if not relay.untrusted:
|
||||
if `ref`.target of RelayEntity and `ref`.target.RelayEntity.relay == relay:
|
||||
stderr.writeLine "do the rewriteRefOut that wasn't being done before"
|
||||
result = WireRef(
|
||||
orKind: WirerefKind.yours,
|
||||
yours: WireRefYours[Ref](
|
||||
oid: `ref`.target.oid,
|
||||
attenuation: `ref`.attenuation))
|
||||
]#
|
||||
let e = grab(relay.exported, `ref`, transient) do () -> WireSymbol:
|
||||
assert(not transient, "Cannot send transient reference")
|
||||
result = WireSymbol( oid: relay.nextLocalOid, `ref`: `ref`)
|
||||
inc relay.nextLocalOid
|
||||
exported.add e
|
||||
WireRef(
|
||||
orKind: WireRefKind.mine,
|
||||
mine: WireRefMine(oid: e.oid))
|
||||
yours: WireRefYours[Ref](oid: `ref`.target.oid))
|
||||
else:
|
||||
var ws = grab(relay.exported, `ref`)
|
||||
if ws.isNil:
|
||||
assert(not transient, "Cannot send transient reference")
|
||||
inc relay.nextLocalOid
|
||||
ws = newWireSymbol(relay.exported, relay.nextLocalOid, `ref`)
|
||||
exported.add ws
|
||||
result = WireRef(
|
||||
orKind: WireRefKind.mine,
|
||||
mine: WireRefMine(oid: ws.oid))
|
||||
|
||||
proc rewriteOut(relay: Relay; v: Assertion; transient: bool):
|
||||
tuple[rewritten: WireAssertion, exported: seq[WireSymbol]] =
|
||||
|
@ -210,8 +177,9 @@ using
|
|||
facet: Facet
|
||||
|
||||
proc lookupLocal(relay; oid: Oid): Ref =
|
||||
try: relay.exported.byOid[oid].`ref`
|
||||
except KeyError: newInertRef()
|
||||
let sym = relay.exported.grab oid
|
||||
if sym.isNil: newInertRef()
|
||||
else: sym.`ref`
|
||||
|
||||
proc isInert(r: Ref): bool =
|
||||
r.target.isNil
|
||||
|
@ -219,10 +187,11 @@ proc isInert(r: Ref): bool =
|
|||
proc rewriteRefIn(relay; facet; n: WireRef, imported: var seq[WireSymbol]): Ref =
|
||||
case n.orKind
|
||||
of WireRefKind.mine:
|
||||
let e = relay.imported.grab(n.mine.oid, false) do () -> WireSymbol:
|
||||
WireSymbol(
|
||||
oid: n.mine.oid,
|
||||
`ref`: newRef(facet, newRelayEntity("rewriteRefIn", relay, n.mine.oid)))
|
||||
var e = relay.imported.grab(n.mine.oid)
|
||||
if e.isNil: e = newWireSymbol(
|
||||
relay.imported,
|
||||
n.mine.oid,
|
||||
newRef(facet, newRelayEntity("rewriteRefIn", relay, n.mine.oid)))
|
||||
imported.add e
|
||||
result = e.`ref`
|
||||
of WireRefKind.yours:
|
Loading…
Reference in New Issue