Skip to content

Commit

Permalink
Use proto3 again.
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed May 25, 2018
1 parent d765300 commit 5b85863
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 58 deletions.
6 changes: 3 additions & 3 deletions dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ func recv(buf []byte) (response []byte) {
msg := &BaseMsg{}
check(proto.Unmarshal(buf, msg))
assert(len(msg.Payload) > 0, "BaseMsg has empty payload.")
subscribers, ok := channels[*msg.Channel]
subscribers, ok := channels[msg.Channel]
if !ok {
panic("No subscribers for channel " + *msg.Channel)
panic("No subscribers for channel " + msg.Channel)
}
for i := 0; i < len(subscribers); i++ {
s := subscribers[i]
Expand All @@ -52,7 +52,7 @@ func Sub(channel string, cb Subscriber) {

func Pub(channel string, payload []byte) {
resChan <- &BaseMsg{
Channel: &channel,
Channel: channel,
Payload: payload,
}
}
Expand Down
10 changes: 5 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ func main() {

var command = Msg_START // TODO use proto3
PubMsg("start", &Msg{
Command: &command,
StartCwd: &cwd,
Command: command,
StartCwd: cwd,
StartArgv: args,
StartDebugFlag: flagDebug,
StartMainJs: &main_js,
StartMainMap: &main_map,
StartDebugFlag: *flagDebug,
StartMainJs: main_js,
StartMainMap: main_map,
})

DispatchLoop()
Expand Down
57 changes: 28 additions & 29 deletions msg.proto
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
syntax = "proto2";
syntax = "proto3";
package main;

message BaseMsg {
optional string channel = 1;
optional bytes payload = 2;
string channel = 1;
bytes payload = 2;
}

message Msg {
optional string error = 1;
string error = 1;

enum Command {
ERROR = 1;
START = 2;
SOURCE_CODE_FETCH_RES = 3;
ERROR = 0;
START = 1;
SOURCE_CODE_FETCH_RES = 2;
ONEOF = 100;
}
optional Command command = 2 [ default = ONEOF ];
Command command = 2;

// We avoid creating a message for each command (and use oneof or any types)
// In order to reduce code in the size of the generated javascript
Expand All @@ -24,21 +24,20 @@ message Msg {
// arguments by their name.

// Start
optional string start_cwd = 10;
string start_cwd = 10;
repeated string start_argv = 11;
optional bool start_debug_flag = 12;
optional string start_main_js = 13; // The contents of dist/main.js
optional string start_main_map = 14; // The contents of dist/main.map
bool start_debug_flag = 12;
string start_main_js = 13; // The contents of dist/main.js
string start_main_map = 14; // The contents of dist/main.map

// SOURCE_CODE_FETCH_RES
// If it's a non-http module, moduleName and filename will be the same.
// For http modules, moduleName is its resolved http URL, and filename
// is the location of the locally downloaded source code.
optional string source_code_fetch_res_module_name = 30;
optional string source_code_fetch_res_filename = 31;
optional string source_code_fetch_res_source_code = 32;
optional string source_code_fetch_res_output_code =
33; // Non-empty only if cached.
string source_code_fetch_res_module_name = 30;
string source_code_fetch_res_filename = 31;
string source_code_fetch_res_source_code = 32;
string source_code_fetch_res_output_code = 33; // Non-empty only if cached.

oneof payload {
SourceCodeFetchMsg source_code_fetch = 100;
Expand All @@ -51,27 +50,27 @@ message Msg {
}

message SourceCodeFetchMsg {
optional string module_specifier = 1;
optional string containing_file = 2;
string module_specifier = 1;
string containing_file = 2;
}

message SourceCodeCacheMsg {
optional string filename = 1;
optional string source_code = 2;
optional string output_code = 3;
string filename = 1;
string source_code = 2;
string output_code = 3;
}

message ExitMsg { optional int32 code = 1; }
message ExitMsg { int32 code = 1; }

message TimerStartMsg {
optional int32 id = 1;
optional bool interval = 2;
optional int32 duration = 3; // In milliseconds.
int32 id = 1;
bool interval = 2;
int32 duration = 3; // In milliseconds.
}

message TimerReadyMsg {
optional int32 id = 1;
optional bool done = 2;
int32 id = 1;
bool done = 2;
}

message TimerClearMsg { optional int32 id = 1; }
message TimerClearMsg { int32 id = 1; }
25 changes: 12 additions & 13 deletions os.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ func InitOS() {
switch msg.Payload.(type) {
case *Msg_Exit:
payload := msg.GetExit()
os.Exit(int(*payload.Code))
os.Exit(int(payload.Code))
case *Msg_SourceCodeFetch:
payload := msg.GetSourceCodeFetch()
return HandleSourceCodeFetch(*payload.ModuleSpecifier, *payload.ContainingFile)
return HandleSourceCodeFetch(payload.ModuleSpecifier,
payload.ContainingFile)
case *Msg_SourceCodeCache:
payload := msg.GetSourceCodeCache()
return HandleSourceCodeCache(*payload.Filename, *payload.SourceCode,
*payload.OutputCode)
return HandleSourceCodeCache(payload.Filename, payload.SourceCode,
payload.OutputCode)
default:
panic("[os] Unexpected message " + string(buf))
}
Expand Down Expand Up @@ -64,8 +65,7 @@ func HandleSourceCodeFetch(moduleSpecifier string, containingFile string) (out [

defer func() {
if err != nil {
var errStr = err.Error()
res.Error = &errStr
res.Error = err.Error()
}
out, err = proto.Marshal(res)
check(err)
Expand Down Expand Up @@ -101,11 +101,11 @@ func HandleSourceCodeFetch(moduleSpecifier string, containingFile string) (out [
var sourceCode = string(sourceCodeBuf)
var command = Msg_SOURCE_CODE_FETCH_RES
res = &Msg{
Command: &command,
SourceCodeFetchResModuleName: &moduleName,
SourceCodeFetchResFilename: &filename,
SourceCodeFetchResSourceCode: &sourceCode,
SourceCodeFetchResOutputCode: &outputCode,
Command: command,
SourceCodeFetchResModuleName: moduleName,
SourceCodeFetchResFilename: filename,
SourceCodeFetchResSourceCode: sourceCode,
SourceCodeFetchResOutputCode: outputCode,
}
return
}
Expand All @@ -118,8 +118,7 @@ func HandleSourceCodeCache(filename string, sourceCode string,
err := ioutil.WriteFile(fn, outputCodeBuf, 0600)
res := &Msg{}
if err != nil {
var errStr = err.Error()
res.Error = &errStr
res.Error = err.Error()
}
out, err := proto.Marshal(res)
check(err)
Expand Down
16 changes: 8 additions & 8 deletions timers.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ func InitTimers() {
switch msg.Payload.(type) {
case *Msg_TimerStart:
payload := msg.GetTimerStart()
timers[*payload.Id] = &Timer{
Id: *payload.Id,
timers[payload.Id] = &Timer{
Id: payload.Id,
Done: false,
Interval: *payload.Interval,
Duration: *payload.Duration,
Interval: payload.Interval,
Duration: payload.Duration,
Cleared: false,
}
timers[*payload.Id].StartTimer()
timers[payload.Id].StartTimer()
return nil
case *Msg_TimerClear:
payload := msg.GetTimerClear()
// TODO maybe need mutex here.
timer := timers[*payload.Id]
timer := timers[payload.Id]
timer.Clear()
return nil
default:
Expand Down Expand Up @@ -64,8 +64,8 @@ func (t *Timer) StartTimer() {
PubMsg("timers", &Msg{
Payload: &Msg_TimerReady{
TimerReady: &TimerReadyMsg{
Id: &t.Id,
Done: &t.Done,
Id: t.Id,
Done: t.Done,
},
},
})
Expand Down

0 comments on commit 5b85863

Please sign in to comment.