3 import "golang.org/x/net/context"
5 // Handler defines an interface for 9p message handlers. A handler
6 // implementation could be used to intercept calls of all types before sending
7 // them to the next handler.
8 type Handler interface {
9 Handle(ctx context.Context, msg Message) (Message, error)
11 // TODO(stevvooe): Right now, this interface is functianally identical to
12 // roundtripper. If we find that this is sufficient on the server-side, we
13 // may unify the types. For now, we leave them separated to differentiate
17 // HandlerFunc is a convenience type for defining inline handlers.
18 type HandlerFunc func(ctx context.Context, msg Message) (Message, error)
20 func (fn HandlerFunc) Handle(ctx context.Context, msg Message) (Message, error) {
24 // Dispatch returns a handler that dispatches messages to the target session.
25 // No concurrency is managed by the returned handler. It simply turns messages
26 // into function calls on the session.
27 func Dispatch(session Session) Handler {
28 return HandlerFunc(func(ctx context.Context, msg Message) (Message, error) {
29 switch msg := msg.(type) {
31 qid, err := session.Auth(ctx, msg.Afid, msg.Uname, msg.Aname)
36 return MessageRauth{Qid: qid}, nil
38 qid, err := session.Attach(ctx, msg.Fid, msg.Afid, msg.Uname, msg.Aname)
43 return MessageRattach{
47 // TODO(stevvooe): This is one of the places where we need to manage
48 // fid allocation lifecycle. We need to reserve the fid, then, if this
49 // call succeeds, we should alloc the fid for future uses. Also need
50 // to interact correctly with concurrent clunk and the flush of this
52 qids, err := session.Walk(ctx, msg.Fid, msg.Newfid, msg.Wnames...)
61 qid, iounit, err := session.Open(ctx, msg.Fid, msg.Mode)
71 qid, iounit, err := session.Create(ctx, msg.Fid, msg.Name, msg.Perm, msg.Mode)
76 return MessageRcreate{
81 p := make([]byte, int(msg.Count))
82 n, err := session.Read(ctx, msg.Fid, p, int64(msg.Offset))
91 n, err := session.Write(ctx, msg.Fid, msg.Data, int64(msg.Offset))
100 // TODO(stevvooe): Manage the clunking of file descriptors based on
101 // walk and attach call progression.
102 if err := session.Clunk(ctx, msg.Fid); err != nil {
106 return MessageRclunk{}, nil
108 if err := session.Remove(ctx, msg.Fid); err != nil {
112 return MessageRremove{}, nil
114 dir, err := session.Stat(ctx, msg.Fid)
123 if err := session.WStat(ctx, msg.Fid, msg.Stat); err != nil {
127 return MessageRwstat{}, nil
129 return nil, ErrUnknownMsg