pagemanager

package
v0.0.0-...-7cdfd4c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2021 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NewUserMsg messageType = iota
	RemoveUserMsg
	RenameUserMsg
	NewFileMsg
	RemoveFileMsg
	InitMsg
	ChatMsg
	SignalingMsg
	UploadProgressMsg
)

those are the message types passed between server/client through websocket or HTTP req/res

View Source
const (
	PageIdLength = 6 + 18 // 6 for 36-based int64 value of timestamp, 18 for random alpha-numeric string

)

Variables

This section is empty.

Functions

func New

func New(tpl *template.Template) *pageManager

New is the constructor of pageManager, returns a pagemanager ptr

func ShiftPath

func ShiftPath(p string) (head, tail string)

ShiftPath is only called in pagemanager.serveHTTP(), it split the req URL "/abc/xyz/123" into 2 parts: "abc", "/xyz/123" serveHTTP use this as a simple route matcher

Types

type File

type File struct {
	Id       fileId `bson:"id" json:"id"`
	Name     string `bson:"name" json:"name"`
	Size     int64  `bson:"size" json:"size"`
	MIME     string `bson:"MIME" json:"MIME"`
	Uploaded bool   `bson:"-" json:"uploaded"` // only the uploaded files info are saved in mgo.
}

type FileNamer

type FileNamer interface {
	// contains filtered or unexported methods
}

Each file has a fileID, which can be a random string, or hashing result of its content. This type must implement name() function returning a string as fileID.

type FileSize

type FileSize float64

type FileUploadProgress

type FileUploadProgress struct {
	FileId        fileId `json:"fileId"`
	FileSize      int64  `json:"-"` // this is not the real filesize but request.ContentLength which include other overhead(multipart boundary string, fieldName/fieldValue)
	UploadedBytes int64  `json:"-"`

	MIME     string  `json:"MIME,omitempty"` // when upload succeeded(progress hits 1), server pass this field with the new value detected by http.DetectContentType
	Progress float32 `json:"progress"`
	// contains filtered or unexported fields
}

FileUploadProgress is a io.Writer, passed to io.Copy() when client is uploading a file, it serves 2 purposes: to calculate the uploaded bytes(return error if exceeded maximum allowed filesize), to broadcast the progress value to all clients

func (*FileUploadProgress) Write

func (up *FileUploadProgress) Write(p []byte) (int, error)

type Message

type Message struct {
	Type messageType `json:"type"`
	//Id messageId `json:"id,omitempty"`
	//From userId `json:"from,omitempty"`
	//To bson.ObjectId `json:"to,omitempty"`
	Ignore  userId      `json:"ignore"`
	Content interface{} `json:"content,omitempty"`
	SentAt  time.Time   `json:"-,omitempty"`
}

type Page

type Page struct {
	Id    pageId  `bson:"pageId" json:"pageId"`
	Users []*User `bson:"-" json:"-"` // when marshaling, skip Users field, otherwise infinite loop would occur: Page has users, which has page, which has user, ...
	Files []*File `bson:"files" json:"-"`

	ExpiresAt time.Time `bson:"expiresAt" json:"expiresAt"`
	// contains filtered or unexported fields
}

func NewPage

func NewPage(pid pageId) *Page

NewPage returns a *Page object, it only get called in pagemanager.handleUserJoin() when a new user(connect via websocket) want to join a page, and pagemanager found the page doesn't exist yet, thus create the page.

func (*Page) AddUser

func (p *Page) AddUser(u *User) error

AddUser add the user into current page object after successfully sending the initMsg(user profile, peerList, fileList, etc)

func (*Page) HandleUpload

func (p *Page) HandleUpload(w http.ResponseWriter, r *http.Request, pageId string)

HandleUpload use reader.NextPart() to read the file data wrapped in multi-part request format. The request only has 2 part: fileId and actual file data. When saving the file data, it use io.TeeReader to call FileUploadProgress.Write() to calculate uploaded bytes and broadcast the progress to all peers.

func (*Page) RemoveUser

func (p *Page) RemoveUser(uId userId) bool

RemoveUser remove the user from current page. The returned boolean value indicates whether this is the last user and there is no incoming new user. In other word, is it safe also to remove the page from the page manager object?

type SignalingData

type SignalingData struct {
	MsgType       string      `json:"msgType"`
	From          string      `json:"from"`
	To            string      `json:"to"`
	SignalingData interface{} `json:"signalingData"`
}

simple-filer need this data to be exchanged between 2 peers before they can talk to each other directly

type User

type User struct {
	Id      userId          `json:"id"`
	Name    string          `json:"name"`
	Page    *Page           `json:"-"` // when marshaling, skip Page field, otherwise, infinite loop would occur. User has page, which has users, which has page, ....
	Socket  *websocket.Conn `json:"-"`
	PeerMsg chan *Message   `json:"-"`
}

func NewUser

func NewUser(pid pageId, socket *websocket.Conn) *User

NewUser is invoked when socket connection is successfully created, and returns a user object. This object requires a *Page field(we pass a dummy &Page{Id: pid} object. At this moment, we don't know whether this Page object exists in fileManager or not. Caller will send the user object to pagemanager.handleUserJoin(userObj) pageManager will decide whether to use the existing page object or create a new one based on pageId.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL