cvs: presentations / wez-streams2003.xml /slides/wez-streams2003 filters-api.xml filters-architecture.xml filters-intro.xml filters-price.xml
filters-read.xml filters-write.xml implement-abstract.xml implement-close-ops.xml implement-flush-seek.xml implement-intro.xml implement-open.xml
implement-read-write.xml implement-summary.xml implement-whatsin.xml intro-accepting.xml intro-doing.xml intro-returning.xml intro-using.xml
intro-what.xml intro-why.xml outro.xml summary.xml wrappers-opener.xml wrappers-ops-struct.xml wrappers-ops.xml wrappers-registering.xml
wrappers-using.xml wrappers.xml
| From: | Wez Furlong | Date: | Tue, 20 May 2003 22:53:55 +0000 |
| Subject: | cvs: presentations / wez-streams2003.xml /slides/wez-streams2003 filters-api.xml filters-architecture.xml filters-intro.xml filters-price.xml filters-read.xml filters-write.xml implement-abstract.xml implement-close-ops.xml implement-flush-seek.xml implement-intro.xml implement-open.xml implement-read-write.xml implement-summary.xml implement-whatsin.xml intro-accepting.xml intro-doing.xml intro-returning.xml intro-using.xml intro-what.xml intro-why.xml outro.xml summary.xml wrappers-opener.xml wrappers-ops-struct.xml wrappers-ops.xml wrappers-registering.xml wrappers-using.xml wrappers.xml |
||
| Groups: | php.pres | ||
| Request: | Send a blank email to pres+get-729@lists.php.net to get a copy of this message | ||
wez Tue May 20 18:53:55 2003 EDT
Added files:
/presentations wez-streams2003.xml
/presentations/slides/wez-streams2003 filters-api.xml
filters-architecture.xml
filters-intro.xml
filters-price.xml
filters-read.xml
filters-write.xml
implement-abstract.xml
implement-close-ops.xml
implement-flush-seek.xml
implement-intro.xml
implement-open.xml
implement-read-write.xml
implement-summary.xml
implement-whatsin.xml
intro-accepting.xml
intro-doing.xml
intro-returning.xml
intro-using.xml
intro-what.xml intro-why.xml
outro.xml summary.xml
wrappers-opener.xml
wrappers-ops-struct.xml
wrappers-ops.xml
wrappers-registering.xml
wrappers-using.xml
wrappers.xml
Log:
Add streams talk from amsterdam conf
Index: presentations/wez-streams2003.xml +++ presentations/wez-streams2003.xml <presentation> <title>Streams and Filters</title> <topic>Streams</topic> <event>International PHP Conference, Spring Edition</event> <location>Amsterdam</location> <date>May 9th, 2003</date> <navmode>html</navmode> <speaker>Wez Furlong</speaker> <email>wez@php.net</email> <slide>slides/wez-streams2003/intro-what.xml</slide> <slide>slides/wez-streams2003/intro-why.xml</slide> <slide>slides/wez-streams2003/intro-doing.xml</slide> <slide>slides/wez-streams2003/intro-using.xml</slide> <slide>slides/wez-streams2003/intro-accepting.xml</slide> <slide>slides/wez-streams2003/intro-returning.xml</slide> <slide>slides/wez-streams2003/implement-intro.xml</slide> <slide>slides/wez-streams2003/implement-whatsin.xml</slide> <slide>slides/wez-streams2003/implement-abstract.xml</slide> <slide>slides/wez-streams2003/implement-read-write.xml</slide> <slide>slides/wez-streams2003/implement-flush-seek.xml</slide> <slide>slides/wez-streams2003/implement-close-ops.xml</slide> <slide>slides/wez-streams2003/implement-open.xml</slide> <slide>slides/wez-streams2003/implement-summary.xml</slide> <slide>slides/wez-streams2003/wrappers.xml</slide> <slide>slides/wez-streams2003/wrappers-ops.xml</slide> <slide>slides/wez-streams2003/wrappers-ops-struct.xml</slide> <slide>slides/wez-streams2003/wrappers-opener.xml</slide> <slide>slides/wez-streams2003/wrappers-registering.xml</slide> <slide>slides/wez-streams2003/wrappers-using.xml</slide> <slide>slides/wez-streams2003/filters-intro.xml</slide> <slide>slides/wez-streams2003/filters-price.xml</slide> <slide>slides/wez-streams2003/filters-architecture.xml</slide> <slide>slides/wez-streams2003/filters-read.xml</slide> <slide>slides/wez-streams2003/filters-write.xml</slide> <slide>slides/wez-streams2003/filters-api.xml</slide> <slide>slides/wez-streams2003/summary.xml</slide> <slide>slides/wez-streams2003/outro.xml</slide> </presentation> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/filters-api.xml +++ presentations/slides/wez-streams2003/filters-api.xml <slide> <title>Filter API</title> <blurb> Each filter "class" implements a single method - %filter()% - which is called whenever a brigade is ready to be processed (writing/push mode), or whenever the caller would like some buckets from its output brigade (reading/pull mode). </blurb> <blurb> Each instance of a filter has a pair of brigades to hold buckets either pending transformation or consumption by the next filter in the chain. </blurb> <example fontsize="1.4em" linenumbers="true">typedef struct _php_stream_filter_ops { php_stream_filter_status_t (*filter)( php_stream *stream, php_stream_filter *thisfilter, php_stream_bucket_brigade *buckets_in, php_stream_bucket_brigade *buckets_out, size_t *bytes_consumed, int flags TSRMLS_DC); void (*dtor)(php_stream_filter *thisfilter TSRMLS_DC); const char *label; } php_stream_filter_ops;</example> </slide> Index: presentations/slides/wez-streams2003/filters-architecture.xml +++ presentations/slides/wez-streams2003/filters-architecture.xml <slide> <title>Filter Architecture</title> <list> <bullet> Filters are OO (just like the streams and wrappers). </bullet> <bullet> We use a bucket-brigade (similar concept to Apache2 filters) </bullet> <bullet> Brigade is a doubly-linked list that contains a number of data buckets chained together. </bullet> <bullet> No need to maintain/realloc a monolithic buffer. </bullet> <bullet> meta-buckets can contain information about EOF conditions. </bullet> <bullet> Useful structure since the data and EOF events may occur on separate invocations of %fread()%. </bullet> <bullet> For filters that simply pass through data, buckets can be passed on to next filter without the overhead of %memcpy()%/%malloc()% - just re-link filter into the next brigade. </bullet> <bullet> Separate stacks for reading and writing. </bullet> </list> </slide> Index: presentations/slides/wez-streams2003/filters-intro.xml +++ presentations/slides/wez-streams2003/filters-intro.xml <slide> <title>Filtering</title> <blurb> Filters... </blurb> <list> <bullet>transparently transform data read from or written to a stream</bullet> <bullet>can be "stacked" into a pipeline</bullet> <bullet>filter stacks can have any number of filtration steps</bullet> <bullet>read and write operations can have separate filter stacks</bullet> </list> <blurb> PHP 5.x will feature a number of stock filters: </blurb> <list> <bullet>case folding</bullet> <bullet>codeset conversion</bullet> <bullet>Compression: zlib and bzip2</bullet> <bullet>HTTP 1.1 chunk encoder/decoder</bullet> <bullet>Crypto: mcrypt</bullet> </list> </slide> Index: presentations/slides/wez-streams2003/filters-price.xml +++ presentations/slides/wez-streams2003/filters-price.xml <slide> <title>the catch...</title> <blurb> Filters sound great... but everything comes with a price. </blurb> <list> <bullet>Filtered streams are essentially FIFOs</bullet> <bullet>%fseek()% is not possible</bullet> <bullet>PHP 5 only</bullet> </list> <blurb> One of the aims of the filter API is code re-use; bzip2 stream is really just a file stream with bzip2 (de)compression added on top. So why not code it that way? </blurb> <blurb> Consider writing a more complex custom stream if you really must be able to %fseek()%. </blurb> <blurb> There ~is~ a filter API in 4.3.x, but it is not compatible with the much better PHP 5 API, so I'm not telling anyone how to use it. </blurb> </slide> Index: presentations/slides/wez-streams2003/filters-read.xml +++ presentations/slides/wez-streams2003/filters-read.xml <slide> <title>Filter Read</title> <blurb> Reading is pull-based and works like this: </blurb> <list> <bullet> %php_stream_read()% or %fread()% requests ~n~ bytes to be placed into a buffer. </bullet> <bullet> The topmost filter on the read stack returns the data from its own buffered brigade, if any, splitting buckets if required. </bullet> <bullet> If it doesn't have enough data, it asks the next filter in the stack for some more data. </bullet> <bullet> This process repeats until the actual underlying stream is hit. </bullet> <bullet> The stream reads a chunk of data and returns it to the filter. </bullet> <bullet> The data moves along the brigades, perhaps being transformed along the way. </bullet> <bullet> Topmost filter gets the data and stores it in the buffer. </bullet> <bullet> Process repeats until the buffer is filled or there is no more data available from the stream. </bullet> </list> </slide> Index: presentations/slides/wez-streams2003/filters-write.xml +++ presentations/slides/wez-streams2003/filters-write.xml <slide> <title>Filter Write</title> <blurb> Writing is push-based and works like this: </blurb> <list> <bullet> %php_stream_write()% or %fwrite()% sends ~n~ bytes to be written to the stream. </bullet> <bullet> A bucket is created from the buffer. </bullet> <bullet> The bucket is pushed to the topmost filter on the write stack. </bullet> <bullet> We wind the handle on the filter; it may tranforms buckets, and pushes them onto the next filter. </bullet> <bullet> repeat the last step until we hit the the underlying stream. </bullet> <bullet> Each bucket is written to the underlying stream. </bullet> <bullet> Process repeats until one of the steps filters in the stack produces no output buckets. </bullet> </list> </slide> Index: presentations/slides/wez-streams2003/implement-abstract.xml +++ presentations/slides/wez-streams2003/implement-abstract.xml <slide> <title>Abstract</title> <blurb> Each instance of a stream has an ~abstract~ data pointer that can hold underlying resource handle(s) and other stateful data required by your stream. </blurb> <blurb> Lets see how to write a very simple file based stream, using POSIX IO. </blurb> <example fontsize="1.5em" linenumbers="true">struct my_stream_data { int filedes; }</example> <blurb> When we create our stream, we will store a pointer to the above structure in the ~abstract~ field of the stream structure. </blurb> <blurb> We can now move on and write the core operations for our stream... </blurb> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/implement-close-ops.xml +++ presentations/slides/wez-streams2003/implement-close-ops.xml <slide> <title>Close</title> <example fontsize="1.4em" linenumbers="0">int my_close(php_stream *stream, int close_handle TSRMLS_DC) { struct my_stream_data *data = stream->abstract; if (close_handle) { close(data->filedes); } efree(data); return 0; }</example> <blurb> Now, we can declare the operations structure. </blurb> <example fontsize="1.4em" linenumbers="0">php_stream_ops my_ops = { my_write, my_read, my_close, my_flush, "my stream", my_seek, NULL, NULL, NULL, NULL };</example> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/implement-flush-seek.xml +++ presentations/slides/wez-streams2003/implement-flush-seek.xml <slide> <title>Flush & Seek</title> <example fontsize="1.4em" linenumbers="true">int my_flush(php_stream *stream TSRMLS_DC) { struct my_stream_data *data = stream->abstract; return fsync(data->fildes); } int my_seek(php_stream *stream, off_t offset, int whence, off_t *newoffset TSRMLS_DC) { int seekret; struct my_stream_data *data = stream->abstract; seekret = lseek(data->filedes, offset, whence); if (seekret == (off_t)-1) { return -1; } *newoffset = seekret; return 0; }</example> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/implement-intro.xml +++ presentations/slides/wez-streams2003/implement-intro.xml <slide> <title>Implementing Streams</title> <blurb> You might want (or need) to implement your own new kind of Stream if: </blurb> <list> <bullet>PHP does not provide one for you.</bullet> <bullet>It is too complicated to re-use a stock implementation.</bullet> </list> <blurb> It is a good idea to re-use the PHP provided implementations for plain files and network streams because: </blurb> <list> <bullet>Handle platform specific quirks.</bullet> <bullet>Support advanced features (casting, memory mapping).</bullet> <bullet>Save you time and effort!</bullet> </list> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/implement-open.xml +++ presentations/slides/wez-streams2003/implement-open.xml <slide> <title>Open</title> <blurb> And finally, we can write a function to open the stream: </blurb> <example fontsize="1.4em" linenumbers="1">PHP_FUNCTION(example_open) { char *filename; long filename_len; php_stream *stream; struct my_stream_data *data; if (FAILURE == zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "s", &filename, &filename_len)) { return; } data = emalloc(sizeof(struct my_stream_data)); data->filedes = open(filename, O_RDWR); stream = php_stream_alloc(&my_ops, data, NULL, "r+"); php_stream_to_zval(stream, return_value); }</example> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/implement-read-write.xml +++ presentations/slides/wez-streams2003/implement-read-write.xml <slide> <title>Read & Write</title> <example fontsize="1.4em" linenumbers="true">size_t my_read(php_stream *stream, char *buf, size_t count TSRMLS_DC) { int readret; struct my_stream_data *data = stream->abstract; readret = read(data->filedes, buf, count); if (readret <= 0) { return 0; } return readret; } size_t my_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC) { int wrote; struct my_stream_data *data = stream->abstract; wrote = write(data->filedes, buf, count); if (wrote <= 0) { return 0; } return wrote; }</example> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/implement-summary.xml +++ presentations/slides/wez-streams2003/implement-summary.xml <slide> <title>Implemented</title> <blurb> Now we have implemented our very own file stream. The script that uses it might look like this: </blurb> <example fontsize="1.4em"><?php $fp = example_open("/path/to/file"); fwrite($fp, "hello there\n"); rewind($fp); echo fgets($fp); ?></example> <blurb> Outputs: </blurb> <example type="screen" fontsize="1.4em">hello there</example> <blurb> Key points: </blurb> <list> <bullet>Custom opener function.</bullet> <bullet>Returned stream works with %fread()%, %fwrite()% etc.</bullet> <bullet>Not covered ~cast~, ~fstat~ and ~option~ interface.</bullet> </list> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/implement-whatsin.xml +++ presentations/slides/wez-streams2003/implement-whatsin.xml <slide> <title>What's in a Stream?</title> <blurb> Streams are Object Orientated. </blurb> <list> <bullet>Each class of Stream has its own ~operations~ structure.</bullet> <bullet>It defines the methods that can be called for that class of stream</bullet> <bullet>Each instance of a given class share the same operations structure.</bullet> <bullet>Each instance has its own abstract data pointer to hold instance specific data (such as the underlying file/resource handles).</bullet> </list> <blurb> To implement your own stream you need to define an operations structure and the methods that apply to your stream. The core methods are: </blurb> <list> <bullet>%write()% - write data to the stream</bullet> <bullet>%read()% - read data from the stream</bullet> <bullet>%close()% - shutdown/release the stream</bullet> <bullet>%flush()% - ensure data is flushed to storage</bullet> <bullet>%seek()% - move to an offset within the stream</bullet> </list> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/intro-accepting.xml +++ presentations/slides/wez-streams2003/intro-accepting.xml <slide> <title>Accepting Streams</title> <blurb> Accepting Streams as parameters to %PHP_FUNCTION()%s is easy: </blurb> <example fontsize="1.5em" linenumbers="true">PHP_FUNCTION(example_write) { zval *zstream; php_stream *stream; if (FAILURE == zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream)) { return; } php_stream_from_zval(stream, &zstream); php_stream_write(stream, "hello\n"); }</example> <blurb>*Note:* You must ~*NOT*~ %php_stream_close()% a stream passed in from a script</blurb> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/intro-doing.xml +++ presentations/slides/wez-streams2003/intro-doing.xml <slide> <title>What's in store</title> <blurb>I will be covering the following topics today:</blurb> <list> <bullet>Brief example of API usage</bullet> <bullet>How to implement a stream</bullet> <bullet>How to implement a wrapper</bullet> <bullet>Filter API in PHP 5</bullet> </list> <blurb> And then it's time for your questions! </blurb> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/intro-returning.xml +++ presentations/slides/wez-streams2003/intro-returning.xml <slide> <title>Returning Streams</title> <blurb> Returning Streams from your own %PHP_FUNCTION()%s is easy too: </blurb> <example fontsize="1.5em" linenumbers="true">PHP_FUNCTION(example_open) { php_stream *stream; if (ZEND_NUM_ARGS() != 0) { WRONG_PARAM_COUNT; } stream = php_stream_open_wrapper( "http://www.php.net", "rb", REPORT_ERRORS, NULL); php_stream_to_zval(stream, return_value); }</example> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/intro-using.xml +++ presentations/slides/wez-streams2003/intro-using.xml <slide> <title>Using Streams</title> <blurb> Opening up an URL and reading data from it is simple using the Streams API: </blurb> <example fontsize="1.5em" linenumbers="true">php_stream *stream; stream = php_stream_open_wrapper("http://www.php.net", "rb", REPORT_ERRORS, NULL); if (stream == NULL) { return; } while (!php_stream_eof(stream)) { char buf[1024]; if (php_stream_gets(stream, buf, sizeof(buf)) { php_printf("%s", buf); } else { break; } } php_stream_close(stream);</example> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/intro-what.xml +++ presentations/slides/wez-streams2003/intro-what.xml <slide> <title>what are they?</title> <list> <bullet>Simplest case: File pointer from %fopen()%</bullet> <bullet>Represent any object or resource with ~streamable~ behaviour</bullet> <bullet>Sockets (network)</bullet> <bullet>Pipes/FIFOs (executed programs)</bullet> <bullet>Memory buffers</bullet> <bullet>Anything else that data can be written to and read from in chunks</bullet> </list> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/intro-why.xml +++ presentations/slides/wez-streams2003/intro-why.xml <slide> <title>Why?</title> <blurb> Before Streams, many functions in PHP were using a nightmare mess to handle sockets and regular files. It worked, but it sucked. </blurb> <blurb> Then we wanted to support SSL - something needed to change. </blurb> <blurb> Goals of the Streams API: </blurb> <list> <bullet>Clear and easy to use</bullet> <bullet>Extensible to support other types of streams</bullet> <bullet>Maintainable</bullet> </list> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/outro.xml +++ presentations/slides/wez-streams2003/outro.xml <slide> <title>That's it!</title> <blurb> You can find more resources on streams... </blurb> <list> <bullet>PHP Magazine issues 2 and 3; Streams API articles (~recommended!~)</bullet> <bullet>Streams API docs http://php.net/streams</bullet> <bullet>Ask on internals@lists.php.net </bullet> <bullet>These slides will be available on http://www.php.net/~wez/conf</bullet> </list> <blurb> Please do your homework ~before~ asking for help. </blurb> </slide> Index: presentations/slides/wez-streams2003/summary.xml +++ presentations/slides/wez-streams2003/summary.xml <slide> <title>Summing Up</title> <blurb> We've seen how to implement streams and wrappers and had a hint of the new filter API in PHP 5. </blurb> <blurb> You can apply this knowledge to... </blurb> <list> <bullet>Implement additional URL protocols (eg: LDAP)</bullet> <bullet>Virtual Filesystem Layers by hooking %opendir()% and %stat()% eg: transparent webdav access</bullet> <bullet>Write reusable filters</bullet> </list> <blurb> You can do most of the above in PHP script too - we have user-space mappings for many of these features. </blurb> </slide> Index: presentations/slides/wez-streams2003/wrappers-opener.xml +++ presentations/slides/wez-streams2003/wrappers-opener.xml <slide> <title>Stream Opener</title> <blurb> Essentially the same code as %example_open()% function we saw earlier. </blurb> <example fontsize="1.4em" linenumbers="1">php_stream *example_opener(php_stream_wrapper *wrapper, char *filename, char *mode, int options, char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC) { php_stream *stream; struct my_stream_data *data; filename += sizeof("example://") - 1; data = emalloc(sizeof(struct my_stream_data)); data->filedes = open(filename, O_RDWR); stream = php_stream_alloc(&my_ops, data, NULL, mode); if (opened_path) { *opened_path = estrdup(filename); } return stream; }</example> </slide> Index: presentations/slides/wez-streams2003/wrappers-ops-struct.xml +++ presentations/slides/wez-streams2003/wrappers-ops-struct.xml <slide> <title>your own wrapper</title> <list> <bullet>declare wrapper operations</bullet> <bullet>declare wrapper instance</bullet> <bullet>register (and unregister!) with the streams layer</bullet> </list> <blurb> Let's replace the %example_open()% function with a wrapper so that %fopen("example:///path/to/file")% does the same job. </blurb> <example fontsize="1.4em" linenumbers="true">php_stream_wrapper_ops example_wops = { example_opener, /* stream_opener */ NULL, /* stream_closer hook */ NULL, /* fstat hook */ NULL, /* stat hook */ NULL, /* diropen()er */, "example wrapper" }; php_stream_wrapper example_wrapper = { &example_wops, /* bind the methods to the instance */ NULL, /* no wrapper instance specific data */ 0, /* not a url that requires network access */ };</example> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/wrappers-ops.xml +++ presentations/slides/wez-streams2003/wrappers-ops.xml <slide> <title>Wrapper Operations</title> <blurb> Wrappers provide a ~factory~ method that is responsible for returning a stream when it the wrapper is invoked using: </blurb> <list> <bullet>%fopen()%/%include()%</bullet> <bullet>%opendir()%</bullet> <bullet>%stat()% (only fully implemented in PHP 5)</bullet>. </list> <blurb> Wrappers can to store a pointer to their own instance specific data in %stream->wrapperthis%. </blurb> <blurb> Make sure that you use the %stream_closer% hook to release any resources allocated by your wrapper when the stream is shutdown. </blurb> <blurb> You can also override the %fstat()% call for the stream (useful when the actual implementation !== the actual resource). </blurb> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/wrappers-registering.xml +++ presentations/slides/wez-streams2003/wrappers-registering.xml <slide> <title>hooks</title> <blurb> Need to make two more changes to your extension before it will work... </blurb> <list> <bullet>register with the streams layer (MINIT)</bullet> <bullet>unregister from the streams layer (MSHUTDOWN)</bullet> </list> <example fontsize="1.4em" linenumbers="true">PHP_MINIT_FUNCTION(example) { ... php_register_url_stream_wrapper("example", &example_wrapper TSRMLS_CC); ... } PHP_MSHUTDOWN_FUNCTION(example) { ... php_unregister_url_stream_wrapper("example" TSRMLS_CC); ... };</example> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/wrappers-using.xml +++ presentations/slides/wez-streams2003/wrappers-using.xml <slide> <title>Using the wrapper</title> <blurb> Remember the %example_open()% function and its usage: </blurb> <example fontsize="1.4em"><?php $fp = example_open("/path/to/file"); fwrite($fp, "hello there\n"); rewind($fp); echo fgets($fp); ?></example> <blurb> We can now use the example:// protocol scheme to access our custom streams via %fopen()%, %include()%, %readfile()% %copy()% etc. </blurb> <example fontsize="1.4em"><?php $fp = fopen("example:///path/to/file"); fwrite($fp, "hello there\n"); rewind($fp); echo fgets($fp); ?></example> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/wrappers.xml +++ presentations/slides/wez-streams2003/wrappers.xml <slide> <title>Wrappers</title> <blurb> Wrappers are... </blurb> <list> <bullet>objects that link streams into the filesystem layer of PHP.</bullet> <bullet>named after the fopen wrapper functions from earlier releases.</bullet> <bullet>often mistaken for streams and vice versa.</bullet> </list> <blurb> You need to implement a wrapper if you want to... </blurb> <list> <bullet> %fopen()% or %include()% custom streams or protocols. </bullet> <bullet> %opendir()% or %stat()% paths and files in your custom protocol. </bullet> </list> </slide>
Index: presentations/wez-streams2003.xml +++ presentations/wez-streams2003.xml <presentation> <title>Streams and Filters</title> <topic>Streams</topic> <event>International PHP Conference, Spring Edition</event> <location>Amsterdam</location> <date>May 9th, 2003</date> <navmode>html</navmode> <speaker>Wez Furlong</speaker> <email>wez@php.net</email> <slide>slides/wez-streams2003/intro-what.xml</slide> <slide>slides/wez-streams2003/intro-why.xml</slide> <slide>slides/wez-streams2003/intro-doing.xml</slide> <slide>slides/wez-streams2003/intro-using.xml</slide> <slide>slides/wez-streams2003/intro-accepting.xml</slide> <slide>slides/wez-streams2003/intro-returning.xml</slide> <slide>slides/wez-streams2003/implement-intro.xml</slide> <slide>slides/wez-streams2003/implement-whatsin.xml</slide> <slide>slides/wez-streams2003/implement-abstract.xml</slide> <slide>slides/wez-streams2003/implement-read-write.xml</slide> <slide>slides/wez-streams2003/implement-flush-seek.xml</slide> <slide>slides/wez-streams2003/implement-close-ops.xml</slide> <slide>slides/wez-streams2003/implement-open.xml</slide> <slide>slides/wez-streams2003/implement-summary.xml</slide> <slide>slides/wez-streams2003/wrappers.xml</slide> <slide>slides/wez-streams2003/wrappers-ops.xml</slide> <slide>slides/wez-streams2003/wrappers-ops-struct.xml</slide> <slide>slides/wez-streams2003/wrappers-opener.xml</slide> <slide>slides/wez-streams2003/wrappers-registering.xml</slide> <slide>slides/wez-streams2003/wrappers-using.xml</slide> <slide>slides/wez-streams2003/filters-intro.xml</slide> <slide>slides/wez-streams2003/filters-price.xml</slide> <slide>slides/wez-streams2003/filters-architecture.xml</slide> <slide>slides/wez-streams2003/filters-read.xml</slide> <slide>slides/wez-streams2003/filters-write.xml</slide> <slide>slides/wez-streams2003/filters-api.xml</slide> <slide>slides/wez-streams2003/summary.xml</slide> <slide>slides/wez-streams2003/outro.xml</slide> </presentation> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/filters-api.xml +++ presentations/slides/wez-streams2003/filters-api.xml <slide> <title>Filter API</title> <blurb> Each filter "class" implements a single method - %filter()% - which is called whenever a brigade is ready to be processed (writing/push mode), or whenever the caller would like some buckets from its output brigade (reading/pull mode). </blurb> <blurb> Each instance of a filter has a pair of brigades to hold buckets either pending transformation or consumption by the next filter in the chain. </blurb> <example fontsize="1.4em" linenumbers="true">typedef struct _php_stream_filter_ops { php_stream_filter_status_t (*filter)( php_stream *stream, php_stream_filter *thisfilter, php_stream_bucket_brigade *buckets_in, php_stream_bucket_brigade *buckets_out, size_t *bytes_consumed, int flags TSRMLS_DC); void (*dtor)(php_stream_filter *thisfilter TSRMLS_DC); const char *label; } php_stream_filter_ops;</example> </slide> Index: presentations/slides/wez-streams2003/filters-architecture.xml +++ presentations/slides/wez-streams2003/filters-architecture.xml <slide> <title>Filter Architecture</title> <list> <bullet> Filters are OO (just like the streams and wrappers). </bullet> <bullet> We use a bucket-brigade (similar concept to Apache2 filters) </bullet> <bullet> Brigade is a doubly-linked list that contains a number of data buckets chained together. </bullet> <bullet> No need to maintain/realloc a monolithic buffer. </bullet> <bullet> meta-buckets can contain information about EOF conditions. </bullet> <bullet> Useful structure since the data and EOF events may occur on separate invocations of %fread()%. </bullet> <bullet> For filters that simply pass through data, buckets can be passed on to next filter without the overhead of %memcpy()%/%malloc()% - just re-link filter into the next brigade. </bullet> <bullet> Separate stacks for reading and writing. </bullet> </list> </slide> Index: presentations/slides/wez-streams2003/filters-intro.xml +++ presentations/slides/wez-streams2003/filters-intro.xml <slide> <title>Filtering</title> <blurb> Filters... </blurb> <list> <bullet>transparently transform data read from or written to a stream</bullet> <bullet>can be "stacked" into a pipeline</bullet> <bullet>filter stacks can have any number of filtration steps</bullet> <bullet>read and write operations can have separate filter stacks</bullet> </list> <blurb> PHP 5.x will feature a number of stock filters: </blurb> <list> <bullet>case folding</bullet> <bullet>codeset conversion</bullet> <bullet>Compression: zlib and bzip2</bullet> <bullet>HTTP 1.1 chunk encoder/decoder</bullet> <bullet>Crypto: mcrypt</bullet> </list> </slide> Index: presentations/slides/wez-streams2003/filters-price.xml +++ presentations/slides/wez-streams2003/filters-price.xml <slide> <title>the catch...</title> <blurb> Filters sound great... but everything comes with a price. </blurb> <list> <bullet>Filtered streams are essentially FIFOs</bullet> <bullet>%fseek()% is not possible</bullet> <bullet>PHP 5 only</bullet> </list> <blurb> One of the aims of the filter API is code re-use; bzip2 stream is really just a file stream with bzip2 (de)compression added on top. So why not code it that way? </blurb> <blurb> Consider writing a more complex custom stream if you really must be able to %fseek()%. </blurb> <blurb> There ~is~ a filter API in 4.3.x, but it is not compatible with the much better PHP 5 API, so I'm not telling anyone how to use it. </blurb> </slide> Index: presentations/slides/wez-streams2003/filters-read.xml +++ presentations/slides/wez-streams2003/filters-read.xml <slide> <title>Filter Read</title> <blurb> Reading is pull-based and works like this: </blurb> <list> <bullet> %php_stream_read()% or %fread()% requests ~n~ bytes to be placed into a buffer. </bullet> <bullet> The topmost filter on the read stack returns the data from its own buffered brigade, if any, splitting buckets if required. </bullet> <bullet> If it doesn't have enough data, it asks the next filter in the stack for some more data. </bullet> <bullet> This process repeats until the actual underlying stream is hit. </bullet> <bullet> The stream reads a chunk of data and returns it to the filter. </bullet> <bullet> The data moves along the brigades, perhaps being transformed along the way. </bullet> <bullet> Topmost filter gets the data and stores it in the buffer. </bullet> <bullet> Process repeats until the buffer is filled or there is no more data available from the stream. </bullet> </list> </slide> Index: presentations/slides/wez-streams2003/filters-write.xml +++ presentations/slides/wez-streams2003/filters-write.xml <slide> <title>Filter Write</title> <blurb> Writing is push-based and works like this: </blurb> <list> <bullet> %php_stream_write()% or %fwrite()% sends ~n~ bytes to be written to the stream. </bullet> <bullet> A bucket is created from the buffer. </bullet> <bullet> The bucket is pushed to the topmost filter on the write stack. </bullet> <bullet> We wind the handle on the filter; it may tranforms buckets, and pushes them onto the next filter. </bullet> <bullet> repeat the last step until we hit the the underlying stream. </bullet> <bullet> Each bucket is written to the underlying stream. </bullet> <bullet> Process repeats until one of the steps filters in the stack produces no output buckets. </bullet> </list> </slide> Index: presentations/slides/wez-streams2003/implement-abstract.xml +++ presentations/slides/wez-streams2003/implement-abstract.xml <slide> <title>Abstract</title> <blurb> Each instance of a stream has an ~abstract~ data pointer that can hold underlying resource handle(s) and other stateful data required by your stream. </blurb> <blurb> Lets see how to write a very simple file based stream, using POSIX IO. </blurb> <example fontsize="1.5em" linenumbers="true">struct my_stream_data { int filedes; }</example> <blurb> When we create our stream, we will store a pointer to the above structure in the ~abstract~ field of the stream structure. </blurb> <blurb> We can now move on and write the core operations for our stream... </blurb> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/implement-close-ops.xml +++ presentations/slides/wez-streams2003/implement-close-ops.xml <slide> <title>Close</title> <example fontsize="1.4em" linenumbers="0">int my_close(php_stream *stream, int close_handle TSRMLS_DC) { struct my_stream_data *data = stream->abstract; if (close_handle) { close(data->filedes); } efree(data); return 0; }</example> <blurb> Now, we can declare the operations structure. </blurb> <example fontsize="1.4em" linenumbers="0">php_stream_ops my_ops = { my_write, my_read, my_close, my_flush, "my stream", my_seek, NULL, NULL, NULL, NULL };</example> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/implement-flush-seek.xml +++ presentations/slides/wez-streams2003/implement-flush-seek.xml <slide> <title>Flush & Seek</title> <example fontsize="1.4em" linenumbers="true">int my_flush(php_stream *stream TSRMLS_DC) { struct my_stream_data *data = stream->abstract; return fsync(data->fildes); } int my_seek(php_stream *stream, off_t offset, int whence, off_t *newoffset TSRMLS_DC) { int seekret; struct my_stream_data *data = stream->abstract; seekret = lseek(data->filedes, offset, whence); if (seekret == (off_t)-1) { return -1; } *newoffset = seekret; return 0; }</example> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/implement-intro.xml +++ presentations/slides/wez-streams2003/implement-intro.xml <slide> <title>Implementing Streams</title> <blurb> You might want (or need) to implement your own new kind of Stream if: </blurb> <list> <bullet>PHP does not provide one for you.</bullet> <bullet>It is too complicated to re-use a stock implementation.</bullet> </list> <blurb> It is a good idea to re-use the PHP provided implementations for plain files and network streams because: </blurb> <list> <bullet>Handle platform specific quirks.</bullet> <bullet>Support advanced features (casting, memory mapping).</bullet> <bullet>Save you time and effort!</bullet> </list> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/implement-open.xml +++ presentations/slides/wez-streams2003/implement-open.xml <slide> <title>Open</title> <blurb> And finally, we can write a function to open the stream: </blurb> <example fontsize="1.4em" linenumbers="1">PHP_FUNCTION(example_open) { char *filename; long filename_len; php_stream *stream; struct my_stream_data *data; if (FAILURE == zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "s", &filename, &filename_len)) { return; } data = emalloc(sizeof(struct my_stream_data)); data->filedes = open(filename, O_RDWR); stream = php_stream_alloc(&my_ops, data, NULL, "r+"); php_stream_to_zval(stream, return_value); }</example> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/implement-read-write.xml +++ presentations/slides/wez-streams2003/implement-read-write.xml <slide> <title>Read & Write</title> <example fontsize="1.4em" linenumbers="true">size_t my_read(php_stream *stream, char *buf, size_t count TSRMLS_DC) { int readret; struct my_stream_data *data = stream->abstract; readret = read(data->filedes, buf, count); if (readret <= 0) { return 0; } return readret; } size_t my_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC) { int wrote; struct my_stream_data *data = stream->abstract; wrote = write(data->filedes, buf, count); if (wrote <= 0) { return 0; } return wrote; }</example> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/implement-summary.xml +++ presentations/slides/wez-streams2003/implement-summary.xml <slide> <title>Implemented</title> <blurb> Now we have implemented our very own file stream. The script that uses it might look like this: </blurb> <example fontsize="1.4em"><?php $fp = example_open("/path/to/file"); fwrite($fp, "hello there\n"); rewind($fp); echo fgets($fp); ?></example> <blurb> Outputs: </blurb> <example type="screen" fontsize="1.4em">hello there</example> <blurb> Key points: </blurb> <list> <bullet>Custom opener function.</bullet> <bullet>Returned stream works with %fread()%, %fwrite()% etc.</bullet> <bullet>Not covered ~cast~, ~fstat~ and ~option~ interface.</bullet> </list> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/implement-whatsin.xml +++ presentations/slides/wez-streams2003/implement-whatsin.xml <slide> <title>What's in a Stream?</title> <blurb> Streams are Object Orientated. </blurb> <list> <bullet>Each class of Stream has its own ~operations~ structure.</bullet> <bullet>It defines the methods that can be called for that class of stream</bullet> <bullet>Each instance of a given class share the same operations structure.</bullet> <bullet>Each instance has its own abstract data pointer to hold instance specific data (such as the underlying file/resource handles).</bullet> </list> <blurb> To implement your own stream you need to define an operations structure and the methods that apply to your stream. The core methods are: </blurb> <list> <bullet>%write()% - write data to the stream</bullet> <bullet>%read()% - read data from the stream</bullet> <bullet>%close()% - shutdown/release the stream</bullet> <bullet>%flush()% - ensure data is flushed to storage</bullet> <bullet>%seek()% - move to an offset within the stream</bullet> </list> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/intro-accepting.xml +++ presentations/slides/wez-streams2003/intro-accepting.xml <slide> <title>Accepting Streams</title> <blurb> Accepting Streams as parameters to %PHP_FUNCTION()%s is easy: </blurb> <example fontsize="1.5em" linenumbers="true">PHP_FUNCTION(example_write) { zval *zstream; php_stream *stream; if (FAILURE == zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream)) { return; } php_stream_from_zval(stream, &zstream); php_stream_write(stream, "hello\n"); }</example> <blurb>*Note:* You must ~*NOT*~ %php_stream_close()% a stream passed in from a script</blurb> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/intro-doing.xml +++ presentations/slides/wez-streams2003/intro-doing.xml <slide> <title>What's in store</title> <blurb>I will be covering the following topics today:</blurb> <list> <bullet>Brief example of API usage</bullet> <bullet>How to implement a stream</bullet> <bullet>How to implement a wrapper</bullet> <bullet>Filter API in PHP 5</bullet> </list> <blurb> And then it's time for your questions! </blurb> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/intro-returning.xml +++ presentations/slides/wez-streams2003/intro-returning.xml <slide> <title>Returning Streams</title> <blurb> Returning Streams from your own %PHP_FUNCTION()%s is easy too: </blurb> <example fontsize="1.5em" linenumbers="true">PHP_FUNCTION(example_open) { php_stream *stream; if (ZEND_NUM_ARGS() != 0) { WRONG_PARAM_COUNT; } stream = php_stream_open_wrapper( "http://www.php.net", "rb", REPORT_ERRORS, NULL); php_stream_to_zval(stream, return_value); }</example> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/intro-using.xml +++ presentations/slides/wez-streams2003/intro-using.xml <slide> <title>Using Streams</title> <blurb> Opening up an URL and reading data from it is simple using the Streams API: </blurb> <example fontsize="1.5em" linenumbers="true">php_stream *stream; stream = php_stream_open_wrapper("http://www.php.net", "rb", REPORT_ERRORS, NULL); if (stream == NULL) { return; } while (!php_stream_eof(stream)) { char buf[1024]; if (php_stream_gets(stream, buf, sizeof(buf)) { php_printf("%s", buf); } else { break; } } php_stream_close(stream);</example> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/intro-what.xml +++ presentations/slides/wez-streams2003/intro-what.xml <slide> <title>what are they?</title> <list> <bullet>Simplest case: File pointer from %fopen()%</bullet> <bullet>Represent any object or resource with ~streamable~ behaviour</bullet> <bullet>Sockets (network)</bullet> <bullet>Pipes/FIFOs (executed programs)</bullet> <bullet>Memory buffers</bullet> <bullet>Anything else that data can be written to and read from in chunks</bullet> </list> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/intro-why.xml +++ presentations/slides/wez-streams2003/intro-why.xml <slide> <title>Why?</title> <blurb> Before Streams, many functions in PHP were using a nightmare mess to handle sockets and regular files. It worked, but it sucked. </blurb> <blurb> Then we wanted to support SSL - something needed to change. </blurb> <blurb> Goals of the Streams API: </blurb> <list> <bullet>Clear and easy to use</bullet> <bullet>Extensible to support other types of streams</bullet> <bullet>Maintainable</bullet> </list> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/outro.xml +++ presentations/slides/wez-streams2003/outro.xml <slide> <title>That's it!</title> <blurb> You can find more resources on streams... </blurb> <list> <bullet>PHP Magazine issues 2 and 3; Streams API articles (~recommended!~)</bullet> <bullet>Streams API docs http://php.net/streams</bullet> <bullet>Ask on internals@lists.php.net </bullet> <bullet>These slides will be available on http://www.php.net/~wez/conf</bullet> </list> <blurb> Please do your homework ~before~ asking for help. </blurb> </slide> Index: presentations/slides/wez-streams2003/summary.xml +++ presentations/slides/wez-streams2003/summary.xml <slide> <title>Summing Up</title> <blurb> We've seen how to implement streams and wrappers and had a hint of the new filter API in PHP 5. </blurb> <blurb> You can apply this knowledge to... </blurb> <list> <bullet>Implement additional URL protocols (eg: LDAP)</bullet> <bullet>Virtual Filesystem Layers by hooking %opendir()% and %stat()% eg: transparent webdav access</bullet> <bullet>Write reusable filters</bullet> </list> <blurb> You can do most of the above in PHP script too - we have user-space mappings for many of these features. </blurb> </slide> Index: presentations/slides/wez-streams2003/wrappers-opener.xml +++ presentations/slides/wez-streams2003/wrappers-opener.xml <slide> <title>Stream Opener</title> <blurb> Essentially the same code as %example_open()% function we saw earlier. </blurb> <example fontsize="1.4em" linenumbers="1">php_stream *example_opener(php_stream_wrapper *wrapper, char *filename, char *mode, int options, char **opened_path, php_stream_context *context STREAMS_DC TSRMLS_DC) { php_stream *stream; struct my_stream_data *data; filename += sizeof("example://") - 1; data = emalloc(sizeof(struct my_stream_data)); data->filedes = open(filename, O_RDWR); stream = php_stream_alloc(&my_ops, data, NULL, mode); if (opened_path) { *opened_path = estrdup(filename); } return stream; }</example> </slide> Index: presentations/slides/wez-streams2003/wrappers-ops-struct.xml +++ presentations/slides/wez-streams2003/wrappers-ops-struct.xml <slide> <title>your own wrapper</title> <list> <bullet>declare wrapper operations</bullet> <bullet>declare wrapper instance</bullet> <bullet>register (and unregister!) with the streams layer</bullet> </list> <blurb> Let's replace the %example_open()% function with a wrapper so that %fopen("example:///path/to/file")% does the same job. </blurb> <example fontsize="1.4em" linenumbers="true">php_stream_wrapper_ops example_wops = { example_opener, /* stream_opener */ NULL, /* stream_closer hook */ NULL, /* fstat hook */ NULL, /* stat hook */ NULL, /* diropen()er */, "example wrapper" }; php_stream_wrapper example_wrapper = { &example_wops, /* bind the methods to the instance */ NULL, /* no wrapper instance specific data */ 0, /* not a url that requires network access */ };</example> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/wrappers-ops.xml +++ presentations/slides/wez-streams2003/wrappers-ops.xml <slide> <title>Wrapper Operations</title> <blurb> Wrappers provide a ~factory~ method that is responsible for returning a stream when it the wrapper is invoked using: </blurb> <list> <bullet>%fopen()%/%include()%</bullet> <bullet>%opendir()%</bullet> <bullet>%stat()% (only fully implemented in PHP 5)</bullet>. </list> <blurb> Wrappers can to store a pointer to their own instance specific data in %stream->wrapperthis%. </blurb> <blurb> Make sure that you use the %stream_closer% hook to release any resources allocated by your wrapper when the stream is shutdown. </blurb> <blurb> You can also override the %fstat()% call for the stream (useful when the actual implementation !== the actual resource). </blurb> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/wrappers-registering.xml +++ presentations/slides/wez-streams2003/wrappers-registering.xml <slide> <title>hooks</title> <blurb> Need to make two more changes to your extension before it will work... </blurb> <list> <bullet>register with the streams layer (MINIT)</bullet> <bullet>unregister from the streams layer (MSHUTDOWN)</bullet> </list> <example fontsize="1.4em" linenumbers="true">PHP_MINIT_FUNCTION(example) { ... php_register_url_stream_wrapper("example", &example_wrapper TSRMLS_CC); ... } PHP_MSHUTDOWN_FUNCTION(example) { ... php_unregister_url_stream_wrapper("example" TSRMLS_CC); ... };</example> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/wrappers-using.xml +++ presentations/slides/wez-streams2003/wrappers-using.xml <slide> <title>Using the wrapper</title> <blurb> Remember the %example_open()% function and its usage: </blurb> <example fontsize="1.4em"><?php $fp = example_open("/path/to/file"); fwrite($fp, "hello there\n"); rewind($fp); echo fgets($fp); ?></example> <blurb> We can now use the example:// protocol scheme to access our custom streams via %fopen()%, %include()%, %readfile()% %copy()% etc. </blurb> <example fontsize="1.4em"><?php $fp = fopen("example:///path/to/file"); fwrite($fp, "hello there\n"); rewind($fp); echo fgets($fp); ?></example> </slide> <!-- vim:sw=2 ts=2 --> Index: presentations/slides/wez-streams2003/wrappers.xml +++ presentations/slides/wez-streams2003/wrappers.xml <slide> <title>Wrappers</title> <blurb> Wrappers are... </blurb> <list> <bullet>objects that link streams into the filesystem layer of PHP.</bullet> <bullet>named after the fopen wrapper functions from earlier releases.</bullet> <bullet>often mistaken for streams and vice versa.</bullet> </list> <blurb> You need to implement a wrapper if you want to... </blurb> <list> <bullet> %fopen()% or %include()% custom streams or protocols. </bullet> <bullet> %opendir()% or %stat()% paths and files in your custom protocol. </bullet> </list> </slide>