sysvipc 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,48 @@
1
+ = sysvipc
2
+
3
+ * http://github.com/#{github_username}/#{project_name}
4
+
5
+ == DESCRIPTION:
6
+
7
+ FIX (describe your package)
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * FIX (list of features or problems)
12
+
13
+ == SYNOPSIS:
14
+
15
+ FIX (code sample of usage)
16
+
17
+ == REQUIREMENTS:
18
+
19
+ * FIX (list of requirements)
20
+
21
+ == INSTALL:
22
+
23
+ * FIX (sudo gem install, anything else)
24
+
25
+ == LICENSE:
26
+
27
+ (The MIT License)
28
+
29
+ Copyright (c) 2009 FIXME full name
30
+
31
+ Permission is hereby granted, free of charge, to any person obtaining
32
+ a copy of this software and associated documentation files (the
33
+ 'Software'), to deal in the Software without restriction, including
34
+ without limitation the rights to use, copy, modify, merge, publish,
35
+ distribute, sublicense, and/or sell copies of the Software, and to
36
+ permit persons to whom the Software is furnished to do so, subject to
37
+ the following conditions:
38
+
39
+ The above copyright notice and this permission notice shall be
40
+ included in all copies or substantial portions of the Software.
41
+
42
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
43
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
45
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
46
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
47
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
48
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ gem 'hoe', '>= 2.1.0'
3
+ require 'hoe'
4
+ require 'fileutils'
5
+ require './lib/sysvipc'
6
+
7
+ Hoe.plugin :newgem
8
+ # Hoe.plugin :website
9
+ # Hoe.plugin :cucumberfeatures
10
+
11
+ # Generate all the Rake tasks
12
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
13
+ $hoe = Hoe.spec 'sysvipc' do
14
+ self.developer 'name', 'email'
15
+ #self.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
16
+ self.rubyforge_name = self.name # TODO this is default value
17
+ # self.extra_deps = [['activesupport','>= 2.0.2']]
18
+
19
+ end
20
+
21
+ require 'newgem/tasks'
22
+ Dir['tasks/**/*.rake'].each { |t| load t }
23
+
24
+ # TODO - want other tests/tasks run by default? Add them to the list
25
+ # remove_task :default
26
+ # task :default => [:spec, :features]
data/lib/sysvipc.rb ADDED
@@ -0,0 +1,373 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ module SysVIPC
5
+ VERSION = '0.1.0'
6
+ end
7
+
8
+ require 'sysvipc/sysvipc'
9
+
10
+ # Document-class: SysVIPC
11
+ #
12
+ # = SysVIPC
13
+ #
14
+ # Ruby module for System V Inter-Process Communication:
15
+ # message queues, semaphores, and shared memory.
16
+ #
17
+ # Hosted as project sysvipc[http://rubyforge.org/projects/sysvipc/]
18
+ # on RubyForge[http://rubyforge.org/].
19
+ #
20
+ # Copyright (C) 2001, 2006, 2007 Daiki Ueno
21
+ # Copyright (C) 2006, 2007, 2009 James Steven Jenkins
22
+ #
23
+ # == Usage Synopsis
24
+ # === Common Code
25
+ #
26
+ # All programs using this module must include
27
+ #
28
+ # require 'sysvipc'
29
+ #
30
+ # It may be convenient to add
31
+ #
32
+ # include SysVIPC
33
+ #
34
+ # All IPC objects are identified by a key. SysVIPC includes a
35
+ # convenience function for mapping file names and integer IDs into a
36
+ # key:
37
+ #
38
+ # key = ftok('/a/file/that/must/exist', 0)
39
+ #
40
+ # === Message Queues
41
+ #
42
+ # Get (create if necessary) a message queue:
43
+ #
44
+ # mq = MessageQueue.new(key, IPC_CREAT | 0600)
45
+ #
46
+ # Send a message of type 0:
47
+ #
48
+ # mq.send(0, 'message')
49
+ #
50
+ # Receive up to 100 bytes from the first message of type 0:
51
+ #
52
+ # msg = mq.receive(0, 100)
53
+ #
54
+ # === Semaphores
55
+ #
56
+ # Get (create if necessary) a set of 5 semaphores:
57
+ #
58
+ # sm = Semaphore.new(key, 5, IPC_CREAT | 0600)
59
+ #
60
+ # Initialize semaphores if newly created:
61
+ #
62
+ # sm.setall(Array.new(5, 1)) if sm.pid(0) == 0
63
+ #
64
+ # Acquire semaphore 2 (waiting if necessary):
65
+ #
66
+ # sm.op([Sembuf.new(2, -1)])
67
+ #
68
+ # Release semaphore 2:
69
+ #
70
+ # sm.op([Sembuf.new(2, 1)])
71
+ #
72
+ # === Shared Memory
73
+ #
74
+ # Get (create if necessary) an 8192-byte shared memory region:
75
+ #
76
+ # sh = SharedMemory(key, 8192, IPC_CREAT | 0660)
77
+ #
78
+ # Attach shared memory:
79
+ #
80
+ # shmaddr = sh.attach
81
+ #
82
+ # Write data:
83
+ #
84
+ # shmaddr.write('testing')
85
+ #
86
+ # Read 100 bytes of data:
87
+ #
88
+ # data = shmaddr.read(100);
89
+ #
90
+ # Detach shared memory:
91
+ #
92
+ # sh.detach(shmaddr)
93
+ #
94
+ # == Installation
95
+ #
96
+ # 1. <tt>ruby setup.rb config</tt>
97
+ # 2. <tt>ruby setup.rb setup</tt>
98
+ # 3. <tt>ruby setup.rb install</tt> (requires appropriate privilege)
99
+ #
100
+ # == Testing
101
+ #
102
+ # 1. <tt>./test_sysvipc_l</tt> (low-level interface)
103
+ # 2. <tt>./test_sysvipc_h</tt> (high-level interface)
104
+
105
+ module SysVIPC
106
+
107
+ def check_result(res) # :nodoc:
108
+ raise SystemCallError.new(SysVIPC.errno), nil, caller if res == -1
109
+ end
110
+
111
+ class MessageQueue
112
+
113
+ private
114
+
115
+ # Return a MessageQueue object encapsuating a message queue
116
+ # associated with +key+. See msgget(2).
117
+
118
+ def initialize(key, flags = 0)
119
+ @msgid = msgget(key, flags)
120
+ check_result(@msgid)
121
+ end
122
+
123
+ public
124
+
125
+ # Return the Msqid_ds object. See msgctl(2).
126
+
127
+ def ipc_stat
128
+ res, msqid_ds = msgctl(@msgid, IPC_STAT)
129
+ check_result(res)
130
+ msqid_ds
131
+ end
132
+ alias :msqid_ds :ipc_stat
133
+
134
+ # Set the Msqid_ds object. See msgctl(2).
135
+
136
+ def ipc_set(msqid_ds)
137
+ unless Msqid_ds === msqid_ds
138
+ raise ArgumentError,
139
+ "argument to ipc_set must be a Msqid_ds"
140
+ end
141
+ check_result(msgctl(@msgid, IPC_SET, msqid_ds))
142
+ end
143
+ alias :msqid_ds= :ipc_set
144
+
145
+ # Remove. See msgctl(2).
146
+
147
+ def ipc_rmid
148
+ check_result(msgctl(@msgid, IPC_RMID, nil))
149
+ end
150
+ alias :rm :ipc_rmid
151
+
152
+ # Send a message with type +type+ and text +text+. See msgsnd(2).
153
+
154
+ def snd(type, text, flags = 0)
155
+ check_result(msgsnd(@msgid, type, text, flags))
156
+ end
157
+ alias :send :snd
158
+
159
+ # Receive a message of type +type+, limited to +len+ bytes or fewer.
160
+ # See msgsnd(2).
161
+
162
+ def rcv(type, size, flags = 0)
163
+ res, mtype, mtext = msgrcv(@msgid, size, type, flags)
164
+ check_result(res)
165
+ mtext
166
+ end
167
+ alias :receive :rcv
168
+
169
+ end
170
+
171
+ class Sembuf
172
+
173
+ alias :orig_initialize :initialize
174
+
175
+ # Create a new Sembuf object for semaphore number +sem_num+,
176
+ # operation +sem_op+, and flags +sem_flg+. See semop(2).
177
+
178
+ def initialize(sem_num, sem_op, sem_flg = 0)
179
+ orig_initialize
180
+ self.sem_num = sem_num
181
+ self.sem_op = sem_op
182
+ self.sem_flg = sem_flg
183
+ end
184
+
185
+ end
186
+
187
+ class Semaphore
188
+
189
+ private
190
+
191
+ # Return a Sempahore object encapsulating a
192
+ # set of +nsems+ semaphores associated with +key+. See semget(2).
193
+
194
+ def initialize(key, nsems, flags)
195
+ @nsems = nsems
196
+ @semid = semget(key, nsems, flags)
197
+ check_result(@semid)
198
+ end
199
+
200
+ public
201
+
202
+ # Set each value in the semaphore set to the corresponding value
203
+ # in the Array +values+. See semctl(2).
204
+
205
+ def setall(values)
206
+ if values.length > @nsems
207
+ raise ArgumentError,
208
+ "too many values(#{values.length} for semaphore set (#{@nsems})"
209
+ end
210
+ check_result(semctl(@semid, 0, SETALL, values))
211
+ end
212
+
213
+ # Return an Array containing the value of each semaphore in the
214
+ # set. See semctl(2).
215
+
216
+ def getall
217
+ res, array = semctl(@semid, 0, GETALL)
218
+ check_result(res)
219
+ array
220
+ end
221
+
222
+ # Set the value of semaphore +semnum+ to +val+. See semctl(2).
223
+
224
+ def setval(semnum, val)
225
+ check_result(semctl(@semid, semnum, SETVAL, val))
226
+ end
227
+
228
+ # Get the value of semaphore +semnum+. See semctl(2).
229
+
230
+ def getval(semnum)
231
+ semctl(@semid, semnum, GETVAL)
232
+ end
233
+ alias :val :getval
234
+
235
+ # Get the process ID of the last semaphore operation. See
236
+ # semctl(2).
237
+
238
+ def getpid
239
+ semctl(@semid, 0, GETPID)
240
+ end
241
+ alias :pid :getpid
242
+
243
+ # Get the number of processes waiting for a semaphore to become
244
+ # non-zero. See semctl(2).
245
+
246
+ def getncnt
247
+ semctl(@semid, 0, GETNCNT)
248
+ end
249
+ alias :ncnt :getncnt
250
+
251
+ # Get the number of processes waiting for a semaphore to become
252
+ # zero. See semctl(2).
253
+
254
+ def getzcnt
255
+ semctl(@semid, 0, GETZCNT)
256
+ end
257
+ alias :zcnt :getzcnt
258
+
259
+ # Return the Semid_ds object. See semctl(2).
260
+
261
+ def ipc_stat
262
+ res, semid_ds = semctl(@semid, 0, IPC_STAT)
263
+ check_result(res)
264
+ semid_ds
265
+ end
266
+ alias :semid_ds :ipc_stat
267
+
268
+ # Set the Semid_ds object. See semctl(2).
269
+
270
+ def ipc_set(semid_ds)
271
+ unless Semid_ds === semid_ds
272
+ raise ArgumentError,
273
+ "argument to ipc_set must be a Semid_ds"
274
+ end
275
+ check_result(semctl(@semid, 0, IPC_SET, semid_ds))
276
+ end
277
+ alias :semid_ds= :ipc_set
278
+
279
+ # Remove. See semctl(2).
280
+
281
+ def ipc_rmid
282
+ check_result(semctl(@semid, 0, IPC_RMID))
283
+ end
284
+ alias :rm :ipc_rmid
285
+
286
+ # Perform a set of semaphore operations. The argument +array+ is
287
+ # an Array of Sembuf objects. See semop(2).
288
+
289
+ def op(array)
290
+ check_result(semop(@semid, array, array.length))
291
+ end
292
+
293
+ end
294
+
295
+ class SharedMemory
296
+
297
+ private
298
+
299
+ # Return a SharedMemory object encapsulating a
300
+ # shared memory segment of +size+ bytes associated with
301
+ # +key+. See shmget(2).
302
+
303
+ def initialize(key, size, flags = 0)
304
+ @shmid = shmget(key, size, flags)
305
+ check_result(@shmid)
306
+ end
307
+
308
+ public
309
+
310
+ # Return the Shmid_ds object. See shmctl(2).
311
+
312
+ def ipc_stat
313
+ res, shmid_ds = shmctl(@shmid, IPC_STAT)
314
+ check_result(res)
315
+ shmid_ds
316
+ end
317
+ alias :shmid_ds :ipc_stat
318
+
319
+ # Set the Shmid_ds object. See shmctl(2).
320
+
321
+ def ipc_set(shmid_ds)
322
+ unless Shmid_ds === shmid_ds
323
+ raise ArgumentError,
324
+ "argument to ipc_set must be a Shmid_ds"
325
+ end
326
+ check_result(shmctl(@shmid, IPC_SET, shmid_ds))
327
+ end
328
+ alias shmid_ds= :ipc_set
329
+
330
+ # Remove. See shmctl(2).
331
+
332
+ def ipc_rmid
333
+ check_result(shmctl(@shmid, IPC_RMID, nil))
334
+ end
335
+ alias :rm :ipc_rmid
336
+
337
+ # Attach to a shared memory address object and return it.
338
+ # See shmat(2). If +shmaddr+ is nil, the shared memory is attached
339
+ # at the first available address as selected by the system. See
340
+ # shmat(2).
341
+
342
+ def attach(shmaddr = nil, flags = 0)
343
+ shmaddr = shmat(@shmid, shmaddr, flags)
344
+ check_result(shmaddr)
345
+ shmaddr
346
+ end
347
+
348
+ # Detach the +Shmaddr+ object +shmaddr+. See shmdt(2).
349
+
350
+ def detach(shmaddr)
351
+ check_result(shmdt(shmaddr))
352
+ end
353
+
354
+ end
355
+
356
+ class Shmaddr
357
+
358
+ # Write the string +text+ to offset +offset+.
359
+
360
+ def write(text, offset = 0)
361
+ shmwrite(self, text, offset)
362
+ end
363
+ alias :<< :write
364
+
365
+ # Read +len+ bytes at offset +offset+ and return them in a String.
366
+
367
+ def read(len, offset = 0)
368
+ shmread(self, len, offset)
369
+ end
370
+
371
+ end
372
+
373
+ end
Binary file
@@ -0,0 +1,3 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/sysvipc'
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestSysvipc < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_truth
9
+ assert true
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sysvipc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - name
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-25 00:00:00 +08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.3.2
24
+ version:
25
+ description: FIX (describe your package)
26
+ email:
27
+ - email
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files: []
33
+
34
+ files:
35
+ - README.rdoc
36
+ - lib/sysvipc/sysvipc.so
37
+ - lib/sysvipc.rb
38
+ - Rakefile
39
+ has_rdoc: true
40
+ homepage: http://github.com/#{github_username}/#{project_name}
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --main
46
+ - README.rdoc
47
+ require_paths:
48
+ - lib
49
+ - ext
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ requirements: []
63
+
64
+ rubyforge_project: sysvipc
65
+ rubygems_version: 1.3.5
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: FIX (describe your package)
69
+ test_files:
70
+ - test/test_sysvipc.rb
71
+ - test/test_helper.rb