Skip to content

Commit

Permalink
add response method
Browse files Browse the repository at this point in the history
  • Loading branch information
sandyskies committed Nov 5, 2018
1 parent 1dd5ccc commit cf6471b
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 14 deletions.
7 changes: 5 additions & 2 deletions tars/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ var serList []string
var objRunList []string
var TLOG = rogger.GetLogger("TLOG")
var initOnce sync.Once
var initReportOnce sync.Once
var initProReportOnce sync.Once

type adminFn func(string) (string, error)

Expand All @@ -39,13 +41,16 @@ func init() {
adminMethods = make(map[string]adminFn)
//在配置初始化前将日志关闭,减少独立客户端日志
rogger.SetLevel(rogger.ERROR)
Init()
}

//有些场景下需要提前初始化时调用:
//比如Imp里调用了通信器、
//AddServant时使用了全局配置等等。
func Init() {
initOnce.Do(initConfig)
initReportOnce.Do(initReport)
initProReportOnce.Do(initProReport)
}

//服务的初始化函数,默认会在Run的时候启动
Expand Down Expand Up @@ -153,8 +158,6 @@ func initConfig() {

tarsConfig["AdminObj"] = adminCfg
svrCfg.Adapters["AdminAdapter"] = adapterConfig{localpoint, "tcp", "AdminObj", 1}
initReport()
initProReport()
}

func Run() {
Expand Down
2 changes: 0 additions & 2 deletions tars/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ var cltCfg *clientConfig

// GetServerConfig : Get server config
func GetServerConfig() *serverConfig {
Init() //引用配置前先初始化应用
return svrCfg
}

// GetClientConfig : Get client config
func GetClientConfig() *clientConfig {
Init() //引用配置前先初始化应用
return cltCfg
}

Expand Down
8 changes: 6 additions & 2 deletions tars/propertyf.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package tars

import (
"github.com/TarsCloud/TarsGo/tars/protocol/res/propertyf"
"github.com/TarsCloud/TarsGo/tars/util/tools"
"reflect"
"sort"
"strconv"
"strings"
"sync"
"time"

"github.com/TarsCloud/TarsGo/tars/protocol/res/propertyf"
"github.com/TarsCloud/TarsGo/tars/util/tools"
)

type ReportMethod interface {
Expand Down Expand Up @@ -344,6 +345,9 @@ func (p *PropertyReportHelper) Init(comm *Communicator, node string) {
}

func initProReport() {
if GetClientConfig() == nil {
return
}
comm := NewCommunicator()
comm.SetProperty("netthread", 1)
ProHelper = new(PropertyReportHelper)
Expand Down
3 changes: 3 additions & 0 deletions tars/statf.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ func (s *StatFHelper) ReportMicMsg(stStatInfo StatInfo, fromServer bool) {
var StatReport *StatFHelper

func initReport() {
if GetClientConfig() == nil {
return
}
comm := NewCommunicator()
comm.SetProperty("netthread", 1)
StatReport = new(StatFHelper)
Expand Down
40 changes: 34 additions & 6 deletions tars/tools/tars2go/gen_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,12 @@ err = _obj.s.Tars_invoke(ctx, 0, "` + fun.NameStr + `", _os.ToBytes(), _status,
} else {
c.WriteString(`var _status map[string]string
var _context map[string]string
if len(_opt) == 1{
_context =_opt[0]
}else if len(_opt) == 2 {
_context = _opt[0]
_status = _opt[1]
}
_resp := new(requestf.ResponsePacket)
err = _obj.s.Tars_invoke(ctx, 0, "` + fun.NameStr + `", _os.ToBytes(), _status, _context, _resp)
` + errStr + `
Expand Down Expand Up @@ -913,6 +919,28 @@ err = _obj.s.Tars_invoke(ctx, 0, "` + fun.NameStr + `", _os.ToBytes(), _status,
}

c.WriteString(`
if len(_opt) == 1{
for k, _ := range(_context){
delete(_context, k)
}
for k, v := range(_resp.Context){
_context[k] = v
}
}else if len(_opt) == 2 {
for k, _ := range(_context){
delete(_context, k)
}
for k, v := range(_resp.Context){
_context[k] = v
}
for k, _ := range(_status){
delete(_status, k)
}
for k, v := range(_resp.Status){
_status[k] = v
}
}
_ = length
_ = have
_ = ty
Expand Down Expand Up @@ -1016,15 +1044,15 @@ switch req.SFuncName {
default:
return fmt.Errorf("func mismatch")
}
var status map[string]string
var _status map[string]string
s, ok := current.GetResponseStatus(ctx)
if ok && s != nil {
status = s
_status = s
}
var context map[string]string
var _context map[string]string
c, ok := current.GetResponseContext(ctx)
if ok && c != nil {
context = c
_context = c
}
*resp = requestf.ResponsePacket{
IVersion: 1,
Expand All @@ -1033,9 +1061,9 @@ if ok && c != nil {
IMessageType: 0,
IRet: 0,
SBuffer: tools.ByteToInt8(_os.ToBytes()),
Status: status,
Status: _status,
SResultDesc: "",
Context: context,
Context: _context,
}
_ = length
_ = have
Expand Down
3 changes: 1 addition & 2 deletions tars/util/current/tarscurrent.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package current

import "context"

type tarsCurrentKey int64

var tcKey = tarsCurrentKey(0x484900)

//Current contains message for the specify request.
//Current contains message for the specify request. This current is used for server side.
type Current struct {
clientIP string
clientPort string
Expand Down

0 comments on commit cf6471b

Please sign in to comment.