func newXP(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { user, err := getUser(r) if err != nil { w.WriteHeader(401) return } msg := new(protocol.NewXP) err = protocol.Unpack(r, &msg) if err != nil { log.Printf("error unpacking newXP message") log.Println(err) w.WriteHeader(400) return } log.Printf("Creating xp '%s` for user `%s`", msg.Name, user) err = db.CreateDesign(msg.Name, user) if err != nil { log.Println(err) log.Printf("[newXP] error creating design entry db") w.WriteHeader(500) return } design_key, err := db.ReadDesignKey(msg.Name, user) if err != nil { log.Println(err) log.Printf("[newXP] error reading design key") w.WriteHeader(500) return } //use the default sim settings to begin with s := addie.SimSettings{} s.Begin = 0 s.End = 10 s.MaxStep = 1e-3 err = db.CreateSimSettings(s, design_key) if err != nil { log.Println(err) log.Println("[newXP] error creating default sim settings") w.WriteHeader(500) return } w.WriteHeader(200) }
//TODO: as it sits, this function relies on the client to provide a consistent //set of elements to delete, so we are garbage in garbage out essentially. This //is probably not a good policy. For example if a client deletes a bunch of nodes //but not the links that they are connected to, meyham will follow. func onDelete(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { log.Println("addie got delete request") msg := new(protocol.Delete) err := protocol.Unpack(r, msg) if err != nil { w.WriteHeader(http.StatusBadRequest) return } nodes := make(map[addie.Id]addie.Identify) links := make(map[addie.Id]addie.Link) plinks := make(map[addie.Id]addie.Plink) for _, d := range msg.Elements { switch d.Type { case "Computer": var c addie.Computer err := json.Unmarshal(d.Element, &c) if err != nil { log.Println("unable to unmarshal " + d.Type) } log.Printf("deleting %s %v", d.Type, c.Id) nodes[c.Id] = c case "Router": var r addie.Router err := json.Unmarshal(d.Element, &r) if err != nil { log.Println("unable to unmarshal " + d.Type) } log.Printf("deleting %s %v", d.Type, r.Id) nodes[r.Id] = r case "Switch": var s addie.Switch err := json.Unmarshal(d.Element, &s) if err != nil { log.Println("unable to unmarshal " + d.Type) } log.Printf("deleting %s %v", d.Type, s.Id) nodes[s.Id] = s case "Sax": var s addie.Sax err := json.Unmarshal(d.Element, &s) if err != nil { log.Println("unable to unmarshal " + d.Type) } log.Printf("deleting %s %v", d.Type, s.Id) nodes[s.Id] = s case "Phyo": var p addie.Phyo err := json.Unmarshal(d.Element, &p) if err != nil { log.Println("unable to unmarshal " + d.Type) } log.Printf("deleting %s %v", d.Type, p.Id) nodes[p.Id] = p case "Link": var l addie.Link err := json.Unmarshal(d.Element, &l) if err != nil { log.Println("unable to unmarshal " + d.Type) } log.Printf("deleting %s %v", d.Type, l.Id) links[l.Id] = l case "Plink": var p addie.Plink err := json.Unmarshal(d.Element, &p) if err != nil { log.Println("unable to unmarshal " + d.Type) } log.Printf("deleting %s %v", d.Type, p.Id) plinks[p.Id] = p } } for _, n := range nodes { db.DeleteId(n.Identify(), user) delete(design.Elements, n.Identify()) } for _, l := range links { db.DeleteId(l.Identify(), user) db.DeleteInterface(l.Endpoints[0], user) db.DeleteInterface(l.Endpoints[1], user) //todo kill the interface on the in-memory model delete(design.Elements, l.Identify()) } for _, p := range plinks { db.DeleteId(p.Id, user) delete(design.Elements, p.Id) } }
func onUpdate(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { //unpack the message msg := new(protocol.Update) err := protocol.Unpack(r, msg) if err != nil { w.WriteHeader(http.StatusBadRequest) return } var new_nodes, new_links []addie.Identify var changed_nodes, changed_links []addie.Identify var changed_node_oids, changed_link_oids []addie.Id var killList []addie.Id var place = func(oid addie.Id, e addie.Identify) { if e.Identify() != oid { killList = append(killList, oid) } _, ok := design.Elements[oid] if !ok { switch e.(type) { case addie.Link, addie.Plink: new_links = append(new_links, e) default: new_nodes = append(new_nodes, e) } } else { switch e.(type) { case addie.Link, addie.Plink: changed_links = append(changed_links, e) changed_link_oids = append(changed_link_oids, oid) default: changed_nodes = append(changed_nodes, e) changed_node_oids = append(changed_node_oids, oid) } } } var new_models []addie.Model var changed_models []addie.Model var changed_model_oids []addie.Id var modelKillList []string var placeModel = func(oid string, m addie.Model) { if m.Name != oid { modelKillList = append(modelKillList, oid) } _, ok := userModels[oid] if !ok { new_models = append(new_models, m) } else { changed_models = append(changed_models, m) changed_model_oids = append(changed_model_oids, modelId(oid)) } } for _, u := range msg.Elements { switch u.Type { case "Computer": var c addie.Computer err := json.Unmarshal(u.Element, &c) if err != nil { log.Println("unable to unmarshal computer") } place(u.OID, c) case "Switch": var s addie.Switch err := json.Unmarshal(u.Element, &s) if err != nil { log.Println("unable to unmarshal switch") } place(u.OID, s) case "Router": var r addie.Router err := json.Unmarshal(u.Element, &r) if err != nil { log.Println("unable to unmarshal router") } place(u.OID, r) case "Phyo": var p addie.Phyo err := json.Unmarshal(u.Element, &p) if err != nil { log.Println("unable to unmarshal phyo") } place(u.OID, p) case "Model": var m addie.Model err := json.Unmarshal(u.Element, &m) if err != nil { log.Println("unable to unmarshal model") } placeModel(u.OID.Name, m) case "Sensor": var s addie.Sensor err := json.Unmarshal(u.Element, &s) if err != nil { log.Println("unable to unmarshal sensor") } place(u.OID, s) case "Actuator": var a addie.Actuator err := json.Unmarshal(u.Element, &a) if err != nil { log.Println("unable to unmarshal sensor") } place(u.OID, a) case "Link": var l addie.Link err := json.Unmarshal(u.Element, &l) if err != nil { log.Println("unable to unmarshal link") } place(u.OID, l) case "Plink": var p addie.Plink err := json.Unmarshal(u.Element, &p) if err != nil { log.Println("unable to unmarshal plink") } place(u.OID, p) case "Sax": var s addie.Sax err := json.Unmarshal(u.Element, &s) if err != nil { log.Println("unable to marshal sax") } place(u.OID, s) case "SimSettings": var s addie.SimSettings err := json.Unmarshal(u.Element, &s) if err != nil { log.Println(err) log.Println("unable to unmarshal SimSettings") return } updateSimSettings(s) default: log.Println("unkown element type: ", u.Type) } } for i, u := range changed_models { dbUpdate(changed_model_oids[i], u) userModels[u.Name] = u } for _, c := range new_models { dbCreate(c) userModels[c.Name] = c } for i, u := range changed_nodes { dbUpdate(changed_node_oids[i], u) design.Elements[u.Identify()] = u } for _, c := range new_nodes { dbCreate(c) design.Elements[c.Identify()] = c } for i, u := range changed_links { dbUpdate(changed_link_oids[i], u) design.Elements[u.Identify()] = u } for _, c := range new_links { dbCreate(c) design.Elements[c.Identify()] = c } for _, k := range killList { delete(design.Elements, k) } for _, k := range modelKillList { delete(userModels, k) } //log.Println("\n", design.String()) //send response w.WriteHeader(http.StatusOK) }