3
Example DIME client for simple image server. This DIME client
4
demonstrates the new gSOAP DIME streaming feature.
6
Copyright (C) 2000-2002 Robert A. van Engelen. All Rights Reserved.
8
Run from command line. The first optional argument is the image file
9
name. The second optional argument is the service endpoint URL. The
10
third optional argument is the file name to save the image file to.
15
#include <sys/stat.h> // use fstat() for streaming DIME
17
// streaming DIME callbacks
18
static void *dime_write_open(struct soap*, const char*, const char*, const char*);
19
static void dime_write_close(struct soap*, void*);
20
static int dime_write(struct soap*, void*, const char*, size_t);
22
static void *dime_read_open(struct soap*, void*, const char*, const char*, const char*);
23
static void dime_read_close(struct soap*, void*);
24
static size_t dime_read(struct soap*, void*, char*, size_t);
27
static int getImage(const char *name, const char *url, const char *outputfile) {
29
xsd__base64Binary image;
31
soap.user = (void*)outputfile;
32
soap.fdimewriteopen = dime_write_open;
33
soap.fdimewriteclose = dime_write_close;
34
soap.fdimewrite = dime_write;
35
soap.connect_timeout = 10;
36
int nRet = soap_call_ns__getImage(&soap, url, "", (char *)name, image);
37
if (nRet != SOAP_OK) {
38
soap_print_fault(&soap, stderr);
40
printf("got an image, I suppose\n");
46
static int putImage(const char *name, const char *url, const char *inputfile) {
47
FILE *fd = fopen(inputfile, "rb");
49
printf("failed to open %s\n", inputfile);
53
if (0 != fstat(fileno(fd), &sb) || sb.st_size <= 0) {
54
printf("cannot find the length of file %s\n", inputfile);
60
xsd__base64Binary *pimage = soap_new_xsd__base64Binary(&soap, -1);
61
soap.user = (void *)inputfile;
62
soap.fdimereadopen = dime_read_open;
63
soap.fdimereadclose = dime_read_close;
64
soap.fdimeread = dime_read;
65
pimage->__ptr = (unsigned char*)fd;
66
pimage->__size = sb.st_size; // must set size
67
pimage->type = "image/jpeg";
68
pimage->options = soap_dime_option(&soap, 0, "My sent picture");
69
soap.connect_timeout = 10;
72
int nRet = soap_call_ns__putImage(&soap, url, "", (char *)name, pimage, nStatus);
73
if (nRet != SOAP_OK) {
74
soap_print_fault(&soap, stderr);
76
printf("sent an image, I suppose");
82
int main(const int argc, const char *const *const argv) {
84
printf("usage: %s imagename outputfilename\n", argv[0]);
88
nRet = getImage(argv[1], argv[2], argv[3]);
90
nRet = putImage(argv[1], argv[2], argv[3]);
95
static void *dime_write_open(struct soap *soap, const char *id, const char *type, const char *options)
96
{ FILE *handle = NULL;
97
// we can return NULL without setting soap->error if we don't want to use the streaming callback for this DIME attachment
98
handle = fopen((char*)soap->user, "wb");
100
printf("Streaming image id=%s type=%s into file %s\n", id, type, (char*)soap->user);
102
{ soap->error = SOAP_EOF; // could not open file for writing
103
soap->errnum = errno; // get reason
105
return (void*)handle;
108
static void dime_write_close(struct soap *soap, void *handle)
109
{ fclose((FILE*)handle);
112
static int dime_write(struct soap *soap, void *handle, const char *buf, size_t len)
115
{ nwritten = fwrite(buf, 1, len, (FILE*)handle);
117
{ soap->errnum = errno; // get reason
126
//// reading routines for putImage:
127
/** save a file sent by the client to our server */
128
static void *dime_read_open(struct soap *soap, void *handle, const char *id, const char *type, const char *options) {
129
// we should return NULL without setting soap->error if we don't want to use the streaming callback for this DIME attachment. The handle contains the non-NULL __ptr field value which should have been set in the application.
130
// the value of the handle can be changed and will be passed on to the fdimeread and fdimereadclose callbacks. The value will not affect the __ptr field.
134
static void dime_read_close(struct soap *soap, void *handle) {
135
fclose((FILE*)handle);
137
static size_t dime_read(struct soap *soap, void *handle, char *buf, size_t len) {
138
return fread(buf, 1, len, (FILE*)handle);