forked from joewalnes/websocketd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
352 additions
and
651 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2013 Joe Walnes and the websocketd team. | ||
// All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package libwebsocketd | ||
|
||
type Endpoint interface { | ||
StartReading() | ||
Terminate() | ||
Output() chan string | ||
Send(string) bool | ||
} | ||
|
||
func PipeEndpoints(e1, e2 Endpoint) { | ||
e1.StartReading() | ||
e2.StartReading() | ||
|
||
defer e1.Terminate() | ||
defer e2.Terminate() | ||
for { | ||
select { | ||
case msgOne, ok := <-e1.Output(): | ||
if !ok || !e2.Send(msgOne) { | ||
return | ||
} | ||
case msgTwo, ok := <-e2.Output(): | ||
if !ok || !e1.Send(msgTwo) { | ||
return | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package libwebsocketd | ||
|
||
import ( | ||
"strconv" | ||
"testing" | ||
"time" | ||
) | ||
|
||
var eol_tests = []string{ | ||
"", "\n", "\r\n", "ok\n", "ok\n", | ||
"quite long string for our test\n", | ||
"quite long string for our test\r\n", | ||
} | ||
|
||
var eol_answers = []string{ | ||
"", "", "", "ok", "ok", | ||
"quite long string for our test", "quite long string for our test", | ||
} | ||
|
||
func TestTrimEOL(t *testing.T) { | ||
for n := 0; n < len(eol_tests); n++ { | ||
answ := trimEOL(eol_tests[n]) | ||
if answ != eol_answers[n] { | ||
t.Errorf("Answer '%s' did not match predicted '%s'", answ, eol_answers[n]) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkTrimEOL(b *testing.B) { | ||
for n := 0; n < b.N; n++ { | ||
trimEOL(eol_tests[n%len(eol_tests)]) | ||
} | ||
} | ||
|
||
type TestEndpoint struct { | ||
limit int | ||
prefix string | ||
c chan string | ||
result []string | ||
} | ||
|
||
func (e *TestEndpoint) StartReading() { | ||
go func() { | ||
for i := 0; i < e.limit; i++ { | ||
e.c <- e.prefix + strconv.Itoa(i) | ||
} | ||
time.Sleep(time.Millisecond) // should be enough for smaller channel to catch up with long one | ||
close(e.c) | ||
}() | ||
} | ||
|
||
func (e *TestEndpoint) Terminate() { | ||
} | ||
|
||
func (e *TestEndpoint) Output() chan string { | ||
return e.c | ||
} | ||
|
||
func (e *TestEndpoint) Send(msg string) bool { | ||
e.result = append(e.result, msg) | ||
return true | ||
} | ||
|
||
func TestEndpointPipe(t *testing.T) { | ||
one := &TestEndpoint{2, "one:", make(chan string), make([]string, 0)} | ||
two := &TestEndpoint{4, "two:", make(chan string), make([]string, 0)} | ||
PipeEndpoints(one, two) | ||
if len(one.result) != 4 || len(two.result) != 2 { | ||
t.Errorf("Invalid lengths, should be 4 and 2: %v %v", one.result, two.result) | ||
} else if one.result[0] != "two:0" || two.result[0] != "one:0" { | ||
t.Errorf("Invalid first results, should be two:0 and one:0: %#v %#v", one.result[0], two.result[0]) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright 2013 Joe Walnes and the websocketd team. | ||
// All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package libwebsocketd | ||
|
||
import ( | ||
"io" | ||
"os/exec" | ||
) | ||
|
||
type LaunchedProcess struct { | ||
cmd *exec.Cmd | ||
stdin io.WriteCloser | ||
stdout io.ReadCloser | ||
stderr io.ReadCloser | ||
} | ||
|
||
func launchCmd(commandName string, commandArgs []string, env []string) (*LaunchedProcess, error) { | ||
cmd := exec.Command(commandName, commandArgs...) | ||
cmd.Env = env | ||
|
||
stdout, err := cmd.StdoutPipe() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
stderr, err := cmd.StderrPipe() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
stdin, err := cmd.StdinPipe() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = cmd.Start() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &LaunchedProcess{cmd, stdin, stdout, stderr}, err | ||
} |
Oops, something went wrong.