Skip to content

Commit

Permalink
Merge pull request #124 from ZihengSun/master
Browse files Browse the repository at this point in the history
make Geoweaver work with JupyterHub
  • Loading branch information
ZihengSun authored Mar 17, 2021
2 parents 0b46fcf + 79b30f7 commit d87d01d
Show file tree
Hide file tree
Showing 10 changed files with 877 additions and 499 deletions.
5 changes: 3 additions & 2 deletions src/main/java/com/gw/server/Java2JupyterClientEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
import org.springframework.stereotype.Service;

import com.gw.jpa.Host;
import com.gw.server.JupyterRedirectServlet.SessionPair;
import com.gw.tools.JupyterSessionPairTool;
import com.gw.tools.SessionPair;
import com.gw.utils.BaseTool;

/**
Expand Down Expand Up @@ -292,7 +293,7 @@ public void onClose(Session session, CloseReason closeReason) {
// this.new_ws_session_between_geoweaver_and_jupyterserver = null;
// logger.debug("The connection between Javascript and Geoweaver is closed. ");

SessionPair pair = JupyterRedirectServlet.findPairByID(this.pairid);
SessionPair pair = JupyterSessionPairTool.findPairByID(this.pairid);

if(!bt.isNull(pair)) {

Expand Down
198 changes: 13 additions & 185 deletions src/main/java/com/gw/server/JupyterHubRedirectServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.gw.jpa.Host;
import com.gw.tools.HostTool;
import com.gw.tools.JupyterSessionPairTool;
import com.gw.tools.SessionPair;
import com.gw.utils.BaseTool;
import com.gw.utils.BeanTool;

Expand All @@ -46,132 +49,21 @@ public class JupyterHubRedirectServlet{
// @Autowired
// Java2JupyterClientEndpoint client;

public static List<SessionPair> pairs = new ArrayList();
// public static List<SessionPair> pairs = new ArrayList();

// @Autowired
// @Autowired
HostTool ht;

// @Autowired
// @Autowired
BaseTool bt;


public JupyterHubRedirectServlet() {

logger.debug("Initializing JupyterHub Websocket Session...");

}

/**
* First session is between browser and geoweaver
* @param b2gsession
* @return
*/
public static SessionPair findPairBy1stSession(Session b2gsession) {

SessionPair pair = null;

for(SessionPair p : pairs) {

if(p.getBrowse_geoweaver_session()==b2gsession) {

pair = p;

break;

}

}

return pair;

}

public static SessionPair findPairByID(String pairid) {

SessionPair pair = null;

for(SessionPair p : pairs) {

if(p.getId().equals(pairid)) {

pair = p;

break;

}

}

return pair;

}

/**
* 2nd session is between geoweaver and jupyter
* @param b2gsession
* @return
*/
public static SessionPair findPairBy2ndSession(Java2JupyterClientEndpoint g2jclient) {

SessionPair pair = null;

for(SessionPair p : pairs) {

if(p.getGeoweaver_jupyter_client()==g2jclient) {

pair = p;

break;

}

}

return pair;

}

public static void removeClosedPair() {

for(SessionPair p : pairs) {

if(!p.getBrowse_geoweaver_session().isOpen() ) {

System.out.println("Detected one browser_geoweaver_session is closed. Removing it...");

pairs.remove(p);

}

if(!p.getGeoweaver_jupyter_client().getNew_ws_session_between_geoweaver_and_jupyterserver().isOpen()) {

System.out.println("Detected one geoweaver jupyter session is closed. Removing it...");

pairs.remove(p);

}

}

}

public static void removePairByID(String id) {

for(SessionPair p : pairs) {

if(p.getId().equals(id)) {

System.out.println("Detected one session pair is closed on one of the websocket session. Removing it...");

pairs.remove(p);

break;

}

}

}

private void init(Session b2gsession) {

if(ht==null) {
Expand All @@ -187,7 +79,7 @@ private void init(Session b2gsession) {
}

// SessionPair pair = findPairBy1stSession(b2gsession);
SessionPair pair = this.findPairByID(b2gsession.getQueryString());
SessionPair pair = JupyterSessionPairTool.findPairByID(b2gsession.getQueryString());

if(bt.isNull(pair)) {

Expand All @@ -201,7 +93,7 @@ private void init(Session b2gsession) {

pair.setGeoweaver_jupyter_client(client);

pairs.add(pair);
JupyterSessionPairTool.pairs.add(pair);

logger.debug("New Pair is created: ID: " + pair.getId());

Expand Down Expand Up @@ -258,7 +150,7 @@ public void open(Session session,

// client = new Java2JupyterClientEndpoint(new URI(trueurl), session, headers, h);

SessionPair pair = this.findPairByID(session.getQueryString());
SessionPair pair = JupyterSessionPairTool.findPairByID(session.getQueryString());
//
pair.getGeoweaver_jupyter_client().init(new URI(trueurl), session, headers, h, pair.getId());

Expand Down Expand Up @@ -294,7 +186,7 @@ public void echo(String message, @PathParam("uuid1") String uuid1, Session sessi

// SessionPair pair = findPairBy1stSession(session);

SessionPair pair = this.findPairByID(session.getQueryString());
SessionPair pair = JupyterSessionPairTool.findPairByID(session.getQueryString());

if(bt.isNull(pair)) {

Expand Down Expand Up @@ -333,13 +225,13 @@ public void close(final Session session) {
logger.error("Channel closed.");

// SessionPair pair = findPairBy1stSession(session);
SessionPair pair = this.findPairByID(session.getQueryString());
SessionPair pair = JupyterSessionPairTool.findPairByID(session.getQueryString());

if(!bt.isNull(pair)) {

pair.getGeoweaver_jupyter_client().getNew_ws_session_between_geoweaver_and_jupyterserver().close(); //close websocket connection

pairs.remove(pair);
JupyterSessionPairTool.pairs.remove(pair);

// client.getNew_ws_session_between_geoweaver_and_jupyterserver().close();

Expand All @@ -358,68 +250,4 @@ public void close(final Session session) {



public class SessionPair{

String id;

Session browse_geoweaver_session;

Java2JupyterClientEndpoint geoweaver_jupyter_client;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public Session findOpenSession() {

Set<Session> sessionset = browse_geoweaver_session.getOpenSessions();

Iterator it = sessionset.iterator();

Session session = browse_geoweaver_session;

while(it.hasNext()){
// System.out.println(it.next());

Session cs = (Session)it.next();

if(id.equals(cs.getQueryString())) {

session = cs;

break;

}

}

return session;

}

public Session getBrowse_geoweaver_session() {

return findOpenSession();
}

public void setBrowse_geoweaver_session(Session browse_geoweaver_session) {

this.browse_geoweaver_session = browse_geoweaver_session;

}

public Java2JupyterClientEndpoint getGeoweaver_jupyter_client() {
return geoweaver_jupyter_client;
}

public void setGeoweaver_jupyter_client(Java2JupyterClientEndpoint geoweaver_jupyter_client) {
this.geoweaver_jupyter_client = geoweaver_jupyter_client;
}

}

}
Loading

0 comments on commit d87d01d

Please sign in to comment.