func init() { myservices["lookup"] = getServiceDesc myservices["register"] = registerService myservices[msg.PPP_HEARTBEAT] = msg.ProcessHeartBeat services["lookup"] = msg.NewService("lookup", "LooKUp Service", "tcp://localhost:5569", "lookup", "REP") services["register"] = msg.NewService("register", "Registration Service", "tcp://localhost:5569", "lookup", "REP") // Load Service List getServiceList() // List available services listServices() }
// Initialize by setting address and registering service with directory service func init() { myservices["hello"] = doHelloWorld myservices[msg.PPP_HEARTBEAT] = msg.ProcessHeartBeat allowedbinders = "tcp://*:5560" mydescription = msg.NewService("hello", "Hello Service", "tcp://localhost:5560", "hello", "REP") // All I need to know are the details of the lookup service services["lookup"] = msg.NewService("lookup", "LookUp Service", "tcp://localhost:5569", "lookup", "REP") fmt.Println("Registering service...") reply, err := msg.SendRequest(services["lookup"], "register", string(encodeTOJSON(mydescription))) if err != nil { log.Println(err) } fmt.Println("\t", reply) }
// Populates the services map with mappings for available // services - services is a global variable hence no need // for return func getServiceList() { //read service list from file filepointer, err := os.Open(SERVICES_FILENAME) if err != nil { log.Println(err) return } defer filepointer.Close() reader := bufio.NewReader(filepointer) line, err := reader.ReadBytes('\n') service := msg.NewService("", "", "", "", "") for err == nil { ///*Decode from JSON err = json.Unmarshal(line, &service) if err != nil { log.Println("Error decoding from json") continue } if _, isPresent := services[service.SID]; !isPresent { services[service.SID] = service } //read next line line, err = reader.ReadBytes('\n') } if err != io.EOF { log.Println(err) } }
// Populates the services map with mappings for available // services - services is a global variable hence no need // for return func getServiceList() { //read service list from file filepointer, err := os.Open(servicesFileName) if err != nil { log.Println(err) return } defer filepointer.Close() reader := bufio.NewReader(filepointer) line, err := reader.ReadBytes('\n') service := msg.NewService("", "", "", "", "") for err == nil { // Decode from JSON err = json.Unmarshal(line, &service) if err != nil { log.Println("Error decoding from json") continue } fmt.Println(service.SID) services[service.SID] = service //read next line line, err = reader.ReadBytes('\n') } if err != io.EOF { log.Println(err) } }
// Registers a new service by: // 1. Decoding the JSON "register" message // 2. Adding the decoded service to the service list // 3. Encoding Entire service list to JSON and saving to file func registerService(message string) (reply string, err error) { newservice := msg.NewService("", "", "", "", "") if len(services) == 0 { services = make(map[string]msg.Service) } fmt.Println("\n\tProcessing Service Registration Request for ", message, " ...") //Decode Message fmt.Println("\tDecoding...") err = json.Unmarshal([]byte(message), &newservice) if err != nil { log.Println("Error decoding from JSON ", err) err = errors.New(fmt.Sprintf("DecodeFail:%s", err)) reply = fmt.Sprintf("%s", err) return } // Append latest heartbeat to the service description newservice.Heartbeat_state = time.Now().String() fmt.Println("\tRegistering...") //Add to the active service list services[newservice.SID] = newservice fmt.Println("\tUpdating file...") //Encode entire service list to JSON var datab, b []byte for _, value := range services { b, err = json.Marshal(value) if err != nil { log.Println("Error encoding ", value.SID, " to JSON") err = errors.New(fmt.Sprintf("WriteFileFail:EncodeFail:%s", err)) reply = fmt.Sprintf("%s", err) return } datab = append(datab, append(b, byte('\n'))...) } //write to file err = ioutil.WriteFile(SERVICES_FILENAME, datab, os.ModePerm) if err != nil { log.Println("Error writing service list to file: ", err) err = errors.New(fmt.Sprintf("WriteFileFail:%s", err)) reply = fmt.Sprintf("%s", err) return } // Updated service list listServices() reply = "RegistrationSuccess" return }
// Registers a new service by: // 1. Decoding the JSON message // 2. Adding the decoded service and its socket binding to the service list // 3. Encoding Entire service list to JSON and saving to file func registerService(jsonMsg, SID string) { if jsonMsg == "NotAvailable" { err := "Service, " + SID + ", Not available" panic(err) } newservice := msg.NewService("", "", "", "", "") if len(services) == 0 { services = make(map[string]msg.Service) } //Decode Message fmt.Println("\tDecoding...") err := json.Unmarshal([]byte(jsonMsg), &newservice) if err != nil { panic(err) } fmt.Println("\tRegistering...") //Add to the active service list services[newservice.SID] = newservice fmt.Println("\tUpdating file...") //Encode entire service list to JSON var datab, b []byte for _, value := range services { b, err = json.Marshal(value) if err != nil { log.Println("Error encoding ", value.SID, " to JSON\n", "Service has been registered in service list but will have to ", "be reloaded on next run\n", err) return } datab = append(datab, append(b, byte('\n'))...) } //write to file err = ioutil.WriteFile(servicesFileName, datab, os.ModePerm) if err != nil { log.Println("Error writing service list to file: ", err, "\nService has been registered in service list but will have to be", " reloaded on next run") return } }
// Init function requests for all services that the client will require func init() { servicesFileName = "dcservicelist.json" servicesReq = append(servicesReq, "hello") // All I need to know are the details of the lookup service services["lookup"] = msg.NewService("lookup", "LookUp Service", "tcp://localhost:5569", "lookup", "REP") // Get my service list getServiceList() // Get all services I require that are not listed in my service list for _, serviceReq := range servicesReq { if _, isListed := services[serviceReq]; isListed == false { reply, err := msg.SendRequest(services["lookup"], "lookup", serviceReq) if err != nil { panic(err) } for i := range reply { registerService(reply[i], serviceReq) } } } }