Exemple #1
0
// 将session进行管理,session的TerminalSn作为Key
func (this *SessionManager) Put(sess *Session) error {
	// 确保session都是合法的终端
	if sess.TerminalId == 0 || sess.TerminalSn == "" {
		return ERROR_NO_ACCESS_TERMINAL
	}

	this.mutex.Lock()
	defer this.mutex.Unlock()

	if prevSess, ok := this.sessions[sess.TerminalSn]; ok {
		log.Printf("Remote address %s will be replaced by %s \n", prevSess.RemoteAddr, sess.RemoteAddr)
		prevSess.Close()
		(*prevSess).State = SESSION_STATE_GONE
	} else {
		if len(this.sessions) == MAX_SESSION_NUM {
			return ERROR_MAX_CLIENT
		}
		log.Printf("Put session: %s \n", sess.TerminalSn)
	}

	(*sess).State = SESSION_STATE_CONNECTED
	this.sessions[sess.TerminalSn] = sess
	o := mysql.GetOrm()

	// 设备上线
	if _, err := o.Raw("UPDATE terminal SET online_on = ? WHERE id = ?",
		time.Now(),
		sess.TerminalId,
	).Exec(); err != nil {
		log.Println("Update terminal online time failed:", err.Error())
	}
	return nil
}
Exemple #2
0
func (this *SessionManager) Delete(sn string) bool {
	this.mutex.Lock()
	defer this.mutex.Unlock()
	if sess, ok := this.sessions[sn]; ok {
		delete(this.sessions, sn)
		sess.Close()
		(*sess).State = SESSION_STATE_GONE
		o := mysql.GetOrm()

		// 设备离线
		if _, err := o.Raw("UPDATE terminal SET offline_on = ? WHERE id = ?",
			time.Now(),
			sess.TerminalId,
		).Exec(); err != nil {
			log.Println("Update terminal offline time failed:", err.Error())
		}
		return true
	}
	return false
}