Skip to content

Commit

Permalink
Reorganize examples:
Browse files Browse the repository at this point in the history
* The examples/ directory is renamed to example/

* Each program is in its own directory with its own build scripts
  • Loading branch information
vinniefalco committed Jul 20, 2017
1 parent 02419f5 commit 510092d
Show file tree
Hide file tree
Showing 54 changed files with 354 additions and 236 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Version 58:

* Fix unaligned reads in utf8-checker
* Qualify size_t in message template
* Reorganize examples

--------------------------------------------------------------------------------

Expand Down
21 changes: 11 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -183,25 +183,26 @@ file(GLOB_RECURSE BEAST_INCLUDES
${PROJECT_SOURCE_DIR}/include/beast/*.ipp
)

file(GLOB_RECURSE EXAMPLES_INCLUDES
${PROJECT_SOURCE_DIR}/examples/*.hpp
file(GLOB_RECURSE EXAMPLE_INCLUDES
${PROJECT_SOURCE_DIR}/example/*.hpp
)

file(GLOB_RECURSE EXTRAS_INCLUDES
${PROJECT_SOURCE_DIR}/extras/beast/*.hpp
${PROJECT_SOURCE_DIR}/extras/beast/*.ipp
)

add_subdirectory (examples)
if (NOT OPENSSL_FOUND)
message("OpenSSL not found. Not building SSL tests and examples")
else()
add_subdirectory (examples/ssl)
add_subdirectory (test/websocket/ssl)
endif()

add_subdirectory (test)
add_subdirectory (test/core)
add_subdirectory (test/http)
add_subdirectory (test/websocket)
add_subdirectory (test/zlib)

add_subdirectory (example)

if (NOT OPENSSL_FOUND)
message("OpenSSL not found. Not building SSL tests and examples")
else()
add_subdirectory (example/ssl)
add_subdirectory (test/websocket/ssl)
endif()
2 changes: 1 addition & 1 deletion Jamroot
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,4 @@ project beast
;

build-project test ;
build-project examples ;
build-project example ;
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,5 @@ install:
build: off

test_script:
- b2 libs/beast/examples toolset=msvc-14.0
- b2 libs/beast/example toolset=msvc-14.0
- b2 libs/beast/test toolset=msvc-14.0
14 changes: 7 additions & 7 deletions doc/0_main.qbk
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
[template indexterm1[term1] '''<indexterm><primary>'''[term1]'''</primary></indexterm>''']
[template indexterm2[term1 term2] '''<indexterm><primary>'''[term1]'''</primary><secondary>'''[term2]'''</secondary></indexterm>''']
[template repo_file[path] '''<ulink url="https://github.com/vinniefalco/Beast/blob/master/'''[path]'''">'''[path]'''</ulink>''']

[template include_file[path][^<'''<ulink url="https://github.com/vinniefalco/Beast/blob/master/include/'''[path]'''">'''[path]'''</ulink>'''>]]

[def __N3747__ [@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3747.pdf [*N3747]]]
Expand Down Expand Up @@ -76,12 +75,13 @@
[def __static_buffer__ [link beast.ref.beast__static_buffer `static_buffer`]]
[def __static_buffer_n__ [link beast.ref.beast__static_buffer_n `static_buffer_n`]]

[import ../examples/file_body.hpp]
[import ../examples/http_example.cpp]
[import ../examples/websocket_example.cpp]
[import ../examples/echo_op.cpp]
[import ../examples/doc_core_samples.hpp]
[import ../examples/doc_http_samples.hpp]
[import ../example/doc/core_examples.hpp]
[import ../example/doc/http_examples.hpp]
[import ../example/echo-op/echo_op.cpp]
[import ../example/http-client/http_client.cpp]
[import ../example/http-server/file_body.hpp]
[import ../example/websocket-client/websocket_client.cpp]

[import ../test/core/doc_snippets.cpp]
[import ../test/http/doc_snippets.cpp]
[import ../test/websocket/doc_snippets.cpp]
Expand Down
54 changes: 36 additions & 18 deletions doc/2_examples.qbk
Original file line number Diff line number Diff line change
Expand Up @@ -17,76 +17,94 @@ in the examples directory.

Use HTTP to make a GET request to a website and print the response:

File: [repo_file examples/http_example.cpp]
File: [repo_file example/http-client/http_client.cpp]

[example_http_client]


[http_example_get]

[heading WebSocket]

Establish a WebSocket connection, send a message and receive the reply:

File: [repo_file examples/websocket_example.cpp]
File: [repo_file example/websocket-client/websocket_client.cpp]

[example_websocket_client]


[websocket_example_client_echo]

[heading WebSocket Echo Server]

This example demonstrates both synchronous and asynchronous
WebSocket server implementations.

* [repo_file examples/websocket_async_echo_server.hpp]
* [repo_file examples/websocket_sync_echo_server.hpp]
* [repo_file examples/websocket_echo.cpp]
* [repo_file example/websocket-server/main.cpp]
* [repo_file example/websocket-server/websocket_async_echo_server.hpp]
* [repo_file example/websocket-server/websocket_sync_echo_server.hpp]



[heading Secure WebSocket]

Establish a WebSocket connection over an encrypted TLS connection,
send a message and receive the reply. Requires OpenSSL to build.

* [repo_file examples/ssl/websocket_ssl_example.cpp]
* [repo_file example/ssl/websocket_ssl_example.cpp]



[heading HTTPS GET]

This example demonstrates sending and receiving HTTP messages
over a TLS connection. Requires OpenSSL to build.

* [repo_file examples/ssl/http_ssl_example.cpp]
* [repo_file example/ssl/http_ssl_example.cpp]



[heading HTTP Crawl]

This example retrieves the page at each of the most popular domains
as measured by Alexa.

* [repo_file examples/http_crawl.cpp]
* [repo_file example/http-crawl/http_crawl.cpp]



[heading HTTP Server]

This example demonstrates both synchronous and asynchronous server
implementations. It also provides an example of implementing a [*Body]
type, in `file_body`.

* [repo_file examples/file_body.hpp]
* [repo_file examples/http_async_server.hpp]
* [repo_file examples/http_sync_server.hpp]
* [repo_file examples/http_server.cpp]
* [repo_file example/http-server/file_body.hpp]
* [repo_file example/http-server/http_async_server.hpp]
* [repo_file example/http-server/http_sync_server.hpp]
* [repo_file example/http-server/main.cpp]



[heading Composed Operations]

This program shows how to use Beast's core foundations to build a
This program shows how to use Beast's network foundations to build a
composable asynchronous initiation function with associated composed
operation implementation. This is a complete, runnable version of
the example described in the Core Foundations document section.

* [repo_file examples/echo_op.cpp]
* [repo_file example/echo-op/echo_op.cpp]



[heading Documentation Samples]

Here are all of the example functions and classes presented
throughout the documentation, they can be included and used
in your program without modification

* [repo_file examples/doc_core_samples.hpp]
* [repo_file example/doc/core_examples.hpp]

* [repo_file example/doc/http_examples.hpp]


* [repo_file examples/doc_http_samples.hpp]

[endsect]
17 changes: 12 additions & 5 deletions doc/3_4_composed.qbk
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
]

[section:composed Composed Operations]
[block'''<?dbhtml stop-chunking?>''']

Asynchronous operations are started by calling a free function or member
function known as an ['asynchronous initiation function]. The initiation
Expand Down Expand Up @@ -100,7 +101,9 @@ available:
]]
]

[heading Example: Asynchronous Echo]


[section Asynchronous Echo]

Here we develop an asynchronous composed operation called [*echo].
This operation will read up to the first newline on a stream, and
Expand All @@ -112,14 +115,14 @@ initiation function. For our echo operation the only inputs are the
stream and the completion token. The output is the error code which
is usually included in all completion handler signatures.

[core_sample_echo_op_1]
[example_core_echo_op_1]

Now that we have a declaration, we will define the body of the function.
We want to achieve the following goals: perform static type checking on
the input parameters, set up the return value as per __N3747__, and launch
the composed operation by constructing the object and invoking it.

[core_sample_echo_op_2]
[example_core_echo_op_2]

The initiation function contains a few relatively simple parts. There is
the customization of the return value type, static type checking, building
Expand All @@ -133,7 +136,7 @@ without explaining them in depth.
Here is the boilerplate present in all composed operations written
in this style:

[core_sample_echo_op_3]
[example_core_echo_op_3]

We have the common boilerplate for a composed operation and now we just need
to implement the function call operator. Our strategy is to make our composed
Expand All @@ -147,9 +150,13 @@ care must be taken to ensure that no access to data members are made after the
move takes place. Here is the implementation of the function call operator for
this echo operation:

[core_sample_echo_op_4]
[example_core_echo_op_4]

A complete, runnable version of this example may be found in the examples
directory.

[endsect]



[endsect]
14 changes: 7 additions & 7 deletions doc/3_5_detect_ssl.qbk
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@ Here is the declaration for a function to detect the SSL client handshake.
The input to the function is simply a buffer sequence, no stream. This
allows the detection algorithm to be used elsewhere.

[core_sample_detect_tls_1]
[example_core_detect_tls_1]

The implementation checks the buffer for the presence of the SSL
Handshake message octet sequence and returns an apporopriate value:

[core_sample_detect_tls_2]
[example_core_detect_tls_2]

Now we define a stream operation. We start with the simple,
synchronous version which takes the stream and buffer as input:

[core_sample_detect_tls_3]
[example_core_detect_tls_3]

The synchronous algorithm is the model for building the asynchronous
operation which has more boilerplate. First, we declare the asynchronous
initiation function:

[core_sample_detect_tls_4]
[example_core_detect_tls_4]

The implementation of the initiation function is straightforward
and contains mostly boilerplate. It is to construct the return
Expand All @@ -43,13 +43,13 @@ then create the composed operation and launch it. The actual
code for interacting with the stream is in the composed operation,
which is written as a separate class.

[core_sample_detect_tls_5]
[example_core_detect_tls_5]

Now we will declare our composed operation. There is a considerable
amount of necessary boilerplate to get this right, but the result
is worth the effort.

[core_sample_detect_tls_6]
[example_core_detect_tls_6]

The boilerplate is all done, and now we need to implemnt the function
call operator that turns this composed operation a completion handler
Expand All @@ -59,6 +59,6 @@ is a transformation of the synchronous version of `detect_ssl` above,
but with the inversion of flow that characterizes code written in the
callback style:

[core_sample_detect_tls_7]
[example_core_detect_tls_7]

[endsect]
9 changes: 7 additions & 2 deletions doc/5_06_serializer_buffers.qbk
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
]

[section:serializer_buffers Buffer-Oriented Serializing]
[block'''<?dbhtml stop-chunking?>''']

An instance of __serializer__ can be invoked directly, without using
the provided stream operations. This could be useful for implementing
Expand Down Expand Up @@ -53,14 +54,16 @@ C++14 example we print the header first, followed by the body:

[http_snippet_16]

[heading Example: Write To std::ostream]


[section Write To std::ostream]

The standard library provides the type `std::ostream` for performing high
level write operations on character streams. The variable `std::cout` is
based on this output stream. This example uses the buffer oriented interface
of __serializer__ to write an HTTP message to a `std::ostream`:

[http_sample_write_ostream]
[example_http_write_ostream]

[tip
Serializing to a `std::ostream` could be implemented using an alternate
Expand All @@ -69,6 +72,8 @@ of __serializer__ to write an HTTP message to a `std::ostream`:
left as an exercise for the reader.
]

[endsect]



[endsect]
11 changes: 9 additions & 2 deletions doc/5_07_parser_buffers.qbk
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
]

[section:parser_buffers Buffer-Oriented Parsing]
[block'''<?dbhtml stop-chunking?>''']

A subclass of __basic_parser__ can be invoked directly, without using
the provided stream operations. This could be useful for implementing
Expand Down Expand Up @@ -59,15 +60,17 @@ The parser provides two options which may be set before parsing begins:
]]
]

[heading Example: Read From std::istream]


[section Read From std::istream]

The standard library provides the type `std::istream` for performing high
level read operations on character streams. The variable `std::cin` is based
on this input stream. This example uses the buffer oriented interface of
__basic_parser__ to build a stream operation which parses an HTTP message
from a `std::istream`:

[http_sample_read_istream]
[example_http_read_istream]

[tip
Parsing from a `std::istream` could be implemented using an alternate
Expand All @@ -77,3 +80,7 @@ from a `std::istream`:
]

[endsect]



[endsect]
2 changes: 1 addition & 1 deletion doc/5_08_custom_parsers.qbk
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ Buffers provided by the parser are non-owning references; it is the
responsibility of the derived class to copy any information it needs before
returning from the callback.

[http_sample_custom_parser]
[example_http_custom_parser]

[endsect]
Loading

0 comments on commit 510092d

Please sign in to comment.