wisebedclientruby 0.0.49

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ test/credentials.yml
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "eventmachine"
4
+ gem "em-http-request"
5
+ gem "simpleblockingwebsocketclient"
6
+
7
+ # Specify your gem's dependencies in wisebedclient-ruby.gemspec
8
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Marv Cool
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,23 @@
1
+ # Wisebedclient::Ruby
2
+
3
+ Ruby gem to access the wisebed API via REST and websockets.
4
+
5
+ ## Installation
6
+
7
+ Not on official gem repos yet, clone the code from github.
8
+
9
+ ## Usage
10
+
11
+ For now, just do
12
+
13
+ $ ruby test/testapp.rb
14
+
15
+ or have a look at the code and enhance the testapp with whatever you like to do
16
+
17
+ ## Contributing
18
+
19
+ 1. Fork it
20
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
21
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
22
+ 4. Push to the branch (`git push origin my-new-feature`)
23
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ desc "Runs the simply test app"
5
+ task :test_app do
6
+ require File.expand_path('../test/testapp.rb', __FILE__)
7
+ end
8
+
9
+ desc "runs irb with this lib loaded"
10
+ task :c do
11
+ sh "irb" " -r#{File.expand_path('../lib/wisebedclientruby.rb', __FILE__)}"
12
+ end
13
+
14
+ namespace :reservation do
15
+
16
+ desc "lists current reservations on uzl"
17
+ task :list do
18
+ require File.expand_path('../lib/wisebedclientruby.rb', __FILE__)
19
+ res = Wisebed::Testbed.new("uzl").public_reservations(Time.now, Time.now+60*60*24)
20
+ res.each do |r|
21
+ puts "#{r["userData"]} : #{Time.at(r["from"]/1000)} -> #{Time.at(r["to"]/1000)}"
22
+ end
23
+ end
24
+
25
+ desc "deletes a reservation"
26
+ task :delete do
27
+ require File.expand_path('../lib/wisebedclientruby.rb', __FILE__)
28
+ tb = Wisebed::Testbed.new("uzl")
29
+ logindata = YAML.load_file(File.expand_path('../test/credentials.yml', __FILE__))
30
+ tb.login!(logindata)
31
+ puts tb.delete_reservation(JSON.parse(ENV['res']))
32
+ end
33
+
34
+ end
35
+
@@ -0,0 +1,75 @@
1
+ module Wisebed
2
+
3
+ BASEURL = "http://wisebed.itm.uni-luebeck.de"
4
+ WSBASEURL = "ws://wisebed.itm.uni-luebeck.de"
5
+ APIVERSION = "/rest/2.3/"
6
+
7
+ class Client
8
+ require 'httparty'
9
+ include HTTParty
10
+ base_uri Wisebed::BASEURL+Wisebed::APIVERSION
11
+
12
+ def testbeds
13
+ request_from_wisebed("testbeds")["testbedMap"]
14
+ end
15
+
16
+ def experimentconfiguration(url)
17
+ request_from_wisebed("experimentconfiguration?url=#{url}")
18
+ end
19
+
20
+ def request_from_wisebed(url_extension)
21
+ url_extension = "/#{url_extension}" unless url_extension[0] == "/"
22
+ #puts "debug: requesting "+self.class.base_uri+url_extension
23
+ if @cookie
24
+ headers = {'Cookie' => @cookie}
25
+ else
26
+ headers = {}
27
+ end
28
+ res = self.class.get(url_extension, :headers => headers)
29
+ begin
30
+ JSON.parse(res.body)
31
+ rescue JSON::ParserError => e
32
+ res.body
33
+ end
34
+
35
+ end
36
+
37
+
38
+ def post_to_wisebed(url_extension, data)
39
+ url_extension = "/#{url_extension}" unless url_extension[0] == "/"
40
+ #puts "debug: posting "+self.class.base_uri+url_extension
41
+ #puts "debug: with data: #{data.to_json}"
42
+ if @cookie
43
+ headers = {'Cookie' => @cookie}
44
+ else
45
+ headers = {}
46
+ end
47
+ headers.merge!({'Content-Type' => "application/json; charset=utf-8"})
48
+ res = self.class.post(url_extension, :body => data.to_json, :headers => headers)
49
+ @cookie = res.headers['Set-Cookie'] if res.headers['Set-Cookie']
50
+ begin
51
+ JSON.parse(res.body)
52
+ rescue JSON::ParserError => e
53
+ res.body
54
+ end
55
+ end
56
+
57
+ def delete_from_wisebed(url_extension, data)
58
+ url_extension = "/#{url_extension}" unless url_extension[0] == "/"
59
+ #puts "debug: deleting "+self.class.base_uri+url_extension
60
+ if @cookie
61
+ headers = {'Cookie' => @cookie}
62
+ else
63
+ headers = {}
64
+ end
65
+ headers.merge!({'Content-Type' => "application/json; charset=utf-8"})
66
+ res = self.class.delete(url_extension, :body => data.to_json, :headers => headers)
67
+ begin
68
+ JSON.parse(res.body)
69
+ rescue JSON::ParserError => e
70
+ res.body
71
+ end
72
+ end
73
+
74
+ end
75
+ end
@@ -0,0 +1,78 @@
1
+ module Wisebed
2
+
3
+ class Testbed < Client
4
+
5
+ attr_reader :cookie, :credentials
6
+
7
+ def initialize(testbed_id)
8
+ @cookie = ""
9
+ @id = testbed_id
10
+ end
11
+
12
+ def wise_ml(experiment_id = nil, json_or_xml = "json")
13
+ # TODO: handle xml
14
+ request_from_wisebed(@id+ "/experiments/" + (experiment_id ? experiment_id+"/" : "") + "network")
15
+ end
16
+
17
+ def login!(credentials=nil)
18
+ @credentials = credentials if credentials
19
+ raise "Cannot login: No credentials given!" if not @credentials
20
+ res = post_to_wisebed @id+"/login", @credentials
21
+ raise SecurityError, "could not log into #{@id}: wrong username/password" if res.include? "Authentication failed"
22
+ @secret_authentication_keys = res
23
+ end
24
+
25
+ def logout!
26
+ request_from_wisebed @id + "/logout"
27
+ @cookie = ""
28
+ end
29
+
30
+ def is_logged_in?
31
+ res = request_from_wisebed @id+"/isLoggedIn"
32
+ not res.include? "not logged in"
33
+ end
34
+
35
+ def personal_reservations(from=nil, to=nil)
36
+ login! if @cookie.empty?
37
+ public_reservations(from, to, true)["reservations"]
38
+ end
39
+
40
+ def public_reservations(from=nil, to=nil, useronly=false)
41
+ # Time.iso8601 includes the timezone (+01:00), however XMLGreagorianCalendar does not parse this
42
+ res = request_from_wisebed @id+"/reservations?userOnly="+useronly.to_s+"&"+
43
+ (from ? ("from=" + from.iso8601_no_tz + "&") : "") + (to ? ("to="+to.iso8601_no_tz + "&") : "")
44
+ res.nil? ? "not logged in for personal reservations" : res["reservations"]
45
+ end
46
+
47
+ def make_reservation(from, to, user_data, node_URNs)
48
+ content = {
49
+ "from" => from.iso8601_no_tz,
50
+ "nodeURNs" => node_URNs,
51
+ "to" => to.iso8601_no_tz(),
52
+ "userData" => user_data # description or something
53
+ }
54
+ res = post_to_wisebed(@id+"/reservations/create", content)
55
+ raise "Another reservation is in conflict with yours" if res.include? "Another reservation is in conflict with yours"
56
+ res
57
+ end
58
+
59
+ def delete_reservation(reservation_hash)
60
+ delete_from_wisebed(@id+"/reservations", reservation_hash)
61
+ end
62
+
63
+ def experiments(reservation_data=nil)
64
+ unless reservation_data
65
+ reservation_data = personal_reservations(Time.now, Time.now+(24*60*60)).last["data"]
66
+ reservation_data[0].delete("username")
67
+ reservation_data = {"reservations" => reservation_data}
68
+ end
69
+ res = post_to_wisebed(@id+"/experiments", reservation_data)
70
+ res.split("/").last
71
+ end
72
+
73
+ def flash(secret_keservation_key, flash_this_json)
74
+ post_to_wisebed(@id+"/experiments/"+secret_keservation_key+"/flash", flash_this_json)
75
+ end
76
+
77
+ end
78
+ end
@@ -0,0 +1,3 @@
1
+ module Wisebedclient
2
+ VERSION = "0.0.49"
3
+ end
@@ -0,0 +1,21 @@
1
+ module Wisebed
2
+
3
+ class WebsocketClient < Client
4
+ require 'simpleblockingwebsocketclient'
5
+
6
+ def initialize(secret_exp_id)
7
+ @secret_exp_id = secret_exp_id
8
+ end
9
+
10
+ def attach(&message_handler)
11
+ @ws = WebSocket.new(Wisebed::WSBASEURL+"/ws/experiments/"+@secret_exp_id) { |msg| message_handler.call(msg) }
12
+ end
13
+
14
+ def detach
15
+ puts "WebSocket detaching..."
16
+ @ws.close
17
+ end
18
+
19
+ end
20
+
21
+ end
@@ -0,0 +1,19 @@
1
+ require 'json'
2
+ require 'time' # for easy iso8601 format
3
+
4
+ class Time
5
+ # WARN: polluting global namespaces here...
6
+ def iso8601_no_tz
7
+ # example: 2012-10-19T13:00:31
8
+ utc.strftime("%Y-%m-%dT%H:%M:%SZ")
9
+ end
10
+ end
11
+
12
+ module Wisebed
13
+ class Client
14
+ # empty class definition to relief require *.rb from dependencies
15
+ end
16
+ end
17
+
18
+
19
+ Dir[File.dirname(__FILE__) + '/wisebedclientruby/*.rb'].each { |file| require file }
@@ -0,0 +1,7 @@
1
+ # copy this file to "credentials.yml" and fill your user/pass
2
+ ---
3
+ authenticationData:
4
+ - urnPrefix: ! 'urn:wisebed:uzl1:'
5
+ username: user.name@host.example
6
+ password: p4zzw0rd
7
+
data/test/testapp.rb ADDED
@@ -0,0 +1,51 @@
1
+ require File.expand_path('../../lib/wisebedclientruby.rb', __FILE__)
2
+ #require 'YAML'
3
+
4
+ # prints out all testbeds
5
+ #puts Wisebed::Client.new.testbeds
6
+
7
+ #example:
8
+ # logindata = {
9
+ # "authenticationData" => [
10
+ # {
11
+ # "urnPrefix" => "urn:wisebed:uzl1:",
12
+ # "username" => "user.name@host.example",
13
+ # "password"=> "p4zzw0rd"
14
+ # }
15
+ # ]
16
+ # }
17
+ logindata = YAML.load_file(File.expand_path('../credentials.yml', __FILE__))
18
+
19
+ tb = Wisebed::Testbed.new("uzl")
20
+ #puts tb.wise_ml
21
+
22
+ # gets reservations for Uni Luebeck Testbed
23
+ puts "logging in"
24
+ begin
25
+ tb.login!(logindata)
26
+ rescue SecurityError => e
27
+ puts e.message
28
+ exit
29
+ end
30
+ all_nodes_for_packet_tracking = ["urn:wisebed:uzl1:0x211c","urn:wisebed:uzl1:0x2114","urn:wisebed:uzl1:0x2104","urn:wisebed:uzl1:0x2118","urn:wisebed:uzl1:0x2120","urn:wisebed:uzl1:0x2144","urn:wisebed:uzl1:0x2140","urn:wisebed:uzl1:0x2108","urn:wisebed:uzl1:0x2100","urn:wisebed:uzl1:0x210c","urn:wisebed:uzl1:0x2124","urn:wisebed:uzl1:0x2134","urn:wisebed:uzl1:0x2130","urn:wisebed:uzl1:0x212c","urn:wisebed:uzl1:0x2128","urn:wisebed:uzl1:0x2138","urn:wisebed:uzl1:0x213c","urn:wisebed:uzl1:0x2110"]
31
+ exp_reservation = tb.make_reservation(Time.now, Time.now+(60), "test reservation from ruby client", all_nodes_for_packet_tracking)
32
+ puts "reservation: #{exp_reservation}"
33
+ experiment_id = tb.experiments(exp_reservation)
34
+ puts "secret experiment_id: #{experiment_id}"
35
+ flash_this_json = Wisebed::Client.new.experimentconfiguration("https://raw.github.com/itm/wisebed-experiments/master/packet-tracking/config.json")
36
+ tb.flash(experiment_id,flash_this_json)
37
+ wsc = Wisebed::WebsocketClient.new(experiment_id)
38
+
39
+ begin
40
+ attach_time = Time.now
41
+ messages = []
42
+ puts "opening websocket (might take some time before all nodes are flashed!)"
43
+ wsc.attach {|msg| print "."; messages << msg}
44
+ while true do end
45
+ rescue Interrupt
46
+ ensure
47
+ detach_time = Time.now
48
+ wsc.detach
49
+ end
50
+
51
+ puts "received #{messages.length} messages via websocket in #{detach_time-attach_time}"
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/wisebedclientruby/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Marvin Frick"]
6
+ gem.email = ["frick@informatik.uni-luebeck.de"]
7
+ gem.description = %q{Ruby gem to access the wisebed API via REST and websockets}
8
+ gem.summary = %q{Ruby gem to access the wisebed API via REST and websockets}
9
+ gem.homepage = "https://github.com/MrMarvin/wisebedclient-ruby"
10
+
11
+ gem.add_dependency 'simpleblockingwebsocketclient'
12
+ gem.add_dependency 'httparty'
13
+
14
+ gem.files = `git ls-files`.split($\)
15
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.name = "wisebedclientruby"
18
+ gem.require_paths = ["lib"]
19
+ gem.version = Wisebedclient::VERSION
20
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wisebedclientruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.49
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Marvin Frick
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: simpleblockingwebsocketclient
16
+ prerelease: false
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ none: false
23
+ type: :runtime
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ! '>='
27
+ - !ruby/object:Gem::Version
28
+ version: '0'
29
+ none: false
30
+ - !ruby/object:Gem::Dependency
31
+ name: httparty
32
+ prerelease: false
33
+ requirement: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ none: false
39
+ type: :runtime
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ none: false
46
+ description: Ruby gem to access the wisebed API via REST and websockets
47
+ email:
48
+ - frick@informatik.uni-luebeck.de
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE
56
+ - README.md
57
+ - Rakefile
58
+ - lib/wisebedclientruby.rb
59
+ - lib/wisebedclientruby/client.rb
60
+ - lib/wisebedclientruby/testbed.rb
61
+ - lib/wisebedclientruby/version.rb
62
+ - lib/wisebedclientruby/websocketclient.rb
63
+ - test/credentials_example.yml
64
+ - test/testapp.rb
65
+ - wisebedclientruby.gemspec
66
+ homepage: https://github.com/MrMarvin/wisebedclient-ruby
67
+ licenses: []
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ none: false
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ none: false
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 1.8.24
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: Ruby gem to access the wisebed API via REST and websockets
90
+ test_files:
91
+ - test/credentials_example.yml
92
+ - test/testapp.rb