mps

morphux package server
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/mps

commit a22c5f191ec5dd8a7e43c8a3898f89fd6f1fed5d
parent e18acc61177965e4d71d049ca44f269ace544baf
Author: MrGosti <gostitsog@gmail.com>
Date:   Fri, 24 Mar 2017 12:20:31 +0100

WIP(resp_pkg)

Diffstat:
Mdatabase.go | 10++++++----
Mmessage/header.go | 20++++++++++++++++++++
Mnetwork.go | 21++++++++++++++++++++-
Mresponse/resp_pkg.go | 15++++++++++++++-
4 files changed, 60 insertions(+), 6 deletions(-)

diff --git a/database.go b/database.go @@ -17,6 +17,7 @@ import ( type Package struct { ID uint64 Name string + State uint8 Version string Category string Description string @@ -54,7 +55,7 @@ func RequestPackage(data []byte, db *sql.DB) (int, Package, error) { return n, pkg, err } -func PkgtoRespPkg(pkg Package) (error, *response.RespPkg) { +func PkgtoRespPkg(pkg Package) (*response.RespPkg, error) { dep := strings.Split(pkg.Dependencies, ",") @@ -72,6 +73,7 @@ func PkgtoRespPkg(pkg Package) (error, *response.RespPkg) { ret.CompTime = pkg.SBU ret.InstSize = pkg.InstalledSize ret.ArchSize = pkg.ArchiveSize + ret.State = pkg.State ret.NameLen = uint64(len(pkg.Name)) ret.CategoryLen = uint16(len(pkg.Category)) ret.VersionLen = uint16(len(pkg.Version)) @@ -85,7 +87,7 @@ func PkgtoRespPkg(pkg Package) (error, *response.RespPkg) { ret.Checksum = pkg.ArchiveHash ret.Dependencies = depID - return nil, ret + return ret, nil } func QueryPkgNameAndCat(name string, category string, state uint8, db *sql.DB) (Package, error) { @@ -107,7 +109,7 @@ func QueryPkgNameAndCat(name string, category string, state uint8, db *sql.DB) ( log.Fatalln(err) } for rows.Next() { - err := rows.Scan(&pkg.ID, &pkg.Name, &pkg.Version, &pkg.Category, &pkg.Description, + err := rows.Scan(&pkg.ID, &pkg.Name, &pkg.State, &pkg.Version, &pkg.Category, &pkg.Description, &pkg.Archive, &pkg.SBU, &pkg.Dependencies, &pkg.ArchiveSize, &pkg.InstalledSize, &pkg.ArchiveHash) if err != nil { return pkg, err @@ -126,7 +128,7 @@ func QueryPkgID(id uint64, state uint8, db *sql.DB) (Package, error) { log.Fatalln(err) } for rows.Next() { - err := rows.Scan(&pkg.ID, &pkg.Name, &pkg.Version, &pkg.Category, &pkg.Description, + err := rows.Scan(&pkg.ID, &pkg.Name, &pkg.State, &pkg.Version, &pkg.Category, &pkg.Description, &pkg.Archive, &pkg.SBU, &pkg.Dependencies, &pkg.ArchiveSize, &pkg.InstalledSize, &pkg.ArchiveHash) if err != nil { return pkg, err diff --git a/message/header.go b/message/header.go @@ -25,3 +25,23 @@ func (h *Header) Unpack(data []byte) (int, error) { return 3 + int(h.NextPkgLen), nil } + +func (h *Header) Build(Type, number uint8, data []byte) { + h.Type = Type + h.NextPkgLen = 0 + h.NextPkg = "" + + h.Size = uint16(int(1+2+1+h.NextPkgLen+number) + len(data)) + +} + +//BuildWithHeader Is theorically faster than generating header and Packing it +// func BuildWithHeader(Type, number uint8, data []byte) []byte { + +// hash := "" + +// header := []byte{Type, , len(hash), hash} + +// return append() + +// } diff --git a/network.go b/network.go @@ -4,6 +4,7 @@ import ( "database/sql" "errors" "fmt" + "log" "net" "github.com/Morphux/mps/message" @@ -35,7 +36,25 @@ func ParseRequest(data []byte, conn net.Conn, db *sql.DB) error { case 0x01: conn.Write(response.GetAuthACK()) case 0x10: - c, _, _ := RequestPackage(data[cursor+1:], db) + c, pkg, err := RequestPackage(data[cursor+1:], db) + + resp, err := PkgtoRespPkg(pkg) + + resp_data, err := resp.Pack() + + if err != nil { + log.Fatal(err) + } + + resp_header := new(message.Header) + + resp_header.Build(0x20, 1, resp_data) + + header_data, err := resp_header.Pack() + + conn.Write(append(header_data, resp_data...)) + + //conn.Write() cursor += c default: fmt.Println(header.Type) diff --git a/response/resp_pkg.go b/response/resp_pkg.go @@ -1,6 +1,9 @@ package response -import "github.com/Morphux/mps/message" +import ( + "github.com/Morphux/mps/message" + "github.com/Nyarum/barrel" +) type RespPkg struct { message.Message @@ -9,6 +12,7 @@ type RespPkg struct { CompTime float64 InstSize float64 ArchSize float64 + State uint8 NameLen uint64 CategoryLen uint16 VersionLen uint16 @@ -22,3 +26,12 @@ type RespPkg struct { Checksum string Dependencies []uint64 } + +func (p *RespPkg) Pack() ([]byte, error) { + barrel := barrel.NewBarrel() + load := barrel.Load(p, []byte{}, true) + + err := barrel.Pack(load) + + return barrel.Bytes(), err +}