func TitanicRequest(chRequest chan<- string) { worker, _ := mdapi.NewMdwrk("tcp://localhost:5555", "titanic.request", false) reply := []string{} for { // Send reply if it's not null // And then get next request from broker request, err := worker.Recv(reply) if err != nil { break // Interrupted, exit } // Ensure message directory exists os.MkdirAll(TITANIC_DIR, 0700) // Generate UUID and save message to disk uuid := uuid.New() file, err := os.Create(RequestFilename(uuid)) fmt.Fprint(file, strings.Join(request, "\n")) file.Close() // Send UUID through to message queue chRequest <- uuid // Now send UUID back to client // Done by the mdwrk_recv() at the top of the loop reply = []string{"200", uuid} } }
func TitanicReply() { worker, _ := mdapi.NewMdwrk("tcp://localhost:5555", "titanic.reply", false) pending := []string{"300"} unknown := []string{"400"} reply := []string{} for { request, err := worker.Recv(reply) if err != nil { break // Interrupted, exit } uuid := request[0] req_filename := RequestFilename(uuid) rep_filename := ReplyFilename(uuid) data, err := ioutil.ReadFile(rep_filename) if err == nil { reply = strings.Split("200\n"+string(data), "\n") } else { _, err := os.Stat(req_filename) if err == nil { reply = pending } else { reply = unknown } } } }
func main() { var verbose bool if len(os.Args) > 1 && os.Args[1] == "-v" { verbose = true } session, _ := mdapi.NewMdwrk("tcp://localhost:5555", "echo", verbose) var err error var request, reply []string for { request, err = session.Recv(reply) if err != nil { break // Worker was interrupted } reply = request // Echo is complex... :-) } log.Println(err) }
func TitanicClose() { worker, _ := mdapi.NewMdwrk("tcp://localhost:5555", "titanic.close", false) ok := []string{"200"} reply := []string{} for { request, err := worker.Recv(reply) if err != nil { break // Interrupted, exit } uuid := request[0] os.Remove(RequestFilename(uuid)) os.Remove(ReplyFilename(uuid)) reply = ok } }