Esempio n. 1
0
func (peer *Peer) join(newPeer string) {
	for _, p := range peer.Peers {
		httpclient.Put(p+"/peers", newPeer)
		httpclient.Put(newPeer+"/peers", p)
	}

	peer.addPeer(newPeer)
	peer.ring.AddNode(newPeer)
	httpclient.Put(newPeer+"/peers", peer.url)

	nValue := strconv.Itoa(peer.ring.GetNValue())
	httpclient.Put(newPeer+"/settings/set/n", nValue)

	for _, p := range peer.Peers {
		nodes := httpclient.JsonData{
			"ring": peer.ring.GetNodes(),
		}
		httpclient.Put(p+"/ring", nodes.Encode())
	}

	peer.evaluateKeyOwnership()
}
Esempio n. 2
0
func (server *Server) Handler() http.Handler {
	m := pat.New()

	server.Peer.Handler(m)

	m.Put("/set/:key", http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) {
		key := request.URL.Query().Get(":key")
		body, _ := ioutil.ReadAll(request.Body)
		value := string(body)

		server.logger.Printf("Setting '%s'->'%s'", key, value)
		server.values[key] = value
		w.WriteHeader(201)
		io.WriteString(w, value)

	}))

	m.Put("/data/:key", http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) {
		key := request.URL.Query().Get(":key")
		body, _ := ioutil.ReadAll(request.Body)
		value := string(body)

		for _, address := range server.PreferenceListForKey(key) {
			if server.URL() == address {
				server.logger.Printf("Storing '%s'->'%s'", key, value)
				server.values[key] = value
			} else {
				server.handOffKey(address, key, value)
			}
		}

		w.WriteHeader(201)
		io.WriteString(w, value)

	}))

	m.Get("/data/:key", http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) {
		key := request.URL.Query().Get(":key")

		for _, address := range server.PreferenceListForKey(key) {
			if server.URL() == address {
				value, ok := server.values[key]
				if ok {
					server.logger.Printf("Get key '%s' found value '%s'", key, value)
					w.WriteHeader(200)
					io.WriteString(w, value)
				} else {
					server.logger.Printf("Key '%s' not found", key)
					w.WriteHeader(404)
				}

				return
			}
		}

		destinationAddress := server.PeerAddressForKey(key)

		server.logger.Printf("Passing off get of key '%s' to %s", key, destinationAddress)
		statusCode, response := httpclient.Get(destinationAddress+"/data/"+key, "")
		w.WriteHeader(statusCode)
		io.WriteString(w, response)
	}))

	m.Get("/stats/keys", http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) {
		stats := httpclient.JsonData{
			"count": len(server.values),
			"data":  server.values,
		}

		w.Header().Set("Content-Type", "application/json")
		w.WriteHeader(200)
		io.WriteString(w, stats.Encode())
	}))

	return m
}
Esempio n. 3
0
func (peer *Peer) Handler(m *pat.PatternServeMux) {
	m.Get("/stats", http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) {
		stats := httpclient.JsonData{
			"ring":       peer.ring.GetNodes(),
			"vnodeCount": peer.node.VnodeCount(),
			"vnodeSize":  peer.node.VnodeSize(),
			"vnodeStart": peer.node.VnodeStart(),
			"nValue":     peer.ring.GetNValue(),
		}

		w.Header().Set("Content-Type", "application/json")
		w.WriteHeader(200)
		io.WriteString(w, stats.Encode())
	}))

	m.Put("/ring", http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) {
		body, _ := ioutil.ReadAll(request.Body)
		data := map[string][]string{}
		json.Unmarshal(body, &data)

		peer.ring.SetNodes(data["ring"])
		peer.node = peer.ring.Get(peer.url)

		peer.evaluateKeyOwnership()

		w.WriteHeader(201)
	}))

	m.Put("/settings/set/n", http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) {
		body, _ := ioutil.ReadAll(request.Body)
		n, err := strconv.Atoi(string(body))

		if err == nil {
			peer.ring.SetNValue(n)
			w.WriteHeader(201)
		} else {
			w.WriteHeader(422)
		}
	}))

	m.Put("/settings/n", http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) {
		body, _ := ioutil.ReadAll(request.Body)
		n, err := strconv.Atoi(string(body))

		if err == nil && n < peer.peerCount() {
			peer.ring.SetNValue(n)
			for _, p := range peer.Peers {
				httpclient.Put(p+"/settings/set/n", string(body))
			}
			w.WriteHeader(201)
		} else {
			w.WriteHeader(422)
		}
	}))

	m.Put("/peers", http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) {
		body, _ := ioutil.ReadAll(request.Body)
		newPeerURL := string(body)

		if peer.peerExists(newPeerURL) {
			w.WriteHeader(409)
		} else {
			peer.addPeer(newPeerURL)
			w.WriteHeader(201)
		}
	}))

	m.Put("/peers/join", http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) {
		body, _ := ioutil.ReadAll(request.Body)
		newPeerURL := string(body)

		peer.logger.Printf("%s requesting to join", newPeerURL)

		if peer.peerExists(newPeerURL) {
			w.WriteHeader(409)
		} else {
			peer.join(newPeerURL)
			w.WriteHeader(201)
		}
	}))

	m.Get("/peers", http.HandlerFunc(func(w http.ResponseWriter, request *http.Request) {
		if peer.HasPeer() {
			w.Header().Set("Content-Type", "application/json")
			w.WriteHeader(200)
			io.WriteString(w, httpclient.JsonData{"peers": peer.Peers}.Encode())
		} else {
			w.WriteHeader(404)
		}
	}))
}