Split to Threads Complete

This commit is contained in:
Brian Buller 2014-12-29 08:06:24 -06:00
parent 0c96801906
commit 46b8d5bea0
3 changed files with 70 additions and 13 deletions

View File

@ -73,15 +73,29 @@ func main() {
mm := util.NewManager(stdin)
util.LoadConfig(&mm)
// The forever loop to monitor the channel
loop:
for {
s, ok := <-ch
if !ok {
break loop
go func() {
for {
s, ok := <-ch
if !ok {
break
}
m := util.NewMessage(s)
fmt.Printf("\x1b[34;1m%s\x1b[0m", m.Output())
mm.ProcessMessage(s)
}
m := util.NewMessage(s)
fmt.Printf("\x1b[34;1m%s\x1b[0m", m.Output())
mm.ProcessMessage(s)
}()
// The forever loop to monitor everything
for {
time.Sleep(time.Second)
if util.StopServer {
break
}
// fmt.Printf("Monitoring... (%d users)\n", len(util.GetConfig().LoggedInUsers))
// for i, u := range util.GetConfig().LoggedInUsers {
// if !u.HasQuota() {
// fmt.Printf(">> User %s is out of quota\n", u.Name)
// }
// }
}
}

View File

@ -4,9 +4,6 @@ import (
"fmt"
"github.com/antonholmquist/jason"
"io/ioutil"
// "log"
// "os"
// "bytes"
"strings"
)
@ -22,6 +19,7 @@ type Config struct {
}
var c *Config
var StopServer = false
func LoadConfig(mm *MessageManager) {
c = new(Config)
@ -35,6 +33,7 @@ func LoadConfig(mm *MessageManager) {
AddListener(func(i *Message) bool {
if i.User.IsOp && i.Text == "!stop\n" {
mm.Output("stop")
StopServer = true
return true
}
return false
@ -169,11 +168,32 @@ func LoadConfig(mm *MessageManager) {
find = r[0]
// find should be the user name now
LoginUser(*FindUser(find, true))
return true
}
}
}
return false
})
// Add logout listener
AddListener(func(i *Message) bool {
if i.User.Name == "" && strings.Contains(i.Text, " lost connection: ") {
// Find the user that just logged out
r := strings.Split(i.Text, "]: ")
find := ""
if len(r) > 0 {
find = r[1]
r := strings.Split(find, " lost connection: ")
if len(r) > 0 {
find = r[0]
// find should be the user name now
LogoutUser(*FindUser(find, false))
return true
}
}
}
return false
})
// Add !help listener
AddListener(func(i *Message) bool {
if i.User.Name != "" && i.Text == "!help\n" {

View File

@ -1,5 +1,9 @@
package util
import (
"time"
)
type User struct {
Name string
Index int
@ -7,6 +11,9 @@ type User struct {
Home string
Porch string
ToJSONString func() string
Quota time.Duration
quotaUsed time.Duration
loginTime time.Time
}
func NewUser(nm string) *User {
@ -19,7 +26,23 @@ func NewUser(nm string) *User {
m.Home = ""
m.Porch = ""
m.ToJSONString = func() string {
return "{\"name\":\"" + m.Name + "\",\"home\":\"" + m.Home + "\",\"porch\":\"" + m.Porch + "\"}"
return "{\"name\":\"" + m.Name + "\",\"home\":\"" + m.Home + "\",\"porch\":\"" + m.Porch + "\",\"quota\":\"" + string(m.Quota) + "\",\"quota_used\":\"" + string(m.quotaUsed) + "\"}"
}
return m
}
func (u *User) HasQuota() bool {
if u.Quota > 0 {
return u.quotaUsed < u.Quota
} else {
return true
}
}
func (u *User) RemainingQuota() time.Duration {
if u.Quota > 0 {
return u.Quota - u.quotaUsed
} else {
return 0
}
}