-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* duplex rewrite * fix CLI usage bug * fix race * redesign api * update README.md
- Loading branch information
Showing
74 changed files
with
1,636 additions
and
1,110 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,73 +1,64 @@ | ||
// Example of using hookah with a custom input protocol. In this case the input | ||
// protocol is named numbers:// and it can accept "odd" or "even" as the | ||
// argument. | ||
// Example of using hookah with a custom protocol. In this case the protocol is | ||
// named numbers:// and it can accept "odd" or "even" as the argument. | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"log" | ||
"net/url" | ||
"time" | ||
|
||
"github.com/wybiral/hookah" | ||
"github.com/wybiral/hookah/pkg/node" | ||
) | ||
|
||
func main() { | ||
// Create hookah API instance | ||
h := hookah.New() | ||
// Register new protocol | ||
h.RegisterInput("numbers", "numbers://parity", numbersHandler) | ||
// Create hookah input (using new numbers:// protocol) | ||
r, err := h.NewInput("numbers://odd") | ||
h.RegisterProtocol("numbers", "numbers://parity", numbersHandler) | ||
// Create hookah node (using our new numbers:// protocol) | ||
r, err := h.NewNode("numbers://odd") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer r.Close() | ||
// Create hookah output (stdout) | ||
w, err := h.NewOutput("stdout") | ||
// Create hookah node (stdout) | ||
w, err := h.NewNode("stdout") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer w.Close() | ||
// Copy forever | ||
io.Copy(w, r) | ||
io.Copy(w.W, r.R) | ||
} | ||
|
||
// struct type to implement interface on. | ||
type numbers struct { | ||
counter int64 | ||
} | ||
// type to implement Reader interface on. | ||
type numbers int64 | ||
|
||
// Input handlers take an arg string and return an io.ReadCloser for the input | ||
// stream (or an error). | ||
func numbersHandler(arg string, opts url.Values) (io.ReadCloser, error) { | ||
var counter int64 | ||
// Handlers take an arg string and return a Node | ||
func numbersHandler(arg string) (*node.Node, error) { | ||
var counter numbers | ||
if arg == "odd" { | ||
counter = 1 | ||
} else if arg == "even" { | ||
counter = 2 | ||
} else { | ||
return nil, errors.New("numbers requires: odd or even") | ||
} | ||
return &numbers{counter: counter}, nil | ||
// Node can have R: Reader, W: Writer, C: Closer | ||
// In this case it's just a Reader | ||
return &node.Node{R: &counter}, nil | ||
} | ||
|
||
// Read method satisfies the io.ReadCloser interface | ||
// Read the next number (after delay) and increment counter. | ||
func (num *numbers) Read(b []byte) (int, error) { | ||
// Artificial delay | ||
time.Sleep(time.Second) | ||
// Format counter | ||
s := fmt.Sprintf("%d\n", num.counter) | ||
s := fmt.Sprintf("%d\n", *num) | ||
// Increment counter | ||
num.counter += 2 | ||
*num += 2 | ||
// Copy to byte array | ||
n := copy(b, []byte(s)) | ||
return n, nil | ||
} | ||
|
||
// Close method satisfies the io.ReadCloser interface | ||
func (num *numbers) Close() error { | ||
return nil | ||
} |
Oops, something went wrong.