Blame


1 4b33cdd0 2015-11-30 stephen.d package p9p
2 4b33cdd0 2015-11-30 stephen.d
3 4b33cdd0 2015-11-30 stephen.d import "golang.org/x/net/context"
4 4b33cdd0 2015-11-30 stephen.d
5 4b33cdd0 2015-11-30 stephen.d // Handler defines an interface for 9p message handlers. A handler
6 4b33cdd0 2015-11-30 stephen.d // implementation could be used to intercept calls of all types before sending
7 4b33cdd0 2015-11-30 stephen.d // them to the next handler.
8 4b33cdd0 2015-11-30 stephen.d type Handler interface {
9 4b33cdd0 2015-11-30 stephen.d Handle(ctx context.Context, msg Message) (Message, error)
10 4b33cdd0 2015-11-30 stephen.d
11 4b33cdd0 2015-11-30 stephen.d // TODO(stevvooe): Right now, this interface is functianally identical to
12 4b33cdd0 2015-11-30 stephen.d // roundtripper. If we find that this is sufficient on the server-side, we
13 4b33cdd0 2015-11-30 stephen.d // may unify the types. For now, we leave them separated to differentiate
14 4b33cdd0 2015-11-30 stephen.d // between them.
15 4b33cdd0 2015-11-30 stephen.d }
16 4b33cdd0 2015-11-30 stephen.d
17 4b33cdd0 2015-11-30 stephen.d // HandlerFunc is a convenience type for defining inline handlers.
18 4b33cdd0 2015-11-30 stephen.d type HandlerFunc func(ctx context.Context, msg Message) (Message, error)
19 4b33cdd0 2015-11-30 stephen.d
20 4b33cdd0 2015-11-30 stephen.d func (fn HandlerFunc) Handle(ctx context.Context, msg Message) (Message, error) {
21 4b33cdd0 2015-11-30 stephen.d return fn(ctx, msg)
22 4b33cdd0 2015-11-30 stephen.d }
23 4b33cdd0 2015-11-30 stephen.d
24 4b33cdd0 2015-11-30 stephen.d // Dispatch returns a handler that dispatches messages to the target session.
25 4b33cdd0 2015-11-30 stephen.d // No concurrency is managed by the returned handler. It simply turns messages
26 4b33cdd0 2015-11-30 stephen.d // into function calls on the session.
27 4b33cdd0 2015-11-30 stephen.d func Dispatch(session Session) Handler {
28 4b33cdd0 2015-11-30 stephen.d return HandlerFunc(func(ctx context.Context, msg Message) (Message, error) {
29 4b33cdd0 2015-11-30 stephen.d switch msg := msg.(type) {
30 4b33cdd0 2015-11-30 stephen.d case MessageTauth:
31 4b33cdd0 2015-11-30 stephen.d qid, err := session.Auth(ctx, msg.Afid, msg.Uname, msg.Aname)
32 4b33cdd0 2015-11-30 stephen.d if err != nil {
33 4b33cdd0 2015-11-30 stephen.d return nil, err
34 4b33cdd0 2015-11-30 stephen.d }
35 4b33cdd0 2015-11-30 stephen.d
36 4b33cdd0 2015-11-30 stephen.d return MessageRauth{Qid: qid}, nil
37 4b33cdd0 2015-11-30 stephen.d case MessageTattach:
38 4b33cdd0 2015-11-30 stephen.d qid, err := session.Attach(ctx, msg.Fid, msg.Afid, msg.Uname, msg.Aname)
39 4b33cdd0 2015-11-30 stephen.d if err != nil {
40 4b33cdd0 2015-11-30 stephen.d return nil, err
41 4b33cdd0 2015-11-30 stephen.d }
42 4b33cdd0 2015-11-30 stephen.d
43 4b33cdd0 2015-11-30 stephen.d return MessageRattach{
44 4b33cdd0 2015-11-30 stephen.d Qid: qid,
45 4b33cdd0 2015-11-30 stephen.d }, nil
46 4b33cdd0 2015-11-30 stephen.d case MessageTwalk:
47 4b33cdd0 2015-11-30 stephen.d // TODO(stevvooe): This is one of the places where we need to manage
48 4b33cdd0 2015-11-30 stephen.d // fid allocation lifecycle. We need to reserve the fid, then, if this
49 4b33cdd0 2015-11-30 stephen.d // call succeeds, we should alloc the fid for future uses. Also need
50 4b33cdd0 2015-11-30 stephen.d // to interact correctly with concurrent clunk and the flush of this
51 4b33cdd0 2015-11-30 stephen.d // walk message.
52 4b33cdd0 2015-11-30 stephen.d qids, err := session.Walk(ctx, msg.Fid, msg.Newfid, msg.Wnames...)
53 4b33cdd0 2015-11-30 stephen.d if err != nil {
54 4b33cdd0 2015-11-30 stephen.d return nil, err
55 4b33cdd0 2015-11-30 stephen.d }
56 4b33cdd0 2015-11-30 stephen.d
57 4b33cdd0 2015-11-30 stephen.d return MessageRwalk{
58 4b33cdd0 2015-11-30 stephen.d Qids: qids,
59 4b33cdd0 2015-11-30 stephen.d }, nil
60 4b33cdd0 2015-11-30 stephen.d case MessageTopen:
61 4b33cdd0 2015-11-30 stephen.d qid, iounit, err := session.Open(ctx, msg.Fid, msg.Mode)
62 4b33cdd0 2015-11-30 stephen.d if err != nil {
63 4b33cdd0 2015-11-30 stephen.d return nil, err
64 4b33cdd0 2015-11-30 stephen.d }
65 4b33cdd0 2015-11-30 stephen.d
66 4b33cdd0 2015-11-30 stephen.d return MessageRopen{
67 4b33cdd0 2015-11-30 stephen.d Qid: qid,
68 4b33cdd0 2015-11-30 stephen.d IOUnit: iounit,
69 4b33cdd0 2015-11-30 stephen.d }, nil
70 4b33cdd0 2015-11-30 stephen.d case MessageTcreate:
71 4b33cdd0 2015-11-30 stephen.d qid, iounit, err := session.Create(ctx, msg.Fid, msg.Name, msg.Perm, msg.Mode)
72 4b33cdd0 2015-11-30 stephen.d if err != nil {
73 4b33cdd0 2015-11-30 stephen.d return nil, err
74 4b33cdd0 2015-11-30 stephen.d }
75 4b33cdd0 2015-11-30 stephen.d
76 4b33cdd0 2015-11-30 stephen.d return MessageRcreate{
77 4b33cdd0 2015-11-30 stephen.d Qid: qid,
78 4b33cdd0 2015-11-30 stephen.d IOUnit: iounit,
79 4b33cdd0 2015-11-30 stephen.d }, nil
80 4b33cdd0 2015-11-30 stephen.d case MessageTread:
81 4b33cdd0 2015-11-30 stephen.d p := make([]byte, int(msg.Count))
82 4b33cdd0 2015-11-30 stephen.d n, err := session.Read(ctx, msg.Fid, p, int64(msg.Offset))
83 4b33cdd0 2015-11-30 stephen.d if err != nil {
84 4b33cdd0 2015-11-30 stephen.d return nil, err
85 4b33cdd0 2015-11-30 stephen.d }
86 4b33cdd0 2015-11-30 stephen.d
87 4b33cdd0 2015-11-30 stephen.d return MessageRread{
88 4b33cdd0 2015-11-30 stephen.d Data: p[:n],
89 4b33cdd0 2015-11-30 stephen.d }, nil
90 4b33cdd0 2015-11-30 stephen.d case MessageTwrite:
91 4b33cdd0 2015-11-30 stephen.d n, err := session.Write(ctx, msg.Fid, msg.Data, int64(msg.Offset))
92 4b33cdd0 2015-11-30 stephen.d if err != nil {
93 4b33cdd0 2015-11-30 stephen.d return nil, err
94 4b33cdd0 2015-11-30 stephen.d }
95 4b33cdd0 2015-11-30 stephen.d
96 4b33cdd0 2015-11-30 stephen.d return MessageRwrite{
97 4b33cdd0 2015-11-30 stephen.d Count: uint32(n),
98 4b33cdd0 2015-11-30 stephen.d }, nil
99 4b33cdd0 2015-11-30 stephen.d case MessageTclunk:
100 4b33cdd0 2015-11-30 stephen.d // TODO(stevvooe): Manage the clunking of file descriptors based on
101 4b33cdd0 2015-11-30 stephen.d // walk and attach call progression.
102 4b33cdd0 2015-11-30 stephen.d if err := session.Clunk(ctx, msg.Fid); err != nil {
103 4b33cdd0 2015-11-30 stephen.d return nil, err
104 4b33cdd0 2015-11-30 stephen.d }
105 4b33cdd0 2015-11-30 stephen.d
106 4b33cdd0 2015-11-30 stephen.d return MessageRclunk{}, nil
107 4b33cdd0 2015-11-30 stephen.d case MessageTremove:
108 4b33cdd0 2015-11-30 stephen.d if err := session.Remove(ctx, msg.Fid); err != nil {
109 4b33cdd0 2015-11-30 stephen.d return nil, err
110 4b33cdd0 2015-11-30 stephen.d }
111 4b33cdd0 2015-11-30 stephen.d
112 4b33cdd0 2015-11-30 stephen.d return MessageRremove{}, nil
113 4b33cdd0 2015-11-30 stephen.d case MessageTstat:
114 4b33cdd0 2015-11-30 stephen.d dir, err := session.Stat(ctx, msg.Fid)
115 4b33cdd0 2015-11-30 stephen.d if err != nil {
116 4b33cdd0 2015-11-30 stephen.d return nil, err
117 4b33cdd0 2015-11-30 stephen.d }
118 4b33cdd0 2015-11-30 stephen.d
119 4b33cdd0 2015-11-30 stephen.d return MessageRstat{
120 4b33cdd0 2015-11-30 stephen.d Stat: dir,
121 4b33cdd0 2015-11-30 stephen.d }, nil
122 4b33cdd0 2015-11-30 stephen.d case MessageTwstat:
123 4b33cdd0 2015-11-30 stephen.d if err := session.WStat(ctx, msg.Fid, msg.Stat); err != nil {
124 4b33cdd0 2015-11-30 stephen.d return nil, err
125 4b33cdd0 2015-11-30 stephen.d }
126 4b33cdd0 2015-11-30 stephen.d
127 4b33cdd0 2015-11-30 stephen.d return MessageRwstat{}, nil
128 4b33cdd0 2015-11-30 stephen.d default:
129 4b33cdd0 2015-11-30 stephen.d return nil, ErrUnknownMsg
130 4b33cdd0 2015-11-30 stephen.d }
131 4b33cdd0 2015-11-30 stephen.d })
132 4b33cdd0 2015-11-30 stephen.d }