func main() {
	pflag.Usage = func() {
		fmt.Printf(`usage: tuplespaced [flags]

Run tuplespace server.

Flags:
`)
		pflag.PrintDefaults()
	}
	pflag.Parse()
	runtime.GOMAXPROCS(*ncpuFlag)

	log.AddFilter("stdout", logLevels[*logLevelFlag], log.NewConsoleLogWriter())
	debug := logLevels[*logLevelFlag] <= log.DEBUG

	log.Info("Starting server on http://%s/tuplespace/", *bindFlag)

	ts := tuplespace.NewTupleSpace(store.NewMemoryStore())

	srv := &http.Server{
		Addr:         *bindFlag,
		Handler:      makeService(ts, debug),
		ReadTimeout:  *readTimeoutFlag,
		WriteTimeout: *writeTimeoutFlag,
	}
	err := srv.ListenAndServe()
	if err != nil {
		fatalf("error: %s\n", err)
	}
}
func TestTupleSpaceTake(t *testing.T) {
	ts := tuplespace.NewTupleSpace(store.NewMemoryStore())
	defer ts.Shutdown()
	ts.Send(tuplespace.Tuple{"cmd": "uname -a"}, 0)
	ts.Send(tuplespace.Tuple{"cmd": "uptime"}, 0)
	_, err := ts.Take(tuplespace.MustMatch(`cmd != nil`), time.Second)
	assert.NoError(t, err)
	assert.Equal(t, ts.Stats().Tuples, 1)
}
func TestTupleSpaceReadAll(t *testing.T) {
	ts := tuplespace.NewTupleSpace(store.NewMemoryStore())
	defer ts.Shutdown()
	ts.Send(tuplespace.Tuple{"cmd": "uname -a"}, 0)
	ts.Send(tuplespace.Tuple{"cmd": "uptime"}, 0)
	time.Sleep(time.Millisecond * 100)
	tuples, err := ts.ReadAll(tuplespace.MustMatch(`cmd != nil`), time.Second)
	assert.NoError(t, err)
	assert.Equal(t, tuples, []tuplespace.Tuple{tuplespace.Tuple{"cmd": "uname -a"}, tuplespace.Tuple{"cmd": "uptime"}})
	assert.Equal(t, ts.Stats().Tuples, 2)
}
func TestTupleSpaceProcessNewEntriesAlwaysRespondsToReaders(t *testing.T) {
	rts := tuplespace.NewRawTupleSpace(store.NewMemoryStore())
	take := rts.ReadOperation(tuplespace.MustMatch("cmd"), 0, tuplespace.ActionOne|tuplespace.ActionTake)
	read := rts.ReadOperation(tuplespace.MustMatch("cmd"), 0, tuplespace.ActionOne)
	go func() {
		time.Sleep(time.Second * 1)
		take.Cancel()
		read.Cancel()
	}()
	tuples := []tuplespace.Tuple{tuplespace.Tuple{"cmd": "uname -a"}}
	expected := []tuplespace.Tuple{tuplespace.Tuple{"cmd": "uname -a"}}
	assert.NoError(t, rts.ProcessNewEntries(tuples, time.Now()))
	assert.Equal(t, <-take.Get(), expected)
	assert.Equal(t, <-read.Get(), expected)
}
func BuildMemoryStore(dir string) tuplespace.TupleStore {
	return store.NewMemoryStore()
}