pione 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (304) hide show
  1. data/.gitignore +2 -0
  2. data/.s3cfg +42 -0
  3. data/.travis.yml +18 -4
  4. data/.yardopts +1 -0
  5. data/Gemfile +3 -0
  6. data/History.txt +14 -0
  7. data/Procfile +7 -0
  8. data/Rakefile +25 -0
  9. data/bin/pione-interactive +6 -0
  10. data/bin/{pione-compiler → pione-notification-listener} +1 -1
  11. data/bin/{pione-broker → pione-task-worker-broker} +1 -1
  12. data/doc/man/pione-list-param.md +36 -0
  13. data/example/ActionError/ActionError.pione +9 -0
  14. data/example/ActionError/pione-package.json +15 -0
  15. data/example/CountChar/CountChar.pione +1 -1
  16. data/example/ScoreAggregation/ScoreAggregation.pione +1 -1
  17. data/example/Touch/pione-package.json +15 -0
  18. data/lib/pione.rb +12 -0
  19. data/lib/pione/agent.rb +3 -3
  20. data/lib/pione/agent/basic-agent.rb +10 -4
  21. data/lib/pione/agent/input-generator.rb +3 -3
  22. data/lib/pione/agent/{process-manager.rb → job-manager.rb} +10 -7
  23. data/lib/pione/agent/job-terminator.rb +2 -2
  24. data/lib/pione/agent/logger.rb +3 -3
  25. data/lib/pione/agent/messenger.rb +20 -9
  26. data/lib/pione/agent/notification-listener.rb +88 -0
  27. data/lib/pione/agent/task-worker-broker.rb +84 -0
  28. data/lib/pione/agent/task-worker.rb +3 -2
  29. data/lib/pione/agent/tuple-space-client.rb +1 -0
  30. data/lib/pione/agent/tuple-space-provider.rb +17 -35
  31. data/lib/pione/command.rb +33 -15
  32. data/lib/pione/command/action.rb +105 -0
  33. data/lib/pione/command/basic-command.rb +34 -376
  34. data/lib/pione/command/command-exception.rb +12 -11
  35. data/lib/pione/command/common.rb +198 -0
  36. data/lib/pione/command/option.rb +159 -204
  37. data/lib/pione/command/pione-action-exec.rb +85 -0
  38. data/lib/pione/command/pione-action-list.rb +43 -19
  39. data/lib/pione/command/pione-action-print.rb +79 -0
  40. data/lib/pione/command/pione-action.rb +8 -67
  41. data/lib/pione/command/pione-clean.rb +88 -68
  42. data/lib/pione/command/pione-client.rb +475 -332
  43. data/lib/pione/command/pione-command.rb +14 -31
  44. data/lib/pione/command/pione-compile.rb +90 -0
  45. data/lib/pione/command/pione-config-get.rb +53 -0
  46. data/lib/pione/command/pione-config-list.rb +64 -0
  47. data/lib/pione/command/pione-config-set.rb +59 -0
  48. data/lib/pione/command/pione-config-unset.rb +50 -0
  49. data/lib/pione/command/pione-config.rb +61 -0
  50. data/lib/pione/command/pione-diagnosis-notification.rb +235 -0
  51. data/lib/pione/command/pione-diagnosis.rb +21 -0
  52. data/lib/pione/command/pione-interactive.rb +188 -0
  53. data/lib/pione/command/pione-lang-check-syntax.rb +163 -0
  54. data/lib/pione/command/pione-lang-interactive.rb +146 -0
  55. data/lib/pione/command/pione-lang.rb +22 -0
  56. data/lib/pione/command/pione-log-format.rb +163 -0
  57. data/lib/pione/command/pione-log-list-id.rb +48 -0
  58. data/lib/pione/command/pione-log.rb +19 -101
  59. data/lib/pione/command/pione-notification-listener.rb +95 -0
  60. data/lib/pione/command/pione-package-add.rb +137 -0
  61. data/lib/pione/command/pione-package-build.rb +122 -0
  62. data/lib/pione/command/pione-package-show.rb +96 -0
  63. data/lib/pione/command/pione-package-update.rb +59 -0
  64. data/lib/pione/command/pione-package.rb +11 -139
  65. data/lib/pione/command/pione-task-worker-broker.rb +88 -0
  66. data/lib/pione/command/pione-task-worker.rb +148 -98
  67. data/lib/pione/command/pione-tuple-space-provider.rb +62 -54
  68. data/lib/pione/command/pione-tuple-space-viewer.rb +105 -83
  69. data/lib/pione/command/pione-val.rb +39 -39
  70. data/lib/pione/command/spawner.rb +34 -27
  71. data/lib/pione/front.rb +4 -2
  72. data/lib/pione/front/basic-front.rb +86 -23
  73. data/lib/pione/front/client-front.rb +2 -2
  74. data/lib/pione/front/diagnosis-notification-front.rb +40 -0
  75. data/lib/pione/front/front-exception.rb +7 -1
  76. data/lib/pione/front/notification-listener-front.rb +36 -0
  77. data/lib/pione/front/notification-recipient-interface.rb +19 -0
  78. data/lib/pione/front/relay-front.rb +4 -4
  79. data/lib/pione/front/task-worker-broker-front.rb +19 -0
  80. data/lib/pione/front/task-worker-front.rb +2 -2
  81. data/lib/pione/front/tuple-space-provider-front.rb +3 -2
  82. data/lib/pione/global.rb +3 -1
  83. data/lib/pione/global/client-variable.rb +1 -1
  84. data/lib/pione/global/config.rb +63 -7
  85. data/lib/pione/global/diagnosis-variable.rb +26 -0
  86. data/lib/pione/global/item.rb +28 -5
  87. data/lib/pione/global/log-variable.rb +6 -5
  88. data/lib/pione/global/network-variable.rb +75 -4
  89. data/lib/pione/global/path-variable.rb +1 -1
  90. data/lib/pione/global/system-variable.rb +12 -12
  91. data/lib/pione/global/task-worker-broker-variable.rb +43 -0
  92. data/lib/pione/global/tuple-space-notifier-variable.rb +3 -55
  93. data/lib/pione/lang/boolean.rb +4 -0
  94. data/lib/pione/lang/data-expr.rb +1 -1
  95. data/lib/pione/lang/integer.rb +1 -1
  96. data/lib/pione/lang/string.rb +4 -0
  97. data/lib/pione/lang/type.rb +1 -1
  98. data/lib/pione/location.rb +1 -0
  99. data/lib/pione/location/data-location.rb +41 -13
  100. data/lib/pione/location/dropbox-location.rb +175 -56
  101. data/lib/pione/location/ftp-location.rb +4 -1
  102. data/lib/pione/location/http-location.rb +5 -3
  103. data/lib/pione/location/https-location.rb +4 -2
  104. data/lib/pione/location/local-location.rb +6 -3
  105. data/lib/pione/location/location-exception.rb +12 -0
  106. data/lib/pione/location/notification-scheme.rb +46 -0
  107. data/lib/pione/log.rb +8 -7
  108. data/lib/pione/log/debug.rb +9 -9
  109. data/lib/pione/log/domain-log.rb +6 -1
  110. data/lib/pione/log/message-log-receiver.rb +32 -0
  111. data/lib/pione/log/system-log.rb +62 -141
  112. data/lib/pione/model.rb +7 -0
  113. data/lib/pione/model/notification-listener-model.rb +29 -0
  114. data/lib/pione/model/task-worker-broker-model.rb +129 -0
  115. data/lib/pione/notification.rb +13 -0
  116. data/lib/pione/notification/address.rb +104 -0
  117. data/lib/pione/notification/exception.rb +10 -0
  118. data/lib/pione/notification/message.rb +109 -0
  119. data/lib/pione/notification/receiver.rb +90 -0
  120. data/lib/pione/notification/recipient.rb +68 -0
  121. data/lib/pione/notification/task-worker-broker-recipient.rb +85 -0
  122. data/lib/pione/notification/transmitter.rb +84 -0
  123. data/lib/pione/pnml.rb +35 -0
  124. data/lib/pione/pnml/annotation-extractor.rb +47 -0
  125. data/lib/pione/pnml/compiler.rb +176 -0
  126. data/lib/pione/pnml/input-merge-complement.rb +78 -0
  127. data/lib/pione/pnml/input-parallelization-complement.rb +75 -0
  128. data/lib/pione/pnml/input-reduction.rb +80 -0
  129. data/lib/pione/pnml/invalid-arc-elimination.rb +41 -0
  130. data/lib/pione/pnml/io-expansion.rb +88 -0
  131. data/lib/pione/pnml/isolated-element-elimination.rb +32 -0
  132. data/lib/pione/pnml/net-rewriter.rb +44 -0
  133. data/lib/pione/pnml/output-decomposition-complement.rb +77 -0
  134. data/lib/pione/pnml/output-reduction.rb +86 -0
  135. data/lib/pione/pnml/output-synchronization-complement.rb +77 -0
  136. data/lib/pione/pnml/pione-model.rb +417 -0
  137. data/lib/pione/pnml/pnml-exception.rb +23 -0
  138. data/lib/pione/pnml/pnml-model.rb +368 -0
  139. data/lib/pione/pnml/reader.rb +51 -0
  140. data/lib/pione/rule-engine/action-handler.rb +8 -6
  141. data/lib/pione/rule-engine/basic-handler.rb +5 -5
  142. data/lib/pione/rule-engine/engine-exception.rb +7 -6
  143. data/lib/pione/rule-engine/flow-handler.rb +27 -18
  144. data/lib/pione/system.rb +3 -1
  145. data/lib/pione/system/domain-dump.rb +34 -0
  146. data/lib/pione/system/file-cache.rb +92 -13
  147. data/lib/pione/system/init.rb +3 -0
  148. data/lib/pione/system/normalizer.rb +40 -0
  149. data/lib/pione/system/status.rb +5 -5
  150. data/lib/pione/system/system-exception.rb +14 -1
  151. data/lib/pione/task-worker-broker.rb +7 -0
  152. data/lib/pione/task-worker-broker/basic-provider.rb +20 -0
  153. data/lib/pione/task-worker-broker/easy-provider.rb +65 -0
  154. data/lib/pione/test-helper.rb +27 -0
  155. data/lib/pione/test-helper/command-helper.rb +9 -101
  156. data/lib/pione/tuple-space/basic-tuple.rb +1 -1
  157. data/lib/pione/tuple-space/tuple-definition.yml +4 -6
  158. data/lib/pione/tuple-space/tuple-space-interface.rb +3 -45
  159. data/lib/pione/tuple-space/tuple-space-server.rb +45 -0
  160. data/lib/pione/util.rb +1 -2
  161. data/lib/pione/util/boolean-value.rb +62 -0
  162. data/lib/pione/util/completion.rb +111 -0
  163. data/lib/pione/util/evaluatable.rb +13 -27
  164. data/lib/pione/util/package-parameters-list.rb +15 -27
  165. data/lib/pione/util/zip.rb +8 -3
  166. data/lib/pione/version.rb +1 -1
  167. data/lib/rootage.rb +20 -0
  168. data/lib/rootage/action.rb +114 -0
  169. data/lib/rootage/argument.rb +46 -0
  170. data/lib/rootage/command.rb +218 -0
  171. data/lib/rootage/core.rb +532 -0
  172. data/lib/rootage/exception.rb +107 -0
  173. data/lib/rootage/help.rb +148 -0
  174. data/lib/rootage/help.txt.erb +31 -0
  175. data/lib/rootage/log.rb +226 -0
  176. data/lib/rootage/normalizer.rb +184 -0
  177. data/lib/rootage/option.rb +152 -0
  178. data/lib/rootage/scenario-test-result.erb +39 -0
  179. data/lib/rootage/scenario.rb +362 -0
  180. data/lib/rootage/test-helper.rb +115 -0
  181. data/man/pione-list-param.1 +44 -0
  182. data/misc/clock.rb +9 -0
  183. data/misc/machine-info.sh +21 -0
  184. data/misc/pione-completion.bash +238 -0
  185. data/misc/pione-completion.erb +53 -0
  186. data/misc/pione-completion.zsh +238 -0
  187. data/misc/pione.god +22 -0
  188. data/misc/ui.xml +23 -0
  189. data/pione.gemspec +3 -1
  190. data/test/agent/spec_basic-agent.rb +1 -1
  191. data/test/agent/spec_input-generator.rb +2 -2
  192. data/test/agent/spec_messenger.rb +6 -9
  193. data/test/agent/spec_notification-listener.rb +80 -0
  194. data/test/agent/{spec_broker.rb → spec_task-worker-broker.rb} +13 -10
  195. data/test/agent/spec_tuple-space-provider.rb +10 -6
  196. data/test/command/command-behavior.rb +3 -11
  197. data/test/command/data/pione-list-param/AdvancedParameters.pione +12 -0
  198. data/test/command/data/pione-list-param/BasicParameters.pione +12 -0
  199. data/test/command/spec_pione-action-exec.rb +16 -0
  200. data/test/command/spec_pione-action-list.rb +15 -10
  201. data/test/command/spec_pione-action-print.rb +14 -0
  202. data/test/command/spec_pione-action.rb +6 -19
  203. data/test/command/spec_pione-clean.rb +29 -46
  204. data/test/command/spec_pione-client.rb +29 -36
  205. data/test/command/spec_pione-command.rb +6 -6
  206. data/test/command/{spec_pione-compiler.rb → spec_pione-compile.rb} +11 -13
  207. data/test/command/spec_pione-config-get.rb +47 -0
  208. data/test/command/spec_pione-config-list.rb +42 -0
  209. data/test/command/spec_pione-config-set.rb +38 -0
  210. data/test/command/spec_pione-config-unset.rb +44 -0
  211. data/test/command/spec_pione-config.rb +11 -0
  212. data/test/command/spec_pione-diagnosis-notification.rb +23 -0
  213. data/test/command/spec_pione-diagnosis.rb +11 -0
  214. data/test/command/spec_pione-lang-check-syntax.rb +12 -0
  215. data/test/command/spec_pione-lang.rb +11 -0
  216. data/test/command/spec_pione-log-format.rb +29 -0
  217. data/test/command/spec_pione-log-list-id.rb +17 -0
  218. data/test/command/spec_pione-log.rb +6 -20
  219. data/test/command/spec_pione-package-add.rb +55 -0
  220. data/test/command/spec_pione-package-build.rb +57 -0
  221. data/test/command/spec_pione-package-show.rb +72 -0
  222. data/test/command/{spec_pione-update-package-info.rb → spec_pione-package-update.rb} +12 -13
  223. data/test/command/spec_pione-package.rb +4 -104
  224. data/test/command/spec_pione-val.rb +10 -7
  225. data/test/global/spec_config.rb +50 -0
  226. data/test/global/spec_item.rb +1 -1
  227. data/test/literate-action/data/HelloWorld.md +1 -1
  228. data/test/location/location-behavior.rb +1 -1
  229. data/test/location/spec_dropbox-location.rb +39 -0
  230. data/test/location/spec_notification-scheme.rb +37 -0
  231. data/test/log/spec_debug.rb +5 -4
  232. data/test/log/spec_message-log-receiver.rb +13 -0
  233. data/test/log/spec_message-log.rb +6 -9
  234. data/test/log/spec_system-log.rb +5 -3
  235. data/test/notification/spec_address.rb +229 -0
  236. data/test/notification/spec_message.rb +30 -0
  237. data/test/notification/spec_receiver.rb +36 -0
  238. data/test/notification/spec_transmitter.rb +37 -0
  239. data/test/pnml/data/ConditionalBranchIf.pnml +270 -0
  240. data/test/pnml/data/ConditionalBranchIfElse.pnml +309 -0
  241. data/test/pnml/data/IOExpansionComplex.pnml +363 -0
  242. data/test/pnml/data/IOExpansionSimple.pnml +140 -0
  243. data/test/pnml/data/InputMergeComplementComplex.pnml +381 -0
  244. data/test/pnml/data/InputMergeComplementSimple.pnml +248 -0
  245. data/test/pnml/data/InputParallelizationComplementComplex.pnml +433 -0
  246. data/test/pnml/data/InputParallelizationComplementSimple.pnml +288 -0
  247. data/test/pnml/data/InputReductionComplex.pnml +192 -0
  248. data/test/pnml/data/InputReductionLong.pnml +344 -0
  249. data/test/pnml/data/InputReductionSimple.pnml +140 -0
  250. data/test/pnml/data/IsolatedElementElimination.pnml +171 -0
  251. data/test/pnml/data/OutputDecompositionComplementComplex.pnml +381 -0
  252. data/test/pnml/data/OutputDecompositionComplementSimple.pnml +242 -0
  253. data/test/pnml/data/OutputReductionComplex.pnml +186 -0
  254. data/test/pnml/data/OutputReductionLong.pnml +344 -0
  255. data/test/pnml/data/OutputReductionSimple.pnml +140 -0
  256. data/test/pnml/data/OutputSynchronizationComplementComplex.pnml +498 -0
  257. data/test/pnml/data/OutputSynchronizationComplementSimple.pnml +347 -0
  258. data/test/pnml/data/SampleNet.pnml +238 -0
  259. data/test/pnml/spec_input-merge-complement.rb +40 -0
  260. data/test/pnml/spec_input-parallelization-complement.rb +50 -0
  261. data/test/pnml/spec_input-reduction.rb +113 -0
  262. data/test/pnml/spec_invalid-arc-elimination.rb +33 -0
  263. data/test/pnml/spec_io-expansion.rb +126 -0
  264. data/test/pnml/spec_isolated-element-elimination.rb +25 -0
  265. data/test/pnml/spec_output-decomposition-complement.rb +40 -0
  266. data/test/pnml/spec_output-reduction.rb +114 -0
  267. data/test/pnml/spec_output-synchronization-complement.rb +62 -0
  268. data/test/pnml/spec_pione-element.rb +144 -0
  269. data/test/pnml/spec_pnml-element.rb +373 -0
  270. data/test/pnml/spec_reader.rb +16 -0
  271. data/test/rootage/spec_argument.rb +18 -0
  272. data/test/rootage/spec_command.rb +239 -0
  273. data/test/rootage/spec_core.rb +198 -0
  274. data/test/rootage/spec_scenario.rb +149 -0
  275. data/test/system/{spec_domain-info.rb → spec_domain-dump.rb} +6 -6
  276. data/test/system/spec_file-cache.rb +6 -9
  277. data/test/tuple-space/spec_finished-tuple.rb +1 -1
  278. data/test/util/{spec_package-parameters-list_1.pione → data/package-parameters-list/Param1.pione} +0 -0
  279. data/test/util/{spec_package-parameters-list_2.pione → data/package-parameters-list/Param2.pione} +0 -0
  280. data/test/util/{spec_package-parameters-list_3.pione → data/package-parameters-list/Param3.pione} +0 -0
  281. data/test/util/{spec_package-parameters-list_4.pione → data/package-parameters-list/Param4.pione} +0 -0
  282. data/test/util/spec_boolean-value.rb +32 -0
  283. data/test/util/spec_completion.rb +22 -0
  284. data/test/util/spec_package-parameters-list.rb +39 -52
  285. data/test/util/spec_zip.rb +28 -1
  286. metadata +288 -47
  287. data/bin/pione-tuple-space-receiver +0 -5
  288. data/lib/pione/agent/broker.rb +0 -304
  289. data/lib/pione/agent/tuple-space-receiver.rb +0 -137
  290. data/lib/pione/command/pione-broker.rb +0 -104
  291. data/lib/pione/command/pione-compiler.rb +0 -57
  292. data/lib/pione/command/pione-relay-account-db.rb +0 -141
  293. data/lib/pione/command/pione-relay-client-db.rb +0 -118
  294. data/lib/pione/command/pione-relay.rb +0 -59
  295. data/lib/pione/command/pione-syntax-checker.rb +0 -214
  296. data/lib/pione/command/pione-tuple-space-receiver.rb +0 -111
  297. data/lib/pione/command/pione-update-package-info.rb +0 -53
  298. data/lib/pione/front/broker-front.rb +0 -22
  299. data/lib/pione/front/tuple-space-receiver-front.rb +0 -11
  300. data/lib/pione/global/broker-variable.rb +0 -33
  301. data/lib/pione/system/domain-info.rb +0 -25
  302. data/lib/pione/util/pnml-compiler.rb +0 -168
  303. data/test/agent/spec_tuple-space-receiver.rb +0 -47
  304. data/test/util/spec_pnml-compiler.rb +0 -32
@@ -0,0 +1,7 @@
1
+ module Pione
2
+ # `Pione::Model` is a namespace for all command models.
3
+ module Model; end
4
+ end
5
+
6
+ require 'pione/model/notification-listener-model'
7
+ require 'pione/model/task-worker-broker-model'
@@ -0,0 +1,29 @@
1
+ module Pione
2
+ module Model
3
+ # NotificationListenerModel is a model for pione-notification-listener. This
4
+ # manages recipiants lifecycle.
5
+ class NotificationListenerModel < Rootage::Model
6
+ attr_reader :recipients
7
+
8
+ def initialize
9
+ super
10
+ @recipients = []
11
+ @lock = Mutex.new
12
+ end
13
+
14
+ # Add the URI of recipient.
15
+ def add_recipient(uri)
16
+ @lock.synchronize do
17
+ unless @recipients.include?(uri)
18
+ @recipients << uri
19
+ end
20
+ end
21
+ end
22
+
23
+ # Delete the recipients.
24
+ def delete_recipient(*uris)
25
+ @lock.synchronize {uris.each {|uri| @recipients.delete(uri)}}
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,129 @@
1
+ module Pione
2
+ module Model
3
+ class TaskWorkerBrokerModel < Rootage::Model
4
+ attr_accessor :task_workers
5
+ attr_accessor :tuple_space
6
+ attr_reader :tuple_space_lock # lock for tuple space table
7
+
8
+ def initialize
9
+ super
10
+
11
+ @spawnings = 0 # number of current spawning task worker
12
+ @task_worker_lock = Monitor.new # lock for task worker table
13
+ @task_workers = Array.new # known task worker fronts
14
+ @tuple_space_lock = Monitor.new
15
+ @tuple_space = Hash.new # known tuple space table
16
+
17
+ self[:spawn_task_worker] = true
18
+ end
19
+
20
+ # Add the tuple space.
21
+ def add_tuple_space(tuple_space)
22
+ uuid = tuple_space.uuid
23
+
24
+ # update tuple space table with the id
25
+ @tuple_space_lock.synchronize {@tuple_space[uuid] = tuple_space}
26
+ end
27
+
28
+ # Create a task worker for the tuple space. This method returns true if we
29
+ # suceeds to spawn the task worker, or returns false.
30
+ def create_task_worker(tuple_space)
31
+ res = true
32
+
33
+ @task_worker_lock.synchronize do
34
+ @spawnings += 1
35
+
36
+ # spawn a new process of pione-task-worker command
37
+ if self[:spawn_task_worker]
38
+ begin
39
+ spawner = Command::PioneTaskWorker.spawn(self, Global.features, tuple_space.uuid)
40
+ @task_workers << spawner.child_front
41
+ spawner.when_terminated {delete_task_worker(spawner.child_front)}
42
+ rescue Command::SpawnError => e
43
+ Log::Debug.system("Task worker broker agent failed to spawn a task worker: %e" % e.message)
44
+ res = false
45
+ end
46
+ else
47
+ @task_workers << Agent::TaskWorker.start(tuple_space, Global.expressional_features, @env)
48
+ end
49
+
50
+ @spawnings -= 1
51
+ end
52
+
53
+ return res
54
+ end
55
+
56
+ def delete_task_worker(worker)
57
+ @task_worker_lock.synchronize {@task_workers.delete(worker)}
58
+ end
59
+
60
+ # Delete all dead task workers.
61
+ def delete_dead_task_workers
62
+ @task_worker_lock.synchronize do
63
+ @task_workers.delete_if do |worker|
64
+ not(Util.ignore_exception {timeout(1) {worker.ping}})
65
+ end
66
+ end
67
+ end
68
+
69
+ # Delete all dead tuple spaces.
70
+ def delete_dead_tuple_spaces
71
+ @tuple_space_lock.synchronize do
72
+ @tuple_space.delete_if do |_, space|
73
+ not(Util.ignore_exception {timeout(1) {space.ping}})
74
+ end
75
+ end
76
+ end
77
+
78
+ # Return excess number of task workers belong to the broker.
79
+ def excess_task_workers
80
+ @task_worker_lock.synchronize do
81
+ @task_worker_size - @task_workers.size - @spawnings
82
+ end
83
+ end
84
+
85
+ # Get the tuple space.
86
+ def get_tuple_space(tuple_space_id)
87
+ @tuple_space_lock.synchronize {@tuple_space[tuple_space_id]}
88
+ end
89
+
90
+ # Return number of task workers the broker manages.
91
+ def quantity
92
+ @task_worker_lock.synchronize {@task_workers.size}
93
+ end
94
+
95
+ # Terminate first task worker that satisfies the condition.
96
+ def terminate_task_worker_if(&condition)
97
+ @task_worker_lock.synchronize do
98
+ @task_workers.each do |worker|
99
+ if condition.call(worker)
100
+ worker.terminate
101
+ @task_workers.delete(worker)
102
+ return true
103
+ end
104
+ end
105
+ end
106
+ return false
107
+ end
108
+
109
+ # Return known tuple spaces.
110
+ def tuple_spaces
111
+ @tuple_space_lock.synchronize {@tuple_space.values}
112
+ end
113
+
114
+ # Update tuple space list.
115
+ def update_tuple_spaces(tuple_spaces)
116
+ @tuple_space_lock.synchronize do
117
+ # clear and update tuple space list
118
+ @tuple_space.clear
119
+ tuple_spaces.each {|tuple_space| add_tuple_space(tuple_space)}
120
+
121
+ Log::Debug.system do
122
+ list = @tuple_space.values.map{|space| space.__drburi}
123
+ "Task worker broker has updated tuple space table: %s" % [list]
124
+ end
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,13 @@
1
+ module Pione
2
+ # `Pione::Notification` is a namespace for notification related classes.
3
+ module Notification
4
+ end
5
+ end
6
+
7
+ require 'pione/notification/exception'
8
+ require 'pione/notification/message'
9
+ require 'pione/notification/address'
10
+ require 'pione/notification/receiver'
11
+ require 'pione/notification/transmitter'
12
+ require 'pione/notification/recipient'
13
+ require 'pione/notification/task-worker-broker-recipient'
@@ -0,0 +1,104 @@
1
+ module Pione
2
+ module Notification
3
+ # Nofitication::Address provides utility methods for notification addresses.
4
+ module Address
5
+ MULTICAST_ADDRESS = IPAddr.new("224.0.0.0/4")
6
+
7
+ class << self
8
+ # Convert the target address to URI.
9
+ def target_address_to_uri(address)
10
+ address_to_uri(
11
+ address,
12
+ Global.default_notification_target_host,
13
+ Global.default_notification_target_port
14
+ )
15
+ end
16
+
17
+ # Convert the receiver address to URI.
18
+ def receiver_address_to_uri(address)
19
+ address_to_uri(
20
+ address,
21
+ Global.default_notification_receiver_host,
22
+ Global.default_notification_receiver_port
23
+ )
24
+ end
25
+
26
+ # Return default target address.
27
+ def default_target_address
28
+ host_and_port_to_uri(
29
+ Global.default_notification_target_host,
30
+ Global.default_notification_target_port
31
+ )
32
+ end
33
+
34
+ # Return default receiver address.
35
+ def default_receiver_address
36
+ host_and_port_to_uri(
37
+ Global.default_notification_receiver_host,
38
+ Global.default_notification_receiver_port
39
+ )
40
+ end
41
+
42
+ private
43
+
44
+ # Convert the address to URI.
45
+ def address_to_uri(address, default_host, default_port)
46
+ uri = URI.parse(address)
47
+ unless ["pnb", "pnm", "pnu"].include?(uri.scheme)
48
+ raise URI::InvalidURIError
49
+ end
50
+ uri.host = default_host if uri.host == "."
51
+ uri.port = default_port if uri.port.nil?
52
+ return uri
53
+ rescue URI::InvalidURIError
54
+ if port_only?(address)
55
+ return port_to_uri(default_host, address)
56
+ end
57
+
58
+ if host_only?(address)
59
+ return host_to_uri(default_port, address)
60
+ end
61
+
62
+ host_and_port_to_uri(*address.split(":"))
63
+ end
64
+
65
+ # Return true if the address contains port only.
66
+ def port_only?(address)
67
+ address[0] == ":"
68
+ end
69
+
70
+ # Return true if the address conatains host only.
71
+ def host_only?(address)
72
+ not(address.include?(":"))
73
+ end
74
+
75
+ # Return true if the host is a multicast address.
76
+ def multicast?(host)
77
+ _host = IPSocket.getaddress(host)
78
+ MULTICAST_ADDRESS.include?(_host)
79
+ end
80
+
81
+ # Convert the host to URI. The scheme assumes "pnb" excluding multicast
82
+ # addresses.
83
+ def host_to_uri(default_port, host)
84
+ scheme = multicast?(host) ? "pnm" : "pnb"
85
+ URI.parse("%s://%s:%s" % [scheme, host, default_port])
86
+ end
87
+
88
+ # Convert the port to URI. The scheme assumes "pnb" excluding multicast
89
+ # addresses.
90
+ def port_to_uri(default_host, address)
91
+ scheme = multicast?(default_host) ? "pnm" : "pnb"
92
+ URI.parse("%s://%s:%s" % [scheme, default_host, address.sub(":", "")])
93
+ end
94
+
95
+ # Convert the host and port to URI. The scheme assumes "pnb" excluding
96
+ # multicast addresses.
97
+ def host_and_port_to_uri(host, port)
98
+ scheme = multicast?(host) ? "pnm" : "pnb"
99
+ URI.parse("%s://%s:%s" % [scheme, host, port])
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,10 @@
1
+ module Pione
2
+ module Notification
3
+ # `Notification::Error` is a base exception class related notification
4
+ # errors.
5
+ class Error < StandardError; end
6
+
7
+ # `Notification::MessageError` is a error for message format.
8
+ class MessageError < Error; end
9
+ end
10
+ end
@@ -0,0 +1,109 @@
1
+ module Pione
2
+ module Notification
3
+ class Message
4
+ PROTOCOL_VERSION = "PN1"
5
+
6
+ # Load a notification from the dumped data.
7
+ #
8
+ # @param data [String]
9
+ # dumped data
10
+ # @return [Network::Notification]
11
+ # notification object
12
+ def self.load(data)
13
+ io = StringIO.new(data)
14
+
15
+ # read
16
+ total = io.read(2).unpack("n")[0]
17
+ psize = io.read(1).unpack("C")[0]
18
+ version = io.read(psize)
19
+ nsize = io.read(1).unpack("C")[0]
20
+ notifier = io.read(nsize)
21
+ tsize = io.read(1).unpack("C")[0]
22
+ type = io.read(tsize)
23
+ msize = io.read(2).unpack("n")[0]
24
+ json = io.read(msize)
25
+
26
+ # check size
27
+ unless total == psize + nsize + tsize + msize
28
+ raise MessageError.new("Total size and real message size are different.")
29
+ end
30
+
31
+ # convert json to ruby object
32
+ begin
33
+ content = JSON.parse(json)
34
+ rescue JSON::ParserError
35
+ MessageError.new("Format of the message content is invalid.")
36
+ end
37
+
38
+ new(notifier, type, content)
39
+ end
40
+
41
+ attr_reader :notifier
42
+ attr_reader :type
43
+ attr_reader :version
44
+
45
+ # @param notifier [String]
46
+ # name of notification sender
47
+ # @param type [String]
48
+ # notification type name
49
+ # @param message [Hash]
50
+ # JSON data
51
+ # @param version [String]
52
+ # protocol version
53
+ def initialize(notifier, type, content, version=PROTOCOL_VERSION)
54
+ @notifier = notifier
55
+ @type = type
56
+ @content = content
57
+ @version = version
58
+ end
59
+
60
+ # Return the value.
61
+ #
62
+ # @param name [String]
63
+ # key of message content
64
+ # @return [Object]
65
+ # the value
66
+ def [](name)
67
+ @content[name]
68
+ end
69
+
70
+ # Dump the notification message to a serialized string.
71
+ #
72
+ # @return [String]
73
+ # a serialized string
74
+ def dump
75
+ json = JSON.generate(@content)
76
+
77
+ psize = PROTOCOL_VERSION.bytesize
78
+ nsize = @notifier.bytesize
79
+ tsize = @type.bytesize
80
+ msize = json.bytesize
81
+ total = psize + nsize + tsize + msize
82
+
83
+ if nsize > 255
84
+ raise MessageError.new('notifier "%s" is too long.' % @notifier)
85
+ end
86
+
87
+ if tsize > 255
88
+ raise MessageError.new('type "%s" is too long.' % @type)
89
+ end
90
+
91
+ if msize > 65335
92
+ raise MessageError.new('message is too long.')
93
+ end
94
+
95
+ if total > 65535
96
+ raise MessageError.new('The notification is too long.')
97
+ end
98
+
99
+ # build a serialized string
100
+ data = ""
101
+ data << [total].pack("n")
102
+ data << [psize].pack("C") << PROTOCOL_VERSION
103
+ data << [nsize].pack("C") << @notifier
104
+ data << [tsize].pack("C") << @type
105
+ data << [msize].pack("n") << json
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,90 @@
1
+ module Pione
2
+ module Notification
3
+ # `Notification::Receiver` provides the function to receive notification
4
+ # packets. Be careful we assume notification's packet is small.
5
+ class Receiver
6
+ # a lock for receiving notifications
7
+ LOCK = Mutex.new
8
+
9
+ MAX_SIZE = 65535
10
+
11
+ # @param uri [URI]
12
+ # URI of receiver address
13
+ def initialize(uri)
14
+ unless ["pnb", "pnm", "pnu"].include?(uri.scheme)
15
+ raise ArgumentError.new(uri)
16
+ end
17
+
18
+ @uri = uri
19
+ open
20
+ end
21
+
22
+ # Receive a notification packet.
23
+ def receive
24
+ data, addr = LOCK.synchronize {@socket.recvfrom(MAX_SIZE)}
25
+ ip_address = addr[3]
26
+ message = Message.load(data)
27
+ return [ip_address, message]
28
+ rescue TypeError, NoMethodError
29
+ ip_address = ip_address || "(unknown)"
30
+ Log::Debug.notification("Invalid data has been received from %s." % ip_address)
31
+ retry
32
+ end
33
+
34
+ # Close and open receiver socket.
35
+ def reopen
36
+ close
37
+ open(@host, @port)
38
+ end
39
+
40
+ # Close the receiver socket.
41
+ def close
42
+ @socket.close
43
+ end
44
+
45
+ # Return true if receiver socket is closed.
46
+ #
47
+ # @return [Boolean]
48
+ # true if receiver socket is closed
49
+ def closed?
50
+ @socket.closed?
51
+ end
52
+
53
+ private
54
+
55
+ # Open a receiver socket with the port number. Be careful that Linux seems
56
+ # to require INADDR_ANY to receive broadcast packets, and Windows seems
57
+ # not to require.
58
+ #
59
+ # @return [void]
60
+ def open
61
+ _host = IPSocket.getaddress(@uri.host)
62
+
63
+ @socket = UDPSocket.open
64
+ @socket.bind(_host, @uri.port)
65
+
66
+ case @uri.scheme
67
+ when "pnb"
68
+ unless not(OS.windows?) and IPAddr.new(_host) == Socket::INADDR_ANY
69
+ msg = [
70
+ "Notification receiver address %p is not INADDR_ANY(%p), ",
71
+ "this may cause to fail receiving broadcast packets. ",
72
+ "If you can receive it, please tell me your platform name."
73
+ ].join
74
+ Log::SystemLog.warn(msg % [@uri.host, "0.0.0.0"])
75
+ end
76
+
77
+ @socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, 1)
78
+ when "pnm"
79
+ group = IPAddr.new(@uri.host).hton
80
+ if @uri.interface
81
+ interface = IPAddr.new(@uri.interface).hton
82
+ else
83
+ interface = IPAddr.new(Addrinfo.ip(Socket::INADDR_ANY).ip_address).hton
84
+ end
85
+ @socket.setsockopt(Socket::IPPROTO_IP, Socket::IP_ADD_MEMBERSHIP, group + interface)
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end