Skip to content

Commit

Permalink
basic user stats support
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Cooper committed Nov 21, 2014
1 parent 27af6b4 commit 9f45f7a
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 9 deletions.
1 change: 1 addition & 0 deletions gumble/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type UserChangeEvent struct {
NameChanged bool
ChannelChanged bool
CommentChanged bool
StatsChanged bool
}

type ChannelChangeEvent struct {
Expand Down
52 changes: 43 additions & 9 deletions gumble/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gumble
import (
"bytes"
"errors"
"time"

"code.google.com/p/goprotobuf/proto"
"github.com/bontibon/gopus"
Expand Down Expand Up @@ -51,12 +52,7 @@ func init() {
}
}

func handleVersion(client *Client, buffer []byte) error {
var packet MumbleProto.Version
if err := proto.Unmarshal(buffer, &packet); err != nil {
return err
}

func parseVersion(packet *MumbleProto.Version) Version {
var version Version
if packet.Version != nil {
version.version = *packet.Version
Expand All @@ -70,8 +66,16 @@ func handleVersion(client *Client, buffer []byte) error {
if packet.OsVersion != nil {
version.osVersion = *packet.OsVersion
}
client.server.version = version
return version
}

func handleVersion(client *Client, buffer []byte) error {
var packet MumbleProto.Version
if err := proto.Unmarshal(buffer, &packet); err != nil {
return err
}

client.server.version = parseVersion(&packet)
return nil
}

Expand Down Expand Up @@ -515,8 +519,38 @@ func handleCodecVersion(client *Client, buffer []byte) error {
}

func handleUserStats(client *Client, buffer []byte) error {
// TODO
return errUnimplementedHandler
var packet MumbleProto.UserStats
if err := proto.Unmarshal(buffer, &packet); err != nil {
return err
}

if packet.Session == nil {
return errIncompleteProtobuf
}
user := client.users.BySession(uint(*packet.Session))
if user == nil {
return errInvalidProtobuf
}

if packet.Version != nil {
user.stats.version = parseVersion(packet.Version)
}
if packet.Onlinesecs != nil {
user.stats.connected = time.Now().Add(time.Duration(*packet.Onlinesecs) * -time.Second)
}
if packet.Idlesecs != nil {
user.stats.idle = time.Duration(*packet.Idlesecs) * time.Second
}

user.statsFetched = true

event := &UserChangeEvent{
Client: client,
User: user,
StatsChanged: true,
}
client.listeners.OnUserChange(event)
return nil
}

func handleRequestBlob(client *Client, buffer []byte) error {
Expand Down
17 changes: 17 additions & 0 deletions gumble/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ type User struct {
texture, textureHash []byte
prioritySpeaker bool
recording bool

statsFetched bool
stats UserStats
}

// Session returns the user's session Id.
Expand Down Expand Up @@ -197,3 +200,17 @@ func (u *User) SetSelfDeafened(muted bool) {
}
u.client.Send(protoMessage{&packet})
}

// Stats returns the user's stats, and a boolean value specifying if the stats
// are valid or not.
func (u *User) Stats() (UserStats, bool) {
return u.stats, u.statsFetched
}

// RequestStats requests user stats of the given user.
func (u *User) RequestStats() {
packet := MumbleProto.UserStats{
Session: &u.session,
}
u.client.Send(protoMessage{&packet})
}
26 changes: 26 additions & 0 deletions gumble/user_stats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package gumble

import (
"time"
)

type UserStats struct {
version Version
connected time.Time
idle time.Duration
}

// Version returns the user's version.
func (us *UserStats) Version() Version {
return us.version
}

// Connected returns when the user connected to the server.
func (us *UserStats) Connected() time.Time {
return us.connected
}

// Idle returns how long the user has been idle.
func (us *UserStats) Idle() time.Duration {
return us.idle
}

0 comments on commit 9f45f7a

Please sign in to comment.