This commit is contained in:
Liang Ding 2014-12-07 11:07:32 +08:00
parent f54d1a4259
commit dfde3ef339
6 changed files with 37 additions and 35 deletions

View File

@ -38,7 +38,7 @@ func (procs *procs) add(wSession *session.WideSession, proc *os.Process) {
mutex.Lock()
defer mutex.Unlock()
sid := wSession.Id
sid := wSession.ID
userProcesses := (*procs)[sid]
userProcesses = append(userProcesses, proc)
@ -55,7 +55,7 @@ func (procs *procs) remove(wSession *session.WideSession, proc *os.Process) {
mutex.Lock()
defer mutex.Unlock()
sid := wSession.Id
sid := wSession.ID
userProcesses := (*procs)[sid]
@ -80,7 +80,7 @@ func (procs *procs) kill(wSession *session.WideSession, pid int) {
mutex.Lock()
defer mutex.Unlock()
sid := wSession.Id
sid := wSession.ID
userProcesses := (*procs)[sid]

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// Session manipulations.
// Package session includes session related manipulations.
//
// Wide server side needs maintain two kinds of sessions:
//
@ -40,30 +40,30 @@ import (
)
const (
SessionStateActive = iota // session state: active
SessionStateClosed // session state: closed (not used so far)
sessionStateActive = iota
sessionStateClosed // (not used so far)
)
var (
// Session channels. <sid, *util.WSChannel>
// SessionWS holds all session channels. <sid, *util.WSChannel>
SessionWS = map[string]*util.WSChannel{}
// Editor channels. <sid, *util.WSChannel>
// EditorWS holds all editor channels. <sid, *util.WSChannel>
EditorWS = map[string]*util.WSChannel{}
// Output channels. <sid, *util.WSChannel>
// OutputWS holds all output channels. <sid, *util.WSChannel>
OutputWS = map[string]*util.WSChannel{}
// Notification channels. <sid, *util.WSChannel>
// NotificationWS holds all notification channels. <sid, *util.WSChannel>
NotificationWS = map[string]*util.WSChannel{}
)
// HTTP session store.
var HTTPSession = sessions.NewCookieStore([]byte("BEYOND"))
// Wide session, associated with a browser tab.
// WideSession represents a session associated with a browser tab.
type WideSession struct {
Id string // id
ID string // id
Username string // username
HTTPSession *sessions.Session // HTTP session related
Processes []*os.Process // process set
@ -75,14 +75,16 @@ type WideSession struct {
}
// Type of wide sessions.
type Sessions []*WideSession
type wSessions []*WideSession
// Wide sessions.
var WideSessions Sessions
var WideSessions wSessions
// Exclusive lock.
var mutex sync.Mutex
// FixedTimeRelease releases invalid sessions.
//
// In some special cases (such as a browser uninterrupted refresh / refresh in the source code view) will occur
// some invalid sessions, the function checks and removes these invalid sessions periodically (1 hour).
//
@ -95,9 +97,9 @@ func FixedTimeRelease() {
for _, s := range WideSessions {
if s.Updated.Before(threshold) {
glog.V(3).Infof("Removes a invalid session [%s], user [%s]", s.Id, s.Username)
glog.V(3).Infof("Removes a invalid session [%s], user [%s]", s.ID, s.Username)
WideSessions.Remove(s.Id)
WideSessions.Remove(s.ID)
}
}
}
@ -261,7 +263,7 @@ func (s *WideSession) Refresh() {
}
// New creates a wide session.
func (sessions *Sessions) New(httpSession *sessions.Session, sid string) *WideSession {
func (sessions *wSessions) New(httpSession *sessions.Session, sid string) *WideSession {
mutex.Lock()
defer mutex.Unlock()
@ -271,11 +273,11 @@ func (sessions *Sessions) New(httpSession *sessions.Session, sid string) *WideSe
userEventQueue := event.UserEventQueues.New(sid)
ret := &WideSession{
Id: sid,
ID: sid,
Username: httpSession.Values["username"].(string),
HTTPSession: httpSession,
EventQueue: userEventQueue,
State: SessionStateActive,
State: sessionStateActive,
Content: &conf.LatestSessionContent{},
Created: now,
Updated: now,
@ -287,12 +289,12 @@ func (sessions *Sessions) New(httpSession *sessions.Session, sid string) *WideSe
}
// Get gets a wide session with the specified session id.
func (sessions *Sessions) Get(sid string) *WideSession {
func (sessions *wSessions) Get(sid string) *WideSession {
mutex.Lock()
defer mutex.Unlock()
for _, s := range *sessions {
if s.Id == sid {
if s.ID == sid {
return s
}
}
@ -307,12 +309,12 @@ func (sessions *Sessions) Get(sid string) *WideSession {
// 1. user event queue
// 2. process set
// 3. websocket channels
func (sessions *Sessions) Remove(sid string) {
func (sessions *wSessions) Remove(sid string) {
mutex.Lock()
defer mutex.Unlock()
for i, s := range *sessions {
if s.Id == sid {
if s.ID == sid {
// remove from session set
*sessions = append((*sessions)[:i], (*sessions)[i+1:]...)
@ -359,7 +361,7 @@ func (sessions *Sessions) Remove(sid string) {
}
// GetByUsername gets wide sessions.
func (sessions *Sessions) GetByUsername(username string) []*WideSession {
func (sessions *wSessions) GetByUsername(username string) []*WideSession {
mutex.Lock()
defer mutex.Unlock()

View File

@ -32,15 +32,15 @@ import (
)
const (
UserExists = "user exists"
UserCreated = "user created"
UserCreateError = "user create error"
userExists = "user exists"
userCreated = "user created"
userCreateError = "user create error"
)
// Exclusive lock for adding user.
var addUserMutex sync.Mutex
// PreferenceHandle handles request of preference page.
// PreferenceHandler handles request of preference page.
func PreferenceHandler(w http.ResponseWriter, r *http.Request) {
httpSession, _ := HTTPSession.Get(r, "wide-session")
@ -242,7 +242,7 @@ func SignUpUser(w http.ResponseWriter, r *http.Request) {
password := args["password"].(string)
msg := addUser(username, password)
if UserCreated != msg {
if userCreated != msg {
succ = false
data["msg"] = msg
}
@ -260,7 +260,7 @@ func addUser(username, password string) string {
for _, user := range conf.Wide.Users {
if user.Name == username {
return UserExists
return userExists
}
}
@ -272,7 +272,7 @@ func addUser(username, password string) string {
conf.Wide.Users = append(conf.Wide.Users, newUser)
if !conf.Save() {
return UserCreateError
return userCreateError
}
conf.CreateWorkspaceDir(workspace)
@ -284,7 +284,7 @@ func addUser(username, password string) string {
glog.Infof("Created a user [%s]", username)
return UserCreated
return userCreated
}
// helloWorld generates the 'Hello, 世界' source code in workspace/src/hello/main.go.

View File

@ -476,7 +476,7 @@
"output": '{{.conf.OutputChannel}}',
"session": '{{.conf.SessionChannel}}'
},
"wideSessionId": '{{.session.Id}}',
"wideSessionId": '{{.session.ID}}',
"editorTheme": '{{.user.Editor.Theme}}',
"latestSessionContent": {{.latestSessionContent}},
"editorTabSize": '{{.user.Editor.TabSize}}'

View File

@ -19,7 +19,7 @@
channel: {
shell: '{{.conf.ShellChannel}}'
},
wideSessionId: {{.session.Id}}
wideSessionId: {{.session.ID}}
};</script>
<script type="text/javascript" src="{{.conf.StaticServer}}/static/js/lib/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="{{.conf.StaticServer}}/static/js/lib/reconnecting-websocket.js"></script>

View File

@ -7,7 +7,7 @@
</li>
<li>
<label>{{.i18n.current_session}}{{.i18n.colon}}</label>
{{.session.Id}}
{{.session.ID}}
</li>
<li class="border workspace">
<label>{{.i18n.workspace}}{{.i18n.colon}}</label>