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 // Handle implements the requirements for the Handler interface.
21 func (fn HandlerFunc) Handle(ctx context.Context, msg Message) (Message, error) {
25 // Dispatch returns a handler that dispatches messages to the target session.
26 // No concurrency is managed by the returned handler. It simply turns messages
27 // into function calls on the session.
28 func Dispatch(session Session) Handler {
29 return HandlerFunc(func(ctx context.Context, msg Message) (Message, error) {
30 switch msg := msg.(type) {
32 qid, err := session.Auth(ctx, msg.Afid, msg.Uname, msg.Aname)
37 return MessageRauth{Qid: qid}, nil
39 qid, err := session.Attach(ctx, msg.Fid, msg.Afid, msg.Uname, msg.Aname)
44 return MessageRattach{
48 // TODO(stevvooe): This is one of the places where we need to manage
49 // fid allocation lifecycle. We need to reserve the fid, then, if this
50 // call succeeds, we should alloc the fid for future uses. Also need
51 // to interact correctly with concurrent clunk and the flush of this
53 qids, err := session.Walk(ctx, msg.Fid, msg.Newfid, msg.Wnames...)
62 qid, iounit, err := session.Open(ctx, msg.Fid, msg.Mode)
72 qid, iounit, err := session.Create(ctx, msg.Fid, msg.Name, msg.Perm, msg.Mode)
77 return MessageRcreate{
82 p := make([]byte, int(msg.Count))
83 n, err := session.Read(ctx, msg.Fid, p, int64(msg.Offset))
92 n, err := session.Write(ctx, msg.Fid, msg.Data, int64(msg.Offset))
101 // TODO(stevvooe): Manage the clunking of file descriptors based on
102 // walk and attach call progression.
103 if err := session.Clunk(ctx, msg.Fid); err != nil {
107 return MessageRclunk{}, nil
109 if err := session.Remove(ctx, msg.Fid); err != nil {
113 return MessageRremove{}, nil
115 dir, err := session.Stat(ctx, msg.Fid)
124 if err := session.WStat(ctx, msg.Fid, msg.Stat); err != nil {
128 return MessageRwstat{}, nil
130 return nil, ErrUnknownMsg