Commit fddbe14c authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent e8954823
...@@ -8,6 +8,8 @@ import ( ...@@ -8,6 +8,8 @@ import (
_ "../../storage" // XXX rel ok? _ "../../storage" // XXX rel ok?
neo "../.." neo "../.."
"fmt" "fmt"
"context"
"time"
) )
...@@ -18,4 +20,16 @@ func main() { ...@@ -18,4 +20,16 @@ func main() {
var t neo.Tid = neo.MAX_TID var t neo.Tid = neo.MAX_TID
fmt.Printf("%T %x\n", t, t) fmt.Printf("%T %x\n", t, t)
println("TODO") println("TODO")
storsrv := &neo.StorageApplication{}
ctx, cancel := context.WithCancel(context.Background())
go func() {
time.Sleep(5 * time.Second)
cancel()
}()
//ctx := context.Background()
err := neo.ListenAndServe(ctx, "tcp", "localhost:1234", storsrv)
fmt.Println(err)
} }
...@@ -4,13 +4,22 @@ package neo ...@@ -4,13 +4,22 @@ package neo
import ( import (
"context" "context"
"net"
"fmt"
) )
// NEO Storage application // NEO Storage application
// XXX naming
type StorageApplication struct { type StorageApplication struct {
} }
func (stor *StorageApplication) ServeConn(ctx context.Context, conn net.Conn) {
fmt.Printf("stor: serving new client %s <-> %s\n", conn.LocalAddr(), conn.RemoteAddr())
fmt.Fprintf(conn, "Hello up there, you address is %s\n", conn.RemoteAddr()) // XXX err
conn.Close() // XXX err
}
// ---------------------------------------- // ----------------------------------------
...@@ -32,24 +41,26 @@ type Server interface { ...@@ -32,24 +41,26 @@ type Server interface {
// XXX text // XXX text
// XXX move -> generic place ? // XXX move -> generic place ?
func Serve(ctx context.Context, l net.Listener, srv Server) error { func Serve(ctx context.Context, l net.Listener, srv Server) error {
fmt.Printf("stor: serving on %s ...\n", l.Addr())
// close listener when either cancelling or returning (e.g. due to an error) // close listener when either cancelling or returning (e.g. due to an error)
// ( when cancelling - listener close will signal to all accepts to // ( when cancelling - listener close will signal to all accepts to
// terminate with an error ) // terminate with an error )
retch := make(chan struct{}) retch := make(chan struct{})
defer func() { close(retch) }() defer func() { close(retch) }()
go func( go func() {
select { select {
case <-ctx.Done(): case <-ctx.Done():
case <-retch: case <-retch:
} }
l.Close() // XXX err l.Close() // XXX err
)() }()
// main Accept -> ServeConn loop // main Accept -> ServeConn loop
for { for {
conn, err := l.Accept() conn, err := l.Accept()
if err != nil { if err != nil {
// TODO check done channel of l/srv somehow // TODO err == closed <-> ctx was cancelled
// TODO err -> net.Error && .Temporary() -> some throttling // TODO err -> net.Error && .Temporary() -> some throttling
return err return err
} }
...@@ -62,8 +73,8 @@ func Serve(ctx context.Context, l net.Listener, srv Server) error { ...@@ -62,8 +73,8 @@ func Serve(ctx context.Context, l net.Listener, srv Server) error {
// XXX move -> generic place ? // XXX move -> generic place ?
// XXX get (net, laddr) from srv ? // XXX get (net, laddr) from srv ?
// XXX split -> separate Listen() // XXX split -> separate Listen()
func ListenAndServe(ctx context.Context, net, laddr string, srv Server) error { func ListenAndServe(ctx context.Context, net_, laddr string, srv Server) error {
l, err := net.Listen(net, laddr) l, err := net.Listen(net_, laddr)
if err != nil { if err != nil {
return err return err
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment