diff --git a/src/main.go b/main.go similarity index 95% rename from src/main.go rename to main.go index fbdd46a..7300618 100644 --- a/src/main.go +++ b/main.go @@ -3,7 +3,6 @@ package main import "log" import "os" import "sircd" -import "sync" import "time" func main() { diff --git a/sircd/client.go b/sircd/client.go new file mode 100644 index 0000000..e71aa31 --- /dev/null +++ b/sircd/client.go @@ -0,0 +1,40 @@ +package sircd + +import ( + "log" + "net" + "bytes" +) + +func newClientIRCConnection(conn net.Conn, log *log.Logger) *clientIRCConnection { + c := new(clientIRCConnection) + c.conn = conn + c.log = log + c.inputBytes = new(bytes.Buffer) + c.outputBytes = new(bytes.Buffer) + c.ReadSocket() + return c +} + +type clientIRCConnection struct { + Id uint64 + conn net.Conn + log *log.Logger + inputBytes *bytes.Buffer + outputBytes *bytes.Buffer +} + +func (c *clientIRCConnection) ReadSocket() { + // Read the incoming connection into the buffer. + buf := make([]byte, 1024*1024) + bytesRead, err := c.conn.Read(buf) + c.log.Printf("conn<%d>: read %d bytes from net", c.Id, bytesRead) + if err != nil { + c.log.Println("Error reading:", err.Error()) + } + c.inputBytes.Write(buf) +} + +func (c *clientIRCConnection) ParseMessages() { + c.log.Fatalln("not implemented") +} diff --git a/src/sircd/server.go b/sircd/server.go similarity index 52% rename from src/sircd/server.go rename to sircd/server.go index b74c052..283adb3 100644 --- a/src/sircd/server.go +++ b/sircd/server.go @@ -1,19 +1,20 @@ package sircd import ( + "log" "net" "os" - "log" + "container/list" ) type sircd struct { - Running bool - log *log.Logger - netName string - ircPort uint16 - httpPort uint16 + Running bool + log *log.Logger + netName string + ircPort uint16 + httpPort uint16 ircListener net.Listener - ircClients []*clientIRCConnection + ircClients *list.List } const ( @@ -24,25 +25,26 @@ const ( func NewSircd() *sircd { s := new(sircd) - s.Running = true + s.Running = true + s.ircClients = list.New() return s } func (s *sircd) SetLogger(l *log.Logger) { - s.log = l + s.log = l } func (s *sircd) startIRCServer() { - var err error + var err error s.ircListener, err = net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT) - if err != nil { + if err != nil { log.Println("Error listening:", err.Error()) os.Exit(1) - } - defer s.ircListener.Close() - s.log.Println("Listening for irc on " + CONN_HOST + ":" + CONN_PORT) + } + defer s.ircListener.Close() + s.log.Println("Listening for irc on " + CONN_HOST + ":" + CONN_PORT) - for { + for { // Listen for an incoming connection. conn, err := s.ircListener.Accept() if err != nil { @@ -52,24 +54,27 @@ func (s *sircd) startIRCServer() { go s.handleIRCConnection(conn) } +} +func (s *sircd) processIRC() { + for { + s.readFromClients() + } +} + +func (s *sircd) readFromClients() { + for e := s.ircClients.Front(); e != nil; e = e.Next() { + client := e.Value.(clientIRCConnection) + client.ReadSocket() + client.ParseMessages() + } } func (s *sircd) Start() { - go s.startIRCServer() + go s.startIRCServer() } func (s *sircd) handleIRCConnection(conn net.Conn) { - c := new(clientIRCConnection) - c.conn = conn - c.log = s.log - - if err != nil { - c.log.Println("Error reading:", err.Error()) - } - // Send a response back to person contacting us. - conn.Write([]byte("Message received.")) - s.log.Println("Received message (%i bytes): %s", byteLen, buf) - // Close the connection when you're done with it. - conn.Close() + c := newClientIRCConnection(conn, s.log) + s.ircClients.PushBack(c) } diff --git a/src/sircd/client.go b/src/sircd/client.go deleted file mode 100644 index 7f1f197..0000000 --- a/src/sircd/client.go +++ /dev/null @@ -1,22 +0,0 @@ -package sircd - -import ( - "net" - "os" - "log" -) - -type clientIRCConnection struct { - conn net.Conn - log *log.Logger -} - -func (c *clientIRCConnection) Read() *[]byte { - buf := make([]byte, 1024*1024) - // Read the incoming connection into the buffer. - _, err := c.conn.Read(buf) - if err != nil { - s.log.Println("Error reading:", err.Error()) - } - return &buf -}