diff options
author | Andrew Gallant (Ocelot) <Andrew.Gallant@tufts.edu> | 2012-05-06 17:48:40 -0400 |
---|---|---|
committer | Andrew Gallant (Ocelot) <Andrew.Gallant@tufts.edu> | 2012-05-06 17:48:40 -0400 |
commit | 6d545e723a7e972998a0e77adcf2219a31a9b800 (patch) | |
tree | 1aa0e2cb470b178669d1abab52c7b8ebac406964 /nexgb/xgbgen/context.go | |
parent | 135cee57610cccd10009b264cce7fbcd5af367cb (diff) | |
download | haven-6d545e723a7e972998a0e77adcf2219a31a9b800.tar.gz haven-6d545e723a7e972998a0e77adcf2219a31a9b800.tar.xz haven-6d545e723a7e972998a0e77adcf2219a31a9b800.zip |
add more extension cruft. make extension checking more uniform.
Diffstat (limited to 'nexgb/xgbgen/context.go')
-rw-r--r-- | nexgb/xgbgen/context.go | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/nexgb/xgbgen/context.go b/nexgb/xgbgen/context.go index d433531..35dd37e 100644 --- a/nexgb/xgbgen/context.go +++ b/nexgb/xgbgen/context.go @@ -5,6 +5,7 @@ import ( "encoding/xml" "fmt" "log" + "strings" "time" ) @@ -70,6 +71,43 @@ func (c *Context) Morph(xmlBytes []byte) { c.Putln("") } + // If this is an extension, create a function to initialize the extension + // before it can be used. + if c.protocol.isExt() { + name := strings.Title(c.protocol.Name) + "Init" + xname := c.protocol.ExtXName + + c.Putln("// %s must be called before using the %s extension.", + name, xname) + c.Putln("func (c *Conn) %s() error {", name) + c.Putln("reply, err := c.QueryExtension(%d, \"%s\").Reply()", + len(xname), xname) + c.Putln("switch {") + c.Putln("case err != nil:") + c.Putln("return err") + c.Putln("case !reply.Present:") + c.Putln("return newError(\"No extension named %s could be found on " + + "on the server.\")", xname) + c.Putln("}") + c.Putln("") + c.Putln("c.extLock.Lock()") + c.Putln("c.extensions[\"%s\"] = reply.MajorOpcode", xname) + c.Putln("for evNum, fun := range newExtEventFuncs[\"%s\"] {", xname) + c.Putln("newEventFuncs[int(reply.FirstEvent) + evNum] = fun") + c.Putln("}") + c.Putln("c.extLock.Unlock()") + c.Putln("") + c.Putln("return nil") + c.Putln("}") + c.Putln("") + + // Make sure newExtEventFuncs["EXT_NAME"] map is initialized. + c.Putln("func init() {") + c.Putln("newExtEventFuncs[\"%s\"] = make(map[int]newEventFun)", xname) + c.Putln("}") + c.Putln("") + } + // Now write Go source code for _, typ := range c.protocol.Types { typ.Define(c) |