~ubuntu-branches/ubuntu/wily/afnix/wily

« back to all changes in this revision

Viewing changes to src/srv/wax/shl/JsonMime.cpp

  • Committer: Package Import Robot
  • Author(s): Nobuhiro Iwamatsu
  • Date: 2015-07-11 02:00:35 UTC
  • mfrom: (10.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20150711020035-2nhpztq7s15qyc0v
Tags: 2.5.1-1
* New upstream release. (Closes: #789968)
* Update debian/control.
  - Update Standards-Version to 3.9.6.
* Add support mips64(el) and ppc64el. (Closes: #741508, #748146)
* Add patches/support-gcc-5.x.patch. (Closes: #777767)
  - Fix build with gcc-5.x.
* Add patches/Disable-NET0001.als.patch.
  - Disable test of NET0001.als.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// ---------------------------------------------------------------------------
 
2
// - JsonMime.cpp                                                            -
 
3
// - afnix:wax service - json mime class implementation                      -
 
4
// ---------------------------------------------------------------------------
 
5
// - This program is free software;  you can redistribute it  and/or  modify -
 
6
// - it provided that this copyright notice is kept intact.                  -
 
7
// -                                                                         -
 
8
// - This program  is  distributed in  the hope  that it will be useful, but -
 
9
// - without  any  warranty;  without  even   the   implied    warranty   of -
 
10
// - merchantability or fitness for a particular purpose.  In no event shall -
 
11
// - the copyright holder be liable for any  direct, indirect, incidental or -
 
12
// - special damages arising in any way out of the use of this software.     -
 
13
// ---------------------------------------------------------------------------
 
14
// - copyright (c) 1999-2015 amaury darsch                                   -
 
15
// ---------------------------------------------------------------------------
 
16
 
 
17
#include "Vector.hpp"
 
18
#include "Utility.hpp"
 
19
#include "Rsamples.hpp"
 
20
#include "JsonMime.hpp"
 
21
#include "Runnable.hpp"
 
22
#include "QuarkZone.hpp"
 
23
#include "Exception.hpp"
 
24
#include "OutputFile.hpp"
 
25
 
 
26
namespace afnix {
 
27
 
 
28
  // -------------------------------------------------------------------------
 
29
  // - private section                                                       -
 
30
  // -------------------------------------------------------------------------
 
31
 
 
32
  // the json content type
 
33
  static const String JSWR_MIME_TYPE = "application/json"; 
 
34
 
 
35
  // the rsamples data array constants
 
36
  static const String JSWR_RSPL_MODE = "\"mode\"";
 
37
  static const String JSWR_RSPL_HIDX = "\"hidx\"";
 
38
  static const String JSWR_RSPL_TIME = "\"time\"";
 
39
  static const String JSWR_RSPL_DATA = "\"data\"";
 
40
  static const String JSWR_RSPL_NAME = "\"RSAMPLES\"";
 
41
 
 
42
  // this procedure writes a rsamples object to a buffer
 
43
  static void json_write (const Rsamples& data, Buffer& buf) {
 
44
    // collect name
 
45
    String name = data.getname ();
 
46
    // collect data elements
 
47
    long rows = data.getrows ();
 
48
    long cols = data.getcols ();
 
49
    long rmax = rows - 1L;
 
50
    long cmax = cols - 1L;
 
51
    bool sflg = data.getsflg ();
 
52
    long psiz = data.getpsiz ();
 
53
    // the line to write
 
54
    String line;
 
55
    // write the name first if any
 
56
    line = "{"; line+= eolc;
 
57
    if (name.isnil () == false) {
 
58
      line = name; line+= " = {"; line+= eolc;
 
59
    }
 
60
    buf.add (line);
 
61
    // data mode
 
62
    line = "  "; line+= JSWR_RSPL_MODE; line += " : "; line+= JSWR_RSPL_NAME;
 
63
    line+= ',';  line+= eolc; 
 
64
    buf.add (line);
 
65
    // data index
 
66
    line = "  "; line+= JSWR_RSPL_HIDX; line += " : "; line+= "null";
 
67
    line+= ',';  line+= eolc; 
 
68
    buf.add (line);
 
69
    // time data
 
70
    line = "  "; line+= JSWR_RSPL_TIME; line+= " : ";
 
71
    buf.add (line);
 
72
    if (data.stamped () == true) {
 
73
      line = "["; line += eolc;
 
74
      buf.add (line);
 
75
      for (long k = 0; k < rows; k++) {
 
76
        t_real t = data.gettime (k);
 
77
        String s = Utility::tostring (t, psiz, sflg);
 
78
        line = "    "; line+= s;
 
79
        if (k < rmax) line+= ",";
 
80
        line+= eolc;
 
81
        buf.add (line);
 
82
      }
 
83
      line = "  ],"; line+= eolc;
 
84
      buf.add (line);
 
85
    } else {
 
86
      line = "null,"; line+= eolc;
 
87
      buf.add (line);
 
88
    }
 
89
    // data array
 
90
    line = "  \"data\" : ["; line+= eolc;
 
91
    buf.add (line);
 
92
    for (long j = 0; j < cols; j++) {
 
93
      line = "    ["; line+= eolc;
 
94
      buf.add (line);
 
95
      for (long k = 0; k < rows; k++) {
 
96
        t_real v = data.get (k, j);
 
97
        String s = Utility::tostring (v, psiz, sflg);
 
98
        line = "      "; line += s;
 
99
        if (k < rmax) line+= ",";
 
100
        line+= eolc;
 
101
        buf.add (line);
 
102
      }
 
103
      if (j < cmax) 
 
104
        line = "    ],";
 
105
      else
 
106
        line = "    ]";
 
107
      line += eolc;
 
108
      buf.add (line);
 
109
    }
 
110
    line = "  ]"; line+= eolc;
 
111
    buf.add (line);
 
112
    // close the name
 
113
    line = "}"; line+= eolc;
 
114
    buf.add (line);
 
115
  }
 
116
 
 
117
  // this procedure writes a rsamples object to an output stream
 
118
  static void json_write (const Rsamples& data, OutputStream& os) {
 
119
    // collect name and info
 
120
    String name = data.getname ();
 
121
    String info = data.getinfo ();
 
122
    // check for valid name and stram
 
123
    if (name.isnil () == true) {
 
124
      throw Exception ("json-error", "invalid nil name for mime writer");
 
125
    }
 
126
    // collect data elements
 
127
    long rows = data.getrows ();
 
128
    long cols = data.getcols ();
 
129
    long rmax = rows - 1L;
 
130
    long cmax = cols - 1L;
 
131
    bool sflg = data.getsflg ();
 
132
    long psiz = data.getpsiz ();
 
133
    // the line to write
 
134
    String line;
 
135
    // write the name first if any
 
136
    line = "{";
 
137
    if (name.isnil () == false) {
 
138
      line = name; line+= " = {";
 
139
    }
 
140
    os.writeln (line);
 
141
    // data mode
 
142
    line = "  "; line +=JSWR_RSPL_MODE; line += " : "; line+= JSWR_RSPL_NAME;
 
143
    line+= ',';
 
144
    os.writeln (line);
 
145
    // data index
 
146
    line = "  "; line +=JSWR_RSPL_HIDX; line += " : "; line+= "null";
 
147
    line+= ',';
 
148
    os.writeln (line);
 
149
    // time data
 
150
    line = "  "; line+= JSWR_RSPL_TIME; line+= " : ";
 
151
    os.write (line);
 
152
    if (data.stamped () == true) {
 
153
      os.writeln ("[");
 
154
      for (long k = 0; k < rows; k++) {
 
155
        t_real t = data.gettime (k);
 
156
        String s = Utility::tostring (t, psiz, sflg);
 
157
        line = "    "; line+= s;
 
158
        if (k < rmax) line+= ",";
 
159
        os.writeln (line);
 
160
      }
 
161
      os.writeln ("  ],");
 
162
    } else {
 
163
      os.writeln ("null,");
 
164
    }
 
165
    // data array
 
166
    os.writeln ("  \"data\" : [");
 
167
    for (long j = 0; j < cols; j++) {
 
168
      os.writeln ("    [");
 
169
      for (long k = 0; k < rows; k++) {
 
170
        t_real v = data.get (k, j);
 
171
        String s = Utility::tostring (v, psiz, sflg);
 
172
        line = "      "; line += s;
 
173
        if (k < rmax) line+= ",";
 
174
        os.writeln (line);
 
175
      }
 
176
      if (j < cmax) 
 
177
        os.writeln ("    ],");
 
178
      else
 
179
        os.writeln ("    ]");
 
180
    }
 
181
    os.writeln ("  ]");
 
182
    // close the name
 
183
    os.writeln ("}");
 
184
  }
 
185
 
 
186
  // -------------------------------------------------------------------------
 
187
  // - class section                                                         -
 
188
  // -------------------------------------------------------------------------
 
189
 
 
190
  // create a default json mime
 
191
 
 
192
  JsonMime::JsonMime (void) {
 
193
    d_mime = JSWR_MIME_TYPE;
 
194
    p_mobj = nilp;
 
195
  }
 
196
 
 
197
  // create a json mime by object
 
198
 
 
199
  JsonMime::JsonMime (Object* mobj) {
 
200
    d_mime = JSWR_MIME_TYPE;
 
201
    Object::iref (p_mobj = mobj);
 
202
  }
 
203
 
 
204
  // destroy this writer
 
205
 
 
206
  JsonMime::~JsonMime (void) {
 
207
    Object::dref (p_mobj);
 
208
  }
 
209
 
 
210
  // return the object class name
 
211
 
 
212
  String JsonMime::repr (void) const {
 
213
    return "JsonMime";
 
214
  }
 
215
 
 
216
  // write the data samples to a buffer
 
217
 
 
218
  void JsonMime::write (Buffer& buf) const {
 
219
    rdlock ();
 
220
    try {
 
221
      // check for rsamples object
 
222
      Rsamples* data = dynamic_cast <Rsamples*> (p_mobj);
 
223
      if (data != nilp) json_write (*data, buf);
 
224
      unlock ();
 
225
    } catch (...) {
 
226
      unlock ();
 
227
      throw;
 
228
    }
 
229
  }
 
230
 
 
231
  // write the data samples to an output stream
 
232
 
 
233
  void JsonMime::write (OutputStream& os) const {
 
234
    rdlock ();
 
235
    try {
 
236
      // check for rsamples object
 
237
      Rsamples* data = dynamic_cast <Rsamples*> (p_mobj);
 
238
      if (data != nilp) json_write (*data, os);
 
239
      unlock ();
 
240
    } catch (...) {
 
241
      unlock ();
 
242
      throw;
 
243
    }
 
244
  }
 
245
 
 
246
  // get the mime object
 
247
 
 
248
  Object* JsonMime::getobj (void) const {
 
249
    rdlock ();
 
250
    try {
 
251
      Object* result = p_mobj;
 
252
      unlock ();
 
253
      return result;
 
254
    } catch (...) {
 
255
      unlock ();
 
256
      throw;
 
257
    }
 
258
  }
 
259
 
 
260
  // -------------------------------------------------------------------------
 
261
  // - object section                                                        -
 
262
  // -------------------------------------------------------------------------
 
263
 
 
264
  // the quark zone
 
265
  static const long QUARK_ZONE_LENGTH = 1;
 
266
  static QuarkZone  zone (QUARK_ZONE_LENGTH);
 
267
 
 
268
  // the object supported quarks
 
269
  static const long QUARK_GETOBJ = zone.intern ("get-object");
 
270
 
 
271
  // create a new object in a generic way
 
272
 
 
273
  Object* JsonMime::mknew (Vector* argv) {
 
274
    long argc = (argv == nilp) ? 0 : argv->length ();
 
275
    // create a default html tree object
 
276
    if (argc == 0) return new JsonMime;
 
277
    // check for 1 argument
 
278
    if (argc == 1) {
 
279
      Object* obj = argv->get (0);
 
280
      return new JsonMime (obj);
 
281
    }
 
282
    throw Exception ("argument-error",
 
283
                     "too many argument with json mime constructor");
 
284
  }
 
285
  
 
286
  // return true if the given quark is defined
 
287
 
 
288
  bool JsonMime::isquark (const long quark, const bool hflg) const {
 
289
    rdlock ();
 
290
    try {
 
291
      if (zone.exists (quark) == true) {
 
292
        unlock ();
 
293
        return true;
 
294
      } 
 
295
      bool result = hflg ? Mime::isquark (quark, hflg) : false;
 
296
      unlock ();
 
297
      return result;
 
298
    } catch (...) {
 
299
      unlock ();
 
300
      throw;
 
301
    }
 
302
  }
 
303
 
 
304
  // apply this object with a set of arguments and a quark
 
305
 
 
306
  Object* JsonMime::apply (Runnable* robj, Nameset* nset, const long quark,
 
307
                             Vector* argv) {
 
308
    // get the number of arguments
 
309
    long argc = (argv == nilp) ? 0 : argv->length ();
 
310
    
 
311
    // dispatch 0 argument
 
312
    if (argc == 1) {
 
313
      if (quark == QUARK_GETOBJ) {
 
314
        rdlock ();
 
315
        try {
 
316
          Object* result = getobj ();
 
317
          robj->post (result);
 
318
          unlock ();
 
319
          return result;
 
320
        } catch (...) {
 
321
          unlock ();
 
322
          throw;
 
323
        }
 
324
      }
 
325
    }
 
326
    // call the mime method
 
327
    return Mime::apply (robj, nset, quark, argv);
 
328
  }
 
329
}