Commit f72a2d41 authored by Felix Hamann's avatar Felix Hamann

implemented ffi binding to _new, _destroy, _ping

parent a2c6b266
source 'https://rubygems.org'
# Specify your gem's dependencies in samwise.gemspec
gemspec
PATH
remote: .
specs:
samwise (0.1.0)
ffi
GEM
remote: https://rubygems.org/
specs:
ffi (1.9.8)
rake (10.4.2)
PLATFORMS
ruby
DEPENDENCIES
bundler (~> 1.9)
rake (~> 10.0)
samwise!
The MIT License (MIT)
Copyright (c) 2015 Felix Hamann
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
# Samwise
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/samwise`. To experiment with that code, run `bin/console` for an interactive prompt.
TODO: Delete this and the text above, and describe your gem
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'samwise'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install samwise
## Usage
TODO: Write usage instructions here
## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
## Contributing
1. Fork it ( https://github.com/[my-github-username]/samwise/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request
ENV['RC_ARCHS'] = '' if RUBY_PLATFORM =~ /darwin/
require 'rbconfig'
require 'mkmf'
$CFLAGS += ' -std=gnu99'
LIBDIR = RbConfig::CONFIG['libdir']
INCLUDEDIR = RbConfig::CONFIG['includedir']
HEADER_DIRS = [
'/opt/local/include',
'/usr/local/include',
INCLUDEDIR,
'/usr/include',
]
LIB_DIRS = [
'/opt/local/lib',
'/usr/local/lib',
LIBDIR,
'/usr/lib',
]
dir_config('samwise', HEADER_DIRS, LIB_DIRS)
unless have_header('samwise.h')
abort 'samwise.h is missing'
end
unless find_library('samwise', 'samwise_new')
abort 'libsamwise is missing'
end
create_makefile ('samwise/samwise')
#include <ruby.h>
#include "samwise.h"
VALUE samwise;
void init_samwise ()
{
samwise = rb_define_module ("Samwise");
VALUE cSamwiseRabbitMQ = rb_define_class_under (samwise, "RabbitMQ", rb_cObject);
}
#ifndef __RUBY_SAMWISE__
#define __RUBY_SAMWISE__
#include <ruby.h>
#include <samwise.h>
extern VALUE samwise;
#endif
require 'samwise/samwise'
require 'ffi'
require 'samwise/version'
require 'samwise/client'
##
# Mapping-class to access libsamwise. All native functions are
# prefixed by samwise_* and are invoked by the wrapper functions
# provided by the Client class.
#
module Samwise
extend FFI::Library
ffi_lib 'samwise'
attach_function :samwise_new, [:string], :pointer
attach_function :samwise_destroy, [:pointer], :void
attach_function :samwise_ping, [:pointer], :int
VERSION = 1.2
end
class Samwise::Client
##
# Create a new samwise client instance. Connects to a running
# samwise daemon. The endpoint must be a valid ZeroMQ string such as
# "ipc://sam".
def initialize endpoint
@sam = Samwise.samwise_new endpoint
raise "Could not connect" if @sam.nil?
end
##
# Destroy the samwise client instance. Maybe there is a way to set
# the free funtion for the ruby garbage collection but I was not
# able to find this.
def close!
samptr = FFI::MemoryPointer.new :pointer
samptr.write_pointer @sam
Samwise.samwise_destroy samptr
end
##
# Send a ping to samwise. Returns true if the daemon answered, false
# otherwise.
def ping
rc = Samwise.samwise_ping @sam
rc == 0? true: false
end
end
module Samwise
VERSION = "0.1.0"
end
#!/usr/bin/env ruby
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/lib')
require 'samwise'
sam = Samwise::Client.new 'ipc://../../sam_ipc'
if sam.ping
puts 'Samwise is reachable'
else
puts 'Samwise is not reachable'
end
sam.close!
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'samwise/version'
Gem::Specification.new do |spec|
spec.name = "samwise"
spec.version = Samwise::VERSION
spec.authors = ["Felix Hamann"]
spec.email = ["nvri@dreadworks.de"]
spec.summary = %q{Samwise client library}
spec.description = %q{Client library speaking the samwise protocol to send messages to running samwise daemons}
spec.homepage = "https://github.com/dreadworks/samwise"
spec.license = "MIT"
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.add_development_dependency "bundler", "~> 1.9"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_runtime_dependency "ffi"
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment