Пример #1
0
// NewCodeGen .
func NewCodeGen(rootpath string, skips []string) (gslang.Visitor, error) {

	codeGen := &_CodeGen{
		Log:      gslogger.Get("gen4go"),
		rootpath: rootpath,
		prefix:   make(map[string]string),
	}

	for _, skip := range skips {
		exp, err := regexp.Compile(skip)

		if err != nil {
			return nil, gserrors.Newf(err, "invalid skip regex string :%s", skip)
		}

		codeGen.skips = append(codeGen.skips, exp)
	}

	funcs := template.FuncMap{
		"title":           codeGen.title,
		"title2":          strings.Title,
		"enumFields":      codeGen.enumFields,
		"typeName":        codeGen.typeName,
		"enumRead":        codeGen.enumRead,
		"enumWrite":       codeGen.enumWrite,
		"fieldDecl":       codeGen.fieldDecl,
		"defaultVal":      codeGen.defaultVal,
		"marshalField":    codeGen.marshalField,
		"unmarshalField":  codeGen.unmarshalField,
		"methodDecl":      codeGen.methodDecl,
		"rpcMethodDecl":   codeGen.rpcMethodDecl,
		"unmarshalParam":  codeGen.unmarshalParam,
		"marshalParam":    codeGen.marshalParam,
		"methodCall":      codeGen.methodCall,
		"marshalReturn":   codeGen.marshalReturn,
		"unmarshalReturn": codeGen.unmarshalReturn,
		"notVoid":         gslang.NotVoid,
		"isPOD":           gslang.IsPOD,
		"isAsync":         gslang.IsAsync,
		"isException":     gslang.IsException,
		"enumSize":        gslang.EnumSize,
		"enumType": func(typeDecl ast.Type) string {
			return builtin[gslang.EnumType(typeDecl)]
		},
		"builtin":       gslang.IsBuiltin,
		"marshalParams": codeGen.marshalParams,
		"callback":      codeGen.callback,
		"tagValue":      codeGen.tagValue,
	}

	tpl, err := template.New("t4objc").Funcs(funcs).Parse(t4objc)

	if err != nil {
		return nil, err
	}

	codeGen.tpl = tpl

	return codeGen, nil
}
Пример #2
0
func newContext(name string, handler Handler, pipeline *_Pipeline, prev *_Context) (*_Context, error) {
	context := &_Context{
		Log:      gslogger.Get("handler-context"),
		name:     name,
		handler:  handler,
		pipeline: pipeline,
		prev:     prev,
		state:    handlerUnregister,
	}

	context.shared, _ = handler.(SharedHandler)

	err := context.handler.Register(context)

	if err != nil {
		return nil, err
	}

	if context.prev != nil {
		context.prev.next = context
	}

	context.state = handlerRegister

	return context, nil

}
Пример #3
0
// Open create new tsdb with root directory
func Open(filepath string) (DataSource, error) {

	if !fs.Exists(filepath) {
		err := os.MkdirAll(filepath, 0644)
		if err != nil {
			return nil, err
		}
	}

	idgen, err := newSEQIDGen(filepath)

	if err != nil {
		return nil, err
	}

	persistence, err := newDB(filepath)

	if err != nil {
		return nil, err
	}

	return &_DataSource{
		Log:         gslogger.Get("tsdb"),
		SEQIDGen:    idgen,
		Persistence: persistence,
		cached:      make(map[string]*_Cached),
		cachedsize:  gsconfig.Int("tsdb.cached.size", 1024),
	}, nil
}
Пример #4
0
func (proxy *_Proxy) newClientHandler() gorpc.Handler {

	return &_Client{
		Log:     gslogger.Get("gsproxy-client"),
		context: proxy,
	}
}
Пример #5
0
// Visit visit ast tree
func (compiler *Compiler) Visit(codeGen Visitor) (err error) {

	defer func() {
		if e := recover(); e != nil {

			gserr, ok := e.(gserrors.GSError)

			if ok {
				err = gserr
			} else {
				err = gserrors.Newf(e.(error), "catch unknown error")
			}
		}
	}()

	gen := &_Visitor{
		Log:      gslogger.Get("codegen"),
		codeGen:  codeGen,
		module:   compiler.module,
		compiler: compiler,
	}

	gen.visit()

	return
}
Пример #6
0
// Build create new Pipeline
func (builder *PipelineBuilder) Build(name string) (Pipeline, error) {

	pipeline := &_Pipeline{
		Log:        gslogger.Get("pipeline"),
		name:       name,
		timewheel:  builder.timewheel,
		sendcached: make(chan func() (*Message, error), builder.cachedsize),
		closedflag: make(chan bool),
	}

	close(pipeline.closedflag)

	pipeline.Sink = NewSink(name, pipeline, pipeline.TimeWheel(), builder.timeout)

	var err error

	for i, f := range builder.handlers {
		pipeline.tail, err = newContext(builder.names[i], f(), pipeline, pipeline.tail)

		if pipeline.header == nil {
			pipeline.header = pipeline.tail
		}

		if err != nil {
			return nil, err
		}
	}

	return pipeline, err
}
Пример #7
0
// NewHeartbeatHandler create new heartbeat handler
func NewHeartbeatHandler(timeout time.Duration) gorpc.Handler {
	return &_HeartbeatHandler{
		Log:       gslogger.Get("heartbeat"),
		timeout:   timeout,
		timestamp: time.Now(),
	}
}
Пример #8
0
// New create new gsdocker runner
func New(name string) Runner {
	runner := &_Runner{
		Log:              gslogger.Get(name),
		flagString:       make(map[string]*string),
		flagInt:          make(map[string]*int64),
		flagUint:         make(map[string]*uint64),
		flagFloat32:      make(map[string]*float64),
		flagFloat64:      make(map[string]*float64),
		flagSecond:       make(map[string]*uint64),
		flagMilliseconds: make(map[string]*uint64),
		fullname:         make(map[string]string),
	}

	runner.FlagString(
		"log", "gsrunner.log", "", "the gsrunner log root path",
	).FlagString(
		"level", "gsrunner.log.level", "", "the gsrunner log level",
	).FlagString(
		"pprof", "gsrunner.pprof", "", "set gsrunner pprof listen address",
	).FlagString(
		"config", "gsrunner.config", "", "set gsrunner config file",
	).FlagString(
		"registry", "gsrunner.registry", "", "set the rpc services registry file",
	)

	return runner
}
Пример #9
0
func newEval(errorHandler ErrorHandler, module *ast.Module) Eval {
	return &_Eval{
		Log:          gslogger.Get("eval"),
		module:       module,
		errorHandler: errorHandler,
	}
}
Пример #10
0
Файл: dh.go Проект: gsrpc/gorpc
// NewCryptoClient .
func NewCryptoClient(device *gorpc.Device, dhKey *DHKey) gorpc.Handler {
	return &_CryptoClient{
		Log:    gslogger.Get("crpyto-client"),
		dhKey:  dhKey,
		device: device,
	}
}
Пример #11
0
func (proxy *_Proxy) newTunnelServer() gorpc.Handler {
	return &_TunnelServerHandler{
		Log:   gslogger.Get("agent-server-tunnel"),
		proxy: proxy,
		id:    proxy.tunnelID(),
	}
}
Пример #12
0
func (system *_System) newTunnelClient(name string) gorpc.Handler {
	return &_TunnelClient{
		Log:     gslogger.Get("gsagent-tunnel"),
		name:    name,
		system:  system,
		timeout: system.timeout,
	}
}
Пример #13
0
// NewAcceptor create new server channel acceptor
func NewAcceptor(name string, builder *PipelineBuilder) *Acceptor {
	return &Acceptor{
		Log:     gslogger.Get(name),
		builder: builder,
		timeout: gsconfig.Seconds("timeout", 5),
		name:    name,
	}
}
Пример #14
0
func (proxy *_Proxy) newTransProxyHandler() gorpc.Handler {
	return &_TransProxyHandler{
		Log:     gslogger.Get("trans-proxy"),
		proxy:   proxy,
		servers: make(map[uint16]Server),
		tunnels: make(map[byte]Server),
	}
}
Пример #15
0
func main() {
	gslogger.Console("$content", "2006-01-02 15:04:05.999999")
	gslogger.NewFlags(gslogger.ERROR | gslogger.WARN | gslogger.INFO)
	log := gslogger.Get("gsrpc")

	defer func() {
		if e := recover(); e != nil {
			log.E("%s", e)
		}

		gslogger.Join()
	}()

	flag.Parse()

	*output, _ = filepath.Abs(*output)

	log.I("Start gsRPC With Target Language(%s)", *lang)

	codegenF, ok := langs[*lang]

	if !ok {
		log.E("unknown gsrpc object language :%s", *lang)
		os.Exit(1)
	}

	codegen, err := codegenF(*output, []string{"github.com/gsrpc/gslang"})

	if err != nil {
		gserrors.Panicf(err, "create language(%s) codegen error", *lang)
	}

	compiler := gslang.NewCompiler("gsrpc", gslang.HandleError(func(err *gslang.Error) {
		gserrors.Panicf(err.Orignal, "parse %s error\n\t%s", err.Start, err.Text)
	}))

	for _, file := range flag.Args() {
		log.I("Compile gsLang File :%s", file)
		if err := compiler.Compile(file); err != nil {
			gserrors.Panicf(err, "compile %s error", file)
		}
	}

	log.I("Link ...")
	err = compiler.Link()

	if err != nil {
		gserrors.Panicf(err, "link error")
	}

	log.I("Output Directory :%s", *output)

	if err := compiler.Visit(codegen); err != nil {
		gserrors.Panicf(err, "generate language codes(%s) error", *lang)
	}

	log.I("Run gsRPC Compile -- Success")
}
Пример #16
0
func (compiler *Compiler) parse(lexer *lexer.Lexer, errorHandler ErrorHandler) *ast.Script {

	return (&Parser{
		Log:          gslogger.Get("parser"),
		lexer:        lexer,
		script:       compiler.module.NewScript(lexer.String()),
		errorHandler: errorHandler,
	}).parse()
}
Пример #17
0
// Build .
func (builder *ProxyBuilder) Build(name string) Context {

	proxy := &_Proxy{
		Log:     gslogger.Get("gsproxy"),
		proxy:   builder.proxy,
		clients: make(map[string]*_Client),
		name:    name,
		tunnels: make(map[byte]byte),
	}

	proxy.frontend = gorpc.NewAcceptor(
		fmt.Sprintf("%s.frontend", name),
		gorpc.BuildPipeline(time.Millisecond*10).Handler(
			"gsproxy-profile",
			gorpc.ProfileHandler,
		).Handler(
			"gsproxy-hb",
			func() gorpc.Handler {
				return handler.NewHeartbeatHandler(builder.timeout)
			},
		).Handler(
			"gsproxy-dh",
			func() gorpc.Handler {
				return handler.NewCryptoServer(builder.dhkeyResolver)
			},
		).Handler(
			transProxyHandler,
			proxy.newTransProxyHandler,
		).Handler(
			"gsproxy-client",
			proxy.newClientHandler,
		),
	)

	proxy.backend = gorpc.NewAcceptor(
		fmt.Sprintf("%s.backend", name),
		gorpc.BuildPipeline(time.Millisecond*10).Handler(
			tunnelHandler,
			proxy.newTunnelServer,
		),
	)

	go func() {
		if err := gorpc.TCPListen(proxy.backend, builder.laddrE); err != nil {
			proxy.E("start agent backend error :%s", err)
		}
	}()

	go func() {
		if err := gorpc.TCPListen(proxy.frontend, builder.laddrF); err != nil {
			proxy.E("start agent frontend error :%s", err)
		}
	}()

	return proxy
}
Пример #18
0
// NewSink .
func NewSink(name string, channel MessageChannel, timeWheel *timer.Wheel, timeout time.Duration) Sink {
	return &_Sink{
		Log:         gslogger.Get("sink"),
		name:        name,
		timeout:     timeout,
		dispatchers: make(map[uint16]Dispatcher),
		promises:    make(map[uint32]Promise),
		channel:     channel,
		timeWheel:   timeWheel,
	}
}
Пример #19
0
// NewCompiler .
func NewCompiler(name string, errorHandler ErrorHandler) *Compiler {

	module := ast.NewModule(name)

	return &Compiler{
		Log:          gslogger.Get("compiler"),
		module:       module,
		errorHandler: errorHandler,
		eval:         newEval(errorHandler, module),
	}

}
Пример #20
0
//NewLexer create new gslang lexer
func NewLexer(tag string, reader io.Reader) *Lexer {
	return &Lexer{
		Log:    gslogger.Get("gslang[lexer]"),
		reader: bufio.NewReader(reader),
		ws:     1<<'\t' | 1<<'\n' | 1<<'\r' | 1<<' ',
		position: Position{
			FileName: tag,
			Lines:    1,
			Column:   1,
		},
		curr: rune(TokenEOF),
	}
}
Пример #21
0
Файл: id.go Проект: gschat/tsdb
func newSEQIDGen(dir string) (*_SEQIDGen, error) {

	db, err := leveldb.OpenFile(filepath.Join(dir, "id.db"), nil)

	if err != nil {
		return nil, err
	}

	return &_SEQIDGen{
		Log: gslogger.Get("tsdb-id"),
		db:  db,
	}, nil
}
Пример #22
0
func newDB(dir string) (Persistence, error) {
	db, err := leveldb.OpenFile(filepath.Join(dir, "keyspace.db"), nil)

	if err != nil {
		return nil, err
	}

	return &_DB{
		Log:      gslogger.Get("tsdb.backend"),
		db:       db,
		storages: make(map[string]Storage),
		valen:    gsconfig.Int("tsdb.value.length", 1024),
	}, nil
}
Пример #23
0
// NewCodeGen .
func NewCodeGen(rootpath string, skips []string) (gslang.Visitor, error) {

	codeGen := &_CodeGen{
		Log:      gslogger.Get("gen4go"),
		rootpath: rootpath,
	}

	for _, skip := range skips {
		exp, err := regexp.Compile(skip)

		if err != nil {
			return nil, gserrors.Newf(err, "invalid skip regex string :%s", skip)
		}

		codeGen.skips = append(codeGen.skips, exp)
	}

	funcs := template.FuncMap{
		"title": strings.Title,
		"enumType": func(typeDecl ast.Type) string {
			return builtin[gslang.EnumType(typeDecl)]
		},
		"notVoid":     gslang.NotVoid,
		"isPOD":       gslang.IsPOD,
		"isAsync":     gslang.IsAsync,
		"isException": gslang.IsException,
		"enumSize":    gslang.EnumSize,
		"builtin":     gslang.IsBuiltin,
		"typeName":    codeGen.typeName,
		"defaultVal":  codeGen.defaultVal,
		"readType":    codeGen.readType,
		"writeType":   codeGen.writeType,
		"params":      codeGen.params,
		"returnParam": codeGen.returnParam,
		"callArgs":    codeGen.callArgs,
		"returnArgs":  codeGen.returnArgs,
		"tagValue":    codeGen.tagValue,
	}

	tpl, err := template.New("gen4go").Funcs(funcs).Parse(tpl4go)

	if err != nil {
		return nil, err
	}

	codeGen.tpl = tpl

	return codeGen, nil
}
Пример #24
0
// Build create new client
func (builder *ClientBuilder) Build(name string, F ConnectF) (Client, error) {
	client := &_Client{
		Log:   gslogger.Get(fmt.Sprintf("%s.%s", builder.name, name)),
		name:  fmt.Sprintf("%s.%s", builder.name, name),
		f:     F,
		state: uint32(StateDisconnect),
	}

	client.Log = gslogger.Get(name)

	var err error

	client.pipeline, err = builder.builder.Build(name)

	if err != nil {
		return nil, err
	}

	client.pipeline.Active()

	client.doconnect()

	return client, nil
}
Пример #25
0
// Link do sematic paring and type link
func (compiler *Compiler) Link() (err error) {

	defer func() {
		if e := recover(); e != nil {
			err = e.(error)
		}
	}()

	linker := &_Linker{
		Log:          gslogger.Get("linker"),
		types:        make(map[string]ast.Type),
		errorHandler: compiler.errorHandler,
		compiler:     compiler,
	}

	compiler.module.Foreach(func(script *ast.Script) bool {
		linker.createSymbolTable(script)
		return true
	})

	compiler.module.Types = linker.types

	compiler.module.Foreach(func(script *ast.Script) bool {
		linker.linkTypes(script)
		return true
	})

	compiler.module.Foreach(func(script *ast.Script) bool {

		script.TypeForeach(func(gslangType ast.Type) {

			linker.checkAnnotation(script, gslangType)
		})

		return true
	})

	return
}
Пример #26
0
// NewCodeGen .
func NewCodeGen(rootpath string, skips []string) (gslang.Visitor, error) {

	codeGen := &_CodeGen{
		Log:      gslogger.Get("gen4go"),
		rootpath: rootpath,
	}

	for _, skip := range skips {
		exp, err := regexp.Compile(skip)

		if err != nil {
			return nil, gserrors.Newf(err, "invalid skip regex string :%s", skip)
		}

		codeGen.skips = append(codeGen.skips, exp)
	}

	funcs := template.FuncMap{
		"exception": exception,
		"title":     strings.Title,
		"tableName": func(typeDecl ast.Type) string {
			if gslang.IsException(typeDecl) {
				return exception(strings.Title(typeDecl.Name()))
			}

			return strings.Title(typeDecl.Name())
		},
		"methodName":  methodName,
		"fieldName":   fieldname,
		"enumFields":  codeGen.enumFields,
		"notVoid":     gslang.NotVoid,
		"isPOD":       gslang.IsPOD,
		"isAsync":     gslang.IsAsync,
		"isException": gslang.IsException,
		"enumSize":    gslang.EnumSize,
		"enumType": func(typeDecl ast.Type) string {
			return builtin[gslang.EnumType(typeDecl)]
		},
		"builtin":         gslang.IsBuiltin,
		"typeName":        codeGen.typeName,
		"objTypeName":     codeGen.objTypeName,
		"defaultVal":      codeGen.defaultVal,
		"readType":        codeGen.readType,
		"writeType":       codeGen.writeType,
		"params":          codeGen.params,
		"returnParam":     codeGen.returnParam,
		"callArgs":        codeGen.callArgs,
		"returnArgs":      codeGen.returnArgs,
		"marshalField":    codeGen.marshalfield,
		"unmarshalField":  codeGen.unmarshalfield,
		"unmarshalParam":  codeGen.unmarshalParam,
		"methodcall":      codeGen.methodcall,
		"marshalParam":    codeGen.marshalParam,
		"marshalReturn":   codeGen.marshalReturn,
		"methodRPC":       codeGen.methodRPC,
		"marshalParams":   codeGen.marshalParams,
		"callback":        codeGen.callback,
		"unmarshalReturn": codeGen.unmarshalReturn,
		"constructor":     codeGen.constructor,
		"tagValue":        codeGen.tagValue,
	}

	tpl, err := template.New("t4java").Funcs(funcs).Parse(t4java)

	if err != nil {
		return nil, err
	}

	codeGen.tpl = tpl

	return codeGen, nil
}
Пример #27
0
package test

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"testing"

	"github.com/gsdocker/gserrors"
	"github.com/gsdocker/gslogger"
	"github.com/gsrpc/gslang"
	"github.com/gsrpc/gslang/lexer"
)

var (
	log = gslogger.Get("gslang")
)

func TestToken(t *testing.T) {

	content, err := ioutil.ReadFile("test.gs")

	if err != nil {
		t.Fatal(err)
	}

	tokenizer := lexer.NewLexer("mem", bytes.NewBuffer(content))

	for {

		token, err := tokenizer.Next()
Пример #28
0
// NewIMServer create new im server
func NewIMServer() gsactor.AgentSystem {
	return &_IMServer{
		Log: gslogger.Get("imserver"),
	}
}
Пример #29
0
	"net/http"
	"net/url"
	"testing"
	"time"

	"github.com/gorilla/websocket"
	"github.com/gsdocker/gslogger"
	"github.com/gsrpc/gorpc"
	"github.com/gsrpc/gorpc/handler"
)

var G *big.Int

var P *big.Int

var log = gslogger.Get("test")

func init() {
	G, _ = new(big.Int).SetString("6849211231874234332173554215962568648211715948614349192108760170867674332076420634857278025209099493881977517436387566623834457627945222750416199306671083", 0)

	P, _ = new(big.Int).SetString("13196520348498300509170571968898643110806720751219744788129636326922565480984492185368038375211941297871289403061486510064429072584259746910423138674192557", 0)
}

type _MockRESTful struct {
	kv map[string]string //
}

func (mock *_MockRESTful) Put(callSite *gorpc.CallSite, name string, val string) error {
	mock.kv[name] = val

	return nil
Пример #30
0
Файл: dh.go Проект: gsrpc/gorpc
// NewCryptoServer .
func NewCryptoServer(resovler DHKeyResolver) gorpc.Handler {
	return &_CryptoServer{
		Log:      gslogger.Get("crpyto-server"),
		resovler: resovler,
	}
}