Skip to content

Commit

Permalink
feat(SwingSet)!: fill out addresses in attempt.accept
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Feb 4, 2022
1 parent 4256c54 commit bc69f80
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 36 deletions.
73 changes: 40 additions & 33 deletions packages/SwingSet/src/vats/network/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const rethrowUnlessMissing = err => {
) {
throw err;
}
return false;
return undefined;
};

/**
Expand Down Expand Up @@ -149,14 +149,10 @@ export function crossoverConnection(
if (closed) {
throw closed;
}
const ack =
/** @type {Bytes} */
(
await E(handlers[r])
.onReceive(conns[r], toBytes(packetBytes), handlers[r])
.catch(rethrowUnlessMissing)
);
return toBytes(ack);
const ack = await E(handlers[r])
.onReceive(conns[r], toBytes(packetBytes), handlers[r])
.catch(rethrowUnlessMissing);
return toBytes(ack || '');
},
async close() {
if (closed) {
Expand Down Expand Up @@ -419,7 +415,11 @@ export function makeNetworkProtocol(protocolHandler) {
.onReject(port, localAddr, remoteAddr, listener)
.catch(rethrowUnlessMissing);
},
async accept(rchandler) {
async accept({
localAddress = localAddr,
remoteAddress = remoteAddr,
handler: rchandler,
}) {
if (consummated) {
throw consummated;
}
Expand All @@ -435,9 +435,9 @@ export function makeNetworkProtocol(protocolHandler) {

return crossoverConnection(
lchandler,
localAddr,
localAddress,
rchandler,
remoteAddr,
remoteAddress,
current,
)[1];
},
Expand All @@ -453,31 +453,38 @@ export function makeNetworkProtocol(protocolHandler) {
(await E(port).getLocalAddress());

// Allocate a local address.
const localInstance = await E(protocolHandler)
const initialLocalInstance = await E(protocolHandler)
.onInstantiate(port, localAddr, remoteAddr, protocolHandler)
.catch(rethrowUnlessMissing);
const la = localInstance ? `${localAddr}/${localInstance}` : localAddr;
const initialLocalAddr = initialLocalInstance
? `${localAddr}/${initialLocalInstance}`
: localAddr;

let lastFailure;
try {
// Attempt the loopback connection.
const attempt = await protocolImpl.inbound(remoteAddr, la);
return attempt.accept(lchandler);
const attempt = await protocolImpl.inbound(
remoteAddr,
initialLocalAddr,
);
return attempt.accept({ handler: lchandler });
} catch (e) {
lastFailure = e;
}

const [connectedAddress, rchandler] =
/** @type {[Endpoint, ConnectionHandler]} */
(
await E(protocolHandler).onConnect(
port,
la,
remoteAddr,
lchandler,
protocolHandler,
)
);
const {
remoteAddress = remoteAddr,
handler: rchandler,
localAddress = localAddr,
} =
/** @type {Partial<AttemptDescription>} */
(await E(protocolHandler).onConnect(
port,
initialLocalAddr,
remoteAddr,
lchandler,
protocolHandler,
));

if (!rchandler) {
throw lastFailure;
Expand All @@ -486,9 +493,9 @@ export function makeNetworkProtocol(protocolHandler) {
const current = currentConnections.get(port);
return crossoverConnection(
lchandler,
la,
localAddress,
rchandler,
connectedAddress,
remoteAddress,
current,
)[0];
},
Expand Down Expand Up @@ -571,10 +578,10 @@ export function makeLoopbackProtocolHandler(
const remoteInstance = await E(protocolHandler)
.onInstantiate(lport, remoteAddr, localAddr, protocolHandler)
.catch(rethrowUnlessMissing);
const ra = remoteInstance
? `${remoteAddr}/${remoteInstance}`
: remoteAddr;
return [ra, rchandler];
return {
remoteInstance,
handler: rchandler,
};
},
onInstantiate,
async onListen(port, localAddr, listenHandler, _protocolHandler) {
Expand Down
11 changes: 9 additions & 2 deletions packages/SwingSet/src/vats/network/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@
* @typedef {any?} CloseReason The reason a connection was closed
*/

/**
* @typedef {Object} AttemptDescription
* @property {ConnectionHandler} handler
* @property {Endpoint} [remoteAddress]
* @property {Endpoint} [localAddress]
*/

/**
* @typedef {Object} ProtocolHandler A handler for things the protocol implementation will invoke
* @property {(protocol: ProtocolImpl, p: ProtocolHandler) => Promise<void>} onCreate This protocol is created
Expand All @@ -64,11 +71,11 @@
* @property {(port: Port, localAddr: Endpoint, listenHandler: ListenHandler, p: ProtocolHandler) => Promise<void>} onListenRemove A port listener has been reset
* @property {(port: Port, localAddr: Endpoint, remote: Endpoint, p: ProtocolHandler) => Promise<Endpoint>} [onInstantiate] Return unique suffix for
* local address
* @property {(port: Port, localAddr: Endpoint, remote: Endpoint, c: ConnectionHandler, p: ProtocolHandler) => Promise<[Endpoint, ConnectionHandler]>} onConnect A port initiates an outbound connection
* @property {(port: Port, localAddr: Endpoint, remote: Endpoint, c: ConnectionHandler, p: ProtocolHandler) => Promise<AttemptDescription>} onConnect A port initiates an outbound connection
* @property {(port: Port, localAddr: Endpoint, p: ProtocolHandler) => Promise<void>} onRevoke The port is being completely destroyed
*
* @typedef {Object} InboundAttempt An inbound connection attempt
* @property {(connectionHandler: ConnectionHandler) => Promise<Connection>} accept Establish the connection
* @property {(desc: AttemptDescription) => Promise<Connection>} accept Establish the connection
* @property {() => Endpoint} getLocalAddress Return the local address for this attempt
* @property {() => Endpoint} getRemoteAddress Return the remote address for this attempt
* @property {() => Promise<void>} close Abort the attempt
Expand Down
2 changes: 1 addition & 1 deletion packages/SwingSet/test/test-network.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const makeProtocolHandler = t => {
.onAccept(lp, localAddr, remoteAddr, l)
.then(ch => [localAddr, ch]);
}
return [remoteAddr, makeEchoConnectionHandler()];
return { handler: makeEchoConnectionHandler() };
},
async onListen(port, localAddr, listenHandler) {
t.assert(port, `port is tracked in onListen`);
Expand Down

0 comments on commit bc69f80

Please sign in to comment.