-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay_stream.js
95 lines (87 loc) · 3.17 KB
/
play_stream.js
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
"use strict"
var Config = require('./config')
var MaxHeartbeatCnt = 3;
var get_offsetted_now=function(offset){
var now = new Date()
var time = new Date(now.setDate(now.getDate() - offset))
if(Config.OffsetHour !==0){
return new Date(time.setHours(time.getHours()+Config.OffsetHour))
}
return time
}
class PlayStream{
constructor(date, hashtag, stream, Network){
this.Network = Network
this.date = date
this.hashtag=hashtag
this.stream=stream
this.time = new Date()
this.days_offset = Math.floor((new Date() - stream[0])/86400000)
this.t_last
this.t_now
this.ip = null
this.heartbeat_cnt = 0;
this.playHead = 0; //for performance reason, we use counter
this.timeoutInsts = [];
Log("created play for ", date, hashtag, stream.length, "events")
}
add(ip){
if(this.ip == null){
Log("adding", ip, "to", this.date, this.hashtag)
setTimeout(()=>{ this.checkHealth()}, 3000);
}
this.ip = ip
this.heartbeat_cnt = MaxHeartbeatCnt
}
checkHealth(){ // check every 3 second
if(this.ip!==null){
var that = this;
this.heartbeat_cnt --
if(this.heartbeat_cnt<0){
Log("removing", this.ip, "from", this.date, this.hashtag)
this.ip = null
}else{
setTimeout(()=>{ this.checkHealth()}, 3000);
}
}
}
remove(ip){
this.ip = null
}
tick(remove_from_timeoutInsts, msg){
if(remove_from_timeoutInsts) this.timeoutInsts.shift()
this.Network.send(this.ip, msg)
}
startTweet(){ //start from current time
this.days_offset = Math.floor((new Date() - this.stream[0])/86400000)
this.t_last = get_offsetted_now(this.days_offset)
this.playHead = _.filter(this.stream, t=>{return t < this.t_last }).length
}
renderTweet(){
this.t_now = get_offsetted_now(this.days_offset)
if(this.stream[this.playHead]<this.t_now){
if(this.ip!==null){
this.Network.send(this.ip, "255,255,255,1")
if(Config.DEBUG) Log(this.date, this.hashtag, this.t_now)
}
this.playHead++
}
this.t_last = this.t_now
}
renderRegular(option, idx){
if(option.sequential==false && option.randomize == false){
this.tick(false, option.msg)
}else if(option.sequential && option.randomize == false){
this.timeoutInsts.push(setTimeout(()=>{this.tick(true, option.msg)}, idx*option.sequential_delta))
}else if(option.sequential==false && option.randomize){
this.timeoutInsts.push(setTimeout(()=>{this.tick(true, option.msg)}, Math.random()* option.randomize_range))
}else if(option.sequential && option.randomize){
this.timeoutInsts.push(setTimeout(()=>{this.tick(true, option.msg)}, idx*option.sequential_delta + Math.random()* option.randomize_range))
}
}
stopRegular(){
this.timeoutInsts.forEach(inst=>{clearTimeout(inst)})
this.timeoutInsts.length = 0
}
}
module.exports=PlayStream;