Fixes against weekly.2012-02-22

Convert time/duration variables
Clean up duration math
This commit is contained in:
Michael Jard 2012-02-25 00:52:19 -08:00
parent 4e661a3954
commit a0256382be
3 changed files with 19 additions and 15 deletions

12
irc.go
View File

@ -26,7 +26,7 @@ func reader(irc *IRCConnection) {
irc.Error <- err irc.Error <- err
break break
} }
irc.lastMessage = time.Seconds() irc.lastMessage = time.Now()
msg = msg[0 : len(msg)-2] //Remove \r\n msg = msg[0 : len(msg)-2] //Remove \r\n
event := &IRCEvent{Raw: msg} event := &IRCEvent{Raw: msg}
if msg[0] == ':' { if msg[0] == ':' {
@ -75,18 +75,18 @@ func writer(irc *IRCConnection) {
//Pings the server if we have not recived any messages for 5 minutes //Pings the server if we have not recived any messages for 5 minutes
func pinger(i *IRCConnection) { func pinger(i *IRCConnection) {
i.ticker = time.Tick(1000 * 1000 * 1000 * 60 * 1) //Tick every minute. i.ticker = time.Tick(1 * time.Minute) //Tick every minute.
i.ticker2 = time.Tick(1000 * 1000 * 1000 * 60 * 15) //Tick every 15 minutes. i.ticker2 = time.Tick(15 * time.Minute) //Tick every 15 minutes.
for { for {
select { select {
case <-i.ticker: case <-i.ticker:
//Ping if we haven't recived anything from the server within 4 minutes //Ping if we haven't recived anything from the server within 4 minutes
if time.Seconds()-i.lastMessage >= 60*4 { if time.Since(i.lastMessage) >= (4 * time.Minute) {
i.SendRaw(fmt.Sprintf("PING %d", time.Nanoseconds())) i.SendRaw(fmt.Sprintf("PING %d", time.Now().UnixNano()))
} }
case <-i.ticker2: case <-i.ticker2:
//Ping every 15 minutes. //Ping every 15 minutes.
i.SendRaw(fmt.Sprintf("PING %d", time.Nanoseconds())) i.SendRaw(fmt.Sprintf("PING %d", time.Now().UnixNano()))
//Try to recapture nickname if it's not as configured. //Try to recapture nickname if it's not as configured.
if i.nick != i.nickcurrent { if i.nick != i.nickcurrent {
i.nickcurrent = i.nick i.nickcurrent = i.nick

View File

@ -1,10 +1,10 @@
package irc package irc
import ( import (
"strings"
"fmt" "fmt"
"time"
"strconv" "strconv"
"strings"
"time"
) )
func (irc *IRCConnection) AddCallback(eventcode string, callback func(*IRCEvent)) { func (irc *IRCConnection) AddCallback(eventcode string, callback func(*IRCEvent)) {
@ -80,7 +80,7 @@ func (irc *IRCConnection) setupCallbacks() {
}) })
irc.AddCallback("CTCP_TIME", func(e *IRCEvent) { irc.AddCallback("CTCP_TIME", func(e *IRCEvent) {
ltime := time.LocalTime() ltime := time.Now()
irc.SendRaw(fmt.Sprintf("NOTICE %s :\x01TIME %s\x01", e.Nick, ltime.String())) irc.SendRaw(fmt.Sprintf("NOTICE %s :\x01TIME %s\x01", e.Nick, ltime.String()))
}) })
@ -101,8 +101,9 @@ func (irc *IRCConnection) setupCallbacks() {
}) })
irc.AddCallback("PONG", func(e *IRCEvent) { irc.AddCallback("PONG", func(e *IRCEvent) {
ns, _ := strconv.Atoi64(e.Message) ns, _ := strconv.ParseInt(e.Message, 10, 64)
fmt.Printf("Lag: %fs\n", float32((time.Nanoseconds()-ns))/1000/1000/1000) delta := time.Duration(time.Now().UnixNano() - ns)
fmt.Printf("Lag: %vs\n", delta)
}) })
irc.AddCallback("NICK", func(e *IRCEvent) { irc.AddCallback("NICK", func(e *IRCEvent) {

View File

@ -4,7 +4,10 @@
package irc package irc
import "net" import (
"net"
"time"
)
type IRCConnection struct { type IRCConnection struct {
socket net.Conn socket net.Conn
@ -19,9 +22,9 @@ type IRCConnection struct {
Password string Password string
events map[string][]func(*IRCEvent) events map[string][]func(*IRCEvent)
lastMessage int64 lastMessage time.Time
ticker <-chan int64 ticker <-chan time.Time
ticker2 <-chan int64 ticker2 <-chan time.Time
VerboseCallbackHandler bool VerboseCallbackHandler bool