Class: CZMQ::FFI::ZhttpServer

Inherits:
Object
  • Object
show all
Defined in:
lib/czmq-ffi-gen/czmq/ffi/zhttp_server.rb

Overview

Note:

This class is 100% generated using zproject.

Simple http server. To start handling requests:

  1. Create a dealer socket

  2. Connect the socket to the server backend address provided in the options.

  3. Create a zhttp_request.

  4. Call zhttp_request_recv to accept a new request.

  5. Call zhttp_response_send to send a response.

You can connect as many dealers as you want. The Server is using simple dealer socket to route the requests.

NOTE: when using libmicrohttpd << 0.9.34 the application might experience high CPU usage due to the lack of MHD_suspend_connection and MHD_resume_connection APIs. It is recommended to use this class only with libmicrohttpd at least 0.9.34 in production.

Defined Under Namespace

Classes: DestroyedError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ptr, finalize = true) ⇒ ZhttpServer

Attaches the pointer ptr to this instance and defines a finalizer for it if necessary.

Parameters:

  • ptr (::FFI::Pointer)
  • finalize (Boolean) (defaults to: true)


38
39
40
41
42
43
44
45
46
# File 'lib/czmq-ffi-gen/czmq/ffi/zhttp_server.rb', line 38

def initialize(ptr, finalize = true)
  @ptr = ptr
  if @ptr.null?
    @ptr = nil # Remove null pointers so we don't have to test for them.
  elsif finalize
    @finalizer = self.class.create_finalizer_for @ptr
    ObjectSpace.define_finalizer self, @finalizer
  end
end

Class Method Details

.__newObject



32
# File 'lib/czmq-ffi-gen/czmq/ffi/zhttp_server.rb', line 32

alias :__new :new

.create_finalizer_for(ptr) ⇒ Proc

Parameters:

  • ptr (::FFI::Pointer)

Returns:

  • (Proc)


49
50
51
52
53
54
55
56
# File 'lib/czmq-ffi-gen/czmq/ffi/zhttp_server.rb', line 49

def self.create_finalizer_for(ptr)
  ptr_ptr = ::FFI::MemoryPointer.new :pointer

  Proc.new do
    ptr_ptr.write_pointer ptr
    ::CZMQ::FFI.zhttp_server_destroy ptr_ptr
  end
end

.new(options) ⇒ CZMQ::ZhttpServer

Create a new http server

Parameters:

Returns:

  • (CZMQ::ZhttpServer)


94
95
96
97
98
# File 'lib/czmq-ffi-gen/czmq/ffi/zhttp_server.rb', line 94

def self.new(options)
  options = options.__ptr if options
  ptr = ::CZMQ::FFI.zhttp_server_new(options)
  __new ptr
end

.test(verbose) ⇒ void

This method returns an undefined value.

Self test of this class.

Parameters:

  • verbose (Boolean)


124
125
126
127
128
# File 'lib/czmq-ffi-gen/czmq/ffi/zhttp_server.rb', line 124

def self.test(verbose)
  verbose = !(0==verbose||!verbose) # boolean
  result = ::CZMQ::FFI.zhttp_server_test(verbose)
  result
end

Instance Method Details

#__ptr::FFI::Pointer Also known as: to_ptr

Return internal pointer

Returns:

  • (::FFI::Pointer)

Raises:



63
64
65
66
# File 'lib/czmq-ffi-gen/czmq/ffi/zhttp_server.rb', line 63

def __ptr
  raise DestroyedError unless @ptr
  @ptr
end

#__ptr_give_ref::FFI::MemoryPointer

Note:

This detaches the current instance from the native object and thus makes it unusable.

Nullify internal pointer and return pointer pointer.

Returns:

  • (::FFI::MemoryPointer)

    the pointer pointing to a pointer pointing to the native object

Raises:



74
75
76
77
78
79
80
81
# File 'lib/czmq-ffi-gen/czmq/ffi/zhttp_server.rb', line 74

def __ptr_give_ref
  raise DestroyedError unless @ptr
  ptr_ptr = ::FFI::MemoryPointer.new :pointer
  ptr_ptr.write_pointer @ptr
  __undef_finalizer if @finalizer
  @ptr = nil
  ptr_ptr
end

#__undef_finalizervoid

Note:

Only use this if you need to and can guarantee that the native object will be freed by other means.

This method returns an undefined value.

Undefines the finalizer for this object.



86
87
88
89
# File 'lib/czmq-ffi-gen/czmq/ffi/zhttp_server.rb', line 86

def __undef_finalizer
  ObjectSpace.undefine_finalizer self
  @finalizer = nil
end

#destroyvoid

This method returns an undefined value.

Destroy an http server



103
104
105
106
107
108
# File 'lib/czmq-ffi-gen/czmq/ffi/zhttp_server.rb', line 103

def destroy()
  return unless @ptr
  self_p = __ptr_give_ref
  result = ::CZMQ::FFI.zhttp_server_destroy(self_p)
  result
end

#null?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/czmq-ffi-gen/czmq/ffi/zhttp_server.rb', line 58

def null?
  !@ptr or @ptr.null?
end

#portInteger

Return the port the server is listening on.

Returns:

  • (Integer)

Raises:



113
114
115
116
117
118
# File 'lib/czmq-ffi-gen/czmq/ffi/zhttp_server.rb', line 113

def port()
  raise DestroyedError unless @ptr
  self_p = @ptr
  result = ::CZMQ::FFI.zhttp_server_port(self_p)
  result
end