-
Notifications
You must be signed in to change notification settings - Fork 7
/
handlers.go
133 lines (125 loc) · 3.5 KB
/
handlers.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
package main
import (
"log"
"regexp"
"strconv"
"strings"
"time"
"github.com/amrav/sparrow/client"
"github.com/amrav/sparrow/proto"
"github.com/amrav/sparrow/server"
)
func SendHubMessages(c *client.Client, sendCh chan interface{},
recvCh chan server.JsonMsg, done chan struct{}) {
chatRegexp := regexp.MustCompile(`(?s)^<(.+?)>\s(.+)\|$`)
ch := c.HubMessagesMatch(done, chatRegexp)
for msg := range ch {
matches := chatRegexp.FindSubmatch(msg)
if matches != nil {
msg := map[string]string{
"type": "RECEIVE_MESSAGE",
"from": string(matches[1]),
"text": string(matches[2]),
}
if strings.HasPrefix(msg["from"], "%") {
continue
}
sendCh <- msg
}
}
}
func SendPrivateMessages(c *client.Client, sendCh chan interface{},
recvCh chan server.JsonMsg, done chan struct{}) {
chatRegexp := regexp.MustCompile(`(?s)^\$To: (.+?) From: (.+?) \$<(.+?)>\s(.+)\|$`)
ch := c.HubMessagesMatch(done, chatRegexp)
for msg := range ch {
matches := chatRegexp.FindSubmatch(msg)
if matches != nil {
msg := map[string]string{
"type": "RECEIVE_PRIVATE_MESSAGE",
"from": string(matches[2]),
"text": string(matches[4]),
}
select {
case sendCh <- msg:
case <-done:
return
}
}
}
}
func HandleSearchRequests(c *client.Client, sendCh chan interface{},
recvCh chan server.JsonMsg, done chan struct{}) {
log.Printf("HSR: Waiting for message\n")
for msg := range recvCh {
log.Printf("HSR: Received message %s\n", msg)
if msg["type"] == "MAKE_SEARCH_QUERY" {
log.Printf("Searching for %s\n", msg["searchText"])
go c.Search(msg["searchText"])
}
}
}
// HandleSearchResults listens for incoming search results
func HandleSearchResults(c *client.Client, sendCh chan interface{},
recvCh chan server.JsonMsg, done chan struct{}) {
resultsCh := make(chan proto.SearchResult)
go c.SearchResults(resultsCh, done)
var srsBatch []proto.SearchResult
var timeoutCh <-chan time.Time
const MAX_BATCH_SIZE = 1000
sendBatchedSearchResults := func() {
select {
case sendCh <- srsBatch:
default:
log.Fatalf("Unable to send result %+v to sendCh", srsBatch)
}
// Reset timeout channel. This will
// be set the next time a search result comes in.
timeoutCh = nil
// Make sure slice with new underlying array is initialized
// the next time to not overwrite results just sent.
srsBatch = nil
}
for {
select {
case <-done:
return
case <-timeoutCh:
log.Printf("Sending batched search results after timer")
sendBatchedSearchResults()
case res := <-resultsCh:
if srsBatch == nil {
timeoutCh = time.After(500 * time.Millisecond)
}
srsBatch = append(srsBatch, res)
if len(srsBatch) == MAX_BATCH_SIZE {
log.Printf("Sending batched search results after capacity")
sendBatchedSearchResults()
}
// log.Printf("Got search result: %+v", res)
}
}
}
func HandleDownloadFile(c *client.Client, sendCh chan interface{},
recvCh chan server.JsonMsg, done chan struct{}) {
for msg := range recvCh {
if msg["type"] == "DOWNLOAD_FILE" {
log.Printf("Downloading file: %s (%s) from %s", msg["fileName"], msg["tth"], msg["nick"])
go func() {
progressCh := make(chan int, 10)
size, _ := strconv.ParseUint(msg["size"], 10, 64)
go c.DownloadFile(msg["fileName"], msg["tth"], msg["nick"], size, progressCh)
var progress int
for progress = range progressCh {
if progress == int(size) {
log.Printf("Download complete")
return
}
}
if progress != int(size) {
// TODO: Handle download error
}
}()
}
}
}