// handleClientStateChange makes clean transitions as the connection with // avahi-daemon changes. //export handleClientStateChange func handleClientStateChange(client *C.AvahiClient, newState C.AvahiClientState, userdata unsafe.Pointer) { z := instance z.spMutex.Lock() defer z.spMutex.Unlock() // Name conflict. if newState == C.AVAHI_CLIENT_S_COLLISION { log.Warning("Avahi reports a host name collision.") } // Transition from not connecting to connecting. Warn in logs. if newState == C.AVAHI_CLIENT_CONNECTING { log.Warning("Cannot find Avahi daemon. Is it running?") } // Transition from running to not running. Free all groups. if newState != C.AVAHI_CLIENT_S_RUNNING { log.Info("Local printing disabled (Avahi client is not running).") for name, r := range z.printers { if r.group != nil { if errstr := C.removeAvahiGroup(z.threadedPoll, r.group); errstr != nil { err := errors.New(C.GoString(errstr)) log.Errorf("Failed to remove Avahi group: %s", err) } r.group = nil z.printers[name] = r } } } // Transition from not running to running. Recreate all groups. if newState == C.AVAHI_CLIENT_S_RUNNING { log.Info("Local printing enabled (Avahi client is running).") for name, r := range z.printers { txt := prepareTXT(r.ty, r.url, r.id, r.online) defer C.avahi_string_list_free(txt) if errstr := C.addAvahiGroup(z.threadedPoll, z.client, &r.group, r.name, C.ushort(r.port), txt); errstr != nil { err := errors.New(C.GoString(errstr)) log.Errorf("Failed to add Avahi group: %s", err) } z.printers[name] = r } } // Transition from not failure to failure. Recreate thread poll and client. if newState == C.AVAHI_CLIENT_FAILURE { z.restart <- struct{}{} } z.state = newState }
func (api *privetAPI) accesstoken(w http.ResponseWriter, r *http.Request) { log.Debugf("Received /accesstoken request: %+v", r) if ok := api.checkRequest(w, r, "GET"); !ok { return } user := r.Form.Get("user") if len(user) == 0 { writeError(w, "invalid_params", "user parameter expected") return } responseBody, httpStatusCode, err := api.getProximityToken(api.gcpID, user) if err != nil { log.Errorf("Failed to get proximity token: %s", err) } if responseBody == nil || len(responseBody) == 0 { log.Warning("Cloud returned empty response body") writeError(w, "server_error", "Check connector logs") return } var response struct { Success bool `json:"success"` Message string `json:"message"` ErrorCode int `json:"errorCode"` ProximityToken map[string]interface{} `json:"proximity_token"` } if err = json.Unmarshal(responseBody, &response); err != nil { log.Errorf("Failed to unmarshal ticket from cloud: %s", err) writeError(w, "server_error", "Check connector logs") return } if response.Success { token, err := json.MarshalIndent(response.ProximityToken, "", " ") if err != nil { log.Errorf("Failed to marshal something that was just unmarshalled: %s", err) writeError(w, "server_error", "Check connector logs") } else { w.Write(token) } return } if response.ErrorCode != 0 { e := privetError{ Error: "server_error", Description: response.Message, ServerAPI: "/proximitytoken", ServerCode: response.ErrorCode, ServerHTTPCode: httpStatusCode, }.json() w.Write(e) return } writeError(w, "server_error", "Check connector logs") }
func (z *zeroconf) restartAndQuit() { for { select { case <-z.restart: log.Warning("Avahi client failed. Make sure that avahi-daemon is running while I restart the client.") C.stopAvahiClient(z.threadedPoll, z.client) if errstr := C.startAvahiClient(&z.threadedPoll, &z.client); errstr != nil { err := errors.New(C.GoString(errstr)) log.Errorf("Failed to restart Avahi client: %s", err) } case <-z.q: for name := range z.printers { z.removePrinter(name) } C.stopAvahiClient(z.threadedPoll, z.client) close(z.q) return } } }