From 79eb6b4388000c572a4fe2916403600a2c827b24 Mon Sep 17 00:00:00 2001 From: Akshay Shekher Date: Sun, 14 Dec 2014 09:29:34 +0530 Subject: [PATCH] Embedded lock in to server struct --- server.go | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/server.go b/server.go index bf86515..be3843d 100644 --- a/server.go +++ b/server.go @@ -28,7 +28,6 @@ type Server struct { sshConfig *ssh.ServerConfig done chan struct{} clients Clients - lock sync.Mutex count int history *History motd string @@ -36,6 +35,7 @@ type Server struct { bannedPk map[string]*time.Time // fingerprint lookup bannedIp map[net.Addr]*time.Time started time.Time + sync.Mutex } func NewServer(privateKey []byte) (*Server, error) { @@ -119,9 +119,9 @@ func (s *Server) Privmsg(nick, message string, sender *Client) error { } func (s *Server) SetMotd(client *Client, motd string) { - s.lock.Lock() + s.Lock() s.motd = motd - s.lock.Unlock() + s.Unlock() } func (s *Server) MotdUnicast(client *Client) { @@ -142,7 +142,7 @@ func (s *Server) Add(client *Client) { client.SysMsg("Welcome to ssh-chat. Enter /help for more.") }() - s.lock.Lock() + s.Lock() s.count++ newName, err := s.proposeName(client.Name) @@ -153,15 +153,15 @@ func (s *Server) Add(client *Client) { client.Rename(newName) s.clients[client.Name] = client num := len(s.clients) - s.lock.Unlock() + s.Unlock() s.Broadcast(ContinuousFormat(SYSTEM_MESSAGE_FORMAT, fmt.Sprintf(" * %s joined. (Total connected: %d)", client.ColoredName(), num)), client) } func (s *Server) Remove(client *Client) { - s.lock.Lock() + s.Lock() delete(s.clients, client.Name) - s.lock.Unlock() + s.Unlock() s.SysMsg("%s left.", client.ColoredName()) } @@ -187,12 +187,12 @@ func (s *Server) proposeName(name string) (string, error) { } func (s *Server) Rename(client *Client, newName string) { - s.lock.Lock() + s.Lock() newName, err := s.proposeName(newName) if err != nil { client.SysMsg("%s", err) - s.lock.Unlock() + s.Unlock() return } @@ -201,7 +201,7 @@ func (s *Server) Rename(client *Client, newName string) { oldName := client.Name client.Rename(newName) s.clients[client.Name] = client - s.lock.Unlock() + s.Unlock() s.SysMsg("%s is now known as %s.", ColorString(client.Color, oldName), ColorString(client.Color, newName)) } @@ -225,9 +225,9 @@ func (s *Server) Who(name string) *Client { func (s *Server) Op(fingerprint string) { logger.Infof("Adding admin: %s", fingerprint) - s.lock.Lock() + s.Lock() s.admins[fingerprint] = struct{}{} - s.lock.Unlock() + s.Unlock() } func (s *Server) Uptime() string { @@ -256,19 +256,19 @@ func (s *Server) IsBanned(fingerprint string) bool { func (s *Server) Ban(fingerprint string, duration *time.Duration) { var until *time.Time - s.lock.Lock() + s.Lock() if duration != nil { when := time.Now().Add(*duration) until = &when } s.bannedPk[fingerprint] = until - s.lock.Unlock() + s.Unlock() } func (s *Server) Unban(fingerprint string) { - s.lock.Lock() + s.Lock() delete(s.bannedPk, fingerprint) - s.lock.Unlock() + s.Unlock() } func (s *Server) Start(laddr string) error {