Esempio n. 1
0
func main() {
	sio := socketio.NewSocketIO(nil)

	sio.OnConnect(func(c *socketio.Conn) {
		sio.Broadcast("connected: socket.io/" + c.String())
	})

	sio.OnDisconnect(func(c *socketio.Conn) {
		sio.BroadcastExcept(c, "disconnected: socket.io/"+c.String())
	})

	sio.OnMessage(func(c *socketio.Conn, msg socketio.Message) {
		sio.BroadcastExcept(c, msg.Data())
	})

	go func() {
		count := 0
		for {
			sio.Broadcast(fmt.Sprintf("ping%d", count))
			count++
			syscall.Sleep(1e9)
		}
	}()

	mux := sio.ServeMux()
	mux.Handle("/", http.FileServer("www/", "/"))
	if err := http.ListenAndServe(":8080", mux); err != nil {
		fmt.Println("ListenAndServe:", err)
	}
}
Esempio n. 2
0
func sysSleep(t int64) os.Error {
	err := syscall.Sleep(t)
	if err != nil {
		return os.NewSyscallError("sleep", err)
	}
	return nil
}
Esempio n. 3
0
func (db *Database) Backup(p BackupParameters) (r Reporter, e os.Error) {
	if target, e := Open(p.Target); e == nil {
		if backup, e := NewBackup(target, "main", db, "main"); e == nil && p.PagesPerStep > 0 {
			r = make(Reporter, p.QueueLength)
			go func() {
				defer target.Close()
				defer backup.Finish()
				defer close(r)
				for {
					report := &ProgressReport{
						Source:    db.Filename,
						Target:    p.Target,
						Error:     backup.Step(p.PagesPerStep),
						Total:     backup.PageCount(),
						Remaining: backup.Remaining(),
					}
					r <- report
					if e, ok := report.Error.(Errno); ok && !(e == OK || e == BUSY || e == LOCKED) {
						break
					}
					if p.Interval > 0 {
						syscall.Sleep(p.Interval)
					}
				}
			}()
		} else {
			target.Close()
			e = target.Error()
		}
	}
	return
}
Esempio n. 4
0
func monitorServices() {
	for {
		skylib.LoadConfig()
		for _, v := range skylib.NS.Services {
			if (v.Port != *skylib.Port) || (v.IPAddress != *skylib.BindIP) {
				portString := fmt.Sprintf("%s:%d", v.IPAddress, v.Port)
				x, err := rpc.DialHTTP("tcp", portString)
				if err != nil {
					log.Println("BAD CON:", err)
					v.RemoveFromConfig()
					skylib.Errors.Add(1)
					break
				}
				hc := skylib.HeartbeatRequest{Timestamp: time.Seconds()}
				hcr := skylib.HeartbeatResponse{}
				err = x.Call("Service.Ping", hc, &hcr)
				if err != nil {
					log.Println(err.String())
					skylib.Errors.Add(1)
				}
				x.Close()
				skylib.Requests.Add(1)
			}
		}
		syscall.Sleep(2000 * 1000000) // sleep then do it again!
	}
}
Esempio n. 5
0
func sysSleep(t int64) error {
	errno := syscall.Sleep(t)
	if errno != 0 && errno != syscall.EINTR {
		return os.NewSyscallError("sleep", errno)
	}
	return nil
}
Esempio n. 6
0
func gracefulShutdown() {
	log.Println("Graceful Shutdown")
	svc.RemoveFromConfig()

	//would prefer to unregister HTTP and RPC handlers
	//need to figure out how to do that
	syscall.Sleep(10e9) // wait 10 seconds for requests to finish  #HACK
	syscall.Exit(0)
}
Esempio n. 7
0
// sleep takes the current time and a duration,
// pauses for at least ns nanoseconds, and
// returns the current time and an error.
func sleep(t, ns int64) (int64, os.Error) {
	// TODO(cw): use monotonic-time once it's available
	end := t + ns
	for t < end {
		errno := syscall.Sleep(end - t)
		if errno != 0 && errno != syscall.EINTR {
			return 0, os.NewSyscallError("sleep", errno)
		}
		t = Nanoseconds()
	}
	return t, nil
}
Esempio n. 8
0
func monitorServices() {
	for {
		skylib.LoadConfig()
		for _, v := range skylib.NS.Services {
			fmt.Println(v)
			// Insert your code to do something for
			// each service here
			// or get rid of this loop
			// and do something else interesting!
		}
		syscall.Sleep(2000 * 1000000) // sleep then do it again!
	}
}
Esempio n. 9
0
func main() {
	jsx = gojs.Init()
	for {
		go test()
		syscall.Sleep(1E9) // 1 sec pause
	}
	/*
	   js := gojs.NewContext(jsx)
	   gojs.SetProperty(js, "test.property", "hi");
	   gojs.DefineCallback(js, "test_callback", cb);
	   gojs.Exec(js, "test_callback({filename: 'test.wav', play: false});")
	   gojs.DestroyContext(js)
	*/
}
Esempio n. 10
0
func monitorServices() {
	println("Fear the reaper...")
	for {
		skylib.LoadConfig()
		clients := skylib.GetAllClientsByService("CommonService")
		println("#Agents:", len(clients))
		for _, x := range clients {
			fmt.Printf("%v\n", x)
			// Insert your code to do something for
			// each service here
			// or get rid of this loop
			// and do something else interesting!
			x.Close()
			skylib.Requests.Add(1) // Should we count Heartbeat requests?
		}
		syscall.Sleep(2000 * 1000000) // sleep then do it again!
	}
}
Esempio n. 11
0
func monitorServices() {
	println("Fear the reaper...")
	for {
		skylib.LoadConfig()
		clients := skylib.GetAllClientsByService("CommonService")
		println("#Agents:", len(clients))
		for _, x := range clients {
			hc := skylib.HeartbeatRequest{Timestamp: time.Seconds()}
			hcr := skylib.HeartbeatResponse{}
			err := x.Call("CommonService.Ping", hc, &hcr)
			if err != nil {
				panic(err)
			}
			fmt.Printf("hcr:%v\n", hcr)
			x.Close()
			skylib.Requests.Add(1) // Should be count Heartbeat requests?
		}
		syscall.Sleep(2000 * 1000000) // sleep then do it again!
	}
}
Esempio n. 12
0
func main() {
	runtime.GOMAXPROCS(10)
	gdk.ThreadsInit()
	gdk.ThreadsEnter()
	gtk.Init(nil)
	window := gtk.Window(gtk.GTK_WINDOW_TOPLEVEL)
	window.Connect("destroy", func() {
		gtk.MainQuit()
	}, nil)

	vbox := gtk.VBox(false, 1)

	label1 := gtk.Label("")
	vbox.Add(label1)
	label2 := gtk.Label("")
	vbox.Add(label2)

	window.Add(vbox)

	window.SetSizeRequest(100, 100)
	window.ShowAll()
	syscall.Sleep(1000 * 1000 * 100)
	go (func() {
		for i := 0; i < 300000; i++ {
			gdk.ThreadsEnter()
			label1.SetLabel(strconv.Itoa(i))
			gdk.ThreadsLeave()
		}
		gtk.MainQuit()
	})()
	go (func() {
		for i := 300000; i >= 0; i-- {
			gdk.ThreadsEnter()
			label2.SetLabel(strconv.Itoa(i))
			gdk.ThreadsLeave()
		}
		gtk.MainQuit()
	})()
	gtk.Main()
}
Esempio n. 13
0
func main() {

	//instantiate a new Resque
	r := goresque.NewResque("127.0.0.1", 6379, 0)
	for { // loop forever
		//pop a job off the queue the easy way
		job2, err2 := r.Reserve("flavors")
		if err2 != nil {
			fmt.Print(".")         // makes it look very Wargams-ish
			syscall.Sleep(1000000) // this is a .1 second sleep
		} else {
			fmt.Println("return:", job2)

			switch job2.Class {
			case "FlavorSaver":
				is := new(FlavorSaver)
				is.perform(job2.Args)
			default:
				fmt.Println("Incorrect Class!")
			}
		}
	}
}
Esempio n. 14
0
// Create context, wait 2 seconds, destroy it again
func test() {
	js := gojs.NewContext(jsx)
	syscall.Sleep(2E9)
	gojs.DestroyContext(js)
}
Esempio n. 15
0
File: sleep.go Progetto: 8l/go-learn
// Sleep pauses the current goroutine for ns nanoseconds.
// It returns os.EINTR if interrupted.
func Sleep(ns int64) os.Error {
	return os.NewSyscallError("sleep", syscall.Sleep(ns))
}
Esempio n. 16
0
func main() {
	connections := make(map[net.Conn]bool)

	newMessages := make(chan ConnMessage)
	go func() {
		count := 0
		for {
			newMessages <- ConnMessage{nil, fmt.Sprintf("ping%d\r\n", count)}
			count++
			err := syscall.Sleep(1e10) //10 secs
			if err != 0 {
				return
			}
		}
	}()

	tcp, tcperr := net.ListenTCP("tcp", &net.TCPAddr{})
	if tcperr != nil {
		fmt.Println("Error opening TCP socket:", tcperr)
		return
	}
	defer tcp.Close()
	newConnections := make(chan *net.TCPConn)
	fmt.Println("Listening on", tcp.Addr().Network(), tcp.Addr())
	go func() {
		for {
			conn, connerr := tcp.AcceptTCP()
			if connerr != nil {
				fmt.Println("Error accepting TCP connection:", connerr)
			} else {
				newConnections <- conn
			}
		}
	}()

	for {
		select {
		case signal := <-signal.Incoming:
			fmt.Println("got signal:", signal)
			return
		case conn := <-newConnections:
			fmt.Printf("Got connection from %s %s\n",
				conn.RemoteAddr().Network(),
				conn.RemoteAddr())
			conn.SetNoDelay(true)
			conn.Write([]uint8("Hello\r\nHow are you?\r\n"))
			connections[conn] = true
			go func() {
				defer conn.Close()
				reader := bufio.NewReader(conn)
				for {
					line, err := reader.ReadString('\n')
					if err != nil {
						//todo: remove conn from connections
						fmt.Printf("error reading %s %s: %s\n",
							conn.RemoteAddr().Network(),
							conn.RemoteAddr(),
							err)
						connections[conn] = false, false
						return //closes conn
					}
					newMessages <- ConnMessage{conn, string(line)}
				}
			}()
		case msg := <-newMessages:
			for conn, _ := range connections {
				if conn != msg.Conn {
					_, err := fmt.Fprintln(conn, msg.Message)
					if err != nil {
						fmt.Printf("error writing%s: %s\n",
							conn,
							conn.RemoteAddr(),
							err)
						connections[conn] = false, false
					}
				}
			}
		}
	}
}
Esempio n. 17
0
func Sleep(ms int) {
	err := syscall.Sleep(int64(ms) * NR_NANOSEC_IN_MS)
	if err != 0 {
		Logf("sleep error: %d\n", err)
	}
}