implement disconnected state.

This commit is contained in:
Liam Stanley 2016-11-19 11:13:49 -05:00
parent 9681ea7ea6
commit cffade04c8
4 changed files with 21 additions and 10 deletions

@ -28,11 +28,9 @@
- [ ] ip/host binding?
- [ ] `User.Age()`? (`FirstActive()`?) (time since first seen)
- [ ] cleanup docs in conn.go & event.go
- [ ] add `DISCONNECTED` command state
- [ ] add `Client.IsInChannel()`? and/or basic channel list
- [ ] add `Client.Topic(topic string)`
- [ ] `MODE` tracking on a per-channel basis
- [ ] `Event.PrettyString()`?
- [ ] `Client.AddTmpCallback()` for one time use callbacks?
## Installing

@ -13,7 +13,7 @@ import (
// handleEvent runs the necessary callbacks for the incoming event
func (c *Client) handleEvent(event *Event) {
// Log the event.
c.log.Print("<-- " + event.String())
c.log.Print("<-- " + event.Raw())
// Wildcard callbacks first.
if callbacks, ok := c.callbacks[ALLEVENTS]; ok {

@ -6,9 +6,10 @@ package girc
// Misc constants for use with the client.
const (
ALLEVENTS = "*" // trigger on all events
CONNECTED = "CONNECTED" // event command which can be used to start responding, after SUCCESS
SUCCESS = "001" // RPL_WELCOME alias, assumes successful connection
ALLEVENTS = "*" // trigger on all events
CONNECTED = "CONNECTED" // event command which can be used to start responding, after SUCCESS
DISCONNECTED = "DISCONNECTED" // event command which lets the client know it disconnected.
SUCCESS = "001" // RPL_WELCOME alias, assumes successful connection
)
// User/channel prefixes :: RFC1459

20
main.go

@ -131,14 +131,22 @@ func New(config Config) *Client {
// Quit disconnects from the server.s
func (c *Client) Quit(message string) {
c.hasQuit = true
c.Send(&Event{Command: QUIT, Trailing: message})
c.hasQuit = true
// Send the disconnected event for anything broadcasting.
c.Events <- &Event{Command: DISCONNECTED}
if c.conn != nil {
c.conn.Close()
}
// Sleep for a second, to give enough time to the callbacks setup
// for the DISCONNECTED event to actually start running.
time.Sleep(1 * time.Second)
// Send to the quit channel, so if Client.Loop() is being used, this will
// return.
c.quitChan <- struct{}{}
}
@ -237,6 +245,9 @@ func (c *Client) Reconnect() (err error) {
return nil
}
// Send the disconnected event for anything broadcasting.
c.Events <- &Event{Command: DISCONNECTED}
if c.Config.ReconnectDelay < (10 * time.Second) {
c.Config.ReconnectDelay = 10 * time.Second
}
@ -245,14 +256,15 @@ func (c *Client) Reconnect() (err error) {
var err error
c.conn.Close()
// Re-setup events.
c.Events = make(chan *Event, 40)
// Delay so we're not slaughtering the server with a bunch of
// connections.
c.log.Printf("reconnecting to %s in %s", c.Server(), c.Config.ReconnectDelay)
time.Sleep(c.Config.ReconnectDelay)
// Re-setup events. Do this after we've slept (giving callbacks enough time to
// finish their tasks.)
c.Events = make(chan *Event, 40)
for err = c.Connect(); err != nil && c.tries < c.Config.MaxRetries; c.tries++ {
c.log.Printf("reconnecting to %s in %s (%d tries)", c.Server(), c.Config.ReconnectDelay, c.tries)
time.Sleep(c.Config.ReconnectDelay)