Skip to content

Commit db8cbe1

Browse files
committed
<添加><NET><UDP通信方法>
1 parent a237b28 commit db8cbe1

File tree

8 files changed

+765
-6
lines changed

8 files changed

+765
-6
lines changed

Diff for: demo/bigcache/main.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
package main
22

33
import (
4-
"log"
54
"time"
6-
7-
"github.com/google/gops/agent"
5+
"fmt"
6+
"github.com/allegro/bigcache"
87
)
98

109
func main() {
11-
if err := agent.Listen(nil); err != nil {
12-
log.Fatal(err)
10+
cache, _ := bigcache.NewBigCache(bigcache.DefaultConfig(10 * time.Minute))
11+
12+
13+
cache.Set("my-unique-key", []byte("value"))
14+
15+
entry, _ := cache.Get("my-unique-key")
16+
fmt.Println(string(entry))
17+
if string(entry) == "" {
18+
fmt.Println("不存在")
19+
}
20+
entry, _ = cache.Get("my-unique-key1")
21+
if string(entry) == "" {
22+
fmt.Println("不存在")
1323
}
14-
time.Sleep(time.Hour)
24+
fmt.Println(string(entry))
1525
}

Diff for: demo/net/udp/client/client.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net"
6+
)
7+
8+
func main() {
9+
// 创建连接
10+
socket, err := net.DialUDP("udp4", nil, &net.UDPAddr{
11+
IP: net.IPv4(127, 0, 0, 1),
12+
Port: 8080,
13+
})
14+
if err != nil {
15+
fmt.Println("连接失败!", err)
16+
return
17+
}
18+
defer socket.Close()
19+
20+
// 发送数据
21+
senddata := []byte("hello server!")
22+
_, err = socket.Write(senddata)
23+
if err != nil {
24+
fmt.Println("发送数据失败!", err)
25+
return
26+
}
27+
28+
// 接收数据
29+
data := make([]byte, 4096)
30+
read, remoteAddr, err := socket.ReadFromUDP(data)
31+
if err != nil {
32+
fmt.Println("读取数据失败!", err)
33+
return
34+
}
35+
fmt.Println(read, remoteAddr)
36+
fmt.Printf("%s\n", data)
37+
}

Diff for: demo/net/udp/server.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net"
6+
)
7+
8+
func main() {
9+
// 创建监听
10+
socket, err := net.ListenUDP("udp4", &net.UDPAddr{
11+
IP: net.IPv4(0, 0, 0, 0),
12+
Port: 8080,
13+
})
14+
if err != nil {
15+
fmt.Println("监听失败!", err)
16+
return
17+
}
18+
defer socket.Close()
19+
20+
for {
21+
// 读取数据
22+
data := make([]byte, 4096)
23+
read, remoteAddr, err := socket.ReadFromUDP(data)
24+
if err != nil {
25+
fmt.Println("读取数据失败!", err)
26+
continue
27+
}
28+
fmt.Println(read, remoteAddr)
29+
fmt.Printf("%s\n\n", data)
30+
31+
// 发送数据
32+
senddata := []byte("hello client!")
33+
_, err = socket.WriteToUDP(senddata, remoteAddr)
34+
if err != nil {
35+
return
36+
fmt.Println("发送数据失败!", err)
37+
}
38+
}
39+
}

Diff for: demo/riot/weibo/main.go

+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"encoding/gob"
6+
"flag"
7+
"fmt"
8+
"log"
9+
"os"
10+
"reflect"
11+
"strconv"
12+
"strings"
13+
14+
"github.com/go-ego/riot"
15+
"github.com/go-ego/riot/types"
16+
)
17+
18+
const (
19+
// SecondsInADay seconds in a day
20+
SecondsInADay = 86400
21+
// MaxTokenProximity max token proximity
22+
MaxTokenProximity = 150
23+
)
24+
25+
var (
26+
weiboData = flag.String(
27+
"weibo_data",
28+
"../../testdata/weibo_data.txt",
29+
"索引的微博帖子,每行当作一个文档")
30+
query = flag.String(
31+
"query",
32+
"chinajoy 游戏",
33+
"待搜索的短语")
34+
dictionaries = flag.String(
35+
"dictionaries",
36+
"../data/dict/dictionary.txt",
37+
"分词字典文件")
38+
stopTokenFile = flag.String(
39+
"stop_token_file",
40+
"../data/dict/stop_tokens.txt",
41+
"停用词文件")
42+
43+
searcher = riot.Engine{}
44+
options = types.RankOpts{
45+
ScoringCriteria: WeiboScoringCriteria{},
46+
OutputOffset: 0,
47+
MaxOutputs: 100,
48+
}
49+
searchQueries = []string{}
50+
)
51+
52+
// WeiboScoringFields 微博评分字段
53+
type WeiboScoringFields struct {
54+
// 帖子的时间戳
55+
Timestamp uint32
56+
57+
// 帖子的转发数
58+
RepostsCount uint32
59+
60+
// 帖子的长度
61+
TextLength int
62+
}
63+
64+
// WeiboScoringCriteria 自定义的微博评分规则
65+
type WeiboScoringCriteria struct {
66+
}
67+
68+
// Score score and sort
69+
func (criteria WeiboScoringCriteria) Score(
70+
doc types.IndexedDoc, fields interface{}) []float32 {
71+
if doc.TokenProximity > MaxTokenProximity { // 评分第一步
72+
return []float32{}
73+
}
74+
if reflect.TypeOf(fields) != reflect.TypeOf(WeiboScoringFields{}) {
75+
return []float32{}
76+
}
77+
output := make([]float32, 4)
78+
wsf := fields.(WeiboScoringFields)
79+
output[0] = float32(wsf.Timestamp / SecondsInADay) // 评分第二步
80+
output[1] = float32(int(doc.BM25)) // 评分第三步
81+
output[2] = float32(wsf.RepostsCount) // 评分第四步
82+
output[3] = float32(wsf.TextLength) // 评分第五步
83+
return output
84+
}
85+
86+
func main() {
87+
// 解析命令行参数
88+
flag.Parse()
89+
log.Printf("待搜索的短语为\"%s\"", *query)
90+
91+
// 初始化
92+
gob.Register(WeiboScoringFields{})
93+
searcher.Init(types.EngineOpts{
94+
Using: 1,
95+
SegmenterDict: *dictionaries,
96+
StopTokenFile: *stopTokenFile,
97+
IndexerOpts: &types.IndexerOpts{
98+
IndexType: types.LocsIndex,
99+
},
100+
DefaultRankOpts: &options,
101+
})
102+
defer searcher.Close()
103+
104+
// 读入微博数据
105+
file, err := os.Open(*weiboData)
106+
if err != nil {
107+
log.Fatal(err)
108+
}
109+
defer file.Close()
110+
log.Printf("读入文本 %s", *weiboData)
111+
scanner := bufio.NewScanner(file)
112+
lines := []string{}
113+
fieldsSlice := []WeiboScoringFields{}
114+
for scanner.Scan() {
115+
data := strings.Split(scanner.Text(), "||||")
116+
if len(data) != 10 {
117+
continue
118+
}
119+
timestamp, _ := strconv.ParseUint(data[1], 10, 32)
120+
repostsCount, _ := strconv.ParseUint(data[4], 10, 32)
121+
text := data[9]
122+
if text != "" {
123+
lines = append(lines, text)
124+
fields := WeiboScoringFields{
125+
Timestamp: uint32(timestamp),
126+
RepostsCount: uint32(repostsCount),
127+
TextLength: len(text),
128+
}
129+
fieldsSlice = append(fieldsSlice, fields)
130+
}
131+
}
132+
log.Printf("读入%d条微博\n", len(lines))
133+
134+
// 建立索引
135+
log.Print("建立索引")
136+
for i, text := range lines {
137+
searcher.IndexDoc(
138+
uint64(i),
139+
types.DocIndexData{Content: text, Fields: fieldsSlice[i]})
140+
}
141+
searcher.FlushIndex()
142+
log.Print("索引建立完毕")
143+
144+
// 搜索
145+
log.Printf("开始查询")
146+
output := searcher.Search(types.SearchReq{Text: *query})
147+
148+
// 显示
149+
fmt.Println("output...")
150+
fmt.Println(output)
151+
log.Printf("查询完毕")
152+
}

0 commit comments

Comments
 (0)