wiretap 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. data/LICENSE +32 -0
  2. data/README +93 -0
  3. data/ext/Makefile +146 -0
  4. data/ext/audio_format.cpp +224 -0
  5. data/ext/bmp.cpp +65 -0
  6. data/ext/extconf.rb +85 -0
  7. data/ext/format.cpp +347 -0
  8. data/ext/image.h +27 -0
  9. data/ext/image_format.cpp +194 -0
  10. data/ext/node.cpp +401 -0
  11. data/ext/nodechildren.cpp +67 -0
  12. data/ext/nodeframes.cpp +233 -0
  13. data/ext/nodemetadata.cpp +90 -0
  14. data/ext/ppm.cpp +132 -0
  15. data/ext/server.cpp +202 -0
  16. data/ext/serverlist.cpp +183 -0
  17. data/ext/sgi.cpp +69 -0
  18. data/ext/testserver/Makefile +5 -0
  19. data/ext/testserver/cfg/wiretap_path_translation_db.xml +44 -0
  20. data/ext/testserver/cfg/wiretapd.cfg +74 -0
  21. data/ext/testserver/cfg/wiretapd.res +60 -0
  22. data/ext/testserver/db.sql +50 -0
  23. data/ext/testserver/server.cpp +206 -0
  24. data/ext/testserver/testserver.rb +146 -0
  25. data/ext/thread_worker.cpp +85 -0
  26. data/ext/wiretap.cpp +68 -0
  27. data/ext/wiretap.h +115 -0
  28. data/lib/wiretap.rb +280 -0
  29. data/test/audio.rb +27 -0
  30. data/test/convert.rb +35 -0
  31. data/test/image.rb +101 -0
  32. data/test/read_frames.rb +142 -0
  33. data/test/wiretap-images/01.ppm +0 -0
  34. data/test/wiretap-images/32bit.stoneimage +621 -0
  35. data/test/wiretap-images/36bit.stoneimage +1036 -0
  36. data/test/wiretap-images/48bit.stoneimage +800 -1
  37. data/test/wiretap-images/a.stoneimage +0 -0
  38. data/test/wiretap-images/a0.stoneimage +0 -0
  39. data/test/wiretap-images/a1.stoneimage +0 -0
  40. data/test/wiretap-images/a2.stoneimage +0 -0
  41. data/test/wiretap-images/a3.stoneimage +0 -0
  42. data/test/wiretap-images/a4.stoneimage +0 -0
  43. data/test/wiretap-images/b1.stonesound +0 -0
  44. data/test/wiretap-images/importable-seq/00000001.ppm +0 -0
  45. data/test/wiretap-images/importable-seq/00000002.ppm +0 -0
  46. data/test/wiretap-images/importable-seq/00000003.ppm +0 -0
  47. data/test/wiretap-images/importable-seq/00000004.ppm +0 -0
  48. data/test/wiretap-images/importable-seq/00000005.ppm +0 -0
  49. data/test/wiretap-images/importable-seq/00000006.ppm +0 -0
  50. data/test/wiretap-images/importable-seq/00000007.ppm +0 -0
  51. data/test/wiretap-images/importable-seq/00000008.ppm +0 -0
  52. data/test/wiretap-images/importable-seq/00000009.ppm +0 -0
  53. data/test/wiretap-images/importable-seq/00000010.ppm +0 -0
  54. data/test/wiretap-images/importable-seq/00000011.ppm +0 -0
  55. data/test/wiretap-images/importable-seq/00000012.ppm +0 -0
  56. data/test/wiretap-images/monsters_001.tif +0 -0
  57. data/test/wiretap-images/monsters_002.tif +0 -0
  58. data/test/wiretap-images/monsters_003.tif +0 -0
  59. data/test/wiretap-images/output.mov +0 -0
  60. data/test/wiretap-images/output.wav +0 -0
  61. data/test/write_frames.rb +82 -0
  62. metadata +138 -0
@@ -0,0 +1,90 @@
1
+ #include "wiretap.h"
2
+
3
+
4
+
5
+ /*
6
+ * Get count of node metadata entries
7
+ */
8
+ static VALUE wiretap_node_metadata_count(VALUE self) {
9
+ WireTapNodeHandle* node;
10
+ int num;
11
+ Data_Get_Struct(self, WireTapNodeHandle, node);
12
+ NODERUN_E(node, node->getNumAvailableMetaDataStreams(num));
13
+ return INT2FIX(num);
14
+ }
15
+
16
+ /*
17
+ * Get metadata by index
18
+ */
19
+ static VALUE wiretap_node_metadata_get_at(int argc, VALUE *argv, VALUE self) {
20
+ WireTapNodeHandle* node;
21
+ VALUE stream_name, filter, depth;
22
+
23
+ rb_scan_args(argc, argv, "12", &stream_name, &filter, &depth);
24
+ Check_Type(stream_name, T_STRING);
25
+ if(filter != Qnil) {
26
+ Check_Type(filter, T_STRING);
27
+ }
28
+ if(depth != Qnil) {
29
+ Check_Type(depth, T_FIXNUM);
30
+ }
31
+
32
+ Data_Get_Struct(self, WireTapNodeHandle, node);
33
+
34
+ WireTapStr data;
35
+ NODERUN_E(node, node->getMetaData(CSTR(stream_name), filter == Qnil ? "all" : CSAFESTR(filter), depth == Qnil ? 0 : FIX2INT(depth), data));
36
+
37
+ return wiretap_to_str(data);
38
+ }
39
+
40
+ /*
41
+ * Set metadata at index
42
+ */
43
+ static VALUE wiretap_node_metadata_set_at(VALUE self, VALUE index, VALUE value) {
44
+ WireTapNodeHandle* node;
45
+ Data_Get_Struct(self, WireTapNodeHandle, node);
46
+ Check_Type(index, T_STRING);
47
+ Check_Type(value, T_STRING);
48
+
49
+ NODERUN_E(node, node->setMetaData(CSTR(index), WireTapStr(CSTR(value))));
50
+ return self;
51
+ }
52
+
53
+ /*
54
+ * eval block on each metadata entry
55
+ */
56
+ static VALUE wiretap_node_metadata_each(VALUE self) {
57
+ WireTapNodeHandle* node;
58
+ int i, num;
59
+ Data_Get_Struct(self, WireTapNodeHandle, node);
60
+ NODERUN_E(node, node->getNumAvailableMetaDataStreams(num));
61
+
62
+ for(i = 0; i < num; i++) {
63
+ WireTapStr stream;
64
+ NODERUN_E(node, node->getAvailableMetaDataStream(i, stream));
65
+
66
+ WireTapStr data;
67
+ NODERUN_E(node, node->getMetaData(stream.c_str(), "all", -1, data));
68
+ rb_yield(rb_ary_new3(2, wiretap_to_str(stream), wiretap_to_str(data)));
69
+ }
70
+ return self;
71
+ }
72
+
73
+ /*
74
+ * Access to metadata directly.
75
+ */
76
+ static VALUE wiretap_node_metadata_body(VALUE self) {
77
+ WireTapNodeHandle* node;
78
+ Data_Get_Struct(self, WireTapNodeHandle, node);
79
+ return Qnil;
80
+ }
81
+
82
+ void Init_metadata() {
83
+
84
+ cNodeMetaData = rb_define_class_under(mWiretap, "NodeMetaData", rb_cObject);
85
+ rb_define_method(cNodeMetaData, "count", VALUEFUNC(wiretap_node_metadata_count), 0);
86
+ rb_define_method(cNodeMetaData, "[]", VALUEFUNC(wiretap_node_metadata_get_at), -1);
87
+ rb_define_method(cNodeMetaData, "[]=", VALUEFUNC(wiretap_node_metadata_set_at), 2);
88
+ rb_define_method(cNodeMetaData, "each", VALUEFUNC(wiretap_node_metadata_each), 0);
89
+ }
90
+
data/ext/ppm.cpp ADDED
@@ -0,0 +1,132 @@
1
+ #include "wiretap.h"
2
+
3
+
4
+
5
+ static int extract_int(FILE* f) {
6
+ char buffer[256];
7
+ int pos = 0;
8
+ char c;
9
+ while(!feof(f)) {
10
+ c = fgetc(f);
11
+ if(!isalnum(c)) {
12
+ break;
13
+ }
14
+ buffer[pos++] = c;
15
+ }
16
+ buffer[pos] = '\0';
17
+ return atoi(buffer);
18
+ }
19
+
20
+ bool ppm_read_format(FILE* f, WireTapClipFormat* format) {
21
+ extract_int(f);
22
+
23
+ int width = extract_int(f);
24
+ if(format) {
25
+ format->setWidth(width);
26
+ }
27
+
28
+ int height = extract_int(f);
29
+ if(format) {
30
+ format->setHeight(height);
31
+ }
32
+
33
+ int maxval = extract_int(f);
34
+ if(maxval > 255) {
35
+ THROW("Decoding 16bit PPM files is not supported");
36
+ }
37
+
38
+ if(format) {
39
+ format->setBitsPerPixel(24);
40
+ format->setNumChannels(3);
41
+ format->setFrameBufferSize((format->bitsPerPixel()/8)*width*height);
42
+ format->setFrameRate(25);
43
+ WireTapClipFormat::ScanFormat scan_format = format->strToScanFormat("progressive");
44
+ format->setScanFormat(scan_format);
45
+ format->setFormatTag("rgb");
46
+ format->setMetaDataTag("IFFFS_XML");
47
+ format->setMetaData("<IFFFS_XML Version=\"1.0\"><ClipData></ClipData></IFFFS_XML>");
48
+ }
49
+
50
+ return true;
51
+ }
52
+
53
+
54
+ bool ppm_read_image(FILE* f, unsigned char* frame, int width, int height, int bpp) {
55
+ size_t row_size = width*3;
56
+
57
+ for(int i = 0; i < height; i++) {
58
+ size_t read_bytes = fread(frame + (height - 1 - i)*row_size, 1, row_size, f);
59
+ if(read_bytes != row_size) {
60
+ rb_warn("Read %d bytes, expected %d. Step: %d", read_bytes, row_size, i);
61
+ return false;
62
+ }
63
+ }
64
+
65
+ return true;
66
+ }
67
+
68
+
69
+ /*
70
+ * Extract Wiretap clip format from PPM file. The format will inherit the bit depths of the image and allocate
71
+ * the framebuffer size into which the imported image will fit
72
+ *
73
+ * @clipformat = Wiretap::PPM.format("test/wiretap-images/01.ppm")
74
+ */
75
+ static VALUE wiretap_get_ppm_format(VALUE self, VALUE input) {
76
+
77
+ FILE* infile = fopen(CSTR(input), "r");
78
+ if(!infile) {
79
+ THROW("Couldn't open file %s for reading", CSTR(input));
80
+ }
81
+ WireTapClipFormat format;
82
+ if(!ppm_read_format(infile, &format)) {
83
+ fclose(infile);
84
+ return Qfalse;
85
+ }
86
+ fclose(infile);
87
+ VALUE clip_format = rb_funcall(cClipFormat, rb_intern("new"), 0);
88
+ DATA_PTR(clip_format) = new WireTapClipFormat(format);
89
+ return clip_format;
90
+ }
91
+
92
+ /*
93
+ * Convert a PPM file to an SGI file
94
+ *
95
+ * Wiretap::PPM.to_sgi("test/wiretap-images/01.ppm", "/tmp/a.sgi")
96
+ */
97
+ static VALUE wiretap_convert_ppm_to_sgi(VALUE self, VALUE input, VALUE output) {
98
+ Check_Type(input, T_STRING);
99
+ Check_Type(output, T_STRING);
100
+ FILE* infile = fopen(CSTR(input), "r");
101
+ if(!infile) {
102
+ THROW("Couldn't open file %s for read", CSTR(input));
103
+ }
104
+ WireTapClipFormat format;
105
+ if(!ppm_read_format(infile, &format)) {
106
+ fclose(infile);
107
+ rb_warn("Couldn't read PPM format from file %s", CSTR(input));
108
+ return Qfalse;
109
+ }
110
+
111
+ unsigned char frame[format.frameBufferSize()];
112
+ if(!ppm_read_image(infile, frame, format.width(), format.height(), format.bitsPerPixel())) {
113
+ fclose(infile);
114
+ rb_warn("Couldn't read PPM image data from file %s", CSTR(input));
115
+ return Qfalse;
116
+ }
117
+ fclose(infile);
118
+
119
+ if(!wiretap_write_image_frame(format.width(), format.height(), format.bitsPerPixel(), frame, CSTR(output))) {
120
+ rb_warn("Couldn't dump image to file %s", CSTR(output));
121
+ return Qfalse;
122
+ }
123
+ return Qtrue;
124
+ }
125
+
126
+
127
+ void Init_ppm() {
128
+ mPPM = rb_define_module_under(mWiretap, "PPM");
129
+
130
+ rb_define_singleton_method(mPPM, "to_sgi", VALUEFUNC(wiretap_convert_ppm_to_sgi), 2);
131
+ rb_define_singleton_method(mPPM, "format", VALUEFUNC(wiretap_get_ppm_format), 1);
132
+ }
data/ext/server.cpp ADDED
@@ -0,0 +1,202 @@
1
+ #include "wiretap.h"
2
+
3
+ #define Safe_Get_Server(obj, server) do { \
4
+ Data_Get_Struct(obj, WireTapServerHandle, server); \
5
+ if(!server) { \
6
+ rb_warn("Connection to server is already closed on %s: %d", __FILE__, __LINE__); \
7
+ return Qnil; \
8
+ } \
9
+ } while(0)
10
+
11
+ static void wiretap_server_free(WireTapServerHandle* server) {
12
+ if(server) {
13
+ if(server->ping()) {
14
+ server->stop();
15
+ }
16
+ delete server;
17
+ }
18
+ }
19
+
20
+ /*
21
+ * If destruction of server interface is required, call close on it to be sure,
22
+ * that server has been closed right now.
23
+ *
24
+ */
25
+ static VALUE wiretap_server_close(VALUE self) {
26
+ rb_funcall(self, rb_intern("stop!"), 0);
27
+
28
+ WireTapServerHandle* server;
29
+ Safe_Get_Server(self, server);
30
+ delete server;
31
+ DATA_PTR(self) = 0;
32
+ return Qtrue;
33
+ }
34
+
35
+ static VALUE wiretap_server_allocate(VALUE klass) {
36
+ return Data_Wrap_Struct(klass, 0, wiretap_server_free, 0);
37
+ }
38
+
39
+ /*
40
+ * Use Server.new(hostname) to connect to specified server.
41
+ * This method opens server by it's hostname. Not by id.
42
+ */
43
+ static VALUE wiretap_server_connect(VALUE self, VALUE hostname) {
44
+ Check_Type(hostname, T_STRING);
45
+ Check_Type(self, T_DATA);
46
+
47
+ DATA_PTR(self) = new WireTapServerHandle(CSTR(hostname));
48
+ return self;
49
+ }
50
+
51
+ /*
52
+ * Get hostname of server, to which connection is established.
53
+ */
54
+ static VALUE wiretap_server_hostname(VALUE self) {
55
+ WireTapServerHandle* server;
56
+ Safe_Get_Server(self, server);
57
+ return rb_str_new2(server->getHostName());
58
+ }
59
+
60
+
61
+ /*
62
+ * Get root node of server
63
+ */
64
+ static VALUE wiretap_server_root(VALUE self) {
65
+ WireTapServerHandle* server;
66
+ Safe_Get_Server(self, server);
67
+
68
+ WireTapNodeHandle root_node;
69
+ SERVERRUN_E(server, server->getRootNode(root_node));
70
+
71
+ return wiretap_node_create_with(root_node, self, Qnil);
72
+ }
73
+
74
+ /*
75
+ * Open node by id
76
+ */
77
+ static VALUE wiretap_server_open_node(VALUE self, VALUE id) {
78
+ Check_Type(id, T_STRING);
79
+
80
+ WireTapServerHandle* server;
81
+ Safe_Get_Server(self, server);
82
+
83
+ WireTapNodeHandle node(*server, CSTR(id));
84
+ return wiretap_node_create_with(node, self, Qnil);
85
+ }
86
+
87
+
88
+ /*
89
+ * Get vendor of server
90
+ */
91
+ static VALUE wiretap_server_vendor(VALUE self) {
92
+ WireTapStr str;
93
+ WireTapServerHandle* server;
94
+ Safe_Get_Server(self, server);
95
+ RUN_E(server->getVendor(str), server);
96
+ return wiretap_to_str(str);
97
+ }
98
+
99
+ /*
100
+ * Get product name, installed on server
101
+ */
102
+ static VALUE wiretap_server_product(VALUE self) {
103
+ WireTapStr str;
104
+ WireTapServerHandle* server;
105
+ Safe_Get_Server(self, server);
106
+ RUN_E(server->getProduct(str), server);
107
+ return wiretap_to_str(str);
108
+ }
109
+
110
+ /*
111
+ * Get version of server
112
+ */
113
+ static VALUE wiretap_server_version(VALUE self) {
114
+ int major, minor;
115
+ char buf[256];
116
+ WireTapServerHandle* server;
117
+ Safe_Get_Server(self, server);
118
+ RUN_E(server->getVersion(major, minor), server);
119
+ snprintf(buf, sizeof(buf), "%d.%d", major, minor);
120
+ return rb_str_new2(buf);
121
+ }
122
+
123
+ /*
124
+ * Get ID of server storage
125
+ */
126
+ static VALUE wiretap_server_storage(VALUE self) {
127
+ WireTapStr str;
128
+ WireTapServerHandle* server;
129
+ Safe_Get_Server(self, server);
130
+ RUN_E(server->getStorageId(str), server);
131
+ return wiretap_to_str(str);
132
+ }
133
+
134
+ /*
135
+ * Get version of server protocol. major.minor
136
+ */
137
+ static VALUE wiretap_server_protocol(VALUE self) {
138
+ int major, minor;
139
+ char buf[256];
140
+ WireTapServerHandle* server;
141
+ Safe_Get_Server(self, server);
142
+ RUN_E(server->getProtocolVersion(major, minor), server);
143
+ snprintf(buf, sizeof(buf), "%d.%d", major, minor);
144
+ return rb_str_new2(buf);
145
+ }
146
+
147
+ /*
148
+ * Check, whether connection to server is still alive
149
+ */
150
+ static VALUE wiretap_server_is_alive(VALUE self) {
151
+ WireTapServerHandle* server;
152
+ Data_Get_Struct(self, WireTapServerHandle, server);
153
+ if(!server) {
154
+ return Qfalse;
155
+ }
156
+ return server->ping() ? Qtrue : Qfalse;
157
+ }
158
+
159
+
160
+ /*
161
+ * Theoretically, it is possible to stop continious operation on server
162
+ * from other thread. Practically, it is impossible unless ruby gets threads
163
+ */
164
+ static VALUE wiretap_server_stop(VALUE self) {
165
+ WireTapServerHandle* server;
166
+ Safe_Get_Server(self, server);
167
+ if(server->ping()) {
168
+ server->stop();
169
+ }
170
+ return self;
171
+ }
172
+
173
+ /*
174
+ * Get message, explaining last error, occured on server
175
+ */
176
+ static VALUE wiretap_server_last_error(VALUE self) {
177
+ WireTapServerHandle* server;
178
+ Safe_Get_Server(self, server);
179
+ return rb_str_new2(server->lastError());
180
+ }
181
+
182
+ #define READER(name) rb_define_method(cServer, #name, VALUEFUNC(wiretap_server_ ## name), 0);
183
+
184
+ void Init_server() {
185
+ cServer = rb_define_class_under(mWiretap, "Server", rb_cObject);
186
+ rb_define_alloc_func(cServer, wiretap_server_allocate);
187
+ rb_define_method(cServer, "initialize", VALUEFUNC(wiretap_server_connect), 1);
188
+ rb_define_method(cServer, "hostname", VALUEFUNC(wiretap_server_hostname), 0);;
189
+ rb_define_method(cServer, "root", VALUEFUNC(wiretap_server_root), 0);;
190
+ rb_define_method(cServer, "vendor", VALUEFUNC(wiretap_server_vendor), 0);;
191
+ rb_define_method(cServer, "product", VALUEFUNC(wiretap_server_product), 0);;
192
+ rb_define_method(cServer, "version", VALUEFUNC(wiretap_server_version), 0);;
193
+ rb_define_method(cServer, "storage", VALUEFUNC(wiretap_server_storage), 0);;
194
+ rb_define_method(cServer, "protocol", VALUEFUNC(wiretap_server_protocol), 0);;
195
+ rb_define_method(cServer, "last_error", VALUEFUNC(wiretap_server_last_error), 0);;
196
+ rb_define_method(cServer, "alive?", VALUEFUNC(wiretap_server_is_alive), 0);
197
+ rb_define_method(cServer, "stop!", VALUEFUNC(wiretap_server_stop), 0);
198
+ rb_define_method(cServer, "open", VALUEFUNC(wiretap_server_open_node), 1);
199
+ rb_define_method(cServer, "close!", VALUEFUNC(wiretap_server_close), 0);
200
+ }
201
+
202
+ #undef READER
@@ -0,0 +1,183 @@
1
+ #include "wiretap.h"
2
+
3
+ static void wiretap_server_info_free(WireTapServerList::ServerInfo* server_info) {
4
+ delete server_info;
5
+ }
6
+
7
+
8
+ static VALUE wiretap_server_info_create(WireTapServerList::ServerInfo & info) {
9
+ WireTapServerList::ServerInfo *server_info = new WireTapServerList::ServerInfo(info);
10
+ return Data_Wrap_Struct(cServerInfo, 0, wiretap_server_info_free, server_info);
11
+ }
12
+
13
+ /*
14
+ * Extract hostname from server information
15
+ */
16
+ static VALUE wiretap_server_info_hostname(VALUE self) {
17
+ WireTapServerList::ServerInfo* server_info;
18
+ Data_Get_Struct(self, WireTapServerList::ServerInfo, server_info);
19
+ return rb_str_new2(server_info->getHostname());
20
+ }
21
+
22
+ /*
23
+ * Extract version of server from server information
24
+ */
25
+ static VALUE wiretap_server_info_version(VALUE self) {
26
+ WireTapServerList::ServerInfo* server_info;
27
+ Data_Get_Struct(self, WireTapServerList::ServerInfo, server_info);
28
+ int major = server_info->getVersionMajor();
29
+ int minor = server_info->getVersionMinor();
30
+ char buf[256];
31
+ snprintf(buf, sizeof(buf), "%d.%d", major, minor);
32
+ return rb_str_new2(buf);
33
+ }
34
+
35
+ /*
36
+ * Extract product name from server information
37
+ */
38
+ static VALUE wiretap_server_info_product(VALUE self) {
39
+ WireTapServerList::ServerInfo* server_info;
40
+ Data_Get_Struct(self, WireTapServerList::ServerInfo, server_info);
41
+ return rb_str_new2(server_info->getProduct());
42
+ }
43
+
44
+ /*
45
+ * Extract vendor name from server information
46
+ */
47
+ static VALUE wiretap_server_info_vendor(VALUE self) {
48
+ WireTapServerList::ServerInfo* server_info;
49
+ Data_Get_Struct(self, WireTapServerList::ServerInfo, server_info);
50
+ return rb_str_new2(server_info->getVendor());
51
+ }
52
+
53
+ /*
54
+ * Extract product version from server information
55
+ */
56
+ static VALUE wiretap_server_info_product_version(VALUE self) {
57
+ WireTapServerList::ServerInfo* server_info;
58
+ Data_Get_Struct(self, WireTapServerList::ServerInfo, server_info);
59
+ int major = server_info->getProductVersionMajor();
60
+ int minor = server_info->getProductVersionMinor();
61
+ char buf[256];
62
+ snprintf(buf, sizeof(buf), "%d.%d", major, minor);
63
+ return rb_str_new2(buf);
64
+ }
65
+
66
+ /*
67
+ * Extract storage ID from server information
68
+ */
69
+ static VALUE wiretap_server_info_storage(VALUE self) {
70
+ WireTapServerList::ServerInfo* server_info;
71
+ Data_Get_Struct(self, WireTapServerList::ServerInfo, server_info);
72
+ const char* storage_id = server_info->getStorageId();
73
+ if(!storage_id) {
74
+ rb_raise(eError, "Problem in wiretap. File: %s: %d", __FILE__, __LINE__);
75
+ }
76
+ return rb_str_new2(storage_id);
77
+ }
78
+
79
+ /*
80
+ * Connect to server, described in this information.
81
+ */
82
+ static VALUE wiretap_server_info_server(VALUE self) {
83
+ VALUE hostname = rb_funcall(self, rb_intern("hostname"), 0);
84
+ return rb_funcall(cServer, rb_intern("new"), 1, hostname);
85
+ }
86
+
87
+ static void wiretap_server_list_free(WireTapServerList* server_list) {
88
+ delete server_list;
89
+ }
90
+
91
+
92
+ static VALUE wiretap_server_list_s_allocate(VALUE klass) {
93
+ WireTapServerList *server_list = new WireTapServerList();
94
+ return Data_Wrap_Struct(klass, 0, wiretap_server_list_free, server_list);
95
+ }
96
+
97
+ /*
98
+ * Server list doesn't require any params for initialization
99
+ */
100
+ static VALUE wiretap_server_list_initialize(VALUE self) {
101
+ return self;
102
+ }
103
+
104
+ /*
105
+ * resolve server by it's storage ID
106
+ */
107
+ static VALUE wiretap_server_list_resolve(VALUE self, VALUE storage) {
108
+ WireTapServerList* server_list;
109
+ Data_Get_Struct(self, WireTapServerList, server_list);
110
+
111
+ WireTapServerId host;
112
+ RUN_E(server_list->resolveStorageId(CSTR(storage), host), server_list);
113
+ return Qnil;
114
+ //return wiretap_to_str(host);
115
+ }
116
+
117
+ /*
118
+ * Number of available servers
119
+ */
120
+ static VALUE wiretap_server_list_count(VALUE self) {
121
+ WireTapServerList* server_list;
122
+ Data_Get_Struct(self, WireTapServerList, server_list);
123
+
124
+ int num = 0;
125
+ //RUN_E(server_list->getNumNodes(num), server_list);
126
+ if(!server_list->getNumNodes(num)) {
127
+ rb_warn("Problem in wiretap: %s. File: %s: %d", server_list->lastError(), __FILE__, __LINE__);
128
+ }
129
+ return INT2FIX(num);
130
+ }
131
+
132
+ /*
133
+ * Get server by it's index
134
+ */
135
+ static VALUE wiretap_server_list_get_at(VALUE self, VALUE index) {
136
+ WireTapServerList* server_list;
137
+ Data_Get_Struct(self, WireTapServerList, server_list);
138
+
139
+ WireTapServerList::ServerInfo node;
140
+ RUN_E(server_list->getNode(FIX2INT(index), node), server_list);
141
+ return wiretap_server_info_create(node);;
142
+ }
143
+
144
+ /*
145
+ * Eval block on each server
146
+ */
147
+ static VALUE wiretap_server_list_each(VALUE self) {
148
+ WireTapServerList* server_list;
149
+ Data_Get_Struct(self, WireTapServerList, server_list);
150
+
151
+ int num;
152
+
153
+ RUN_E(server_list->getNumNodes(num), server_list);
154
+
155
+ for(int i = 0; i < num; i++) {
156
+ WireTapServerList::ServerInfo node;
157
+ RUN_E(server_list->getNode(i, node), server_list);
158
+ rb_yield(wiretap_server_info_create(node));
159
+ }
160
+ return self;
161
+ }
162
+
163
+
164
+ void Init_serverlist() {
165
+ cServerList = rb_define_class_under(mWiretap, "ServerList", rb_cObject);
166
+ rb_define_alloc_func(cServerList, wiretap_server_list_s_allocate);
167
+ rb_define_method(cServerList, "initialize", VALUEFUNC(wiretap_server_list_initialize), 0);
168
+ rb_define_method(cServerList, "resolve", VALUEFUNC(wiretap_server_list_resolve), 1);
169
+ rb_define_method(cServerList, "length", VALUEFUNC(wiretap_server_list_count), 0);
170
+ rb_define_method(cServerList, "count", VALUEFUNC(wiretap_server_list_count), 0);
171
+ rb_define_method(cServerList, "[]", VALUEFUNC(wiretap_server_list_get_at), 1);
172
+ rb_define_method(cServerList, "each", VALUEFUNC(wiretap_server_list_each), 0);
173
+
174
+ cServerInfo = rb_define_class_under(mWiretap, "ServerInfo", rb_cObject);
175
+ rb_define_method(cServerInfo, "hostname", VALUEFUNC(wiretap_server_info_hostname), 0);
176
+ rb_define_method(cServerInfo, "version", VALUEFUNC(wiretap_server_info_version), 0);
177
+ rb_define_method(cServerInfo, "product", VALUEFUNC(wiretap_server_info_product), 0);
178
+ rb_define_method(cServerInfo, "vendor", VALUEFUNC(wiretap_server_info_vendor), 0);
179
+ rb_define_method(cServerInfo, "product_version", VALUEFUNC(wiretap_server_info_product_version), 0);
180
+ rb_define_method(cServerInfo, "storage", VALUEFUNC(wiretap_server_info_storage), 0);
181
+ rb_define_method(cServerInfo, "server", VALUEFUNC(wiretap_server_info_server), 0);
182
+ }
183
+
data/ext/sgi.cpp ADDED
@@ -0,0 +1,69 @@
1
+ #include "wiretap.h"
2
+
3
+
4
+ static void write_channel(int width, int height, int number, unsigned char* frame, FILE* f) {
5
+ std::auto_ptr<unsigned char> buffer(new unsigned char[width*height]);
6
+ for(int i = 0; i < width*height; i++) {
7
+ buffer.get()[i] = frame[3*i + number];
8
+ }
9
+ fwrite(buffer.get(), width*height, 1, f);
10
+ }
11
+
12
+ static void write_int32_be_value(int value, bitstream_writer writer, void *data) {
13
+ writer(data, value >> 24& 0xFF);
14
+ writer(data, value >> 16& 0xFF);
15
+ writer(data, value >> 8 & 0xFF);
16
+ writer(data, value >> 0 & 0xFF);
17
+ }
18
+ static void write_int16_be_value(short value, bitstream_writer writer, void *data) {
19
+ writer(data, value >> 8 & 0xFF);
20
+ writer(data, value >> 0 & 0xFF);
21
+ }
22
+
23
+ static int wiretap_write_sgi_header(int width, int height, bitstream_writer writer, void *data) {
24
+ //const int header_size = 512;
25
+ const int header_padding = 404;
26
+ const int magic = 474;
27
+
28
+ write_int16_be_value(magic, writer, data); // Magic 474
29
+ writer(data, 0); // 0 for uncompressed
30
+ writer(data, 1); // 1 for 1 byte per channel. 24 bit picture
31
+ write_int16_be_value(3, writer, data); // 3 dimensions - one plain image with multiple channels
32
+ write_int16_be_value(width, writer, data);
33
+ write_int16_be_value(height, writer, data);
34
+ write_int16_be_value(3, writer, data); // 3 for RGB mode: 3 channels
35
+ write_int32_be_value(0, writer, data); // minimum pixel value
36
+ write_int32_be_value(0xFF, writer, data); // maximum pixel value
37
+ write_int32_be_value(0, writer, data); // dummy
38
+
39
+ char image_name[80] = "Ruby wiretap encoded";
40
+ for(size_t i = 0; i < sizeof(image_name); i++) {
41
+ writer(data, image_name[i]);
42
+ }
43
+
44
+ write_int32_be_value(0, writer, data); // colormap is in normal mode (0)
45
+ for(int i = 0; i < header_padding; i++) {
46
+ writer(data, 0);
47
+ }
48
+ return 0;
49
+ }
50
+
51
+
52
+ bool wiretap_write_frame_sgi(int width, int height, int bpp, unsigned char* frame, FILE* f) {
53
+ filestream stream;
54
+ memstream mem;
55
+ std::auto_ptr<unsigned char> buffer(new unsigned char[width*height*3]);
56
+ memset(&stream, 0, sizeof(stream));
57
+ memset(&mem, 0, sizeof(mem));
58
+ mem.ptr = buffer.get();
59
+ stream.f = f;
60
+
61
+ wiretap_write_sgi_header(width, height, write_to_file, (void *)&stream);
62
+ wiretap_write_image_data(width, height, bpp, frame, write_to_memory, (void *)&mem);
63
+
64
+ write_channel(width, height, 0, buffer.get(), f);
65
+ write_channel(width, height, 1, buffer.get(), f);
66
+ write_channel(width, height, 2, buffer.get(), f);
67
+ return 0;
68
+ }
69
+
@@ -0,0 +1,5 @@
1
+
2
+ all: server.bundle
3
+
4
+ server.bundle: server.cpp ../format.cpp
5
+ g++ ../format.cpp server.cpp -I../../../wiretap_api/api -L../../../wiretap_api/lib/dbg/MACOSX/fat/Darwin_8_3_0/GCC_4_0_1/ -lwiretapServerAPI -o server.bundle -I /opt/local/lib/ruby/1.8/i686-darwin8.8.3/ -lruby -g -dynamic -bundle -undefined suppress -flat_namespace