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

View File

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

View File

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

View File

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

View File

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

View File

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