17.01.2015 Views

Erlang and OTP in Action.pdf - Synrc

Erlang and OTP in Action.pdf - Synrc

Erlang and OTP in Action.pdf - Synrc

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

310<br />

NULL, /* F_PTR <strong>in</strong>it, N/A */<br />

drv_start, /* L_PTR start, called when port is opened */<br />

drv_stop, /* F_PTR stop, called when port is closed */<br />

NULL, /* F_PTR output, called when erlang has sent */<br />

NULL,<br />

/* F_PTR ready_<strong>in</strong>put, called when <strong>in</strong>put descriptor ready<br />

*/<br />

NULL,<br />

/* F_PTR ready_output, called when output descriptor<br />

ready */<br />

"lzjb_compress", /* char *driver_name, the argument to open_port */<br />

NULL, /* F_PTR f<strong>in</strong>ish, called when unloaded */<br />

NULL, /* F_PTR h<strong>and</strong>le */<br />

NULL, /* F_PTR control, port_comm<strong>and</strong> callback */<br />

NULL, /* F_PTR timeout, reserved */<br />

drv_outputv, /* F_PTR outputv, reserved */<br />

NULL, /* ready_async */<br />

NULL, /* flush */<br />

NULL, /* call */<br />

NULL, /* event */<br />

ERL_DRV_EXTENDED_MARKER, /* extended marker */ /* enable the<br />

rest */<br />

ERL_DRV_EXTENDED_MAJOR_VERSION, /* major version */<br />

ERL_DRV_EXTENDED_MINOR_VERSION, /* m<strong>in</strong>or version */<br />

ERL_DRV_FLAG_USE_PORT_LOCKING, /* driver_flags */ /* better<br />

lock<strong>in</strong>g */<br />

NULL, /* h<strong>and</strong>le2 */ /* set by the VM<br />

*/<br />

NULL /* process_exit */<br />

};<br />

This is almost the same struct that we declared <strong>in</strong> our earlier example. There are two ma<strong>in</strong><br />

differences. The first is that we changed a couple of names to be a little more clear <strong>and</strong><br />

consistent. The second is that we are go<strong>in</strong>g to use the outputv callback <strong>in</strong>stead of the<br />

process callback. This allows us to access the data we need to parse directly <strong>and</strong> <strong>in</strong> an<br />

asynchrounous fasion. Other than that th<strong>in</strong>gs are pretty similar.<br />

The last th<strong>in</strong>g we do <strong>in</strong> sett<strong>in</strong>g up our driver is just declare the entry po<strong>in</strong>t to <strong>Erlang</strong> <strong>and</strong><br />

return this struct to the VM.<br />

DRIVER_INIT(lzjb_compress)<br />

{<br />

return &driver_entry;<br />

}<br />

As you can see this is exactly the same as the function we used <strong>in</strong> our last example. Of<br />

course, the name that we pass <strong>in</strong>to the DRIVER_INIT macro changes but that’s it. You should<br />

remember that this name must match the name specified <strong>in</strong> the name section of the<br />

description struct.<br />

©Mann<strong>in</strong>g Publications Co. Please post comments or corrections to the Author Onl<strong>in</strong>e forum:<br />

http://www.mann<strong>in</strong>g-s<strong>and</strong>box.com/forum.jspaforumID=454

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!