Exemple #1
0
func sendUpdates() {
	// Query the database for the balances
	q := database.Find(nil)

	// Retrieve results
	var wubba []*Wrapper
	err := q.All(&wubba)
	if err != nil {
		log.Error(err)
	}

	result := make(map[string]*betternode.Balance)
	for _, w := range wubba {
		_, ok := balanceState[w.Value.Device]
		if ok {
			if balanceState[w.Value.Device] == len(w.Value.History) {
				continue
			}
		}
		balanceState[w.Value.Device] = len(w.Value.History)

		result[w.Value.Device] = w.Value
	}

	// Send results to the clients
	betternode.SendBalances(result)
}
Exemple #2
0
func MapReduce2(from time.Time, jsMap, jsReduce, jsFinalize string) *mgo.MapReduceInfo {
	mr := &mgo.MapReduce{
		Map:      jsMap,
		Reduce:   jsReduce,
		Finalize: jsFinalize,
		Out:      bson.M{"replace": "balances"},
	}

	q := transactions.Find(nil)

	info, err := q.MapReduce(mr, nil)
	if err != nil {
		log.Error(err)
		return nil
	}

	return info
}
Exemple #3
0
func MapReduce1(from time.Time, jsMap, jsReduce, jsFinalize string) *mgo.MapReduceInfo {
	mr := &mgo.MapReduce{
		Map:      jsMap,
		Reduce:   jsReduce,
		Finalize: jsFinalize,
		Out:      bson.M{"replace": "transactions"},
	}

	q := packets.Find(bson.M{"type": bson.M{"$exists": true}})

	info, err := q.MapReduce(mr, nil)
	if err != nil {
		log.Error(err)
		return nil
	}

	return info
}
Exemple #4
0
// Gets the data from a node and saves into the database
func (n *Node) receiver() {
	dec := json.NewDecoder(n.Conn)

	for {
		var obj interface{}
		err := dec.Decode(&obj)
		if err != nil {
			log.Error(err)
			if err.Error() == "EOF" {
				close(n.Transactions)
				return
			}
			// Hack: recover
			dec = json.NewDecoder(n.Conn)
		}

		log.Message("Received message from " + n.Conn.RemoteAddr().String() + ".")

		//		n.updateDevices(obj)
		database.Insert(obj)
	}
}
Exemple #5
0
func main() {
	defer func() {
		log.Close()             // Close log
		server.Close()          // Close server
		betternode.Close()      // Close nodes
		profile.StopProfile()   // Close profile
		time.Sleep(time.Second) // Wait for everything to close
	}()

	// Make Go run parallel
	runtime.GOMAXPROCS(runtime.NumCPU())

	log.Message("ManiacMaster starting.")

	// Initialize database
	log.Message("Connecting to database.")
	err := database.Connect("localhost:27018", "maniac", "", "")
	if err != nil {
		log.Error(err)
		fmt.Println("Error:", err.Error())
	}

	// Create TCP server
	log.Message("Starting TCP server.")
	err = server.Start(":6789", betternode.NewNode)
	if err != nil {
		log.Error(err)
		fmt.Println("Error:", err.Error())
	}

	// Take commands
	log.Message("Ready for command line input.")

	fmt.Println("MANIAC Master ready.")
	var command string

	s := bufio.NewScanner(os.Stdin)

quit:
	for {
		// Get next command
		fmt.Print("M> ")
		if !s.Scan() {
			fmt.Println("Input ended!")
			break
		}
		command = s.Text()

		// to trimmed lower case
		command = strings.ToLower(strings.TrimSpace(command))

		switch command {
		case "help": // show helpful text
			printHelp()

		case "quit", "exit": // quits the master
			break quit

		case "next": // starts new round
			rounds.NewRound()

		case "send": // sends new message
			rounds.NewTransaction()

		case "info": // show info about the master
			printInfo()

		case "load":
			fmt.Print("Profile file: ")
			fmt.Scanln(&command)
			profile.OpenProfile(command)

		case "run":
			fmt.Print("Profile name: ")
			fmt.Scanln(&command)
			go profile.RunProfile(command)

		case "kill":
			profile.StopProfile()

		case "list":
			profile.ListProfiles()

		case "close":
			fmt.Print("Profile name: ")
			fmt.Scanln(&command)
			profile.CloseProfile(command)

		default:
			fmt.Println("Invalid command: Type \"help\" to get help.")

		}
	}
}