1 8a7ec697 2015-10-26 adrien package p9pnew
3 499f8c59 2015-10-27 stephen.d import "fmt"
5 8a7ec697 2015-10-26 adrien type FcallType uint8
8 8a7ec697 2015-10-26 adrien Tversion FcallType = iota + 100
39 499f8c59 2015-10-27 stephen.d func (fct FcallType) String() string {
40 499f8c59 2015-10-27 stephen.d switch fct {
41 499f8c59 2015-10-27 stephen.d case Tversion:
42 499f8c59 2015-10-27 stephen.d return "Tversion"
43 499f8c59 2015-10-27 stephen.d case Rversion:
44 499f8c59 2015-10-27 stephen.d return "Rversion"
45 499f8c59 2015-10-27 stephen.d case Tauth:
46 499f8c59 2015-10-27 stephen.d return "Tauth"
47 499f8c59 2015-10-27 stephen.d case Rauth:
48 499f8c59 2015-10-27 stephen.d return "Rauth"
49 499f8c59 2015-10-27 stephen.d case Tattach:
50 499f8c59 2015-10-27 stephen.d return "Tattach"
51 499f8c59 2015-10-27 stephen.d case Rattach:
52 499f8c59 2015-10-27 stephen.d return "Rattach"
53 499f8c59 2015-10-27 stephen.d case Terror:
54 499f8c59 2015-10-27 stephen.d // invalid.
55 499f8c59 2015-10-27 stephen.d return "Terror"
56 499f8c59 2015-10-27 stephen.d case Rerror:
57 499f8c59 2015-10-27 stephen.d return "Rerror"
58 499f8c59 2015-10-27 stephen.d case Tflush:
59 499f8c59 2015-10-27 stephen.d return "Tflush"
60 499f8c59 2015-10-27 stephen.d case Rflush:
61 499f8c59 2015-10-27 stephen.d return "Rflush"
62 499f8c59 2015-10-27 stephen.d case Twalk:
63 499f8c59 2015-10-27 stephen.d return "Twalk"
64 499f8c59 2015-10-27 stephen.d case Rwalk:
65 499f8c59 2015-10-27 stephen.d return "Rwalk"
66 499f8c59 2015-10-27 stephen.d case Topen:
67 499f8c59 2015-10-27 stephen.d return "Topen"
68 499f8c59 2015-10-27 stephen.d case Ropen:
69 499f8c59 2015-10-27 stephen.d return "Ropen"
70 499f8c59 2015-10-27 stephen.d case Tcreate:
71 499f8c59 2015-10-27 stephen.d return "Tcreate"
72 499f8c59 2015-10-27 stephen.d case Rcreate:
73 499f8c59 2015-10-27 stephen.d return "Rcreate"
74 499f8c59 2015-10-27 stephen.d case Tread:
75 499f8c59 2015-10-27 stephen.d return "Tread"
76 499f8c59 2015-10-27 stephen.d case Rread:
77 499f8c59 2015-10-27 stephen.d return "Rread"
78 499f8c59 2015-10-27 stephen.d case Twrite:
79 499f8c59 2015-10-27 stephen.d return "Twrite"
80 499f8c59 2015-10-27 stephen.d case Rwrite:
81 499f8c59 2015-10-27 stephen.d return "Rwrite"
82 499f8c59 2015-10-27 stephen.d case Tclunk:
83 499f8c59 2015-10-27 stephen.d return "Tclunk"
84 499f8c59 2015-10-27 stephen.d case Rclunk:
85 499f8c59 2015-10-27 stephen.d return "Rclunk"
86 499f8c59 2015-10-27 stephen.d case Tremove:
87 499f8c59 2015-10-27 stephen.d return "Tremote"
88 499f8c59 2015-10-27 stephen.d case Rremove:
89 499f8c59 2015-10-27 stephen.d return "Rremove"
90 499f8c59 2015-10-27 stephen.d case Tstat:
91 499f8c59 2015-10-27 stephen.d return "Tstat"
92 499f8c59 2015-10-27 stephen.d case Rstat:
93 499f8c59 2015-10-27 stephen.d return "Rstat"
94 499f8c59 2015-10-27 stephen.d case Twstat:
95 499f8c59 2015-10-27 stephen.d return "Twstat"
96 499f8c59 2015-10-27 stephen.d case Rwstat:
97 499f8c59 2015-10-27 stephen.d return "Rwstat"
98 499f8c59 2015-10-27 stephen.d default:
99 499f8c59 2015-10-27 stephen.d return "Tunknown"
103 8a7ec697 2015-10-26 adrien type Fcall struct {
104 499f8c59 2015-10-27 stephen.d Type FcallType
106 8a7ec697 2015-10-26 adrien Message Message
109 e6bcde66 2015-10-29 stephen.d func newFcall(msg Message) *Fcall {
110 74ec7ac9 2015-10-30 stephen.d var tag Tag
112 74ec7ac9 2015-10-30 stephen.d switch msg.Type() {
113 74ec7ac9 2015-10-30 stephen.d case Tversion, Rversion:
114 74ec7ac9 2015-10-30 stephen.d tag = NOTAG
117 e6bcde66 2015-10-29 stephen.d return &Fcall{
118 e6bcde66 2015-10-29 stephen.d Type: msg.Type(),
119 74ec7ac9 2015-10-30 stephen.d Tag: tag,
120 e6bcde66 2015-10-29 stephen.d Message: msg,
124 e6bcde66 2015-10-29 stephen.d func (fc *Fcall) String() string {
125 74ec7ac9 2015-10-30 stephen.d return fmt.Sprintf("%v(%v) %v", fc.Type, fc.Tag, string9p(fc.Message))
128 499f8c59 2015-10-27 stephen.d type Message interface {
129 e6bcde66 2015-10-29 stephen.d // Type indicates the Fcall type of the message. This must match
130 e6bcde66 2015-10-29 stephen.d // Fcall.Type.
131 e6bcde66 2015-10-29 stephen.d Type() FcallType
134 499f8c59 2015-10-27 stephen.d // newMessage returns a new instance of the message based on the Fcall type.
135 499f8c59 2015-10-27 stephen.d func newMessage(typ FcallType) (Message, error) {
136 499f8c59 2015-10-27 stephen.d // NOTE(stevvooe): This is a nasty bit of code but makes the transport
137 499f8c59 2015-10-27 stephen.d // fairly simple to implement.
138 499f8c59 2015-10-27 stephen.d switch typ {
139 e6bcde66 2015-10-29 stephen.d case Tversion:
140 e6bcde66 2015-10-29 stephen.d return &MessageTversion{}, nil
141 e6bcde66 2015-10-29 stephen.d case Rversion:
142 e6bcde66 2015-10-29 stephen.d return &MessageRversion{}, nil
143 8a7ec697 2015-10-26 adrien case Tauth:
145 8a7ec697 2015-10-26 adrien case Rauth:
147 8a7ec697 2015-10-26 adrien case Tattach:
148 e6bcde66 2015-10-29 stephen.d return &MessageTattach{}, nil
149 8a7ec697 2015-10-26 adrien case Rattach:
150 e6bcde66 2015-10-29 stephen.d return &MessageRattach{}, nil
151 8a7ec697 2015-10-26 adrien case Rerror:
152 e6bcde66 2015-10-29 stephen.d return &MessageRerror{}, nil
153 8a7ec697 2015-10-26 adrien case Tflush:
154 d6198009 2015-10-28 stephen.d return &MessageTflush{}, nil
155 8a7ec697 2015-10-26 adrien case Rflush:
156 97423e8b 2015-10-29 stephen.d return &MessageRflush{}, nil // No message body for this response.
157 8a7ec697 2015-10-26 adrien case Twalk:
158 e9f5e414 2015-10-27 stephen.d return &MessageTwalk{}, nil
159 8a7ec697 2015-10-26 adrien case Rwalk:
160 e9f5e414 2015-10-27 stephen.d return &MessageRwalk{}, nil
161 8a7ec697 2015-10-26 adrien case Topen:
162 e6bcde66 2015-10-29 stephen.d return &MessageTopen{}, nil
163 8a7ec697 2015-10-26 adrien case Ropen:
164 e6bcde66 2015-10-29 stephen.d return &MessageRopen{}, nil
165 8a7ec697 2015-10-26 adrien case Tcreate:
167 8a7ec697 2015-10-26 adrien case Rcreate:
169 8a7ec697 2015-10-26 adrien case Tread:
170 5f1e8105 2015-10-28 stephen.d return &MessageTread{}, nil
171 8a7ec697 2015-10-26 adrien case Rread:
172 d6198009 2015-10-28 stephen.d return &MessageRread{}, nil
173 8a7ec697 2015-10-26 adrien case Twrite:
174 5f1e8105 2015-10-28 stephen.d return &MessageTwrite{}, nil
175 8a7ec697 2015-10-26 adrien case Rwrite:
176 5f1e8105 2015-10-28 stephen.d return &MessageRwrite{}, nil
177 8a7ec697 2015-10-26 adrien case Tclunk:
178 e6bcde66 2015-10-29 stephen.d return &MessageTclunk{}, nil
179 8a7ec697 2015-10-26 adrien case Rclunk:
180 97423e8b 2015-10-29 stephen.d return &MessageRclunk{}, nil // no response body
181 8a7ec697 2015-10-26 adrien case Tremove:
183 8a7ec697 2015-10-26 adrien case Rremove:
185 8a7ec697 2015-10-26 adrien case Tstat:
187 8a7ec697 2015-10-26 adrien case Rstat:
188 d6198009 2015-10-28 stephen.d return &MessageRstat{}, nil
189 8a7ec697 2015-10-26 adrien case Twstat:
191 8a7ec697 2015-10-26 adrien case Rwstat:
195 e6bcde66 2015-10-29 stephen.d return nil, fmt.Errorf("unknown message type")
198 8a7ec697 2015-10-26 adrien // MessageVersion encodes the message body for Tversion and Rversion RPC
199 8a7ec697 2015-10-26 adrien // calls. The body is identical in both directions.
200 e6bcde66 2015-10-29 stephen.d type MessageTversion struct {
201 8a7ec697 2015-10-26 adrien MSize uint32
202 8a7ec697 2015-10-26 adrien Version string
205 e6bcde66 2015-10-29 stephen.d type MessageRversion struct {
206 e6bcde66 2015-10-29 stephen.d MSize uint32
207 e6bcde66 2015-10-29 stephen.d Version string
210 e6bcde66 2015-10-29 stephen.d type MessageTauth struct {
211 e9f5e414 2015-10-27 stephen.d Afid Fid
212 e9f5e414 2015-10-27 stephen.d Uname string
213 e9f5e414 2015-10-27 stephen.d Aname string
216 e6bcde66 2015-10-29 stephen.d type MessageRauth struct {
217 e9f5e414 2015-10-27 stephen.d Qid Qid
220 d6198009 2015-10-28 stephen.d type MessageRerror struct {
221 e9f5e414 2015-10-27 stephen.d Ename string
224 d6198009 2015-10-28 stephen.d type MessageTflush struct {
225 d6198009 2015-10-28 stephen.d Oldtag Tag
228 97423e8b 2015-10-29 stephen.d type MessageRflush struct{}
230 e9f5e414 2015-10-27 stephen.d type MessageTattach struct {
231 e9f5e414 2015-10-27 stephen.d Fid Fid
232 e9f5e414 2015-10-27 stephen.d Afid Fid
233 e9f5e414 2015-10-27 stephen.d Uname string
234 e9f5e414 2015-10-27 stephen.d Aname string
237 e9f5e414 2015-10-27 stephen.d type MessageRattach struct {
238 e9f5e414 2015-10-27 stephen.d Qid Qid
241 e9f5e414 2015-10-27 stephen.d type MessageTwalk struct {
242 e9f5e414 2015-10-27 stephen.d Fid Fid
243 e9f5e414 2015-10-27 stephen.d Newfid Fid
244 e6bcde66 2015-10-29 stephen.d Wnames []string
247 e9f5e414 2015-10-27 stephen.d type MessageRwalk struct {
248 d6198009 2015-10-28 stephen.d Qids []Qid
251 e9f5e414 2015-10-27 stephen.d type MessageTopen struct {
252 e9f5e414 2015-10-27 stephen.d Fid Fid
253 e9f5e414 2015-10-27 stephen.d Mode uint8
256 e9f5e414 2015-10-27 stephen.d type MessageRopen struct {
257 fb37ce2a 2015-10-30 stephen.d Qid Qid
258 fb37ce2a 2015-10-30 stephen.d IOUnit uint32
261 e9f5e414 2015-10-27 stephen.d type MessageTcreate struct {
262 e9f5e414 2015-10-27 stephen.d Fid Fid
263 e9f5e414 2015-10-27 stephen.d Name string
264 e9f5e414 2015-10-27 stephen.d Perm uint32
265 e9f5e414 2015-10-27 stephen.d Mode uint8
268 d6198009 2015-10-28 stephen.d type MessageRcreate struct {
269 d6198009 2015-10-28 stephen.d Qid Qid
270 d6198009 2015-10-28 stephen.d IOUnit uint32
273 e9f5e414 2015-10-27 stephen.d type MessageTread struct {
274 e9f5e414 2015-10-27 stephen.d Fid Fid
275 e9f5e414 2015-10-27 stephen.d Offset uint64
276 e9f5e414 2015-10-27 stephen.d Count uint32
279 e9f5e414 2015-10-27 stephen.d type MessageRread struct {
280 e9f5e414 2015-10-27 stephen.d Data []byte
283 d6198009 2015-10-28 stephen.d type MessageTwrite struct {
284 d6198009 2015-10-28 stephen.d Fid Fid
285 d6198009 2015-10-28 stephen.d Offset uint64
286 d6198009 2015-10-28 stephen.d Data []byte
289 d6198009 2015-10-28 stephen.d type MessageRwrite struct {
290 d6198009 2015-10-28 stephen.d Count uint32
293 d6198009 2015-10-28 stephen.d type MessageTclunk struct {
294 d6198009 2015-10-28 stephen.d Fid Fid
297 97423e8b 2015-10-29 stephen.d type MessageRclunk struct{}
299 d6198009 2015-10-28 stephen.d type MessageTremove struct {
300 d6198009 2015-10-28 stephen.d Fid Fid
303 d6198009 2015-10-28 stephen.d type MessageTstat struct {
304 d6198009 2015-10-28 stephen.d Fid Fid
307 d6198009 2015-10-28 stephen.d type MessageRstat struct {
308 d6198009 2015-10-28 stephen.d Stat Dir
311 d6198009 2015-10-28 stephen.d type MessageTwstat struct {
312 d6198009 2015-10-28 stephen.d Fid Fid
313 d6198009 2015-10-28 stephen.d Stat Dir
316 e6bcde66 2015-10-29 stephen.d func (MessageTversion) Type() FcallType { return Tversion }
317 e6bcde66 2015-10-29 stephen.d func (MessageRversion) Type() FcallType { return Rversion }
318 e6bcde66 2015-10-29 stephen.d func (MessageTauth) Type() FcallType { return Tauth }
319 e6bcde66 2015-10-29 stephen.d func (MessageRauth) Type() FcallType { return Rauth }
320 e6bcde66 2015-10-29 stephen.d func (MessageRerror) Type() FcallType { return Rerror }
321 e6bcde66 2015-10-29 stephen.d func (MessageTflush) Type() FcallType { return Tflush }
322 97423e8b 2015-10-29 stephen.d func (MessageRflush) Type() FcallType { return Rflush }
323 e6bcde66 2015-10-29 stephen.d func (MessageTattach) Type() FcallType { return Tattach }
324 e6bcde66 2015-10-29 stephen.d func (MessageRattach) Type() FcallType { return Rattach }
325 e6bcde66 2015-10-29 stephen.d func (MessageTwalk) Type() FcallType { return Twalk }
326 e6bcde66 2015-10-29 stephen.d func (MessageRwalk) Type() FcallType { return Rwalk }
327 e6bcde66 2015-10-29 stephen.d func (MessageTopen) Type() FcallType { return Topen }
328 e6bcde66 2015-10-29 stephen.d func (MessageRopen) Type() FcallType { return Ropen }
329 e6bcde66 2015-10-29 stephen.d func (MessageTcreate) Type() FcallType { return Tcreate }
330 e6bcde66 2015-10-29 stephen.d func (MessageRcreate) Type() FcallType { return Rcreate }
331 e6bcde66 2015-10-29 stephen.d func (MessageTread) Type() FcallType { return Tread }
332 e6bcde66 2015-10-29 stephen.d func (MessageRread) Type() FcallType { return Rread }
333 e6bcde66 2015-10-29 stephen.d func (MessageTwrite) Type() FcallType { return Twrite }
334 e6bcde66 2015-10-29 stephen.d func (MessageRwrite) Type() FcallType { return Rwrite }
335 e6bcde66 2015-10-29 stephen.d func (MessageTclunk) Type() FcallType { return Tclunk }
336 97423e8b 2015-10-29 stephen.d func (MessageRclunk) Type() FcallType { return Rclunk }
337 e6bcde66 2015-10-29 stephen.d func (MessageTremove) Type() FcallType { return Tremove }
338 e6bcde66 2015-10-29 stephen.d func (MessageTstat) Type() FcallType { return Tstat }
339 e6bcde66 2015-10-29 stephen.d func (MessageRstat) Type() FcallType { return Rstat }
340 e6bcde66 2015-10-29 stephen.d func (MessageTwstat) Type() FcallType { return Twstat }