Skip to content

Commit

Permalink
Merge branch 'master' of github.com:3dmol/3Dmol.js
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoes committed Feb 10, 2020
2 parents a90be1a + f35c858 commit f977054
Show file tree
Hide file tree
Showing 4 changed files with 7,659 additions and 5,001 deletions.
58 changes: 44 additions & 14 deletions learning-environment/server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from flask import Flask, request
from flask_socketio import SocketIO, send, emit, join_room, leave_room, close_room
import json
import json, random, datetime

# for socketio
import eventlet
Expand All @@ -15,13 +15,13 @@

@socketio.on('message')
def handleMessage(msg):
print('Message: ' + msg)
print(datetime.datetime.now(),'Message: ' + msg)
send(msg, broadcast=True)

@socketio.on('check session name event')
def handleCheckName(json):
name = json['name']
print("checking ",name,name in sessions)
#print("checking ",name,name in sessions)
if name in sessions:
emit('check session name response', "exists")
else:
Expand All @@ -32,28 +32,58 @@ def handleCreateSession(json):
name = json['name']
if name in sessions:
print("Invalid name",name)
emit('create session response', False)
emit('create session response', 0)
else:
emit('create session response', True)
print("Session Created",name)
secret = random.randint(1,4e9)
emit('create session response', secret)
print("Session Created",name,request.sid)
sessions[name] = {'cnt': 0,
'sid': request.sid, # id of the controller
'state': json['state'],
'view': json['view'],
'query_updates': {}, # each clients individual selections
'query_active' : False
'query_active' : False,
'secret' : secret
}
join_room(name)
emit('session count', 0, room=request.sid)

@socketio.on('reconnection')
def handleReconnection(json):
'''Reconnections change the socket id'''
name = json['name']
secret = json['secret']
oldsid = json['sid']

# print("Reconnect",name,secret,oldsid,sessions[name]['secret'])

if name not in sessions:
return

#reconnecting clients need to re-join the room as well
join_room(name)
if secret == sessions[name]['secret']: #controller
sessions[name]['sid'] = request.sid
else : #client
updates = sessions[name]['query_updates']
if oldsid in updates: #change updates to match new sid
updates[request.sid] = updates[oldsid]
del updates[oldsid]
#make sure clients are up to date
if 'view' in sessions[name]:
emit('viewer view change response', sessions[name]['view'], room=request.sid)
if 'state' in sessions[name]:
emit('viewer state change response', sessions[name]['state'], room=request.sid)


@socketio.on('join session event')
def handleJoinSession(json):
name = json['name']
print("Join",name)
#print("Join",name)
if name not in sessions:
emit('join session response', "0")
else:
emit('join session response', "1")
emit('join session response', request.sid)
join_room(name)
try:
sessions[name]['cnt'] += 1
Expand All @@ -65,7 +95,7 @@ def handleJoinSession(json):
emit('viewer view change response', sessions[name]['view'], room=request.sid)
if 'state' in sessions[name]:
emit('viewer state change response', sessions[name]['state'], room=request.sid)
print("Joined Session: " + str(json))
#print("Joined Session: " + str(json))

if sessions[name]['query_active']:
emit('query start response', sessions[name]['query_active'], room=request.sid)
Expand Down Expand Up @@ -110,7 +140,7 @@ def handleViewerChange(json):
if name not in sessions:
return
if request.sid != sessions[name]['sid']:
print("Incorrect sid for viewer change")
#print(datetime.datetime.now(),"Incorrect sid for viewer change %s vs %s"%(request.sid,sessions[name]['sid']))
return #only the controller can send updates
sessions[name]['view'] = viewstate
emit('viewer view change response', viewstate, room=name)
Expand All @@ -119,19 +149,19 @@ def handleViewerChange(json):
def handleStateChange(json):
name = json['name']
state = json['state']
print("State change",name)
#print("State change",name)
if name not in sessions:
return
if request.sid != sessions[name]['sid']:
print("Incorrect sid for state change")
#print("Incorrect sid for state change")
return #only the controller can send updates
sessions[name]['state'] = state
emit('viewer state change response', state, room=name)

@socketio.on('query start')
def handleQueryStart(json):
name = json['name']
print("query start",name)
#print("query start",name)
if name not in sessions:
return
if request.sid != sessions[name]['sid']:
Expand Down
52 changes: 37 additions & 15 deletions learning-environment/sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ var initSessions = function() {
// setup callbacks
glviewer.setViewChangeCallback(viewUpdateCallback);
glviewer.setStateChangeCallback(stateUpdateCallback);
initiator = true;
initiator = parseInt(msg);

// close the connection create pane and open the
// connection monitoring
Expand All @@ -128,12 +128,13 @@ var initSessions = function() {
$('#sessioncontrol').show();
} else {
alert("Session name was already taken/ could not be created. Try Again");
initiator = 0;
}
});

socket.on('join session response', function(msg) {
if (msg == 1) {
joined = true;
if (msg != "0") {
joined = msg;
// close the connection create pane and open the connection
// monitoring
// disable the sidebar
Expand Down Expand Up @@ -176,15 +177,15 @@ var initSessions = function() {
leaveSession();
});

var sessionEnded = function() {
var sessionEnded = function(reason) {
joined = false;
resetUpperRight();
clientFinishQuery();
};

socket.on('delete session response', function() {
$('#createSession,#joinSession').prop('disabled', false);
initiator = false;
initiator = 0;
// remove callbacks
glviewer.setViewChangeCallback(null);
glviewer.setStateChangeCallback(null);
Expand All @@ -193,11 +194,32 @@ var initSessions = function() {
});

socket.on('leave session response', sessionEnded);
socket.on('disconnect', sessionEnded);
socket.on('disconnect', function(reason) {
console.log("disconnect: "+reason);
//do NOT end session since we will attempt to reconnect
});

socket.on('error: restart connection', function() {
location.reload();
})
console.log("error: restart connection");
});

socket.on('error', (error) => {
console.log('error: '+error);
});

socket.on("reconnect", function() {
console.log("Reconnected");
if(initiator) {
socket.emit('reconnection', {name: session_name, secret: initiator, sid: null});
} else if(joined) {
socket.emit('reconnection', {name: session_name, secret: null, sid: joined});
}
});

socket.on("connect_timeout", function() {
console.log("Connect timeout");
});

socket.on('session count', setNumConnections);

socket.on('viewer view change response', function(new_view) {
Expand Down Expand Up @@ -328,14 +350,13 @@ var initSessions = function() {
glviewer.removeLabel(atom.hoverlabel);
if(current_hover_label == atom.hoverlabel) current_hover_label = null;
delete atom.hoverlabel;
}

//send clicked atom to server
socket.emit('query update', {
name : session_name,
selected : getClicked()
});
}
}
//send clicked atoms to server
socket.emit('query update', {
name : session_name,
selected : getClicked()
});
});

glviewer.render(); //calling render is required to register clickables - should probably fix that
Expand Down Expand Up @@ -381,4 +402,5 @@ var initSessions = function() {
result_labels.push(l);
}
});

};
817 changes: 202 additions & 615 deletions py3Dmol/prody.ipynb

Large diffs are not rendered by default.

Loading

0 comments on commit f977054

Please sign in to comment.