mps

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

commit 2243ad0af07aca45cf4816cffc6fe970f2194ab6
parent 58ba4e7d403d5a7015722b9f29c969c56bf5836a
Author: MrGosti <gostitsog@gmail.com>
Date:   Mon, 27 Feb 2017 23:44:09 +0100

I hope nothing break

Diffstat:
Adatabase.go | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aheader.go | 29+++++++++++++++++++++++++++++
Amps.go | 60++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Anetwork.go | 25+++++++++++++++++++++++++
Areq_get_pkg.go | 22++++++++++++++++++++++
Astruct.go | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
6 files changed, 263 insertions(+), 0 deletions(-)

diff --git a/database.go b/database.go @@ -0,0 +1,56 @@ +package main + +import ( + "database/sql" + "fmt" + "log" + + _ "github.com/mattn/go-sqlite3" +) + +// MPM package +type Package struct { + id uint64 + name string + version string + category string + description string + archive string + sbu uint64 + dependencies string + archive_size uint64 + installed_size uint64 + archive_hash string +} + +func queryPackageByName(name string, state int, db *sql.DB) { + pkg := Package{} + rows, err := db.Query("SELECT * FROM pkgs where name = ?", name) + if err != nil { + log.Fatalln(err) + } + for rows.Next() { + err := rows.Scan(&pkg.id, &pkg.name, &pkg.version, &pkg.category, &pkg.description, + &pkg.archive, &pkg.sbu, &pkg.dependencies, &pkg.archive_size, &pkg.installed_size, &pkg.archive_hash) + if err != nil { + log.Fatalln(err) + } + fmt.Printf("%#v\n", pkg) + } +} + +func queryPackageByid(id uint64, state int, db *sql.DB) { + pkg := Package{} + rows, err := db.Query("SELECT * FROM pkgs where id = ?", id) + if err != nil { + log.Fatalln(err) + } + for rows.Next() { + err := rows.Scan(&pkg.id, &pkg.name, &pkg.version, &pkg.category, &pkg.description, + &pkg.archive, &pkg.sbu, &pkg.dependencies, &pkg.archive_size, &pkg.installed_size, &pkg.archive_hash) + if err != nil { + log.Fatalln(err) + } + fmt.Printf("%#v\n", pkg) + } +} diff --git a/header.go b/header.go @@ -0,0 +1,29 @@ +package main + +import "github.com/Nyarum/barrel" + +type Header struct { + Type uint8 + Size uint16 + NextPkgLen uint8 + NextPkgs string +} + +func (p *Header) Default() { + +} +func (p Header) Check(stats *barrel.Stats) bool { + return true +} + +func (h *Header) Unpack(data []byte) error { + barrel := barrel.NewBarrel() + load := barrel.Load(h, data, false) + + err := barrel.Unpack(load) + if err != nil { + return err + } + + return nil +} diff --git a/mps.go b/mps.go @@ -0,0 +1,60 @@ +package main + +import ( + "database/sql" + "flag" + "fmt" + "io/ioutil" + "log" + "net" + "os" + + _ "github.com/mattn/go-sqlite3" +) + +func main() { + + databasePtr := flag.String("db", "", "a sqlite database") + + flag.Parse() + + if *databasePtr == "" || len(flag.Args()) == 0 { + flag.Usage() + os.Exit(0) + } + + db, err := sql.Open("sqlite3", *databasePtr) + if err != nil { + log.Fatal(err) + } + defer db.Close() + + l, err := net.Listen("tcp", flag.Args()[0]) + if err != nil { + fmt.Println("Error listening:", err.Error()) + os.Exit(1) + } + + defer l.Close() + fmt.Println("Listening on " + flag.Args()[0]) + for { + + conn, err := l.Accept() + if err != nil { + fmt.Println("Error accepting: ", err.Error()) + os.Exit(1) + } + + go handleRequest(conn, db) + } +} + +func handleRequest(conn net.Conn, db *sql.DB) { + + message, err := ioutil.ReadAll(conn) + if err != nil { + log.Print("Error reading:", err.Error()) + } + ParseRequest(message, conn) + conn.Close() +} diff --git a/network.go b/network.go @@ -0,0 +1,25 @@ +package main + +import ( + "errors" + "net" +) + +func ParseRequest(message []byte, conn net.Conn) error { + var header *Header + + if len(message) < 5 { + return errors.New("Header too short") + } + + headerSize := uint8(message[3]) + + header.Unpack(message[0 : 3+headerSize]) + + switch header.Type { + case 0x10: + //handle request + } + + return nil +} diff --git a/req_get_pkg.go b/req_get_pkg.go @@ -0,0 +1,22 @@ +package main + +import "github.com/Nyarum/barrel" + +type GetPkg struct { + ID uint64 + State uint8 + NameLen uint16 + CategLen uint16 + VersionLen uint16 + Name []byte + Category []byte + Version []byte +} + +func (p *GetPkg) Default() { + +} + +func (p GetPkg) Check(stats *barrel.Stats) bool { + return true +} diff --git a/struct.go b/struct.go @@ -0,0 +1,71 @@ +package main + +type header struct { + T uint8 + Number uint8 +} + +type getPkg_ struct { + H header + + ID uint64 + State uint8 + NameLen uint16 + CategLen uint16 + VersionLen uint16 +} + +type respPkg struct { + H header + + ID uint64 + CompTime float64 + InstSize float64 + ArchSize float64 + NameLen uint64 + CategoryLen uint16 + VersionLen uint16 + ArchiveLen uint16 + ChecksumLen uint16 + DependenciesSize uint16 + Name []byte + Category []byte + Version []byte + Archive []byte + Checksum []byte + Dependencies []uint64 +} + +type respPkg_ struct { + H header + + ID uint64 + CompTime float64 + InstSize float64 + ArchSize float64 + NameLen uint64 + CategoryLen uint16 + VersionLen uint16 + ArchiveLen uint16 + ChecksumLen uint16 + DependenciesSize uint16 +} + +type resp_file_t struct { + ID uint64 + kind uint8 + parentID uint64 + pathLen uint16 + path []byte +} + +type resp_news_t struct { + ID uint64 + parentID uint64 + authorLen uint16 + authorMailLen uint16 + textLen uint16 + author []byte + authorMail []byte + text []byte +}