Source

aquchinchay / src / aquchinchay / csp / session.go

Full commit
package csp

import (
	"aquchinchay/clients"
	"net/url"
	"strconv"
)

type CSPSession struct {
	Vars map[string]string
	Packets []CSPPacket
	Client clients.RemoteClient
	NextPacketId int
}

func NewCSPSession (client clients.RemoteClient) (session CSPSession) {
	vars := map[string]string{
		"rp": "",
		"rs": "",
		"du": "30",
		"is": "0",
		"i": "0",
		"ps": "0",
		"p": "",
		"bp": "",
		"bs": "",
		"g": "",
		"se": "",
		"ct": "text/html",
	}
	packets := make([]CSPPacket, 0)
	session = CSPSession{vars, packets, client, 1}
	return session
}

func (session CSPSession) Duration () int64 {
	duration, _ := strconv.ParseInt(session.Vars["du"], 0, 0)
	return duration
}

func (session CSPSession) IsStreaming () bool {
	return session.Vars["is"] == "1"
}

func (session CSPSession) Send (message string) {
	session.Client.Send(message)
}

func (session *CSPSession) handleRequest (form url.Values) {
	for key, value := range(form) {
		if _, ok := session.Vars[key]; ok {
			session.Vars[key] = value[0]
		}
	}
	ack, _ := strconv.ParseInt(form.Get("a"), 0, 0)
	lastIndex := 0
	for lastIndex < len(session.Packets) && int(ack) <= session.Packets[lastIndex].ID {
		lastIndex += 1
	}
	session.Packets = session.Packets[lastIndex:len(session.Packets)]
}

func (session CSPSession) hasPackets () bool {
	return len(session.Packets) > 0
}

func (session CSPSession) batchPackets () (batch CSPPacketBatch) {
	batch = newPacketBatch()
	return batch
}

func (session CSPSession) prepareBatch (batch string) string {
	return session.Vars["bp"] + batch + session.Vars["bs"]
}

func (session CSPSession) prepareResponse (response string) string {
	return session.Vars["rp"] + response + session.Vars["rs"]
}

func (session CSPSession) Close () {
	session.Client.Close()
}