Exemplo n.º 1
0
func CreateCredHandler(w http.ResponseWriter, r *http.Request) {
	// TODO: Prevent making the same cred a second time
	cred := &credence.Cred{}
	if err := jsonpb.Unmarshal(r.Body, cred); err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	fingerprint := len(r.URL.Query()["fingerprint"]) > 0
	confirm := len(r.URL.Query()["confirm"]) > 0

	signingUser := models.Me()

	if !confirm && helpers.StatementAlreadyMade(cred, signingUser) {
		w.WriteHeader(http.StatusConflict)
		return
	}

	if fingerprint {
		cred.AuthorFingerprint = signingUser.Fingerprint
	}

	// Set attributes
	cred.Timestamp = time.Now().Unix()
	err := helpers.SetSignature(cred)
	if err != nil {
		panic(err)
	}

	// Store in the DB
	helpers.StoreCredWithAuthor(cred, signingUser)

	// Set up the broadcaster
	broadcaster, err := goczmq.NewPush("inproc://broadcast")
	if err != nil {
		panic(err)
	}
	defer broadcaster.Destroy()

	// Create the broadcastable bytes
	msg := &credence.Message{
		Type: &credence.Message_Cred{
			Cred: cred,
		},
	}

	msgBytes, err := proto.Marshal(msg)
	if err != nil {
		log.Print(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	_, err = broadcaster.Write(msgBytes)
	if err != nil {
		panic(err)
	}

	helpers.ModelNegotiator().Negotiate(w, r, cred)
}
Exemplo n.º 2
0
func BroadcastSearch(searchRequest *credence.SearchRequest) {
	// Set up the broadcaster
	broadcaster, err := goczmq.NewPush("inproc://broadcast")
	if err != nil {
		panic(err)
	}
	defer broadcaster.Destroy()

	// Create the broadcastable bytes
	msg := &credence.Message{
		Type: &credence.Message_SearchRequest{
			SearchRequest: searchRequest,
		},
	}

	msgBytes, err := proto.Marshal(msg)
	if err != nil {
		panic(err)
	}

	_, err = broadcaster.Write(msgBytes)
	if err != nil {
		panic(err)
	}
}
Exemplo n.º 3
0
func main() {
	var messageSize = flag.Int("message_size", 0, "size of message")
	var messageCount = flag.Int("message_count", 0, "number of messages")
	flag.Parse()

	pullSock, err := czmq.NewPull("inproc://test")
	if err != nil {
		panic(err)
	}

	defer pullSock.Destroy()

	go func() {
		pushSock, err := czmq.NewPush("inproc://test")
		if err != nil {
			panic(err)
		}

		defer pushSock.Destroy()
		for i := 0; i < *messageCount; i++ {
			payload := make([]byte, *messageSize)
			err = pushSock.SendMessage([][]byte{payload})
			if err != nil {
				panic(err)
			}
		}
	}()

	startTime := time.Now()
	for i := 0; i < *messageCount; i++ {
		msg, err := pullSock.RecvMessage()
		if err != nil {
			panic(err)
		}
		if len(msg) != 1 {
			panic("msg too small")
		}
	}
	endTime := time.Now()
	elapsed := endTime.Sub(startTime)
	throughput := float64(*messageCount) / elapsed.Seconds()
	megabits := float64(throughput*float64(*messageSize)*8.0) / 1e6

	log.Printf("message size: %d", *messageSize)
	log.Printf("message count: %d", *messageCount)
	log.Printf("test time (seconds): %f", elapsed.Seconds())
	log.Printf("mean throughput: %f [msg/s]", throughput)
	log.Printf("mean throughput: %f [Mb/s]", megabits)
}
Exemplo n.º 4
0
func Setup() {
	config := config.Read()
	broadcatchUri := fmt.Sprintf("tcp://%s:%d", config.Broadcatcher.Host, config.Broadcatcher.Port)

	var err error
	broadcaster, err = goczmq.NewPush("inproc://broadcast")
	if err != nil {
		panic(err)
	}

	broadcatcher, err = goczmq.NewPull(broadcatchUri)
	if err != nil {
		panic(err)
	}
	log.Println("Broadcatcher will start on", broadcatchUri)

	receiver = goczmq.NewSock(goczmq.Sub)
	receiver.SetSubscribe("")

	helpers.ConnectToPeers(false, ConnectToBroadcaster)
}