Commit 499704c0 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 67cc55ab
func (p *Address) NEODecode(data []byte) (int, error) {
{
l := BigEndian.Uint32(data[0:])
data = data[4:]
if len(data) < l {
return 0, ErrDecodeOverflow
}
p.Host = string(data[:l])
data = data[l:]
}
p.Port = BigEndian.Uint16(data[0:])
return 2 /* + TODO variable part */, nil
}
func (p *NodeInfo) NEODecode(data []byte) (int, error) {
p.NodeType = int32(BigEndian.Uint32(data[0:]))
{
l := BigEndian.Uint32(data[4:])
data = data[8:]
if len(data) < l {
return 0, ErrDecodeOverflow
}
p.Address.Host = string(data[:l])
data = data[l:]
}
p.Address.Port = BigEndian.Uint16(data[0:])
p.UUID = int32(BigEndian.Uint32(data[2:]))
p.NodeState = int32(BigEndian.Uint32(data[6:]))
p.IdTimestamp = float64_NEODecode(data[10:])
return 18 /* + TODO variable part */, nil
}
// NEO. Protocol description
//go:generate sh -c "go run protogen.go >marshal.go"
package neo
// XXX move imports out of here
......@@ -54,7 +56,7 @@ const (
ADMIN
)
type NodeState int
type NodeState int32
const (
RUNNING NodeState = iota //short: R // XXX tag prefix name ?
TEMPORARILY_DOWN //short: T
......@@ -152,7 +154,6 @@ func float64_NEODecode(b []byte) float64 {
return math.Float64frombits(fu)
}
/*
// NOTE original NodeList = []NodeInfo
type NodeInfo struct {
NodeType
......@@ -162,6 +163,7 @@ type NodeInfo struct {
IdTimestamp float64
}
/*
//type CellList []struct {
type CellInfo struct {
UUID
......
......@@ -49,11 +49,13 @@ func pos(x interface { Pos() token.Pos }) token.Position {
}
func main() {
var err error
log.SetFlags(0)
//typeMap := map[string]*PacketType{} // XXX needed ?
// go through proto.go and collect packets type definitions
var mode parser.Mode = 0 // parser.Trace
var fv []*ast.File
for _, src := range []string{"proto.go", "neo.go"} {
......@@ -65,7 +67,7 @@ func main() {
}
conf := types.Config{Importer: importer.Default()}
_, err := conf.Check("neo", fset, fv, info)
_, err = conf.Check("neo", fset, fv, info)
if err != nil {
log.Fatalf("typecheck: %v", err)
}
......@@ -77,7 +79,7 @@ func main() {
//return
f := fv[0] // proto.go comes first
out := Buffer{}
buf := Buffer{}
for _, decl := range f.Decls {
// we look for types (which can be only under GenDecl)
......@@ -101,35 +103,7 @@ func main() {
//fmt.Println(t)
//ast.Print(fset, t)
out.WriteString(gendecode(typespec))
/*
PacketType{name: typename, msgCode: ncode}
// if ncode != 0 {
// fmt.Println()
// }
for _, fieldv := range t.Fields.List {
// we only support simple types like uint16
ftype, ok := fieldv.Type.(*ast.Ident)
if !ok {
// TODO log
// TODO proper error message
panic(fmt.Sprintf("%#v not supported", fieldv.Type))
}
if len(fieldv.Names) != 0 {
for _, field := range fieldv.Names {
fmt.Printf("%s(%d).%s\t%s\n", typename, ncode, field.Name, ftype)
}
} else {
// no names means embedding
fmt.Printf("%s(%d).<%s>\n", typename, ncode, ftype)
}
}
ncode++
*/
buf.WriteString(gendecode(typespec))
}
}
......@@ -137,14 +111,13 @@ func main() {
//ast.Print(fset, gdecl)
}
// format & emit out
outf, err := format.Source(out.Bytes())
// format & emit bufferred code
code, err := format.Source(buf.Bytes())
if err != nil {
panic(err) // should not happen
}
_, err = os.Stdout.Write(outf)
//_, err = os.Stdout.Write(out.Bytes())
_, err = os.Stdout.Write(code)
if err != nil {
log.Fatal(err)
}
......@@ -208,7 +181,7 @@ func (d *decoder) emit(format string, a ...interface{}) {
func (d *decoder) decodedBasic (obj types.Object, typ *types.Basic) string {
bdec, ok := basicDecode[typ.Kind()]
if !ok {
log.Fatalf("%v: basic type %v not supported", pos(obj), typ)
log.Fatalf("%v: %v: basic type %v not supported", pos(obj), obj.Name(), typ)
}
dataptr := fmt.Sprintf("data[%v:]", d.n)
decoded := fmt.Sprintf(bdec.decode, dataptr)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment