-
Notifications
You must be signed in to change notification settings - Fork 1
/
stats.go
155 lines (133 loc) · 2.82 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
// this process provides service to API calls regarding basic staistics of node operation
import (
"bufio"
"fmt"
"io/ioutil"
"log"
"net"
"os"
"os/exec"
"strconv"
"strings"
"time"
utils "ouroboros/src/utils"
)
type Conf struct {
Dir0 string
}
var conf Conf
func listen() {
log.Println("listen for peers", ":9001")
ln, e := net.Listen("tcp", ":9001")
utils.Check(e, "listening error")
for {
conn, _ := ln.Accept()
fmt.Println("inbox: got connection")
go talk(conn)
}
}
// this decribes communication protocol regarding possible requests
func talk(conn net.Conn) {
r := bufio.NewReader(conn)
for {
s, e := r.ReadBytes(0x11)
if e != nil {
log.Println("closing connection")
conn.Close()
return
}
s = s[0 : len(s)-1]
log.Println("received", string(s))
if string(s) == "list" {
fs, _ := ioutil.ReadDir(conf.Dir0 + "/resou")
n := 0
for i := range fs {
fn := fs[i].Name()
if len(fn) < 4 || fn[len(fn)-4:] != ".poi" {
n += len(fn) + 1
}
}
ns := strconv.Itoa(n)
conn.Write([]byte(ns))
conn.Write([]byte{0x11})
for i := range fs {
fn := fs[i].Name()
if len(fn) < 4 || fn[len(fn)-4:len(fn)] != ".poi" {
conn.Write([]byte(fn + string(0x13)))
}
}
conn.Write([]byte{0x11})
}
if string(s[0:4]) == "host" {
name := string(s[4 : len(s)-1])
f, e := os.OpenFile(conf.Dir0+"/resou/"+name, os.O_RDONLY, 0644)
if e != nil {
log.Println("error::", e)
conn.Write([]byte("does not exist"))
conn.Write([]byte{0x11})
continue
}
u := bufio.NewReader(f)
for {
w, e := u.ReadBytes('\n')
if e != nil {
log.Println("red ", e)
break
}
if len(w) < 1 {
continue
}
conn.Write([]byte(w[0 : len(w)-1]))
conn.Write([]byte{0x13})
}
conn.Write([]byte{0x11})
conn.Write([]byte{0x13})
f.Close()
}
if string(s) == "tar" {
utils.MakeDir0(conf.Dir0 + "/tar")
cmd := exec.Command("tar", "-czvf", conf.Dir0+"/tar/tar.tar", conf.Dir0+"/resou")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Println("error tar", err)
}
f, _ := os.OpenFile(conf.Dir0+"/tar/tar.tar", os.O_RDONLY, 0644)
b := bufio.NewReader(f)
buf := make([]byte, 4096)
for {
n, e := b.Read(buf)
conn.Write(buf[0:n])
if e != nil {
log.Println("read error", e)
break
}
}
f.Close()
conn.Write([]byte{0x11})
conn.Close()
}
}
}
func main() {
var dir0 string
if len(os.Args) > 1 {
dir0 = os.Args[1]
} else {
dir0 = "/d"
s, e := ioutil.ReadFile("/etc/dse/dse.conf")
if e != nil {
log.Println("error reding config", e)
time.Sleep(time.Second)
os.Exit(0)
}
q := strings.Split(string(s), "\n")
dir0 = q[0]
log.Println("dir0=", dir0)
}
conf.Dir0 = dir0
utils.MakeDir0(conf.Dir0 + "/tar")
listen()
}