@@ -16,26 +16,30 @@ type IfSharpKernel(connectionInformation : ConnectionInformation) =
1616
1717 // startup 0mq stuff
1818 let context = NetMQContext.Create()
19-
2019 // heartbeat
21- let hbSocket = context.CreateRequestSocket()
22- do hbSocket.Bind( String.Format( " {0}://{1}:{2}" , connectionInformation.transport, connectionInformation.ip, connectionInformation.hb_ port))
23-
24- // shell
25- let shellSocket = context.CreateRouterSocket()
26- do shellSocket.Bind( String.Format( " {0}://{1}:{2}" , connectionInformation.transport, connectionInformation.ip, connectionInformation.shell_ port))
20+ let hbSocket = context.CreateRouterSocket()
21+ let hbSocketURL = String.Format( " {0}://{1}:{2}" , connectionInformation.transport, connectionInformation.ip, connectionInformation.hb_ port)
22+ do hbSocket.Bind( hbSocketURL)
2723
2824 // control
2925 let controlSocket = context.CreateRouterSocket()
30- do controlSocket.Bind( String.Format( " {0}://{1}:{2}" , connectionInformation.transport, connectionInformation.ip, connectionInformation.control_ port))
26+ let controlSocketURL = String.Format( " {0}://{1}:{2}" , connectionInformation.transport, connectionInformation.ip, connectionInformation.control_ port)
27+ do controlSocket.Bind( controlSocketURL)
3128
3229 // stdin
3330 let stdinSocket = context.CreateRouterSocket()
34- do stdinSocket.Bind( String.Format( " {0}://{1}:{2}" , connectionInformation.transport, connectionInformation.ip, connectionInformation.stdin_ port))
31+ let stdinSocketURL = String.Format( " {0}://{1}:{2}" , connectionInformation.transport, connectionInformation.ip, connectionInformation.stdin_ port)
32+ do stdinSocket.Bind( stdinSocketURL)
3533
3634 // iopub
3735 let ioSocket = context.CreatePublisherSocket()
38- do ioSocket.Bind( String.Format( " {0}://{1}:{2}" , connectionInformation.transport, connectionInformation.ip, connectionInformation.iopub_ port))
36+ let ioSocketURL = String.Format( " {0}://{1}:{2}" , connectionInformation.transport, connectionInformation.ip, connectionInformation.iopub_ port)
37+ do ioSocket.Bind( ioSocketURL)
38+
39+ // shell
40+ let shellSocket = context.CreateRouterSocket()
41+ let shellSocketURL = String.Format( " {0}://{1}:{2}" , connectionInformation.transport, connectionInformation.ip, connectionInformation.shell_ port)
42+ do shellSocket.Bind( shellSocketURL)
3943
4044 let data = new List< BinaryOutput>()
4145 let payload = new List< Payload>()
@@ -96,21 +100,20 @@ type IfSharpKernel(connectionInformation : ConnectionInformation) =
96100 ignore ( hmac.TransformFinalBlock( Array.zeroCreate 0 , 0 , 0 ))
97101 BitConverter.ToString( hmac.Hash) .Replace( " -" , " " ) .ToLower()
98102
99- let recvAll ( socket : NetMQSocket ) = socket.ReceiveMessages ()
103+ let recvAll ( socket : NetMQSocket ) = socket.ReceiveMultipartBytes ()
100104
101105 /// Constructs an 'envelope' from the specified socket
102106 let recvMessage ( socket : NetMQSocket ) =
103107
104108 // receive all parts of the message
105- let message =
106- recvAll ( socket)
107- |> Seq.map decode
108- |> Seq.toArray
109+ let message = ( recvAll ( socket)) |> Array.ofSeq
110+ let asStrings = message |> Array.map decode
109111
110112 // find the delimiter between IDS and MSG
111- let idx = Array.IndexOf( message, " <IDS|MSG>" )
113+ let idx = Array.IndexOf( asStrings, " <IDS|MSG>" )
114+
112115 let idents = message.[ 0 .. idx - 1 ]
113- let messageList = message .[ idx + 1 .. message.Length - 1 ]
116+ let messageList = asStrings .[ idx + 1 .. message.Length - 1 ]
114117
115118 // detect a malformed message
116119 if messageList.Length < 4 then failwith ( " Malformed message" )
@@ -172,7 +175,7 @@ type IfSharpKernel(connectionInformation : ConnectionInformation) =
172175 msg.Append( encode parent_ header)
173176 msg.Append( encode " {}" )
174177 msg.Append( encode content)
175- socket.SendMessage ( msg)
178+ socket.SendMultipartMessage ( msg)
176179
177180
178181 /// Convenience method for sending the state of the kernel
@@ -191,12 +194,25 @@ type IfSharpKernel(connectionInformation : ConnectionInformation) =
191194 let kernelInfoRequest ( msg : KernelMessage ) ( content : KernelRequest ) =
192195 let content =
193196 {
194- protocol_ version = [| 4 ; 0 |];
195- ipython_ version = Some [| 1 ; 0 ; 0 |];
196- language_ version = [| 1 ; 0 ; 0 |];
197+ protocol_ version = " 4.0.0" ;
198+ implementation = " ifsharp" ;
199+ implementation_ version = " 4.0.0" ;
200+ banner = " " ;
201+ help_ links = [||];
197202 language = " fsharp" ;
203+ language_ info =
204+ {
205+ name = " fsharp" ;
206+ version = " 4.3.1.0" ;
207+ mimetype = " text/x-fsharp" ;
208+ file_ extension = " .fs" ;
209+ pygments_ lexer = " " ;
210+ codemirror_ mode = " " ;
211+ nbconvert_ exporter = " " ;
212+ };
198213 }
199214
215+ sendStateBusy msg
200216 sendMessage shellSocket msg " kernel_info_reply" content
201217
202218 /// Sends display data information immediately
@@ -407,11 +423,12 @@ type IfSharpKernel(connectionInformation : ConnectionInformation) =
407423
408424 /// Handles a 'shutdown_request' message
409425 let shutdownRequest ( msg : KernelMessage ) ( content : ShutdownRequest ) =
410-
426+ logMessage " shutdown request "
411427 // TODO: actually shutdown
412428 let reply = { restart = true ; }
413429
414- sendMessage shellSocket msg " shutdown_reply" reply
430+ sendMessage shellSocket msg " shutdown_reply" reply;
431+ System.Environment.Exit( 0 )
415432
416433 /// Handles a 'history_request' message
417434 let historyRequest ( msg : KernelMessage ) ( content : HistoryRequest ) =
@@ -442,7 +459,6 @@ type IfSharpKernel(connectionInformation : ConnectionInformation) =
442459 logMessage ( sbOut.ToString())
443460
444461 while true do
445-
446462 let msg = recvMessage ( shellSocket)
447463
448464 try
@@ -456,16 +472,27 @@ type IfSharpKernel(connectionInformation : ConnectionInformation) =
456472 | HistoryRequest( r) -> historyRequest msg r
457473 | ObjectInfoRequest( r) -> objectInfoRequest msg r
458474 | InspectRequest( r) -> inspectRequest msg r
459- | _ -> logMessage ( String.Format( " Unknown content type. msg_type is `{0}`" , msg.Header.msg_ type))
475+ | _ -> logMessage ( String.Format( " Unknown content type on shell . msg_type is `{0}`" , msg.Header.msg_ type))
460476 with
461477 | ex -> handleException ex
462478
479+ let doControl () =
480+ while true do
481+ let msg = recvMessage ( controlSocket)
482+ try
483+ match msg.Content with
484+ | ShutdownRequest( r) -> shutdownRequest msg r
485+ | _ -> logMessage ( String.Format( " Unexpected content type on control. msg_type is `{0}`" , msg.Header.msg_ type))
486+ with
487+ | ex -> handleException ex
488+
463489 /// Loops repeating message from the client
464490 let doHeartbeat () =
465491
466492 try
467493 while true do
468- hbSocket.Send( hbSocket.Receive())
494+ let hb = hbSocket.ReceiveMultipartBytes() in
495+ hbSocket.SendMultipartBytes( hb)
469496 with
470497 | ex -> handleException ex
471498
@@ -485,5 +512,6 @@ type IfSharpKernel(connectionInformation : ConnectionInformation) =
485512 /// Starts the kernel asynchronously
486513 member __.StartAsync () =
487514
488- Async.Start ( async { doHeartbeat() } )
515+ // Async.Start (async { doHeartbeat() } )
489516 Async.Start ( async { doShell() } )
517+ Async.Start ( async { doControl() } )
0 commit comments