-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathad_dict.go
202 lines (189 loc) · 4.92 KB
/
ad_dict.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
package core
import (
"io"
"os"
"fmt"
"strings"
"bufio"
"strconv"
"time"
)
type AdUnitInfo struct {
UnitId uint32
CreativeId uint32
}
type AdCreativeInfo struct {
CreativeId uint32
Title string
Description string
AppPackageName string // for native app only
IconImageUrl string
MainImageUrl string
ClickUrl string
}
type AdDataInfo struct {
AdUnitMap map[uint32]AdUnitInfo
AdCreativeMap map[uint32]AdCreativeInfo
LocationUnitMap map[string][]uint32
}
func NewAdDataInfo() *AdDataInfo {
return &AdDataInfo{
AdUnitMap: make(map[uint32]AdUnitInfo),
AdCreativeMap: make(map[uint32]AdCreativeInfo),
LocationUnitMap: make(map[string][]uint32),
}
}
type AdDict struct {
AdDataArray []*AdDataInfo
CurrentIndex uint32
FileName string
LastModifiedTime int64
}
var AdDictObject *AdDict
// 初始化广告对象
func NewAdDict(dictFileName string) *AdDict {
adDictObject := &AdDict{
AdDataArray: make([]*AdDataInfo, 2, 2),
CurrentIndex: 0,
FileName: dictFileName,
LastModifiedTime: 0,
}
for i := 0; i < 2; i++ {
adDictObject.AdDataArray[i] = NewAdDataInfo()
}
return adDictObject
}
// 初始化之后首次加载广告信息
func (ad *AdDict) Load() error {
adDataInfo, err := ad.loadAdDict()
if err != nil {
return err
}
ad.AdDataArray[ad.CurrentIndex] = adDataInfo
fileStat, err := os.Stat(ad.FileName)
if err != nil {
return err
}
ad.LastModifiedTime = fileStat.ModTime().Unix()
return nil
}
// 启动定时器,用于定期重新加载广告信息
func (ad *AdDict) StartReloadTimer() {
duration := int64(time.Second) * GlobalConfObject.AdFileReloadInterval
t := time.NewTicker(time.Duration(duration))
go func() {
for t1 := range t.C {
AdServerLog.Debug("AdDict reload timer execute")
fileStat, _ := os.Stat(ad.FileName)
currentModifiedTime := fileStat.ModTime().Unix()
// 如果文件有更新,则重新加载广告内容
if currentModifiedTime > ad.LastModifiedTime {
AdServerLog.Info(fmt.Sprintf("start reload ad info dict at %s",
t1.Format("2006-01-02 03:04:05")))
adDataInfo,err := ad.loadAdDict()
if err != nil {
continue
}
nextIndex := 1 - ad.CurrentIndex
ad.AdDataArray[nextIndex] = adDataInfo
ad.CurrentIndex = nextIndex
ad.LastModifiedTime = currentModifiedTime
}
}
}()
}
// 获取当前可用的广告信息
func (ad *AdDict) GetCurrentAdData() *AdDataInfo {
return ad.AdDataArray[ad.CurrentIndex]
}
func (ad *AdDict) loadAdDict() (*AdDataInfo, error) {
dictFile, err := os.Open(ad.FileName)
if err != nil {
AdServerLog.Error(fmt.Sprintf(
"open file error, name=%s\n", ad.FileName))
return nil, err
}
defer dictFile.Close()
adDataInfo := NewAdDataInfo()
lineNum := 0
br := bufio.NewReader(dictFile)
for {
line, _, err := br.ReadLine()
if err == io.EOF {
break
}
lineString := string(line)
lines := strings.Split(lineString, "\t")
level, err := strconv.Atoi(lines[0])
if err != nil {
return nil, err
}
if level == 1 {
// ad unit info
unitId, err := strconv.ParseUint(lines[1], 10, 32)
if err != nil {
return nil, err
}
creativeId, err := strconv.ParseUint(lines[2], 10, 32)
if err != nil {
return nil, err
}
adUnit := AdUnitInfo{
UnitId: uint32(unitId),
CreativeId: uint32(creativeId),
}
adDataInfo.AdUnitMap[adUnit.UnitId] = adUnit
lineNum++
AdServerLog.Debug(fmt.Sprintf(
"read ad unit info, unitId=%d creativeId=%d\n",
unitId, creativeId))
} else if level == 2 {
// creative info
creativeId, _ := strconv.ParseUint(lines[1], 10, 32)
title := lines[2]
description := lines[3]
packageName := lines[4]
iconImageUrl := lines[5]
mainImageUrl := lines[6]
clickUrl := lines[7]
adCreative := AdCreativeInfo{
CreativeId: uint32(creativeId),
Title: title,
Description: description,
AppPackageName: packageName,
IconImageUrl: iconImageUrl,
MainImageUrl: mainImageUrl,
ClickUrl: clickUrl,
}
adDataInfo.AdCreativeMap[adCreative.CreativeId] = adCreative
lineNum++
AdServerLog.Debug(fmt.Sprintf(
"read ad creative info, creativeId=%d " +
"title=%s description=%s package=%s iconImageUrl=%s " +
"mainImageUrl=%s clickUrl=%s\n",
creativeId, title, description, packageName,
iconImageUrl, mainImageUrl, clickUrl))
} else if level == 3 {
// location target
unitId, _ := strconv.ParseUint(lines[1], 10, 32)
country := strings.ToLower(lines[2])
city := strings.ToLower(lines[3])
key := country + "_" + city
unitIdList, exist := adDataInfo.LocationUnitMap[key]
if !exist {
unitIdList = make([]uint32, 0)
}
unitIdList = append(unitIdList, uint32(unitId))
adDataInfo.LocationUnitMap[key] = unitIdList
AdServerLog.Debug(fmt.Sprintf(
"read location target info, unitId=%d country=%s city=%s\n",
unitId, country, city))
lineNum++
} else {
panic(1)
}
}
AdServerLog.Info(fmt.Sprintf(
"read ad info file success, lineNum=%d\n", lineNum))
return adDataInfo, nil
}