コード例 #1
0
ファイル: client.go プロジェクト: ineiti/prifi
func (c *Client) handleServer(s coconet.Conn) error {
	for {
		tsm := &TimeStampMessage{}
		err := s.Get(tsm)
		if err != nil {
			if err == coconet.ErrNotEstablished {
				continue
			}
			if sign.DEBUG {
				log.Warn("error getting from connection:", err)
			}
			return err
		}
		c.handleResponse(tsm)
	}
}
コード例 #2
0
ファイル: client.go プロジェクト: ineiti/prifi
func (c *Client) AddServer(name string, conn coconet.Conn) {
	//c.Servers[name] = conn
	go func(conn coconet.Conn) {
		maxwait := 1 * time.Second
		curWait := 100 * time.Millisecond
		for {
			err := conn.Connect()
			if err != nil {
				time.Sleep(curWait)
				curWait = curWait * 2
				if curWait > maxwait {
					curWait = maxwait
				}
				continue
			} else {
				c.Mux.Lock()
				c.Servers[name] = conn
				c.Mux.Unlock()
				if sign.DEBUG {
					log.Println("SUCCESS: connected to server:", conn)
				}
				err := c.handleServer(conn)
				// if a server encounters any terminating error
				// terminate all pending client transactions and kill the client
				if err != nil {
					if sign.DEBUG {
						log.Errorln("EOF DETECTED: sending EOF to all pending TimeStamps")
					}
					c.Mux.Lock()
					for _, ch := range c.doneChan {
						if sign.DEBUG {
							log.Println("Sending to Receiving Channel")
						}
						ch <- io.EOF
					}
					c.Error = io.EOF
					c.Mux.Unlock()
					return
				} else {
					// try reconnecting if it didn't close the channel
					continue
				}
			}
		}
	}(conn)
}