Last active
June 28, 2017 10:06
-
-
Save htoooth/4e97d1bfa65004468ead73850ce4f4ad to your computer and use it in GitHub Desktop.
proxy
This file contains hidden or 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
| /** | |
| * Created by TaoHuang on 2017/6/28. | |
| */ | |
| const http = { | |
| websocket: {} | |
| }; | |
| function readPack(con) { | |
| return con.read(); | |
| } | |
| function writePack(con, data) { | |
| return con.write(data); | |
| } | |
| function lock(fn) { | |
| fn(); | |
| } | |
| function spawn(fn) { | |
| process.nextTick(fn); | |
| } | |
| const Channel = { | |
| new() { | |
| return []; | |
| } | |
| }; | |
| class Proxy { | |
| constructor() { | |
| this.clients = []; | |
| this.server = null; | |
| this.cmdChannel = null; | |
| } | |
| init(channel) { | |
| this.cmdChannel = channel; | |
| this.server = {}; | |
| } | |
| run() { | |
| for (; ;) { | |
| const client = this.server.accept(); | |
| const resultChannel = Channel.new(); | |
| this.clients.push(client); | |
| client.on('error', () => { | |
| this.clients.splice(client); | |
| }); | |
| spawn(() => { | |
| this.handle(client, resultChannel); | |
| }); | |
| } | |
| } | |
| handle(client, resultChannel) { | |
| for (; ;) { | |
| const data = readPack(client); | |
| data.resultChannel = resultChannel; | |
| lock(() => { | |
| this.cmdChannel.push(data); | |
| }); | |
| } | |
| } | |
| monitor() { | |
| return {}; | |
| } | |
| } | |
| class Upstream { | |
| constructor() { | |
| this.comingReqChannel = null; | |
| this.outgoingResponseChannel = null; | |
| this.server = {}; | |
| } | |
| init(inChannel, outChannel) { | |
| this.comingReqChannel = inChannel; | |
| this.outgoingResponseChannel = outChannel; | |
| } | |
| run() { | |
| for (;;) { | |
| const cmd = this.comingReqChannel.unshift(); | |
| writePack(this.server, cmd.data); | |
| const result = readPack(this.server); | |
| this.outgoingResponseChannel.push(result); | |
| } | |
| } | |
| monitor() { | |
| return {}; | |
| } | |
| } | |
| class Dispatcher { | |
| constructor() { | |
| this.channel = null; | |
| } | |
| init(channel) { | |
| this.channel = channel; | |
| } | |
| run() { | |
| for (;;) { | |
| const result = this.channel.unshift(); | |
| result.outgoingResponseChannel.push(result); | |
| } | |
| } | |
| monitor() { | |
| return {}; | |
| } | |
| } | |
| const POLL_SECONDS = 1800; | |
| class Monitor { | |
| constructor() { | |
| this.proxy = null; | |
| this.upstream = null; | |
| this.dispatcher = null; | |
| } | |
| init(proxy, upstream, dispatcher) { | |
| this.proxy = proxy; | |
| this.upstream = upstream; | |
| this.dispatcher = dispatcher; | |
| } | |
| run() { | |
| http.get('/', (req, res) => { | |
| res.render('index.html'); | |
| }); | |
| http.websocket.get('/', (req, res) => { | |
| setTimeout(() => { | |
| res.ws.send('data', { | |
| proxy: this.proxy.monitor(), | |
| upstream: this.upstream.monitor(), | |
| dispatcher: this.dispatcher.monitor() | |
| }); | |
| }, POLL_SECONDS); | |
| }); | |
| } | |
| } | |
| function main() { | |
| const centerCmdChannel = Channel.new(); | |
| const centerResultChannel = Channel.new(); | |
| const upstream = new Upstream(); | |
| upstream.init(centerCmdChannel, centerResultChannel); | |
| const proxy = new Proxy(); | |
| proxy.init(centerCmdChannel); | |
| const dispatcher = new Dispatcher(); | |
| dispatcher.init(centerResultChannel); | |
| const monitor = new Monitor(); | |
| monitor.init(proxy, upstream, dispatcher); | |
| spawn(() => { | |
| upstream.run(); | |
| }).then(() => { | |
| proxy.run(); | |
| }).then(() => { | |
| dispatcher.run(); | |
| }).then(() => { | |
| monitor.run(); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment