Last active
September 21, 2018 04:28
-
-
Save aikoven/0f088be07db32598c6389adedbdfa75f to your computer and use it in GitHub Desktop.
Ice server with Node.js
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
import signal | |
import sys | |
import os | |
import subprocess | |
import Ice | |
class Forwarder(Ice.BlobjectAsync): | |
""" | |
Forwards incoming requests to Node.js server | |
""" | |
def __init__(self): | |
self.connection = None | |
def ice_invoke_async(self, cb, in_params_bytes, current): | |
if self.connection is None: | |
raise Ice.ObjectNotExistException(current.id, current.facet, | |
current.operation) | |
self.connection.createProxy(current.id).begin_ice_invoke( | |
current.operation, current.mode, in_params_bytes, | |
_response=cb.ice_response, | |
_ex=cb.ice_exception, | |
_ctx=current.ctx, | |
) | |
class Backwarder(Ice.BlobjectAsync): | |
""" | |
Forwards outgoing requests from Node.js server | |
""" | |
def __init__(self): | |
self._proxies = {} # todo: cleanup proxies | |
def add_proxies(self, proxies): | |
for proxy in proxies: | |
self._proxies[proxy.ice_getIdentity()] = proxy | |
def ice_invoke_async(self, cb, in_params_bytes, current): | |
try: | |
proxy = self._proxies[current.id] | |
except KeyError: | |
raise Ice.ObjectNotExistException(current.id, current.facet, | |
current.operation) | |
proxy.begin_ice_invoke( | |
current.operation, current.mode, in_params_bytes, | |
_response=cb.ice_response, | |
_ex=cb.ice_exception, | |
_ctx=current.ctx, | |
) | |
class Router(Ice.Router): | |
def __init__(self, public_adapter, internal_adapter): | |
self.server_proxy = public_adapter.createProxy(Ice.Identity( | |
name='dummy', | |
category=Ice.generateUUID(), | |
)) | |
self.forwarder = Forwarder() | |
self.backwarder = Backwarder() | |
self.public_adapter = public_adapter | |
public_adapter.addDefaultServant(self.forwarder, '') | |
internal_adapter.addDefaultServant(self.backwarder, '') | |
def getServerProxy(self, current): | |
self.forwarder.connection = current.con | |
return self.server_proxy | |
def getClientProxy(self, current): | |
return (None, False) | |
def addProxies(self, proxies, current): | |
self.backwarder.add_proxies(proxies) | |
class Application(Ice.Application): | |
def run(self, args): | |
self.shutdownOnInterrupt() | |
communicator = self.communicator() | |
public_adapter = communicator.createObjectAdapter('Router.Public') | |
internal_adapter = communicator.createObjectAdapter('Router.Internal') | |
router_proxy = internal_adapter.add( | |
Router(public_adapter, internal_adapter), | |
communicator.stringToIdentity('SimpleRouter/router'), | |
) | |
internal_adapter.activate() | |
public_adapter.activate() | |
communicator.waitForShutdown() | |
return 0 | |
def main(): | |
properties = Ice.createProperties() | |
# endpoints where the outside world connects to | |
properties.setProperty('Router.Public.Endpoints', 'tcp -h 0.0.0.0 -p 20000') | |
# endpoints where Node.js server connects to | |
properties.setProperty('Router.Internal.Endpoints', 'tcp -h localhost -p 10000') | |
properties.setProperty('Router.Internal.ACM.Close', '0') # CloseOff | |
# read command line | |
args = sys.argv | |
args = properties.parseIceCommandLineOptions(args) | |
args = properties.parseCommandLineOptions('Router', args) | |
init_data = Ice.InitializationData() | |
init_data.properties = properties | |
sys.exit(Application().main(args, None, init_data)) | |
if __name__ == '__main__': | |
main() |
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
import {Ice} from 'ice'; | |
async function main() { | |
const initData = new Ice.InitializationData(); | |
initData.properties = properties; | |
properties.setProperty('Ice.Default.Router', 'SimpleRouter/router:tcp -h localhost -p 10000'); | |
const communicator = Ice.initialize(initData); | |
const routerPrx = communicator.getDefaultRouter(); | |
const adapter = await communicator.createObjectAdapterWithRouter('', routerPrx); | |
// you can now add servants to adapter which will be accessible by public router endpoints | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment