-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb8607d
commit b3e1e61
Showing
5 changed files
with
182 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,96 @@ | ||
// Avoid importing the full captp bundle, which would carry | ||
// in its own makeHardener, etc. | ||
import { makeCapTP } from '@agoric/captp/lib/captp'; | ||
import { E } from '@agoric/eventual-send'; | ||
import { makePromiseKit } from '@agoric/promise-kit'; | ||
|
||
export const getCapTPHandler = (send, getBootstrapObject) => { | ||
export const getCapTPHandler = ( | ||
send, | ||
getAdminAndConnectionFacets, | ||
fallback = undefined, | ||
) => { | ||
const chans = new Map(); | ||
const doCall = (obj, method, ...args) => { | ||
if (obj) { | ||
return E(obj) | ||
[method](...args) | ||
.catch(_ => {}); | ||
} | ||
return undefined; | ||
}; | ||
const handler = harden({ | ||
onOpen(_obj, meta) { | ||
onOpen(obj, meta) { | ||
const { channelHandle, origin = 'unknown' } = meta || {}; | ||
console.debug(`Starting CapTP`, meta); | ||
const sendObj = o => { | ||
send(o, [channelHandle]); | ||
}; | ||
const { dispatch, abort } = makeCapTP( | ||
const adminFacetPK = makePromiseKit(); | ||
const connectionFacetPK = makePromiseKit(); | ||
let booted = false; | ||
const { dispatch, abort, getBootstrap } = makeCapTP( | ||
origin, | ||
sendObj, | ||
getBootstrapObject, | ||
async () => { | ||
if (!booted) { | ||
booted = true; | ||
const admconnP = getAdminAndConnectionFacets(meta, getBootstrap()); | ||
adminFacetPK.resolve(E.G(admconnP).adminFacet); | ||
connectionFacetPK.resolve(E.G(admconnP).connectionFacet); | ||
} | ||
return connectionFacetPK.promise; | ||
}, | ||
{ | ||
onReject(err) { | ||
// Be quieter for sanity's sake. | ||
console.log('CapTP', origin, 'exception:', err); | ||
}, | ||
}, | ||
); | ||
chans.set(channelHandle, [dispatch, abort]); | ||
chans.set(channelHandle, { | ||
dispatch, | ||
abort, | ||
adminFacet: adminFacetPK.promise, | ||
}); | ||
doCall(fallback, 'onOpen', obj, meta); | ||
}, | ||
onClose(_obj, meta) { | ||
onClose(obj, meta) { | ||
console.debug(`Finishing CapTP`, meta); | ||
const dispatchAbort = chans.get(meta.channelHandle); | ||
if (dispatchAbort) { | ||
(1, dispatchAbort[1])(); | ||
const chan = chans.get(meta.channelHandle); | ||
if (chan) { | ||
const { abort, adminFacet } = chan; | ||
doCall(adminFacet, 'onClose'); | ||
abort(); | ||
} | ||
chans.delete(meta.channelHandle); | ||
doCall(fallback, 'onClose', obj, meta); | ||
}, | ||
onError(obj, meta) { | ||
console.debug(`Error in CapTP`, meta, obj.error); | ||
const chan = chans.get(meta.channelHandle); | ||
if (chan) { | ||
const { abort, adminFacet } = chan; | ||
doCall(adminFacet, 'onError', obj.error); | ||
abort(obj.error); | ||
} | ||
doCall(fallback, 'onError', obj, meta); | ||
}, | ||
onMessage(obj, meta) { | ||
async onMessage(obj, meta) { | ||
console.debug('processing inbound', obj); | ||
const dispatchAbort = chans.get(meta.channelHandle); | ||
if (!dispatchAbort || !(1, dispatchAbort[0])(obj)) { | ||
const chan = chans.get(meta.channelHandle); | ||
if (chan) { | ||
const { dispatch } = chan; | ||
if (dispatch(obj)) { | ||
return true; | ||
} | ||
} | ||
const done = await doCall(fallback, 'onMessage', obj, meta); | ||
if (!done) { | ||
console.error(`Could not find CapTP handler ${obj.type}`, meta); | ||
return false; | ||
} | ||
return true; | ||
return done; | ||
}, | ||
}); | ||
|
||
return harden({ | ||
getCommandHandler() { | ||
return handler; | ||
}, | ||
}); | ||
return harden(handler); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters