Skip to content

Commit 6743fc5

Browse files
committed
comments, variable names, errors
1 parent e46a725 commit 6743fc5

File tree

3 files changed

+27
-21
lines changed

3 files changed

+27
-21
lines changed

execution.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ import (
77
repl "github.com/gopherds/gophernotes/replpkg"
88
)
99

10-
// REPLSession manages the I/O to/from the notebook
11-
var REPLSession *repl.Session
12-
var fset *token.FileSet
10+
var (
11+
// REPLSession manages the I/O to/from the notebook.
12+
REPLSession *repl.Session
13+
fset *token.FileSet
14+
)
1315

14-
// ExecCounter is incremented each time we run user code in the notebook
16+
// ExecCounter is incremented each time we run user code in the notebook.
1517
var ExecCounter int
1618

17-
// SetupExecutionEnvironment initializes the REPL session and set of tmp files
19+
// SetupExecutionEnvironment initializes the REPL session and set of tmp files.
1820
func SetupExecutionEnvironment() {
1921

2022
var err error
@@ -33,7 +35,7 @@ type OutputMsg struct {
3335
Metadata map[string]interface{} `json:"metadata"`
3436
}
3537

36-
// ErrMsg encodes the traceback of errors output to the notebook
38+
// ErrMsg encodes the traceback of errors output to the notebook.
3739
type ErrMsg struct {
3840
EName string `json:"ename"`
3941
EValue string `json:"evalue"`
@@ -54,7 +56,7 @@ func HandleExecuteRequest(receipt MsgReceipt) {
5456
}
5557
content["execution_count"] = ExecCounter
5658

57-
// the compilation/execution magic happen here
59+
// Do the compilation/execution magic.
5860
val, err, stderr := REPLSession.Eval(code)
5961

6062
if err == nil {

gophernotes.go

+11-8
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,12 @@ func HandleShutdownRequest(receipt MsgReceipt) {
147147
os.Exit(0)
148148
}
149149

150-
// RunKernel is the main entry point to start the kernel. This is what is called by the
151-
// gophernotes executable.
150+
// RunKernel is the main entry point to start the kernel.
152151
func RunKernel(connectionFile string, logwriter io.Writer) {
153152

154153
logger = log.New(logwriter, "gophernotes ", log.LstdFlags)
155154

156-
// set up the "Session" with the replpkg
155+
// Set up the "Session" with the replpkg.
157156
SetupExecutionEnvironment()
158157

159158
var connInfo ConnectionInfo
@@ -166,7 +165,7 @@ func RunKernel(connectionFile string, logwriter io.Writer) {
166165
}
167166
logger.Printf("%+v\n", connInfo)
168167

169-
// Set up the ZMQ sockets through which the kernel will communicate
168+
// Set up the ZMQ sockets through which the kernel will communicate.
170169
sockets, err := PrepareSockets(connInfo)
171170
if err != nil {
172171
log.Fatalln(err)
@@ -178,8 +177,8 @@ func RunKernel(connectionFile string, logwriter io.Writer) {
178177
zmq.PollItem{Socket: sockets.ControlSocket, Events: zmq.POLLIN},
179178
}
180179

180+
// Start a message receiving loop.
181181
var msgparts [][]byte
182-
// Message receiving loop:
183182
for {
184183
if _, err = zmq.Poll(pi, -1); err != nil {
185184
log.Fatalln(err)
@@ -189,17 +188,21 @@ func RunKernel(connectionFile string, logwriter io.Writer) {
189188
msgparts, _ = pi[0].Socket.RecvMultipart(0)
190189
msg, ids, err := WireMsgToComposedMsg(msgparts, sockets.Key)
191190
if err != nil {
192-
fmt.Println(err)
191+
log.Println(err)
193192
return
194193
}
195194
HandleShellMsg(MsgReceipt{msg, ids, sockets})
196195
case pi[1].REvents&zmq.POLLIN != 0: // stdin socket - not implemented.
197196
pi[1].Socket.RecvMultipart(0)
198197
case pi[2].REvents&zmq.POLLIN != 0: // control socket - treat like shell socket.
199-
msgparts, _ = pi[2].Socket.RecvMultipart(0)
198+
msgparts, err = pi[2].Socket.RecvMultipart(0)
199+
if err != nil {
200+
log.Println(err)
201+
return
202+
}
200203
msg, ids, err := WireMsgToComposedMsg(msgparts, sockets.Key)
201204
if err != nil {
202-
fmt.Println(err)
205+
log.Println(err)
203206
return
204207
}
205208
HandleShellMsg(MsgReceipt{msg, ids, sockets})

messages.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/pkg/errors"
1313
)
1414

15-
// MsgHeader encodes header info for ZMQ messages
15+
// MsgHeader encodes header info for ZMQ messages.
1616
type MsgHeader struct {
1717
MsgID string `json:"msg_id"`
1818
Username string `json:"username"`
@@ -39,15 +39,16 @@ func (e *InvalidSignatureError) Error() string {
3939
// WireMsgToComposedMsg translates a multipart ZMQ messages received from a socket into
4040
// a ComposedMsg struct and a slice of return identities. This includes verifying the
4141
// message signature.
42-
func WireMsgToComposedMsg(msgparts [][]byte, signkey []byte) (msg ComposedMsg,
43-
identities [][]byte, err error) {
42+
func WireMsgToComposedMsg(msgparts [][]byte, signkey []byte) (ComposedMsg, [][]byte, error) {
43+
4444
i := 0
4545
for string(msgparts[i]) != "<IDS|MSG>" {
4646
i++
4747
}
48-
identities = msgparts[:i]
48+
identities := msgparts[:i]
4949

5050
// Validate signature
51+
var msg ComposedMsg
5152
if len(signkey) != 0 {
5253
mac := hmac.New(sha256.New, signkey)
5354
for _, msgpart := range msgparts[i+2 : i+6] {
@@ -63,7 +64,7 @@ func WireMsgToComposedMsg(msgparts [][]byte, signkey []byte) (msg ComposedMsg,
6364
json.Unmarshal(msgparts[i+3], &msg.ParentHeader)
6465
json.Unmarshal(msgparts[i+4], &msg.Metadata)
6566
json.Unmarshal(msgparts[i+5], &msg.Content)
66-
return
67+
return msg, identities, nil
6768
}
6869

6970
// ToWireMsg translates a ComposedMsg into a multipart ZMQ message ready to send, and
@@ -98,7 +99,7 @@ func (msg ComposedMsg) ToWireMsg(signkey []byte) ([][]byte, error) {
9899
}
99100
msgparts[4] = content
100101

101-
// Sign the message
102+
// Sign the message.
102103
if len(signkey) != 0 {
103104
mac := hmac.New(sha256.New, signkey)
104105
for _, msgpart := range msgparts[1:] {

0 commit comments

Comments
 (0)