client: Start up and shutdown connections properly

This commit is contained in:
Daniel Oaks 2016-02-09 23:51:52 +10:00
parent 44fc747e46
commit 306fefe557
3 changed files with 27 additions and 7 deletions

@ -25,6 +25,10 @@ type ServerConnection struct {
eventsIn eventmgr.EventManager
eventsOut eventmgr.EventManager
// data we keep track of
//features ServerFeatures
//caps ClientCapabilities
// details users must supply before connection
Nick string
InitialUser string
@ -52,6 +56,8 @@ func (sc *ServerConnection) Connect(address string, ssl bool) {
sc.Send(nil, "", "NICK", sc.Nick)
sc.Send(nil, "", "USER", sc.InitialUser, "0", "*", sc.InitialRealName)
sc.Connected = true
go sc.receiveLoop()
}
@ -104,9 +110,11 @@ func (sc *ServerConnection) RegisterEvent(direction string, name string, handler
}
}
// Shutdown exits from the server.
// Shutdown closes the connection to the server.
func (sc *ServerConnection) Shutdown(message string) {
sc.Send(nil, sc.Nick, "QUIT", message)
sc.Send(nil, "", "QUIT", message)
sc.Connected = false
sc.connection.Close()
}
// Send sends an IRC message to the server.

@ -16,7 +16,7 @@ type eventRegistration struct {
// Reactor is the start-point for gircclient. It creates and manages
// ServerConnections.
type Reactor struct {
ServerConnections map[string]ServerConnection
ServerConnections map[string]*ServerConnection
eventsToRegister []eventRegistration
}
@ -24,23 +24,23 @@ type Reactor struct {
func NewReactor() Reactor {
var newReactor Reactor
newReactor.ServerConnections = make(map[string]ServerConnection, 0)
newReactor.ServerConnections = make(map[string]*ServerConnection, 0)
newReactor.eventsToRegister = make([]eventRegistration, 0)
return newReactor
}
// CreateServer creates a ServerConnection and returns it.
func (r *Reactor) CreateServer(name string) ServerConnection {
func (r *Reactor) CreateServer(name string) *ServerConnection {
var sc ServerConnection
r.ServerConnections[name] = sc
r.ServerConnections[name] = &sc
for _, e := range r.eventsToRegister {
sc.RegisterEvent(e.Direction, e.Name, e.Handler, e.Priority)
}
return sc
return &sc
}
// Shutdown shuts down all ServerConnections.

@ -43,6 +43,18 @@ func TestServerConnection(t *testing.T) {
)
}
// shutdown client
reactor.Shutdown(" Get mad! ")
message, _ = reader.ReadString('\n')
if message != "QUIT : Get mad! \r\n" {
t.Error(
"Did not receive QUIT message, received: [",
message,
"]",
)
}
// close connection and listener
conn.Close()
listener.Close()