Пример #1
0
// how to tell server to close listener without blocking or polling?  TODO
func server_A(ready chan int, quit chan int) {
	ch := []byte{0}
	var n int
	var listener net.UnixAddr
	listener.Name = "server_socket"

	_, err := os.Open(listener.Name)
	if err == nil {
		fmt.Printf("socket already in use\n")
		os.Exit(-1)
	}
	l, err := net.ListenUnix("unix", &listener)
	if err != nil {
		fmt.Printf("ListenUnix()  failed with err: %v\n", err)
		os.Exit(1)
	}
	defer l.Close()
	t := time.Now()
	t = t.Add(10 * time.Second)
	err = l.SetDeadline(t)
	fmt.Printf("server() listener started ok, ready to accept connections\n")
	ready <- 1 // send ready signal back to main()
	for {
		//select {  didn't solve problem
		//case _ = <- quit: break
		//default:
		//}
		c, err := l.AcceptUnix()
		if err != nil {
			fmt.Printf("server() accept socket failed with err: %v\n", err)
			break
		}
		fmt.Printf("server() accepted connection from client\n")
		n, err = c.Read(ch)
		if err != nil || n != 1 {
			fmt.Printf("server() read socket failed with err: %v\n", err)
			os.Exit(1)
		}
		ch[0] *= 10 // modify input data and send it back
		n, err = c.Write(ch)
		if err != nil || n != 1 {
			fmt.Printf("server() write to socket failed with err: %v\n", err)
			os.Exit(1)
		}
		fmt.Printf("server() wrote byte value = %d to socket\n", ch)
		c.Close()
	}
	fmt.Printf("Cleaning up after listener\n")
	err = os.Remove(listener.Name)
	if err != nil {
		log.Panicf("cant remove socket")
	}
	quit <- 1
}
Пример #2
0
func createUnixSocket(addr *net.UnixAddr) (l net.Listener, err error) {
	l, err = net.ListenUnix("unix", addr)

	if err != nil {
		return
	}

	// TODO(lcooper): Not sure if this sequence is completely safe.
	// It would be better if we could get the underlying FD of the socket
	// and stat() + chmod() that instead.
	fi, err := os.Stat(addr.String())
	if err != nil {
		return
	}

	// NOTE(lcooper): This only ensures g+w other then on Linux,
	// BSD systems only use parent directory permissions.
	// See http://stackoverflow.com/questions/5977556 .
	err = os.Chmod(addr.String(), fi.Mode()|0060)
	return
}
Пример #3
0
//Threaded, Asynchronous approach to sorting list.
//for the given rpc client, send a portion of the given list list1
//to said client to sort and return.
func SortClient(m1 *[]int, l1 *[]int, list1 *[]int, val string, i int, clientSize int, c chan []int) {

	l2 := *l1
	m := *m1
	//	list := *list1

	defer close(c)

	var err error

	checkError(err)

	client := new(rpc.Client)

	if *protocolFlag == "tcp" {
		//		fmt.Println("Attempting connection to tcp:", val)
		client, err = rpc.DialHTTP("tcp", val)
	} else {
		var unixAddr *net.UnixAddr
		unixAddr, err = net.ResolveUnixAddr("unix", val)
		fmt.Println("Attempting connection to socket:", val)
		fmt.Println(unixAddr)
		//			client, err = rpc.Dial("unix", unixAddr.String())
		c2, err := net.Dial("unix", unixAddr.String())
		checkError(err)

		fmt.Println("Successfully Connected to Unix Port", c2)
	}

	if err != nil {
		log.Fatal("dialing:", err)
	}

	//compensates for remainders when the
	//len(m)%(# of clients) != 0
	remainder := len(m) % clientSize
	start, end := 0, 0

	if remainder > 0 && i < remainder {
		start, end = (len(m)/(clientSize))*i+i, (len(m)/(clientSize))*(i+1)+i
	} else {
		start, end = (len(m)/(clientSize))*i, (len(m)/(clientSize))*(i+1)-1
		start, end = start+remainder, end+remainder
	}

	fmt.Println("Sorting List", start, end)

	l2 = make([]int, end-start+1)
	for j := start; j < end; j++ {
		l2[j-start] = m[j]
	}

	s := ""

	err = client.Call("Util.SetThreads", 4, &s)
	fmt.Println(s)

	r := new(utility.Reply)
	err = client.Call("Util.Sort", l2, &r)

	fmt.Println("Client Returned ", len(r.Data))
	//
	//	list = make([]int, len(r.Data))
	//
	for j, val := range r.Data {
		//?		list[j] = val
		l2[j] = val
	}

	if err != nil {
		log.Fatal("util error error:", err)
	}

	client.Close()

	c <- l2

}