Example #1
0
func (y *Conn) Greet() (string, string, os.Error) {
	y.lk.Lock()
	if y.err != nil {
		y.lk.Unlock()
		return "", "", os.ErrorString("d,conn: closed meantime")
	}
	if y.regime != regimeUnAuth || y.tube == nil {
		panic("d,conn: regime logic")
	}
	y.regime = regimeAuth
	tube := y.tube
	y.lk.Unlock()

	g := &U_Greet{sys.Build, Version}
	err := tube.Encode(g)
	if err == nil {
		err = tube.Decode(g)
	}

	y.lk.Lock()
	if y.err != nil {
		y.lk.Unlock()
		return "", "", os.ErrorString("d,conn: closed meantime")
	}
	if err != nil {
		y.lk.Unlock()
		return "", "", y.kill(os.ErrorString("d,conn: greet failed"))
	}
	y.regime = regimeUnAuth
	y.lk.Unlock()
	return g.Build, g.Version, nil
}
Example #2
0
// Close terminates this handoff and returns control to the underlying Conn.
func (h *handoff) Close() (err os.Error) {
	h.lk.Lock()
	y := h.y
	h.y = nil
	if y == nil {
		h.lk.Unlock()
		return os.EBADF
	}
	rclosed := h.rclosed
	h.lk.Unlock()

	//fmt.Printf(term.FgCyan+"d·conn[%#p]·h[%#p]:%x —— close\n"+term.Reset, y,h,h.session)

	tube, err := y.getTube()
	if err != nil {
		return os.EIO
	}

	msg := &U_Cargo{}
	if err = tube.Encode(msg); err != nil {
		return h.kill(err)
	}

	if !rclosed {
		msg := &U_Cargo{}
		if err = tube.Decode(msg); err != nil {
			goto __End
		}
		if msg.Cargo == nil || len(msg.Cargo) == 0 {
			goto __End
		}
	}
__End:
	h.lk.Lock()
	ff := h.finfun
	h.finfun = nil
	h.lk.Unlock()
	if ff != nil {
		ff()
	}
	if err != nil {
		err = y.kill(err)
	}
	return err
}
Example #3
0
func (h *handoff) Write(p []byte) (n int, err os.Error) {
	tube, err := h.checkForKill()
	if err != nil {
		return 0, err
	}
	if len(p) == 0 {
		return 0, nil
	}

	msg := &U_Cargo{p}
	if err = tube.Encode(msg); err != nil {
		return 0, h.kill(err)
	}
	h.lk.Lock()
	defer h.lk.Unlock()
	h.wn += int64(len(p))
	h.wk++
	if h.y == nil {
		return 0, os.EBADF
	}
	return len(p), nil
}
Example #4
0
func (y *Conn) Poll() (subject string, rwc io.ReadWriteCloser, err os.Error) {
	for {
		y.hlk.Lock()
		y.hlk.Unlock()
		//fmt.Printf(term.FgBlack + "d·conn[%#p] —— loop\n" + term.Reset, y)

		y.lk.Lock()
		if y.err != nil || y.tube == nil {
			y.lk.Unlock()
			return "", nil, os.EBADF
		}
		tube := y.tube
		y.lk.Unlock()

		tch := waitForRead(tube.ExposeBufioReader())
		select {
		case m := <-y.dialch: // dial & kill requests come here
			if m == nil {
				//fmt.Printf(term.FgRed + "d·conn[%#p] —— closing accept loop\n"+ term.Reset, y)
				err = os.EBADF
				return "", nil, err
			}

			//fmt.Printf(term.FgYellow + "d·conn[%#p] —— dialing, subject=%s\n" + term.Reset, y, m.Subject)

			order := y.getRandOrient()
			session := y.getRandSession()
			orient := &U_Orient{order, session}
			if err = tube.Encode(orient); err != nil {
				err = y.kill(err)
				m.Notify <- nil
				return "", nil, err
			}
			err = <-tch // Hack. Reuse wait until data from remote
			if err != nil {
				//fmt.Printf(term.FgRed+"d·conn[%#p] —— dial/orient err: %s\n"+ term.Reset, y, err)
				err = y.kill(err)
				m.Notify <- nil
				return "", nil, err
			}
			err = tube.Decode(orient)
			if err != nil {
				//fmt.Printf(term.FgCyan+"d·conn[%#p] —— orient,dec: %s\n"+ term.Reset, y, err)
				err = y.kill(err)
				m.Notify <- nil
				return "", nil, err
			}
			if orient.Order >= order { // our dial prevails
				y.sendCall(session, m, tube)
			} else { // remote dial prevails
				m.Notify <- nil
				return y.receiveCall(orient.Session, tube)
			}

		case err = <-tch: // read is available on the connection
			if err != nil {
				//fmt.Printf(term.FgRed+"d·conn[%#p] —— ring/orient err: %s\n"+ term.Reset, y, err)
				err = y.kill(err)
				return "", nil, err
			}
			orient := &U_Orient{}
			if err = tube.Decode(orient); err != nil {
				//fmt.Printf(term.FgRed+"d·conn[%#p] —— ori,dec,err: %s\n"+ term.Reset, y, err)
				err = y.kill(err)
				return "", nil, err
			}
			orient.Order++
			session := orient.Session
			if err = tube.Encode(orient); err != nil {
				//fmt.Printf(term.FgRed+"d·conn[%#p] —— ori,enc,err: %s\n"+ term.Reset, y, err)
				err = y.kill(err)
				return "", nil, err
			}
			return y.receiveCall(session, tube)
		} // select
	} // for
	panic("unreach")
}