~vcs-imports/simias/trunk

« back to all changes in this revision

Viewing changes to simias/tools/gsoap/gsoap-linux-2.7/mod_gsoap/gsoap_win/isapi/samples/dime/dimeclient.cpp

  • Committer: kalidasbala
  • Date: 2007-08-25 12:48:51 UTC
  • Revision ID: vcs-imports@canonical.com-20070825124851-vlfvzun3732ld196
Latest gsoap code update

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*      dimeclient.cpp
 
2
 
 
3
        Example DIME client for simple image server. This DIME client
 
4
        demonstrates the new gSOAP DIME streaming feature.
 
5
 
 
6
        Copyright (C) 2000-2002 Robert A. van Engelen. All Rights Reserved.
 
7
 
 
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.
 
11
*/
 
12
 
 
13
#include "soapH.h"
 
14
#include "dime.nsmap"
 
15
#include <sys/stat.h>   // use fstat() for streaming DIME
 
16
 
 
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);
 
21
 
 
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);
 
25
 
 
26
 
 
27
static int getImage(const char *name, const char *url, const char *outputfile) {
 
28
        struct soap soap;
 
29
        xsd__base64Binary image;
 
30
        soap_init(&soap);
 
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);
 
39
        } else {
 
40
                printf("got an image, I suppose\n");
 
41
        }
 
42
        soap_destroy(&soap);
 
43
        soap_end(&soap);
 
44
        return nRet;
 
45
}
 
46
static int putImage(const char *name, const char *url, const char *inputfile) {
 
47
        FILE *fd = fopen(inputfile, "rb");
 
48
        if (NULL == fd) {
 
49
                printf("failed to open %s\n", inputfile);
 
50
                return 3;
 
51
        }
 
52
        struct stat sb;
 
53
        if (0 != fstat(fileno(fd), &sb) || sb.st_size <= 0) {
 
54
                printf("cannot find the length of file %s\n", inputfile);
 
55
                return 4;
 
56
 
 
57
        }
 
58
        struct soap soap;
 
59
        soap_init(&soap);
 
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;
 
70
 
 
71
        int nStatus = 0;
 
72
        int nRet = soap_call_ns__putImage(&soap, url, "", (char *)name, pimage, nStatus);
 
73
        if (nRet != SOAP_OK) {
 
74
                soap_print_fault(&soap, stderr);
 
75
        } else {
 
76
                printf("sent an image, I suppose");
 
77
        }
 
78
        soap_destroy(&soap);
 
79
        soap_end(&soap);
 
80
        return nRet;
 
81
}
 
82
int main(const int argc, const char *const *const argv) { 
 
83
        if (4 != argc) {
 
84
                printf("usage: %s imagename outputfilename\n", argv[0]);
 
85
                return 1;
 
86
        }
 
87
        int nRet = 0;
 
88
        nRet = getImage(argv[1], argv[2], argv[3]);
 
89
        if (0 == nRet) {
 
90
                nRet = putImage(argv[1], argv[2], argv[3]);
 
91
        }
 
92
        return nRet;
 
93
}
 
94
 
 
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");
 
99
  if (handle)
 
100
    printf("Streaming image id=%s type=%s into file %s\n", id, type, (char*)soap->user);
 
101
  else
 
102
  { soap->error = SOAP_EOF; // could not open file for writing
 
103
    soap->errnum = errno; // get reason
 
104
  }
 
105
  return (void*)handle;
 
106
}
 
107
 
 
108
static void dime_write_close(struct soap *soap, void *handle)
 
109
{ fclose((FILE*)handle);
 
110
}
 
111
 
 
112
static int dime_write(struct soap *soap, void *handle, const char *buf, size_t len)
 
113
{ size_t nwritten;
 
114
  while (len)
 
115
  { nwritten = fwrite(buf, 1, len, (FILE*)handle);
 
116
    if (!nwritten)
 
117
    { soap->errnum = errno; // get reason
 
118
      return SOAP_EOF;
 
119
    }
 
120
    len -= nwritten;
 
121
    buf += nwritten;
 
122
  }
 
123
  return SOAP_OK;
 
124
}
 
125
 
 
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.
 
131
  return handle;
 
132
}
 
133
 
 
134
static void dime_read_close(struct soap *soap, void *handle) {
 
135
        fclose((FILE*)handle);
 
136
}
 
137
static size_t dime_read(struct soap *soap, void *handle, char *buf, size_t len) {
 
138
        return fread(buf, 1, len, (FILE*)handle);
 
139
}
 
140