[libvirt] [PATCH] json: add stream parser
Dmitry Guryanov
dguryanov at parallels.com
Thu Mar 14 12:14:04 UTC 2013
On 130314 10:53:59, Daniel P. Berrange wrote:
> On Thu, Mar 14, 2013 at 12:55:15PM +0400, Dmitry Guryanov wrote:
> > Add function virJSONValueFromStream, which reads data from
> > a stream and passes it to json parser. When end of the object
> > is reached, it returns this object.
> > To avoid reading from the stream by single bytes it reads to
> > a buffer (in a structure virJSONStreamParserState), which should
> > be passed to a consequent call of this function. So if the end
> > of one object and the beginning of the next object have been
> > read by a single system call - virJSONValueFromStream handle
> > it correctly.
> >
> > example of usage:
> >
> > virJSONValuePtr v;
> > virJSONStreamParserState state;
> >
> > memset(&state, 0, sizeof(state));
> >
> > while (1) {
> > v = virJSONValueFromStream(mon->fd, &state);
> > if (v == (void *)-1)
> > /* error */
> > break;
> >
> > if (v == NULL)
> > /* file descriptor has been closed */
> > break;
> >
> > /* handle object 'v' */
> > }
> >
> > I need such function for the parallels driver. It caches info
> > about domains and needs some mechanism to update this cache.
> > There is a "prlsrvctl monitor" command which waits for events
> > forever and prints info about events to stdout in json format.
> > So parallels driver could start separate thread which will
> > read from prlsrvctl's stdout and update cache accordingly.
> >
> > There is the same task in qemu_monitor_json, but each json object
> > is printed in a separate line there. It's not possible in my case,
> > because some fields could have line endings.
>
> Even QEMU could output multi-line JSON if you set the 'pretty' flag,
> so this would be useful for QEMU driver too at some point.
>
> >
> > Signed-off-by: Dmitry Guryanov <dguryanov at parallels.com>
> > ---
> > src/util/virjson.c | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++
> > src/util/virjson.h | 8 ++++
> > 2 files changed, 118 insertions(+), 0 deletions(-)
>
> Since this is reasonably complex code can you also add
> some more test cases to the tests/jsontest.c file to
> cover this functionality.
>
Yes, I'll send test in a separate patch.
> Daniel
> --
> |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :|
> |: http://libvirt.org -o- http://virt-manager.org :|
> |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :|
> |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|
--
Dmitry Guryanov
More information about the libvir-list
mailing list