Skip to content
This repository was archived by the owner on Jul 8, 2020. It is now read-only.

Commit b60255f

Browse files
committed
add Init for Driver interface
1 parent 6384818 commit b60255f

3 files changed

Lines changed: 21 additions & 25 deletions

File tree

conn.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,35 +52,33 @@ func newSessionId() string {
5252
// message when the connection closes. This loop will be running inside a
5353
// goroutine, so use this channel to be notified when the connection can be
5454
// cleaned up.
55-
func (Conn *Conn) Serve() {
56-
Conn.logger.Print("Connection Established")
55+
func (conn *Conn) Serve() {
56+
conn.logger.Print("Connection Established")
5757
// send welcome
58-
Conn.writeMessage(220, Conn.server.WelcomeMessage)
58+
conn.writeMessage(220, conn.server.WelcomeMessage)
5959
// read commands
6060
for {
61-
/*if Conn.dataConn == nil {
62-
break
63-
}*/
64-
line, err := Conn.controlReader.ReadString('\n')
61+
line, err := conn.controlReader.ReadString('\n')
6562
if err != nil {
6663
if err == io.EOF {
6764
continue
6865
}
6966

70-
Conn.logger.Print(fmt.Sprintln("read error:", err))
67+
conn.logger.Print(fmt.Sprintln("read error:", err))
7168
break
7269
}
73-
Conn.receiveLine(line)
70+
conn.receiveLine(line)
7471
}
75-
Conn.logger.Print("Connection Terminated")
72+
conn.Close()
73+
conn.logger.Print("Connection Terminated")
7674
}
7775

7876
// Close will manually close this connection, even if the client isn't ready.
79-
func (Conn *Conn) Close() {
80-
Conn.conn.Close()
81-
if Conn.dataConn != nil {
82-
Conn.dataConn.Close()
83-
Conn.dataConn = nil
77+
func (conn *Conn) Close() {
78+
conn.conn.Close()
79+
if conn.dataConn != nil {
80+
conn.dataConn.Close()
81+
conn.dataConn = nil
8482
}
8583
}
8684

driver.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ type DriverFactory interface {
1212
// chosen persistence layer. graval will create a new instance of your
1313
// driver for each client that connects and delegate to it as required.
1414
type Driver interface {
15+
// Init init
16+
Init(*Conn)
17+
1518
// params - a file path
1619
// returns - a time indicating when the requested path was last modified
1720
// - an error if the file doesn't exist or the user lacks

server.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ func (server *Server) newConn(tcpConn net.Conn, driver Driver, auth Auth) *Conn
139139
c.server = server
140140
c.sessionId = newSessionId()
141141
c.logger = newLogger(c.sessionId)
142+
driver.Init(c)
142143
return c
143144
}
144145

@@ -166,16 +167,10 @@ func simpleTLSConfig(certFile, keyFile string) (*tls.Config, error) {
166167
// listening on the same port.
167168
//
168169
func (Server *Server) ListenAndServe() error {
169-
/*laddr, err := net.ResolveTCPAddr("tcp", Server.listenTo)
170-
if err != nil {
171-
return err
172-
}*/
173-
174170
var listener net.Listener
175171
var err error
176-
//fmt.Println("-------", *Server.ServerOpts)
172+
177173
if Server.ServerOpts.TLS {
178-
//fmt.Println("use tls")
179174
config, err := simpleTLSConfig(Server.CertFile, Server.KeyFile)
180175
if err != nil {
181176
return err
@@ -200,10 +195,10 @@ func (Server *Server) ListenAndServe() error {
200195
driver, err := Server.driverFactory.NewDriver()
201196
if err != nil {
202197
Server.logger.Print("Error creating driver, aborting client connection")
203-
} else {
204-
ftpConn := Server.newConn(tcpConn, driver, Server.Auth)
205-
go ftpConn.Serve()
198+
break
206199
}
200+
ftpConn := Server.newConn(tcpConn, driver, Server.Auth)
201+
go ftpConn.Serve()
207202
}
208203
return nil
209204
}

0 commit comments

Comments
 (0)