Skip to content

Commit

Permalink
Merge pull request apache#325 from godkun/dubbo3
Browse files Browse the repository at this point in the history
feat: 调整dubbo transport
  • Loading branch information
hufeng authored Dec 6, 2022
2 parents 1abd8a8 + c63cc72 commit b010f70
Show file tree
Hide file tree
Showing 7 changed files with 302 additions and 38 deletions.
20 changes: 20 additions & 0 deletions packages/dubbo-transport/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "apache-dubbo3-transport",
"version": "0.0.1",
"main": "./lib/index.js",
"types": "./lib/typings/index.d.ts",
"description": "apache dubbo3 transport",
"keywords": [
"dubbo3",
"nodejs",
"dubbo3-transport"
],
"license": "Apache-2.0",
"dependencies": {
"debug": "^4.3.4",
"lodash": "^4.17.21"
},
"devDependencies": {
"@types/lodash": "^4.14.189"
}
}
35 changes: 35 additions & 0 deletions packages/dubbo-transport/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { DubboClientTransport, DubboServerTransport } from '../index'

describe(`dubbo-transport`, () => {
it('test dubbo-transport', () => {
// 创建服务端 transport 实例
const server = new DubboServerTransport({
url: 'http://localhost:3600',
port: 3600
})
expect(server.port).toBe(3600)
// 创建客户端 transport 实例
const client = new DubboClientTransport({
url: 'http://localhost:3600',
port: 3600
})
expect(client.url).toBe('http://localhost:3600')
})
})
57 changes: 57 additions & 0 deletions packages/dubbo-transport/src/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { debug } from 'debug'
import http2 from 'node:http2'
import { IDubboClientTransport, DubboContext } from './transport'

// init log
const log = debug('dubbo3:transport:client')

export class DubboClientTransport implements IDubboClientTransport {
// transport 实例
private transport: any
private ctx: DubboContext

constructor(opts: DubboContext) {
this.ctx = opts
this.connect()
}

get url() {
return this.ctx.url
}

/**
* 建立连接
*/
connect() {
this.transport = http2.connect(this.url)

this.transport.once('connect', () => {
log('has connected')
})
}

/**
* 发送消息
* @param msg
*/
async send(msg: DubboContext): Promise<void> {
this.transport.request(msg)
}
}
21 changes: 21 additions & 0 deletions packages/dubbo-transport/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { DubboClientTransport } from './client'
import { DubboServerTransport } from './server'

export { DubboClientTransport, DubboServerTransport }
70 changes: 70 additions & 0 deletions packages/dubbo-transport/src/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import debug from 'debug'
import EventEmitter from 'node:events'
import http2 from 'node:http2'
import { IDubboServerTransport, DubboContext } from './transport'

// init log
const log = debug('dubbo3:transport:client')

export class DubboServerTransport
extends EventEmitter
implements IDubboServerTransport
{
private ctx: DubboContext
transport: any

constructor(opts: DubboContext) {
super()
this.ctx = opts
this.transport = this.start()
}

get url() {
return this.ctx.url
}

get port() {
return this.ctx.port
}

/**
* 启动服务端 transport
* @returns
*/
start() {
const server = http2.createServer()
server.on('stream', (stream, headers) => {
log(stream)
stream.on('data', (data) => {
log(data)
// TODO:
})
stream.on('end', () => {
log('end...')
// TODO: 通知 client
})
stream.on('error', (error) => {
log(error)
})
})
server.listen(this.port)
return server
}
}
30 changes: 30 additions & 0 deletions packages/dubbo-transport/src/transport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export interface DubboContext {
url: string
body?: Object | null
port: number
}

export interface IDubboClientTransport {
send(msg: DubboContext): Promise<any>
}

export interface IDubboServerTransport {
// start(msg: DubboContext): Promise<any>
}
Loading

0 comments on commit b010f70

Please sign in to comment.