Skip to content

Commit

Permalink
Add RoutePullFunc() and RoutePushFunc()
Browse files Browse the repository at this point in the history
  • Loading branch information
andeya committed Mar 2, 2018
1 parent 1308a7c commit 08b62b6
Show file tree
Hide file tree
Showing 6 changed files with 673 additions and 126 deletions.
91 changes: 74 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,43 +270,84 @@ var peer2 = tp.NewPeer(tp.PeerConfig{})
var sess, err = peer2.Dial("127.0.0.1:8080")
```

### 5.2 PullController Model Demo
### 5.2 Pull-Controller-Struct API template

```go
type Aaa struct {
tp.PullCtx
}
// XxZz register the route: /aaa/xx_zz
func (x *Aaa) XxZz(args *<T>) (<T>, *tp.Rerror) {
...
return r, nil
}
// YyZz register the route: /aaa/yy_zz
func (x *Aaa) YyZz(args *<T>) (<T>, *tp.Rerror) {
```

- register it to root router:

```go
// register the pull route: /aaa/xx_zz
peer.RoutePull(new(Aaa))

// or register the pull route: /xx_zz
peer.RoutePullFunc((*Aaa).XxZz)
```

### 5.3 Pull-Handler-Function API template

```go
func XxZz(ctx tp.PullCtx, args *<T>) (<T>, *tp.Rerror) {
...
return r, nil
}
```

### 5.3 PushController Model Demo
- register it to root router:

```go
// register the pull route: /xx_zz
peer.RoutePullFunc(XxZz)
```

### 5.4 Push-Controller-Struct API template

```go
type Bbb struct {
tp.PushCtx
}
// XxZz register the route: /bbb/yy_zz
func (b *Bbb) XxZz(args *<T>) *tp.Rerror {
func (b *Bbb) YyZz(args *<T>) *tp.Rerror {
...
return r, nil
return nil
}
// YyZz register the route: /bbb/yy_zz
func (b *Bbb) YyZz(args *<T>) *tp.Rerror {
```

- register it to root router:

```go
// register the push route: /bbb/yy_zz
peer.RoutePush(new(Bbb))

// or register the push route: /yy_zz
peer.RoutePushFunc((*Bbb).YyZz)
```

### 5.5 Push-Handler-Function API template

```go
// YyZz register the route: /yy_zz
func YyZz(ctx tp.PushCtx, args *<T>) *tp.Rerror {
...
return r, nil
return nil
}
```

### 5.4 UnknownPullHandler Type Demo
- register it to root router:

```go
// register the push route: /yy_zz
peer.RoutePushFunc(YyZz)
```

### 5.6 Unknown-Pull-Handler-Function API template

```go
func XxxUnknownPull (ctx tp.UnknownPullCtx) (interface{}, *tp.Rerror) {
Expand All @@ -315,7 +356,14 @@ func XxxUnknownPull (ctx tp.UnknownPullCtx) (interface{}, *tp.Rerror) {
}
```

### 5.5 UnknownPushHandler Type Demo
- register it to root router:

```go
// register the unknown pull route: /*
peer.SetUnknownPull(XxxUnknownPull)
```

### 5.7 Unknown-Push-Handler-Function API template

```go
func XxxUnknownPush(ctx tp.UnknownPushCtx) *tp.Rerror {
Expand All @@ -324,7 +372,14 @@ func XxxUnknownPush(ctx tp.UnknownPushCtx) *tp.Rerror {
}
```

### 5.6 Plugin Demo
- register it to root router:

```go
// register the unknown push route: /*
peer.SetUnknownPush(XxxUnknownPush)
```

### 5.8 Plugin Demo

```go
// NewIgnoreCase Returns a ignoreCase plugin.
Expand Down Expand Up @@ -356,19 +411,21 @@ func (i *ignoreCase) PostReadPushHeader(ctx tp.ReadCtx) *tp.Rerror {
}
```

### 5.7 Register above handler and plugin
### 5.9 Register above handler and plugin

```go
// add router group
group := peer.SubRoute("test")
// register to test group
group.RoutePull(new(Aaa), NewIgnoreCase())
peer.RoutePullFunc(XxZz, NewIgnoreCase())
group.RoutePush(new(Bbb))
peer.RoutePushFunc(YyZz)
peer.SetUnknownPull(XxxUnknownPull)
peer.SetUnknownPush(XxxUnknownPush)
```

### 5.8 Config
### 5.10 Config

```go
type PeerConfig struct {
Expand All @@ -385,7 +442,7 @@ type PeerConfig struct {
}
```

### 5.9 Optimize
### 5.11 Optimize

- SetPacketSizeLimit sets max packet size.
If maxSize<=0, set it to max uint32.
Expand Down
91 changes: 74 additions & 17 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,43 +286,84 @@ var sess, err = peer2.Dial("127.0.0.1:8080")
```


### 5.2 PullController模板示例
### 5.2 Pull-Controller-Struct 接口模板

```go
type Aaa struct {
tp.PullCtx
}
// XxZz register the route: /aaa/xx_zz
func (x *Aaa) XxZz(args *<T>) (<T>, *tp.Rerror) {
...
return r, nil
}
// YyZz register the route: /aaa/yy_zz
func (x *Aaa) YyZz(args *<T>) (<T>, *tp.Rerror) {
```

- 注册到根路由:

```go
// register the pull route: /aaa/xx_zz
peer.RoutePull(new(Aaa))

// or register the pull route: /xx_zz
peer.RoutePullFunc((*Aaa).XxZz)
```

### 5.3 Pull-Handler-Function 接口模板

```go
func XxZz(ctx tp.PullCtx, args *<T>) (<T>, *tp.Rerror) {
...
return r, nil
}
```

### 5.3 PushController模板示例
- 注册到根路由:

```go
// register the pull route: /xx_zz
peer.RoutePullFunc(XxZz)
```

### 5.4 Push-Controller-Struct 接口模板

```go
type Bbb struct {
tp.PushCtx
}
// XxZz register the route: /bbb/yy_zz
func (b *Bbb) XxZz(args *<T>) *tp.Rerror {
func (b *Bbb) YyZz(args *<T>) *tp.Rerror {
...
return r, nil
return nil
}
// YyZz register the route: /bbb/yy_zz
func (b *Bbb) YyZz(args *<T>) *tp.Rerror {
```

- 注册到根路由:

```go
// register the push route: /bbb/yy_zz
peer.RoutePush(new(Bbb))

// or register the push route: /yy_zz
peer.RoutePushFunc((*Bbb).YyZz)
```

### 5.5 Push-Handler-Function 接口模板

```go
// YyZz register the route: /yy_zz
func YyZz(ctx tp.PushCtx, args *<T>) *tp.Rerror {
...
return r, nil
return nil
}
```

### 5.4 UnknownPullHandler模板示例
- 注册到根路由:

```go
// register the push route: /yy_zz
peer.RoutePushFunc(YyZz)
```

### 5.6 Unknown-Pull-Handler-Function 接口模板

```go
func XxxUnknownPull (ctx tp.UnknownPullCtx) (interface{}, *tp.Rerror) {
Expand All @@ -331,7 +372,14 @@ func XxxUnknownPull (ctx tp.UnknownPullCtx) (interface{}, *tp.Rerror) {
}
```

### 5.5 UnknownPushHandler模板示例
- 注册到根路由:

```go
// register the unknown pull route: /*
peer.SetUnknownPull(XxxUnknownPull)
```

### 5.7 Unknown-Push-Handler-Function 接口模板

```go
func XxxUnknownPush(ctx tp.UnknownPushCtx) *tp.Rerror {
Expand All @@ -340,7 +388,14 @@ func XxxUnknownPush(ctx tp.UnknownPushCtx) *tp.Rerror {
}
```

### 5.6 插件示例
- 注册到根路由:

```go
// register the unknown push route: /*
peer.SetUnknownPush(XxxUnknownPush)
```

### 5.8 插件示例

```go
// NewIgnoreCase Returns a ignoreCase plugin.
Expand Down Expand Up @@ -372,19 +427,21 @@ func (i *ignoreCase) PostReadPushHeader(ctx tp.ReadCtx) *tp.Rerror {
}
```

### 5.7 注册以上操作和插件示例到路由
### 5.9 注册以上操作和插件示例到路由

```go
// add router group
group := peer.SubRoute("test")
// register to test group
group.RoutePull(new(Aaa), NewIgnoreCase())
peer.RoutePullFunc(XxZz, NewIgnoreCase())
group.RoutePush(new(Bbb))
peer.RoutePushFunc(YyZz)
peer.SetUnknownPull(XxxUnknownPull)
peer.SetUnknownPush(XxxUnknownPush)
```

### 5.8 配置信息
### 5.10 配置信息

```go
type PeerConfig struct {
Expand All @@ -401,7 +458,7 @@ type PeerConfig struct {
}
```

### 5.9 通信优化
### 5.11 通信优化

- SetPacketSizeLimit 设置包大小的上限,
如果 maxSize<=0,上限默认为最大 uint32
Expand Down
22 changes: 18 additions & 4 deletions peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,12 @@ type (
SubRoute(pathPrefix string, plugin ...Plugin) *SubRouter
// RoutePull registers PULL handler.
RoutePull(ctrlStruct interface{}, plugin ...Plugin)
// RoutePullFunc registers PULL handler.
RoutePullFunc(pullHandleFunc interface{}, plugin ...Plugin)
// RoutePush registers PUSH handler.
RoutePush(ctrlStruct interface{}, plugin ...Plugin)
// RoutePushFunc registers PUSH handler.
RoutePushFunc(pushHandleFunc interface{}, plugin ...Plugin)
// SetUnknownPull sets the default handler, which is called when no handler for PULL is found.
SetUnknownPull(fn func(UnknownPullCtx) (interface{}, *Rerror), plugin ...Plugin)
// SetUnknownPush sets the default handler, which is called when no handler for PUSH is found.
Expand Down Expand Up @@ -452,13 +456,23 @@ func (p *peer) SubRoute(pathPrefix string, plugin ...Plugin) *SubRouter {
}

// RoutePull registers PULL handler.
func (p *peer) RoutePull(ctrlStruct interface{}, plugin ...Plugin) {
p.router.RoutePull(ctrlStruct, plugin...)
func (p *peer) RoutePull(pullCtrlStruct interface{}, plugin ...Plugin) {
p.router.RoutePull(pullCtrlStruct, plugin...)
}

// RoutePullFunc registers PULL handler.
func (p *peer) RoutePullFunc(pullHandleFunc interface{}, plugin ...Plugin) {
p.router.RoutePullFunc(pullHandleFunc, plugin...)
}

// RoutePush registers PUSH handler.
func (p *peer) RoutePush(ctrlStruct interface{}, plugin ...Plugin) {
p.router.RoutePush(ctrlStruct, plugin...)
func (p *peer) RoutePush(pushCtrlStruct interface{}, plugin ...Plugin) {
p.router.RoutePush(pushCtrlStruct, plugin...)
}

// RoutePushFunc registers PUSH handler.
func (p *peer) RoutePushFunc(pushHandleFunc interface{}, plugin ...Plugin) {
p.router.RoutePushFunc(pushHandleFunc, plugin...)
}

// SetUnknownPull sets the default handler,
Expand Down
Loading

0 comments on commit 08b62b6

Please sign in to comment.