From e9264923d086aeacbb874b31fe28ded71124d0f5 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sat, 15 Jun 2013 11:32:31 -0400 Subject: [PATCH] moved connection manager to its own package --- connection_manager.go => cm/manager.go | 18 ++++++++---------- tcp_connections.go | 5 ++++- 2 files changed, 12 insertions(+), 11 deletions(-) rename connection_manager.go => cm/manager.go (62%) diff --git a/connection_manager.go b/cm/manager.go similarity index 62% rename from connection_manager.go rename to cm/manager.go index 0a72146..156fbaa 100644 --- a/connection_manager.go +++ b/cm/manager.go @@ -1,19 +1,17 @@ -package main +package cm import ( "net" ) -var manager = newConnectionManager() - -type connectionManager struct { +type Manager struct { active []net.Conn connect chan net.Conn disconnect chan net.Conn } -func newConnectionManager() *connectionManager { - m := &connectionManager{ +func New() *Manager { + m := &Manager{ active: make([]net.Conn, 0, 10), connect: make(chan net.Conn), disconnect: make(chan net.Conn), @@ -22,7 +20,7 @@ func newConnectionManager() *connectionManager { return m } -func (m *connectionManager) run() { +func (m *Manager) run() { for { select { case conn := <-m.connect: @@ -33,15 +31,15 @@ func (m *connectionManager) run() { } } -func (m *connectionManager) Add(conn net.Conn) { +func (m *Manager) Add(conn net.Conn) { m.connect <- conn } -func (m *connectionManager) Remove(conn net.Conn) { +func (m *Manager) Remove(conn net.Conn) { m.disconnect <- conn } -func (m *connectionManager) removeConnection(conn net.Conn) { +func (m *Manager) removeConnection(conn net.Conn) { for i, other := range m.active { if conn.RemoteAddr() == other.RemoteAddr() { m.active = append(m.active[:i], m.active[i+1:]...) diff --git a/tcp_connections.go b/tcp_connections.go index a3984b2..0d3276f 100644 --- a/tcp_connections.go +++ b/tcp_connections.go @@ -1,12 +1,15 @@ package main import ( + "./cm" "bufio" "fmt" "io" "net" ) +var manager = cm.New() + func tcpInterpreter(conn net.Conn, userinput chan string, out chan interface{}, errors chan error) { prompt := func() { io.WriteString(conn, "> ") @@ -42,7 +45,7 @@ func runTCPServer() { } } -func startConnection(conn net.Conn, m *connectionManager) { +func startConnection(conn net.Conn, m *cm.Manager) { m.Add(conn) defer m.Remove(conn)