Added example with echo/discard/chargen services. #52

Merged
merged 3 commits into from Jul 1, 2014

2 participants

@yomimono
MirageOS member

Create an examples/ directory containing sample code for a unikernel with simple echo/discard/chargen TCP services running on ports 7, 9, and 19.

@avsm
MirageOS member

Perfect. I can hook these into Travis to build-test as well. We'll need to replicate these to mirage-skeleton as well, but old-fashioned copying is probably better than messing around with git-submodules for now.

@avsm
MirageOS member

Another thought: why don't we install these as actual libraries with the rest of the TCP/IP stack (off by default)? They do, after all, implement 3 RFCs, and are helpful to ensure basic functionality works.

@avsm avsm commented on an outdated diff Jun 18, 2014
examples/services.ml
+ let rec discard c flow =
+ S.TCPV4.read flow >>= fun result -> (
+ match result with
+ | `Eof -> report_and_close c flow "Discard connection closing normally."
+ | `Error _ -> report_and_close c flow "Discard connection read error;
+ closing."
+ | _ -> discard c flow
+ )
+
+
+ let rec echo c flow =
+ S.TCPV4.read flow >>= fun result -> (
+ match result with
+ | `Eof -> report_and_close c flow "Echo connection closure initiated."
+ | `Error e ->
+ let message = (
@avsm
MirageOS member
avsm added a line comment Jun 18, 2014

minor style: no need for brackets here.

let message =
  match ... with
  | ... ->
  | ... ->
in

(you can use ocp-indent to help with this)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@avsm avsm commented on an outdated diff Jun 18, 2014
examples/services.ml
+
+
+ let rec echo c flow =
+ S.TCPV4.read flow >>= fun result -> (
+ match result with
+ | `Eof -> report_and_close c flow "Echo connection closure initiated."
+ | `Error e ->
+ let message = (
+ match e with
+ | `Timeout -> "Echo connection timed out; closing.\n"
+ | `Refused -> "Echo connection refused; closing.\n"
+ | `Unknown s -> (Printf.sprintf "Echo connection error: %s\n" s)
+ ) in
+ report_and_close c flow message
+ | `Ok buf ->
+ S.TCPV4.write flow buf >> echo c flow
@avsm
MirageOS member
avsm added a line comment Jun 18, 2014

subjective style: the >> operator is actually a camlp4 extension that expands to >>= fun () ->

since camlp4 is slowly being deprecated, we've started to prefer not using >> in new code, and just doing the "raw" version instead (>>= fun () ->) even though it's more verbose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@yomimono
MirageOS member

Thanks for the feedback - I implemented the style changes you mentioned. (I especially appreciate the note on >>; I'd noticed a lot of >>= fun () -> in the existing code but didn't realize the reason for it.)

Are the existing names OK if we move them to lib/? If so, I'll move 'em over.

@avsm
MirageOS member

I can't think of a better name (these are "troubleshooting" protocols" I guess?). Installing them as a library but not hooked into the stack by default is best (but perhaps instantiate them in examples/ as is down now)

@avsm
MirageOS member

I'm going ahead and merging these into 1.1.5 in examples, as they can be installed as libraries in a later release. They're useful to have as-is!

@avsm avsm merged commit 7a82935 into mirage:master Jul 1, 2014

1 check passed

Details continuous-integration/travis-ci The Travis CI build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment