win32-event 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES ADDED
@@ -0,0 +1,40 @@
1
+ == 0.5.0 - 3-May-2007
2
+ * Now pure Ruby.
3
+ * Both the Event.new and Event.close methods now accept a block, and
4
+ automatically close the associated handle at the end of the block.
5
+ * The Event.new method now accepts an optional fourth argument that controls
6
+ whether the Event object can be inherited by other processes.
7
+ * Added a gemspec.
8
+ * Added a Rakefile, including tasks for installation and testing.
9
+ * Removed the doc/event.txt file. The documentation is now inlined via RDoc.
10
+ There is also some documentation in the README file.
11
+ * Now requires the windows-pr package.
12
+
13
+ == 0.4.0 - 28-May-2005
14
+ * All methods now return self or klass (instead of true).
15
+ * Added the Event#inheritable? attribute that stores whether or not the Event
16
+ object is inheritable with regards to CreateProcess().
17
+ * The Event#set and Event#unset now properly set the Event#signaled? value.
18
+ * More Unicode friendly.
19
+ * Removed the event.rd file. The event.txt file is now rdoc friendly.
20
+ * Code cleanup.
21
+
22
+ == 0.3.1 - 1-Mar-2005
23
+ * Moved the 'examples' directory to the toplevel directory.
24
+ * Made the CHANGES and README files rdoc friendly.
25
+
26
+ == 0.3.0 - 17-Jul-2004
27
+ * Now uses the newer allocation framework, as well as replace the deprecated
28
+ STR2CSTR() function with StringValuePtr(). This means that, as of this
29
+ release, Ruby 1.8.0 or later is required.
30
+ * Removed the .html file as part of the distro. You can generate this on your
31
+ own via rd2.
32
+ * Moved the test.rb script to doc/examples.
33
+
34
+ == 0.2.0 - 28-Apr-2004
35
+ * The Event class is now a subclass of Ipc (and thus requires the win32-ipc
36
+ package).
37
+ * Documentation updates
38
+
39
+ == 0.1.0 - 12-Jan-2004
40
+ - Initial release.
data/MANIFEST ADDED
@@ -0,0 +1,7 @@
1
+ * CHANGES
2
+ * MANIFEST
3
+ * README
4
+ * Rakefile
5
+ * win32-event.gemspec
6
+ * lib/win32/event.rb
7
+ * test/tc_event.rb
data/README ADDED
@@ -0,0 +1,52 @@
1
+ == Description
2
+ Interface to MS Windows Event objects.
3
+
4
+ == Prerequsites
5
+ Requires the win32-ipc package.
6
+
7
+ == Installation
8
+ rake test (optional)
9
+ rake install
10
+
11
+ == Synopsis
12
+ Win32::Event.new("Foo") do |event|
13
+ event.set
14
+ # Do stuff
15
+ event.reset
16
+ end
17
+
18
+ e2 = Win32::Event.open("Bar")
19
+ # Do stuff
20
+ e2.close
21
+
22
+ == Documentation
23
+ The event.rb file contains inline RDoc documentation. If you installed
24
+ this file as a gem, then you have the docs.
25
+
26
+ == Notes
27
+ The Event class is a subclass of Win32::Ipc (win32-ipc). This package
28
+ require's the win32-ipc package internally (you don't need to explicitly
29
+ call it).
30
+
31
+ A PulseEvent() wrapper is intentionally omitted. From the MSDN web site:
32
+ "This function is unreliable and should not be used. It exists mainly for
33
+ backward compatibility."
34
+
35
+ == Acknowledgements
36
+ The Win32::Event Perl module by Chris Madsen was used as a general
37
+ guideline for developing the API.
38
+
39
+ == Known Bugs
40
+ None that I'm aware of. Please submit any bug reports to the project page
41
+ at http://www.rubyforge.org/projects/win32utils.
42
+
43
+ == Copyright
44
+ (C) 2003-2007 Daniel J. Berger
45
+ All Rights Reserved
46
+
47
+ == License
48
+ Ruby's
49
+
50
+ == Author
51
+ Park Heesob
52
+ Daniel J. Berger
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rbconfig'
4
+ include Config
5
+
6
+ desc 'Install the win32-event package (non-gem)'
7
+ task :install do
8
+ sitelibdir = CONFIG['sitelibdir']
9
+ installdir = File.join(sitelibdir, 'win32')
10
+ file = 'lib\win32\event.rb'
11
+
12
+ Dir.mkdir(installdir) unless File.exists?(installdir)
13
+ FileUtils.cp(file, installdir, :verbose => true)
14
+ end
15
+
16
+ Rake::TestTask.new do |t|
17
+ t.libs << 'test'
18
+ t.verbose = true
19
+ t.warning = true
20
+ t.test_files = FileList['test/tc_event.rb']
21
+ end
@@ -0,0 +1,157 @@
1
+ require 'win32/ipc'
2
+
3
+ module Win32
4
+ class Event < Ipc
5
+
6
+ # This is the error raised if any of the Event methods fail.
7
+ class Error < StandardError; end
8
+
9
+ extend Windows::Synchronize
10
+ extend Windows::Error
11
+ extend Windows::Handle
12
+
13
+ VERSION = '0.5.0'
14
+
15
+ # The name of the Event object.
16
+ #
17
+ attr_reader :name
18
+
19
+ # Indicates whether or not the Event requires use of the ResetEvent()
20
+ # function set the state to nonsignaled.
21
+ #
22
+ attr_reader :manual_reset
23
+
24
+ # The initial state of the Event object. If true, the initial state
25
+ # is signaled. Otherwise, it is non-signaled.
26
+ #
27
+ attr_reader :initial_state
28
+
29
+ # Creates and returns new Event object. If +name+ is omitted, the
30
+ # Event object is created without a name, i.e. it's anonymous.
31
+ #
32
+ # If +name+ is provided and it already exists, then it is opened
33
+ # instead and the +manual_reset+ and +initial_state+ parameters are
34
+ # ignored.
35
+ #
36
+ # If the +man_reset+ parameter is set to +true+, then it creates an Event
37
+ # object which requires use of the Event#reset method in order to set the
38
+ # state to non-signaled. If this parameter is false (the default) then
39
+ # the system automatically resets the state to non-signaled after a
40
+ # single waiting thread has been released.
41
+ #
42
+ # If the +init_state+ parameter is +true+, the initial state of the
43
+ # Event object is signaled; otherwise, it is nonsignaled (the default).
44
+ #
45
+ # If the +inherit+ parameter is true, then processes created by this
46
+ # process will inherit the handle. Otherwise they will not.
47
+ #
48
+ # In block form this will automatically close the Event object at the
49
+ # end of the block.
50
+ #
51
+ def initialize(name=nil, man_reset=false, init_state=false, inherit=true)
52
+ @name = name
53
+ @manual_reset = man_reset
54
+ @initial_state = init_state
55
+ @inherit = inherit
56
+
57
+ manual_reset = man_reset ? 1 : 0
58
+ initial_state = init_state ? 1 : 0
59
+
60
+ # Used to prevent potential segfaults.
61
+ if name && !name.is_a?(String)
62
+ raise TypeError, 'name must be a string'
63
+ end
64
+
65
+ if inherit
66
+ sec = 0.chr * 12 # sizeof(SECURITY_ATTRIBUTES)
67
+ sec[0,4] = [12].pack('L')
68
+ sec[8,4] = [1].pack('L') # 1 == TRUE
69
+ else
70
+ sec = 0
71
+ end
72
+
73
+ handle = CreateEvent(sec, manual_reset, initial_state, name)
74
+
75
+ if handle == 0 || handle == INVALID_HANDLE_VALUE
76
+ raise Error, get_last_error
77
+ end
78
+
79
+ super(handle)
80
+
81
+ if block_given?
82
+ begin
83
+ yield self
84
+ ensure
85
+ close
86
+ end
87
+ end
88
+ end
89
+
90
+ # Open an existing Event by +name+. The +inherit+ argument sets whether
91
+ # or not the object was opened such that a process created by the
92
+ # CreateProcess() function (a Windows API function) can inherit the
93
+ # handle. The default is true.
94
+ #
95
+ # This method is essentially identical to Event.new, except that the
96
+ # options for manual_reset and initial_state cannot be set (since they
97
+ # are already set). Also, this method will raise an Event::Error if the
98
+ # event doesn't already exist.
99
+ #
100
+ # If you want "open or create" semantics, then use Event.new.
101
+ #--
102
+ # The OpenEvent() call here is strictly to force an error if the user
103
+ # tries to open an event that doesn't already exist.
104
+ #
105
+ def self.open(name, inherit=true, &block)
106
+ if name && !name.is_a?(String)
107
+ raise TypeError, 'name must be a string'
108
+ end
109
+
110
+ bool = inherit ? 1 : 0
111
+ handle = OpenEvent(EVENT_ALL_ACCESS, bool, name)
112
+ if handle == 0 || handle == INVALID_HANDLE_VALUE
113
+ raise Error, get_last_error
114
+ end
115
+ CloseHandle(handle)
116
+
117
+ self.new(name, false, false, inherit, &block)
118
+ end
119
+
120
+ # Returns whether or not the object was opened such that a process
121
+ # created by the CreateProcess() function (a Windows API function) can
122
+ # inherit the handle. The default is true.
123
+ #
124
+ def inheritable?
125
+ @inherit
126
+ end
127
+
128
+ # Sets the Event object to a non-signaled state.
129
+ #
130
+ def reset
131
+ unless ResetEvent(@handle)
132
+ raise Error, get_last_error
133
+ end
134
+ @signaled = false
135
+ end
136
+
137
+ # Sets the Event object to a signaled state.
138
+ #
139
+ def set
140
+ unless SetEvent(@handle)
141
+ raise Error, get_last_error
142
+ end
143
+ @signaled = true
144
+ end
145
+
146
+ # Synonym for Event#reset if +bool+ is false, or Event#set
147
+ # if +bool+ is true.
148
+ #
149
+ def signaled=(bool)
150
+ if bool
151
+ set
152
+ else
153
+ reset
154
+ end
155
+ end
156
+ end
157
+ end
data/test/tc_event.rb ADDED
@@ -0,0 +1,121 @@
1
+ #####################################################################
2
+ # tc_event.rb
3
+ #
4
+ # Test suite for the win32-event package. This test should be run
5
+ # via the 'rake test' task.
6
+ #####################################################################
7
+ require "test/unit"
8
+ require "win32/event"
9
+ include Win32
10
+
11
+ class TC_Win32Event < Test::Unit::TestCase
12
+ def setup
13
+ @event1 = Event.new
14
+ @event2 = Event.new("Foo")
15
+ @event3 = Event.new("Bar", true)
16
+ @event4 = Event.new("Baz", true, true)
17
+ @uni_event = Event.new("Ηελλας")
18
+ end
19
+
20
+ def test_version
21
+ assert_equal('0.5.0', Event::VERSION)
22
+ end
23
+
24
+ def test_constructor_errors
25
+ assert_raises(ArgumentError){ Event.new("Foo", true, false, true, 1) }
26
+ assert_raises(TypeError){ Event.new(1) }
27
+ end
28
+
29
+ def test_open
30
+ assert_respond_to(Event, :open)
31
+ assert_nothing_raised{ Event.open("Bar"){} }
32
+ assert_nothing_raised{ Event.open("Ηελλας"){} }
33
+ assert_nothing_raised{ Event.open("Bar", false) }
34
+
35
+ assert_raises(ArgumentError){ Event.open("Bar", true, false){} }
36
+ assert_raises(Event::Error){ Event.open("Blah"){} }
37
+ assert_raises(TypeError){ Event.open(1){} }
38
+ end
39
+
40
+ def test_inheritable
41
+ @event1 = Event.open("Foo")
42
+ @event2 = Event.open("Baz", false)
43
+
44
+ assert_respond_to(@event1, :inheritable?)
45
+ assert_nothing_raised{ @event1.inheritable? }
46
+
47
+ assert_equal(true, @event1.inheritable?)
48
+ assert_equal(false, @event2.inheritable?)
49
+ end
50
+
51
+ def test_name
52
+ assert_respond_to(@event1, :name)
53
+ assert_nothing_raised{ @event1.name }
54
+ assert_nothing_raised{ @uni_event.name }
55
+
56
+ assert_nil(@event1.name)
57
+ assert_kind_of(String, @event2.name)
58
+ end
59
+
60
+ def test_initial_state
61
+ assert_respond_to(@event1, :initial_state)
62
+ assert_nothing_raised{ @event1.initial_state }
63
+
64
+ assert_equal(false, @event1.initial_state)
65
+ assert_equal(false, @event2.initial_state)
66
+ assert_equal(false, @event3.initial_state)
67
+ assert_equal(true, @event4.initial_state)
68
+ end
69
+
70
+ def test_manual_reset
71
+ assert_respond_to(@event1, :manual_reset)
72
+ assert_nothing_raised{ @event1.manual_reset }
73
+
74
+ assert_equal(false, @event1.manual_reset)
75
+ assert_equal(false, @event2.manual_reset)
76
+ assert_equal(true, @event3.manual_reset)
77
+ assert_equal(true, @event4.manual_reset)
78
+ end
79
+
80
+ def test_set
81
+ assert_respond_to(@event1, :set)
82
+ assert_nothing_raised{ @event1.set }
83
+ end
84
+
85
+ def test_is_signaled
86
+ event = Event.new
87
+ assert_respond_to(event, :signaled?)
88
+ assert_nothing_raised{ event.signaled? }
89
+
90
+ assert_equal(false, event.signaled?)
91
+ event.set
92
+ assert_equal(true, event.signaled?)
93
+ event.reset
94
+ assert_equal(false, event.signaled?)
95
+ end
96
+
97
+ def test_reset
98
+ assert_respond_to(@event1, :reset)
99
+ assert_nothing_raised{ @event1.reset }
100
+ end
101
+
102
+ def test_close
103
+ event = Event.new
104
+ assert_respond_to(event, :close)
105
+ assert_nothing_raised{ event.close }
106
+ end
107
+
108
+ def teardown
109
+ @event1.close
110
+ @event2.close
111
+ @event3.close
112
+ @event4.close
113
+ @uni_event.close
114
+
115
+ @event1 = nil
116
+ @event2 = nil
117
+ @event3 = nil
118
+ @event4 = nil
119
+ @uni_event = nil
120
+ end
121
+ end
@@ -0,0 +1,24 @@
1
+ require "rubygems"
2
+
3
+ spec = Gem::Specification.new do |gem|
4
+ gem.name = "win32-event"
5
+ gem.version = "0.5.0"
6
+ gem.author = "Daniel J. Berger"
7
+ gem.email = "djberg96@gmail.com"
8
+ gem.homepage = "http://www.rubyforge.org/projects/win32utils"
9
+ gem.platform = Gem::Platform::RUBY
10
+ gem.summary = "Interface to MS Windows Event objects."
11
+ gem.description = "Interface to MS Windows Event objects."
12
+ gem.test_file = "test/tc_event.rb"
13
+ gem.has_rdoc = true
14
+ gem.files = Dir["lib/win32/*.rb"] + Dir["test/*"] + Dir["[A-Z]*"]
15
+ gem.files.reject! { |fn| fn.include? "CVS" }
16
+ gem.require_path = "lib"
17
+ gem.extra_rdoc_files = ["README", "CHANGES", "MANIFEST"]
18
+ gem.add_dependency("win32-ipc", ">= 0.5.0")
19
+ end
20
+
21
+ if $0 == __FILE__
22
+ Gem.manage_gems
23
+ Gem::Builder.new(spec).build
24
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0.8
3
+ specification_version: 1
4
+ name: win32-event
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.5.0
7
+ date: 2007-05-03 00:00:00 -06:00
8
+ summary: Interface to MS Windows Event objects.
9
+ require_paths:
10
+ - lib
11
+ email: djberg96@gmail.com
12
+ homepage: http://www.rubyforge.org/projects/win32utils
13
+ rubyforge_project:
14
+ description: Interface to MS Windows Event objects.
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Daniel J. Berger
31
+ files:
32
+ - lib/win32/event.rb
33
+ - test/CVS
34
+ - test/tc_event.rb
35
+ - CHANGES
36
+ - CVS
37
+ - lib
38
+ - MANIFEST
39
+ - Rakefile
40
+ - README
41
+ - test
42
+ - win32-event.gemspec
43
+ test_files:
44
+ - test/tc_event.rb
45
+ rdoc_options: []
46
+
47
+ extra_rdoc_files:
48
+ - README
49
+ - CHANGES
50
+ - MANIFEST
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ requirements: []
56
+
57
+ dependencies:
58
+ - !ruby/object:Gem::Dependency
59
+ name: win32-ipc
60
+ version_requirement:
61
+ version_requirements: !ruby/object:Gem::Version::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 0.5.0
66
+ version: