1 269e4d4b 2015-11-05 stephen.d package p9pnew
3 269e4d4b 2015-11-05 stephen.d import "fmt"
5 269e4d4b 2015-11-05 stephen.d // Message represents the target of an fcall.
6 269e4d4b 2015-11-05 stephen.d type Message interface{}
8 269e4d4b 2015-11-05 stephen.d // newMessage returns a new instance of the message based on the Fcall type.
9 269e4d4b 2015-11-05 stephen.d func newMessage(typ FcallType) (Message, error) {
10 269e4d4b 2015-11-05 stephen.d switch typ {
11 269e4d4b 2015-11-05 stephen.d case Tversion:
12 269e4d4b 2015-11-05 stephen.d return MessageTversion{}, nil
13 269e4d4b 2015-11-05 stephen.d case Rversion:
14 269e4d4b 2015-11-05 stephen.d return MessageRversion{}, nil
15 269e4d4b 2015-11-05 stephen.d case Tauth:
16 269e4d4b 2015-11-05 stephen.d return MessageTauth{}, nil
17 269e4d4b 2015-11-05 stephen.d case Rauth:
18 269e4d4b 2015-11-05 stephen.d return MessageRauth{}, nil
19 269e4d4b 2015-11-05 stephen.d case Tattach:
20 269e4d4b 2015-11-05 stephen.d return MessageTattach{}, nil
21 269e4d4b 2015-11-05 stephen.d case Rattach:
22 269e4d4b 2015-11-05 stephen.d return MessageRattach{}, nil
23 269e4d4b 2015-11-05 stephen.d case Rerror:
24 269e4d4b 2015-11-05 stephen.d return MessageRerror{}, nil
25 269e4d4b 2015-11-05 stephen.d case Tflush:
26 269e4d4b 2015-11-05 stephen.d return MessageTflush{}, nil
27 269e4d4b 2015-11-05 stephen.d case Rflush:
28 269e4d4b 2015-11-05 stephen.d return MessageRflush{}, nil // No message body for this response.
29 269e4d4b 2015-11-05 stephen.d case Twalk:
30 269e4d4b 2015-11-05 stephen.d return MessageTwalk{}, nil
31 269e4d4b 2015-11-05 stephen.d case Rwalk:
32 269e4d4b 2015-11-05 stephen.d return MessageRwalk{}, nil
33 269e4d4b 2015-11-05 stephen.d case Topen:
34 269e4d4b 2015-11-05 stephen.d return MessageTopen{}, nil
35 269e4d4b 2015-11-05 stephen.d case Ropen:
36 269e4d4b 2015-11-05 stephen.d return MessageRopen{}, nil
37 269e4d4b 2015-11-05 stephen.d case Tcreate:
38 269e4d4b 2015-11-05 stephen.d return MessageTcreate{}, nil
39 269e4d4b 2015-11-05 stephen.d case Rcreate:
40 269e4d4b 2015-11-05 stephen.d return MessageRcreate{}, nil
41 269e4d4b 2015-11-05 stephen.d case Tread:
42 269e4d4b 2015-11-05 stephen.d return MessageTread{}, nil
43 269e4d4b 2015-11-05 stephen.d case Rread:
44 269e4d4b 2015-11-05 stephen.d return MessageRread{}, nil
45 269e4d4b 2015-11-05 stephen.d case Twrite:
46 269e4d4b 2015-11-05 stephen.d return MessageTwrite{}, nil
47 269e4d4b 2015-11-05 stephen.d case Rwrite:
48 269e4d4b 2015-11-05 stephen.d return MessageRwrite{}, nil
49 269e4d4b 2015-11-05 stephen.d case Tclunk:
50 269e4d4b 2015-11-05 stephen.d return MessageTclunk{}, nil
51 269e4d4b 2015-11-05 stephen.d case Rclunk:
52 269e4d4b 2015-11-05 stephen.d return MessageRclunk{}, nil // no response body
53 269e4d4b 2015-11-05 stephen.d case Tremove:
54 269e4d4b 2015-11-05 stephen.d return MessageTremove{}, nil
55 269e4d4b 2015-11-05 stephen.d case Rremove:
56 269e4d4b 2015-11-05 stephen.d return MessageRremove{}, nil
57 269e4d4b 2015-11-05 stephen.d case Tstat:
58 269e4d4b 2015-11-05 stephen.d return MessageTstat{}, nil
59 269e4d4b 2015-11-05 stephen.d case Rstat:
60 269e4d4b 2015-11-05 stephen.d return MessageRstat{}, nil
61 269e4d4b 2015-11-05 stephen.d case Twstat:
62 269e4d4b 2015-11-05 stephen.d return MessageTwstat{}, nil
63 269e4d4b 2015-11-05 stephen.d case Rwstat:
64 269e4d4b 2015-11-05 stephen.d return MessageRwstat{}, nil
67 269e4d4b 2015-11-05 stephen.d return nil, fmt.Errorf("unknown message type")
70 269e4d4b 2015-11-05 stephen.d func messageType(m Message) FcallType {
71 269e4d4b 2015-11-05 stephen.d switch v := m.(type) {
72 269e4d4b 2015-11-05 stephen.d case MessageTversion:
73 269e4d4b 2015-11-05 stephen.d return Tversion
74 269e4d4b 2015-11-05 stephen.d case MessageRversion:
75 269e4d4b 2015-11-05 stephen.d return Rversion
76 269e4d4b 2015-11-05 stephen.d case MessageTauth:
77 269e4d4b 2015-11-05 stephen.d return Tauth
78 269e4d4b 2015-11-05 stephen.d case MessageRauth:
79 269e4d4b 2015-11-05 stephen.d return Rauth
80 269e4d4b 2015-11-05 stephen.d case MessageTflush:
81 269e4d4b 2015-11-05 stephen.d return Tflush
82 269e4d4b 2015-11-05 stephen.d case MessageRflush:
83 269e4d4b 2015-11-05 stephen.d return Rflush
84 269e4d4b 2015-11-05 stephen.d case MessageRerror:
85 269e4d4b 2015-11-05 stephen.d return Rerror
86 269e4d4b 2015-11-05 stephen.d case MessageTattach:
87 269e4d4b 2015-11-05 stephen.d return Tattach
88 269e4d4b 2015-11-05 stephen.d case MessageRattach:
89 269e4d4b 2015-11-05 stephen.d return Rattach
90 269e4d4b 2015-11-05 stephen.d case MessageTwalk:
91 269e4d4b 2015-11-05 stephen.d return Twalk
92 269e4d4b 2015-11-05 stephen.d case MessageRwalk:
93 269e4d4b 2015-11-05 stephen.d return Rwalk
94 269e4d4b 2015-11-05 stephen.d case MessageTopen:
95 269e4d4b 2015-11-05 stephen.d return Topen
96 269e4d4b 2015-11-05 stephen.d case MessageRopen:
97 269e4d4b 2015-11-05 stephen.d return Ropen
98 269e4d4b 2015-11-05 stephen.d case MessageTcreate:
99 269e4d4b 2015-11-05 stephen.d return Tcreate
100 269e4d4b 2015-11-05 stephen.d case MessageRcreate:
101 269e4d4b 2015-11-05 stephen.d return Rcreate
102 269e4d4b 2015-11-05 stephen.d case MessageTread:
103 269e4d4b 2015-11-05 stephen.d return Tread
104 269e4d4b 2015-11-05 stephen.d case MessageRread:
105 269e4d4b 2015-11-05 stephen.d return Rread
106 269e4d4b 2015-11-05 stephen.d case MessageTwrite:
107 269e4d4b 2015-11-05 stephen.d return Twrite
108 269e4d4b 2015-11-05 stephen.d case MessageRwrite:
109 269e4d4b 2015-11-05 stephen.d return Rwrite
110 269e4d4b 2015-11-05 stephen.d case MessageTclunk:
111 269e4d4b 2015-11-05 stephen.d return Tclunk
112 269e4d4b 2015-11-05 stephen.d case MessageRclunk:
113 269e4d4b 2015-11-05 stephen.d return Rclunk
114 269e4d4b 2015-11-05 stephen.d case MessageTremove:
115 269e4d4b 2015-11-05 stephen.d return Tremove
116 269e4d4b 2015-11-05 stephen.d case MessageRremove:
117 269e4d4b 2015-11-05 stephen.d return Rremove
118 269e4d4b 2015-11-05 stephen.d case MessageTstat:
119 269e4d4b 2015-11-05 stephen.d return Tstat
120 269e4d4b 2015-11-05 stephen.d case MessageRstat:
121 269e4d4b 2015-11-05 stephen.d return Rstat
122 269e4d4b 2015-11-05 stephen.d case MessageTwstat:
123 269e4d4b 2015-11-05 stephen.d return Twstat
124 269e4d4b 2015-11-05 stephen.d case MessageRwstat:
125 269e4d4b 2015-11-05 stephen.d return Rwstat
126 269e4d4b 2015-11-05 stephen.d case error:
127 269e4d4b 2015-11-05 stephen.d return Rerror
128 269e4d4b 2015-11-05 stephen.d default:
129 269e4d4b 2015-11-05 stephen.d // NOTE(stevvooe): This is considered a programming error.
130 269e4d4b 2015-11-05 stephen.d panic(fmt.Sprintf("unsupported message type: %T", v))
134 269e4d4b 2015-11-05 stephen.d // MessageVersion encodes the message body for Tversion and Rversion RPC
135 269e4d4b 2015-11-05 stephen.d // calls. The body is identical in both directions.
136 269e4d4b 2015-11-05 stephen.d type MessageTversion struct {
137 269e4d4b 2015-11-05 stephen.d MSize uint32
138 269e4d4b 2015-11-05 stephen.d Version string
141 269e4d4b 2015-11-05 stephen.d type MessageRversion struct {
142 269e4d4b 2015-11-05 stephen.d MSize uint32
143 269e4d4b 2015-11-05 stephen.d Version string
146 269e4d4b 2015-11-05 stephen.d type MessageTauth struct {
147 269e4d4b 2015-11-05 stephen.d Afid Fid
148 269e4d4b 2015-11-05 stephen.d Uname string
149 269e4d4b 2015-11-05 stephen.d Aname string
152 269e4d4b 2015-11-05 stephen.d type MessageRauth struct {
153 269e4d4b 2015-11-05 stephen.d Qid Qid
156 269e4d4b 2015-11-05 stephen.d type MessageRerror struct {
157 269e4d4b 2015-11-05 stephen.d Ename string
160 269e4d4b 2015-11-05 stephen.d func (e MessageRerror) Error() string {
161 269e4d4b 2015-11-05 stephen.d return fmt.Sprintf("9p: %v", e.Ename)
164 269e4d4b 2015-11-05 stephen.d type MessageTflush struct {
165 269e4d4b 2015-11-05 stephen.d Oldtag Tag
168 269e4d4b 2015-11-05 stephen.d type MessageRflush struct{}
170 269e4d4b 2015-11-05 stephen.d type MessageTattach struct {
171 269e4d4b 2015-11-05 stephen.d Fid Fid
172 269e4d4b 2015-11-05 stephen.d Afid Fid
173 269e4d4b 2015-11-05 stephen.d Uname string
174 269e4d4b 2015-11-05 stephen.d Aname string
177 269e4d4b 2015-11-05 stephen.d type MessageRattach struct {
178 269e4d4b 2015-11-05 stephen.d Qid Qid
181 269e4d4b 2015-11-05 stephen.d type MessageTwalk struct {
182 269e4d4b 2015-11-05 stephen.d Fid Fid
183 269e4d4b 2015-11-05 stephen.d Newfid Fid
184 269e4d4b 2015-11-05 stephen.d Wnames []string
187 269e4d4b 2015-11-05 stephen.d type MessageRwalk struct {
188 269e4d4b 2015-11-05 stephen.d Qids []Qid
191 269e4d4b 2015-11-05 stephen.d type MessageTopen struct {
192 269e4d4b 2015-11-05 stephen.d Fid Fid
193 deb98ab4 2015-11-05 stephen.d Mode Flag
196 269e4d4b 2015-11-05 stephen.d type MessageRopen struct {
197 269e4d4b 2015-11-05 stephen.d Qid Qid
198 269e4d4b 2015-11-05 stephen.d IOUnit uint32
201 269e4d4b 2015-11-05 stephen.d type MessageTcreate struct {
202 269e4d4b 2015-11-05 stephen.d Fid Fid
203 269e4d4b 2015-11-05 stephen.d Name string
204 269e4d4b 2015-11-05 stephen.d Perm uint32
205 deb98ab4 2015-11-05 stephen.d Mode Flag
208 269e4d4b 2015-11-05 stephen.d type MessageRcreate struct {
209 269e4d4b 2015-11-05 stephen.d Qid Qid
210 269e4d4b 2015-11-05 stephen.d IOUnit uint32
213 269e4d4b 2015-11-05 stephen.d type MessageTread struct {
214 269e4d4b 2015-11-05 stephen.d Fid Fid
215 269e4d4b 2015-11-05 stephen.d Offset uint64
216 269e4d4b 2015-11-05 stephen.d Count uint32
219 269e4d4b 2015-11-05 stephen.d type MessageRread struct {
220 269e4d4b 2015-11-05 stephen.d Data []byte
223 269e4d4b 2015-11-05 stephen.d type MessageTwrite struct {
224 269e4d4b 2015-11-05 stephen.d Fid Fid
225 269e4d4b 2015-11-05 stephen.d Offset uint64
226 269e4d4b 2015-11-05 stephen.d Data []byte
229 269e4d4b 2015-11-05 stephen.d type MessageRwrite struct {
230 269e4d4b 2015-11-05 stephen.d Count uint32
233 269e4d4b 2015-11-05 stephen.d type MessageTclunk struct {
234 269e4d4b 2015-11-05 stephen.d Fid Fid
237 269e4d4b 2015-11-05 stephen.d type MessageRclunk struct{}
239 269e4d4b 2015-11-05 stephen.d type MessageTremove struct {
240 269e4d4b 2015-11-05 stephen.d Fid Fid
243 269e4d4b 2015-11-05 stephen.d type MessageRremove struct{}
245 269e4d4b 2015-11-05 stephen.d type MessageTstat struct {
246 269e4d4b 2015-11-05 stephen.d Fid Fid
249 269e4d4b 2015-11-05 stephen.d type MessageRstat struct {
250 269e4d4b 2015-11-05 stephen.d Stat Dir
253 269e4d4b 2015-11-05 stephen.d type MessageTwstat struct {
254 269e4d4b 2015-11-05 stephen.d Fid Fid
255 269e4d4b 2015-11-05 stephen.d Stat Dir
258 269e4d4b 2015-11-05 stephen.d type MessageRwstat struct{}