| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,6 @@ | |||
| 1 | + package edu.rice.cs.dynamicjava.kernel; | ||
| 2 | + | ||
| 3 | + public interface IKernelRunnable extends Runnable { | ||
| 4 | + public void start(); | ||
| 5 | + public void shutdown(); | ||
| 6 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,10 +19,12 @@ | |||
| 19 | 19 | public class Kernel { | |
| 20 | 20 | private final UUID session; | |
| 21 | 21 | ||
| 22 | - private final Thread heartbeat; | ||
| 23 | - private final Thread executor; | ||
| 22 | + private final IKernelRunnable heartbeat; | ||
| 23 | + private final IKernelRunnable executor; | ||
| 24 | 24 | private final Thread control; | |
| 25 | 25 | ||
| 26 | + private volatile boolean isRunning = true; | ||
| 27 | + | ||
| 26 | 28 | public Kernel(String configPath, Interpreter i) throws FileNotFoundException { | |
| 27 | 29 | session = UUID.randomUUID(); | |
| 28 | 30 | ||
@@ -43,41 +45,43 @@ public Kernel(String configPath, Interpreter i) throws FileNotFoundException { | |||
| 43 | 45 | executor = new Executor(i, session, ctx, shellAddr, iopubAddr); | |
| 44 | 46 | ||
| 45 | 47 | String controlAddr = String.format("%s://%s:%d", transport, ip, config.getInt("control_port")); | |
| 46 | - control = new Control(ctx, controlAddr); | ||
| 48 | + control = new Control(heartbeat, executor, session, ctx, controlAddr); | ||
| 49 | + } | ||
| 47 | 50 | ||
| 48 | - // stdin = ctx.createSocket(ZMQ.ROUTER); | ||
| 49 | - // stdin.bind(String.format("%s://%s:%d", transport, ip, config.getInt("stdin_port"))); | ||
| 50 | - // | ||
| 51 | - // iopub = ctx.createSocket(ZMQ.PUB); | ||
| 52 | - // iopub.bind(); | ||
| 51 | + private void shutdown() { | ||
| 52 | + isRunning = false; | ||
| 53 | 53 | } | |
| 54 | 54 | ||
| 55 | - private static class Heartbeat extends Thread { | ||
| 56 | - private final ZContext ctx; | ||
| 55 | + private static class Heartbeat extends Thread implements IKernelRunnable { | ||
| 57 | 56 | private final ZMQ.Socket skt; | |
| 57 | + private volatile boolean isRunning = true; | ||
| 58 | 58 | ||
| 59 | 59 | public Heartbeat(ZContext ctx, String addr) { | |
| 60 | - this.ctx = ctx; | ||
| 61 | 60 | skt = ctx.createSocket(ZMQ.REP); | |
| 62 | 61 | skt.bind(addr); | |
| 63 | 62 | } | |
| 64 | 63 | ||
| 64 | + public void shutdown() { | ||
| 65 | + isRunning = false; | ||
| 66 | + } | ||
| 67 | + | ||
| 65 | 68 | @Override | |
| 66 | 69 | public void run() { | |
| 67 | - while (true) { | ||
| 70 | + while (isRunning) { | ||
| 68 | 71 | byte[] msg = skt.recv(); | |
| 69 | 72 | skt.send(msg); | |
| 70 | 73 | } | |
| 71 | 74 | } | |
| 72 | 75 | } | |
| 73 | 76 | ||
| 74 | - private static class Executor extends Thread { | ||
| 77 | + private static class Executor extends Thread implements IKernelRunnable { | ||
| 75 | 78 | private final Interpreter i; | |
| 76 | 79 | private final UUID session; | |
| 77 | 80 | private final ZMQ.Socket shellSkt; | |
| 78 | 81 | private final ZMQ.Socket iopubSkt; | |
| 79 | 82 | ||
| 80 | 83 | private int execCount = 1; | |
| 84 | + private volatile boolean isRunning = true; | ||
| 81 | 85 | ||
| 82 | 86 | public Executor(Interpreter i, UUID session, ZContext ctx, String shellAddr, String iopubAddr) { | |
| 83 | 87 | this.i = i; | |
@@ -89,9 +93,13 @@ public Executor(Interpreter i, UUID session, ZContext ctx, String shellAddr, Str | |||
| 89 | 93 | iopubSkt.bind(iopubAddr); | |
| 90 | 94 | } | |
| 91 | 95 | ||
| 96 | + public void shutdown() { | ||
| 97 | + isRunning = false; | ||
| 98 | + } | ||
| 99 | + | ||
| 92 | 100 | @Override | |
| 93 | 101 | public void run() { | |
| 94 | - while (true) { | ||
| 102 | + while (isRunning) { | ||
| 95 | 103 | Message msg = new Message(ZMsg.recvMsg(shellSkt)); | |
| 96 | 104 | ||
| 97 | 105 | String msgType = msg.getHeader().getString("msg_type"); | |
@@ -169,27 +177,43 @@ public void run() { | |||
| 169 | 177 | msg.respond(session, "execute_reply", retContent).serialize().send(shellSkt); | |
| 170 | 178 | } | |
| 171 | 179 | execCount += 1; | |
| 172 | - } else { | ||
| 180 | + } else if (msgType.equals("shutdown_request")) { } | ||
| 181 | + else { | ||
| 173 | 182 | System.out.format("ERROR: Unknown message type: %s\n", msgType); | |
| 174 | 183 | } | |
| 175 | 184 | } | |
| 176 | 185 | } | |
| 177 | 186 | } | |
| 178 | 187 | ||
| 179 | 188 | private static class Control extends Thread { | |
| 180 | - private final ZContext ctx; | ||
| 189 | + private final UUID session; | ||
| 181 | 190 | private final ZMQ.Socket skt; | |
| 191 | + private final IKernelRunnable heartbeat; | ||
| 192 | + private final IKernelRunnable executor; | ||
| 182 | 193 | ||
| 183 | - public Control(ZContext ctx, String addr) { | ||
| 184 | - this.ctx = ctx; | ||
| 194 | + public Control(IKernelRunnable heartbeat, IKernelRunnable executor, UUID session, ZContext ctx, String addr) { | ||
| 195 | + this.heartbeat = heartbeat; | ||
| 196 | + this.executor = executor; | ||
| 197 | + this.session = session; | ||
| 185 | 198 | skt = ctx.createSocket(ZMQ.ROUTER); | |
| 186 | 199 | skt.bind(addr); | |
| 187 | 200 | } | |
| 188 | 201 | ||
| 189 | 202 | @Override | |
| 190 | 203 | public void run() { | |
| 191 | - Message msg = new Message(ZMsg.recvMsg(skt)); | ||
| 192 | - System.out.println(msg.toString()); | ||
| 204 | + while (true) { | ||
| 205 | + Message msg = new Message(ZMsg.recvMsg(skt)); | ||
| 206 | + String msgType = msg.getHeader().getString("msg_type"); | ||
| 207 | + if (msgType.equals("shutdown_request")) { | ||
| 208 | + executor.shutdown(); | ||
| 209 | + heartbeat.shutdown(); | ||
| 210 | + | ||
| 211 | + JSONObject retContent = new JSONObject(); | ||
| 212 | + retContent.put("restart", "false"); | ||
| 213 | + msg.respond(session, "shutdown_reply", retContent).serialize().send(skt); | ||
| 214 | + return; | ||
| 215 | + } | ||
| 216 | + } | ||
| 193 | 217 | } | |
| 194 | 218 | } | |
| 195 | 219 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments