Permalink
Browse files

added working codebase with some specs + working 0.0.1 gem

  • Loading branch information...
1 parent df09e2a commit 83250accc60c4aa226f3aeff2ca5aa4f4913f77f @bsingr bsingr committed with bsingr Nov 1, 2011
Showing with 716 additions and 21 deletions.
  1. +2 −0 .gitignore
  2. +1 −0 .rbenv-version
  3. +6 −0 Guardfile
  4. +64 −0 Rakefile
  5. +21 −0 lib/dbus_interface.rb
  6. +32 −0 lib/dbus_interface/class.rb
  7. +35 −0 lib/dbus_interface/object.rb
  8. +28 −0 lib/network_manager.rb
  9. +6 −0 lib/network_manager/dbus.rb
  10. +4 −0 lib/network_manager/dbus/active_connection.rb
  11. +65 −0 lib/network_manager/dbus/device.rb
  12. +4 −0 lib/network_manager/dbus/dhcp4_config.rb
  13. +4 −0 lib/network_manager/dbus/ethernet_device.rb
  14. +4 −0 lib/network_manager/dbus/ip4_config.rb
  15. +47 −0 lib/network_manager/dbus/root.rb
  16. +21 −0 lib/network_manager/dbus/settings.rb
  17. +4 −0 lib/network_manager/dbus/settings_connection.rb
  18. +14 −0 lib/network_manager/ip4_helper.rb
  19. +3 −0 lib/network_manager/version.rb
  20. +1 −7 lib/networkmanager-dbus.rb
  21. +0 −5 lib/networkmanager-dbus/version.rb
  22. +15 −9 networkmanager-dbus.gemspec
  23. +10 −0 script/spec_client
  24. +18 −0 script/spec_server
  25. +60 −0 spec/fixtures/devices.yml
  26. +14 −0 spec/fixtures/settings.yml
  27. +19 −0 spec/network_manager/dbus/active_connection_spec.rb
  28. +36 −0 spec/network_manager/dbus/device_spec.rb
  29. +14 −0 spec/network_manager/dbus/dhcp4_config_spec.rb
  30. +21 −0 spec/network_manager/dbus/ethernet_device_spec.rb
  31. +14 −0 spec/network_manager/dbus/ip4_config_spec.rb
  32. +22 −0 spec/network_manager/dbus/root_spec.rb
  33. +13 −0 spec/network_manager/dbus/settings_connection_spec.rb
  34. +30 −0 spec/network_manager/dbus/settings_spec.rb
  35. +12 −0 spec/network_manager_spec.rb
  36. +41 −0 spec/spec_helper.rb
  37. +11 −0 spec/support/fixture_helper.rb
View
@@ -2,3 +2,5 @@
.bundle
Gemfile.lock
pkg/*
+log/*
+.DS_Store
View
@@ -0,0 +1 @@
+1.9.2-p290
View
@@ -0,0 +1,6 @@
+# A sample Guardfile
+# More info at https://github.com/guard/guard#readme
+
+guard 'shell' do
+ watch(%r{^*.(rb|yml|file)$}) { `rake spec:remote` }
+end
View
@@ -1 +1,65 @@
require "bundler/gem_tasks"
+
+def pump_dumps(dumps)
+ dumps.each do |obj|
+ case obj['action']
+ when 'dump_failures'
+
+ puts "\n"*50
+ puts "= RSPEC START "*10
+
+ failures = obj['body'].map do |spec, exception, backtrace|
+
+ puts "\n"
+ puts " #{spec} (FAILED)"
+ puts " #{"- "*10}"
+ puts " #{exception}"
+ puts " #{backtrace.join("\n ")}"
+
+ [spec,exception].join("\n")
+ end
+
+ puts ""
+ puts "= RSPEC END "*10
+
+ Growl.notify(failures.join("\n\n"), {:title => "#{failures.size} specs failed", :name => 'rspec'})
+ when 'dump_summary'
+ total = obj['body']['example_count']
+ failed = obj['body']['failure_count']
+ pending = obj['body']['pending_count']
+ succeded = total - (obj['body']['failure_count'] + obj['body']['pending_count'])
+
+ title = "#{total} specs finished in #{obj['body']['duration'].round(1)}s"
+ msg = "#{failed} failed, #{pending} pending, #{succeded} succeded"
+ Growl.notify(msg, {:title => title, :name => 'rspec'})
+ end
+ end
+end
+
+namespace :spec do
+ desc 'run specs on a remote server (script/spec_server)'
+ task :remote do
+ begin
+ client = MarilynRPC::NativeClient.connect_tcp('192.168.56.101', 8483)
+ runner = client.for :rspec
+ json = runner.run
+ begin
+ dumps = JSON.parse(json)
+ pump_dumps(dumps)
+ rescue => e
+ title = "ERROR: Tests were broken #{e}!"
+ msg = json
+ puts msg
+ puts title
+ Growl.notify(msg, {:title => title, :name => 'rspec'})
+ end
+ rescue => e
+ title = "FATAL: Spec server down!?"
+ msg = e.to_s
+ puts title
+ puts msg
+ Growl.notify(msg, {:title => title, :name => 'rspec'})
+ end
+ end
+end
+
View
@@ -0,0 +1,21 @@
+module DBusInterface
+ require 'dbus'
+ require File.join(File.dirname(__FILE__), 'dbus_interface/class')
+ require File.join(File.dirname(__FILE__), 'dbus_interface/object')
+
+ def self.system_bus
+ ::DBus::SystemBus.instance
+ end
+
+ def self.service
+ @@service
+ end
+
+ def self.service=(s)
+ @@service = system_bus.service s
+ end
+
+ def self.configure(&block)
+ yield self
+ end
+end
@@ -0,0 +1,32 @@
+module DBusInterface::Class
+ def map_dbus(conf)
+ @dbus ||= begin
+ # ensure hash
+ raise ArgumentError.new "adapt_dbus must receive a hash instead of"\
+ "#{conf.class}" unless conf.is_a? Hash
+ # ensure :default_iface is set
+ raise ArgumentError.new "adapt_dbus must specify at least :default_iface"\
+ unless conf.has_key? :default_iface
+ conf
+ end
+ end
+
+ def default_iface
+ dbus[:default_iface]
+ end
+
+ def object_path
+ dbus[:object_path]
+ end
+
+ # singletons must be given an :object_path
+ def instance
+ @instance ||= new
+ end
+
+private
+
+ def dbus
+ @dbus
+ end
+end
@@ -0,0 +1,35 @@
+module DBusInterface::Object
+ def self.included(some_base)
+ some_base.extend(DBusInterface::Class)
+ end
+
+ def object
+ @object ||= begin
+ object = DBusInterface.service.object(object_path)
+ object.default_iface = self.class.default_iface
+ object.introspect
+ object
+ end
+ end
+
+ def properties
+ object.all_properties
+ end
+
+ def initialize(object_path = nil)
+ self.object_path = object_path if object_path
+ end
+
+ def object_path=(p)
+ @object_path = p
+ end
+
+ def object_path
+ @object_path || self.class.object_path
+ end
+
+ # shortcut to access properties
+ def [](key)
+ properties[key]
+ end
+end
@@ -0,0 +1,28 @@
+# This Library provides an API based on the specs for network manager dbus api
+# found here: http://projects.gnome.org/NetworkManager/developers/api/09/spec.html
+module NetworkManager
+ # generic dbus api
+ require File.join(File.dirname(__FILE__), 'dbus_interface')
+
+ # version
+ require File.join(File.dirname(__FILE__), 'network_manager/version')
+
+ # helper
+ require File.join(File.dirname(__FILE__), 'network_manager/ip4_helper')
+
+ # network manager dbus api
+ require File.join(File.dirname(__FILE__), 'network_manager/dbus')
+
+ def self.settings
+ NetworkManager::DBus::Settings.instance
+ end
+
+ # delegate to dbus root object
+ def self.method_missing(*args)
+ NetworkManager::DBus::Root.send(*args)
+ end
+
+ DBusInterface.configure do |config|
+ config.service = 'org.freedesktop.NetworkManager'
+ end
+end
@@ -0,0 +1,6 @@
+module NetworkManager::DBus
+ # require all .rb files from lib/network_manager/dbus
+ Dir.glob(File.join(File.dirname(__FILE__), "dbus/*.rb")).each do |p|
+ require p
+ end
+end
@@ -0,0 +1,4 @@
+class NetworkManager::DBus::ActiveConnection
+ include DBusInterface::Object
+ map_dbus :default_iface => 'org.freedesktop.NetworkManager.Connection.Active'
+end
@@ -0,0 +1,65 @@
+class NetworkManager::DBus::Device
+ include DBusInterface::Object
+ map_dbus :default_iface => 'org.freedesktop.NetworkManager.Device'
+
+ NM_DEVICE_TYPE__PROPERTY = 'DeviceType'
+ # The device type is unknown.
+ NM_DEVICE_TYPE_UNKNOWN = 0
+ # The device is wired Ethernet device.
+ NM_DEVICE_TYPE_ETHERNET = 1
+ # The device is an 802.11 WiFi device.
+ NM_DEVICE_TYPE_WIFI = 2
+ # Unused
+ NM_DEVICE_TYPE_UNUSED1 = 3
+ # Unused
+ NM_DEVICE_TYPE_UNUSED2 = 4
+ # The device is Bluetooth device that provides PAN or DUN capabilities.
+ NM_DEVICE_TYPE_BT = 5
+ # The device is an OLPC mesh networking device.
+ NM_DEVICE_TYPE_OLPC_MESH = 6
+ # The device is an 802.16e Mobile WiMAX device.
+ NM_DEVICE_TYPE_WIMAX = 7
+ # The device is a modem
+ # supporting one or more of analog telephone, CDMA/EVDO, GSM/UMTS/HSPA,
+ # or LTE standards to access a cellular or wireline data network.
+ NM_DEVICE_TYPE_MODEM = 8
+
+ def to_s
+ "#{self.class} #{properties}"
+ end
+
+ def ip4_address
+ @ip_addr ||= begin
+ ip4_int = self['Ip4Address']
+ NetworkManager::Ip4Helper.ip4_integer_to_dot_decimal ip4_int
+ end
+ end
+
+ # @return [NetworkManager::DBus::Ip4Config] conf
+ def ip4_config
+ @ip4_config ||= NetworkManager::DBus::Ip4Config.new properties['Ip4Config']
+ end
+
+ # @return [NetworkManager::DBus::ActiveConnection] con
+ def active_connection
+ @active_connection ||=
+ NetworkManager::DBus::ActiveConnection.new properties['ActiveConnection']
+ end
+
+ #
+ # ETHERNET
+ #
+
+ def ethernet?
+ properties[NM_DEVICE_TYPE__PROPERTY] == NM_DEVICE_TYPE_ETHERNET
+ end
+
+ # @return [NetworkManager::DBus::EthernetDevice] dev
+ def ethernet
+ if ethernet?
+ @ethernet ||= NetworkManager::DBus::EthernetDevice.new(self.object_path)
+ else
+ nil
+ end
+ end
+end
@@ -0,0 +1,4 @@
+class NetworkManager::DBus::Dhcp4Config
+ include DBusInterface::Object
+ map_dbus :default_iface => 'org.freedesktop.NetworkManager.DHCP4Config'
+end
@@ -0,0 +1,4 @@
+class NetworkManager::DBus::EthernetDevice
+ include DBusInterface::Object
+ map_dbus :default_iface => 'org.freedesktop.NetworkManager.Device.Wired'
+end
@@ -0,0 +1,4 @@
+class NetworkManager::DBus::Ip4Config
+ include DBusInterface::Object
+ map_dbus :default_iface => 'org.freedesktop.NetworkManager.IP4Config'
+end
@@ -0,0 +1,47 @@
+class NetworkManager::DBus::Root
+ include DBusInterface::Object
+ map_dbus :default_iface => 'org.freedesktop.NetworkManager',
+ :object_path => '/org/freedesktop/NetworkManager'
+
+ # Networking state is unknown.
+ NM_STATE_UNKNOWN = 0
+ # Networking is inactive and all devices are disabled.
+ NM_STATE_ASLEEP = 10
+ # There is no active network connection.
+ NM_STATE_DISCONNECTED = 20
+ # Network connections are being cleaned up.
+ NM_STATE_DISCONNECTING = 30
+ # A network device is connecting to a network and there is no other available network connection.
+ NM_STATE_CONNECTING = 40
+ # A network device is connected, but there is only link-local connectivity.
+ NM_STATE_CONNECTED_LOCAL = 50
+ # A network device is connected, but there is only site-local connectivity.
+ NM_STATE_CONNECTED_SITE = 60
+ # A network device is connected, with global network connectivity.
+ NM_STATE_CONNECTED_GLOBAL = 70
+
+ # @return [Array<NetworkManager::DBus::Device>]] devices
+ def self.devices
+ instance.object.GetDevices.map do |list|
+ list.map do |object_path|
+ new_device(object_path)
+ end
+ end.flatten
+ end
+
+ def self.device_by_interface(interface)
+ paths = instance.object.GetDeviceByIpIface(interface)
+ paths.empty? ? nil : new_device(paths.first)
+ end
+
+ def self.internet_connection?
+ instance.object.state.first == NM_STATE_CONNECTED_GLOBAL
+ end
+
+private
+
+ def self.new_device(object_path)
+ NetworkManager::DBus::Device.new object_path
+ end
+
+end
@@ -0,0 +1,21 @@
+class NetworkManager::DBus::Settings
+ include DBusInterface::Object
+ map_dbus :default_iface => 'org.freedesktop.NetworkManager.Settings',
+ :object_path => '/org/freedesktop/NetworkManager/Settings'
+
+ def self.connections
+ instance.object.ListConnections.map do |list|
+ list.map do |object_path|
+ ::NetworkManager::DBus::SettingsConnection.new(object_path)
+ end
+ end.flatten
+ end
+
+ def hostname=(new_name)
+ object.SaveHostname(new_name)
+ end
+
+ def hostname
+ properties['Hostname']
+ end
+end
@@ -0,0 +1,4 @@
+class NetworkManager::DBus::SettingsConnection
+ include DBusInterface::Object
+ map_dbus :default_iface => 'org.freedesktop.NetworkManager.Settings.Connection'
+end
@@ -0,0 +1,14 @@
+module NetworkManager::Ip4Helper
+ def self.ip4_integer_to_dot_decimal(ip4_integer)
+ integer = ip4_integer
+ result = []
+ 4.times.map do
+ # bitwise AND 255 bits
+ result.push integer & 255
+
+ # bitwise SHIFT RIGHT 8 bits
+ integer = integer >> 8
+ end
+ result.join '.'
+ end
+end
@@ -0,0 +1,3 @@
+module NetworkManager
+ VERSION = "0.0.1"
+end
@@ -1,7 +1 @@
-require "networkmanager-dbus/version"
-
-module Networkmanager
- module Dbus
- # Your code goes here...
- end
-end
+require "network_manager"
Oops, something went wrong.

0 comments on commit 83250ac

Please sign in to comment.