-
Notifications
You must be signed in to change notification settings - Fork 1
/
uqueue.go
342 lines (293 loc) · 7.53 KB
/
uqueue.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package main
// this process maintains queue of urls to fetch
import (
"bufio"
"io"
"io/ioutil"
"log"
"math/rand"
"os"
"strconv"
"strings"
"sync"
"time"
btr "ouroboros/src/btr"
utils "ouroboros/src/utils"
)
var prefix = string(0x11) + string(0x11) + string(0x11) + string(0x11) + string(0x11) // 5 symbols, see btma_hash0
type Conf struct {
Dir0 string
}
var conf Conf
var rs = rand.NewSource(time.Now().UnixNano())
var ra = rand.New(rs)
var hchan = make(chan string, 100)
var uchan = make(chan string, 100)
var inactive = make(chan string, 100)
var ina = make(map[string]bool)
var il sync.Mutex
var symbols []int
var P = 4
func activate(buf *bufio.Reader, y *btr.Tree, ep int64, poi int64, many int64) (int64, bool) {
log.Println("----------------activating-------------------- ep=", ep)
cou := int64(0)
B := false
for ep < poi && cou < many {
s, e := buf.ReadString('\n')
ep += int64(len(s))
cou += int64(len(s))
if e != nil {
B = true
break
}
i := strings.Index(s, prefix)
if i < 0 {
// log.Println("prefix not found", s, []byte(s))
continue
}
if i+P+1 >= len(s) {
log.Println("short+index", s, []byte(s))
continue
}
if s[i+P+1] != '+' {
// log.Println("inactive", s[i:len(s)-1], []byte(s), byte(s[i+P+1]))
if s[i+P+1] != '-' {
log.Println("nonstandard", s)
}
continue
}
s1 := s[i+P+1+1 : len(s)-1]
t := y.Find(s1)
if t != nil {
// log.Println("setting active", s1)
y.SetActive(s1)
} else {
// log.Println("Adding", s1)
y.Add(s1)
}
}
log.Println("activation done -----------------------ep=", ep)
return ep, B
}
// this function loads host list in the segment [i][j] ( so the hostname = [i][j]+rest) and draws a host randomly from it.
//then it calls DrawfromHost function, and attempts to draw a url from that host.
// it successfull, it adds url to the queue,
// if not, it mars host inactive.
// If host is marked as inactive, it will not participate in the further Draws in this session.
// We make at most 100 attmpts to draw from given segment. Then we close and go to the next segment.
// This approach does not have the problem of freshly coming urls, because these will be addressed on next iteration.
// there Is potential problem, when most of the hosts are exhausted: then , we just spend most of the time to make attempts on hosts that are already
// exhausted, and this leaves us little time to actually draw from 'active' hosts.
func DrawHost() {
tot := 0
y := btr.New()
poi := int64(0)
ep := int64(0)
for {
log.Println("reloading hosts \n\n\n\n")
// lfile, e := os.OpenFile(conf.Dir0+"/host_list.txt", os.O_RDONLY, 0644)
lfile, e := os.OpenFile(conf.Dir0+"/hostsd/host.txt", os.O_RDONLY, 0644)
if e != nil {
log.Println("cannot open hostlist")
time.Sleep(time.Second)
continue
}
buf := bufio.NewReader(lfile)
pois, e := ioutil.ReadFile(conf.Dir0 + "/uqueue.poi")
if e == nil {
poi1, e := strconv.ParseInt(string(pois), 10, 64)
if e == nil {
poi = poi1
}
}
lfile.Seek(ep, 0)
var B bool
ep, B = activate(buf, y, ep, poi, int64(100000))
if B {
log.Println("-------------------end reached,", ep, poi)
ep = 0
} else {
log.Println(" activated ep=", ep)
}
lfile.Seek(poi, 0)
q := make([]string, 0)
log.Println("---------------------addingn new------------------- ", " U=", y.GetU())
for {
s, e := buf.ReadString('\n')
poi += int64(len(s))
if e != nil {
break
}
i := strings.Index(s, prefix)
if i < 0 {
log.Println("prefix not found", s)
continue
}
if i+P+1 >= len(s) {
log.Println("short+index", s)
continue
}
if s[i+P+1] != '+' {
// log.Println("inactive", s[i:len(s)-1])
if s[i+P+1] != '-' {
log.Println("nonstandard", s)
}
}
s1 := s[i+P+1 : len(s)-1]
q = append(q, s1)
}
ioutil.WriteFile(conf.Dir0+"/uqueue.poi", []byte(strconv.FormatInt(poi, 10)), 0644)
log.Println("------------------------------------------------got hosts as list", len(q))
if len(q) == 0 {
log.Println("short host file")
//time.Sleep(time.Duration(1e+9))
//continue
}
y = y.Add("google.com")
for i := 0; i < len(q); i++ {
y.Add(q[i])
}
log.Println("-----------------------------------------------------y refreshed", y.GetU())
time.Sleep(time.Second)
COU := 0
for COU < 100000 && y.GetU() > 0 {
cou := 0
real := 0
for real < 1000 && cou < 100000 && y.GetU() > 0 {
host, e := y.Draw(ra)
if e != nil {
log.Println("breaking", host)
y.SetInactive(host)
time.Sleep(time.Duration(1e+9))
break
}
k := DrawFromHost(host)
if k < 0 {
y.SetInactive(host)
} else {
real += 1
COU += 1
tot += 1
}
cou += 1
}
log.Println("drawhost", "cou", cou, "real", real, "y.GetU", y.GetU(), "COU", COU, "tot", tot)
}
log.Println("sleeping 1sec")
time.Sleep(time.Second)
}
}
//maybe draw ~10 from single active host ?
//what about WG and Harmonic centrality here?
// I dont even need to care about activity of the host: I check only once in a batch; it has negligible cost
func DrawFromHost(host string) int {
h, e := os.OpenFile(conf.Dir0+"/resou/"+host, os.O_RDONLY, 0644)
if e != nil {
return 1
}
hi, _ := h.Stat()
s1 := int(hi.Size())
s2, e := ioutil.ReadFile(conf.Dir0 + "/resou/" + host + ".poi")
s3 := 0
if e != nil || len(s2) <= 0 {
} else {
var e1 error
s3, e1 = strconv.Atoi(string(s2))
if e1 != nil {
if string(s2) != "" {
h.Close()
return -2
}
}
}
if s1 <= s3 {
h.Close()
return -3
}
h.Seek(int64(s3), io.SeekStart)
b := bufio.NewReader(h)
s, e := b.ReadString('\n')
if e != nil {
h.Close()
return -4
}
if len(s) <= 1 {
h.Close()
return -5
}
s3 += len(s)
ioutil.WriteFile(conf.Dir0+"/resou/"+host+".poi", []byte(strconv.Itoa(s3)), 0644)
uchan <- string(s[0 : len(s)-1])
h.Close()
return 0
}
//perhaps keep hosts in RAM? how to decide? calculation?
//need to reload host file: "refresh" --- because new hosts become available; You are doing it: on new .AsList() call you get fresh file
// in principle , can use a db: host -> activity_status; and need not get hosts in RAM
func Run() {
COU := 0
for {
f, e := os.OpenFile(conf.Dir0+"/queue.txt", os.O_CREATE|os.O_RDWR, 0644)
if e != nil {
return
}
fi, _ := f.Stat()
f.Close()
poi := 0
ps, e := ioutil.ReadFile(conf.Dir0 + "/queue.txt.poi")
if e != nil || len(ps) == 0 {
} else {
poi, e = strconv.Atoi(string(ps))
if e != nil {
time.Sleep(time.Second)
continue
}
}
//log.Println("diff", int(fi.Size())-poi, len(uchan))
if int(fi.Size())-poi < 100000 {
url := <-uchan
COU += 1
que, _ := os.OpenFile(conf.Dir0+"/queue.txt", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
que.Write([]byte(url + "\n"))
que.Close()
// log.Println("added", url)
} else {
log.Println("enough", int(fi.Size())-poi)
time.Sleep(time.Second)
}
}
}
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 + "/hostsd")
utils.MakeDir0(conf.Dir0 + "/host_msg")
utils.MakeDir0(conf.Dir0 + "/host_msg_tmp")
/*
for i := 48; i < 58; i++ {
symbols = append(symbols, i)
}
for i := 97; i < 123; i++ {
symbols = append(symbols, i)
}
*/
for i := 0; i < 256; i++ {
symbols = append(symbols, i)
}
go DrawHost()
Run()
}