Class DRb::DRbTCPSocket
In: lib/drb/drb.rb
Parent: Object

The default drb protocol.

Communicates over a TCP socket.

Methods

Attributes

uri  [R]  Get the URI that we are connected to.

Public Class methods

[Source]

     # File lib/drb/drb.rb, line 830
830:     def self.getservername
831:       host = Socket::gethostname
832:       begin
833:         Socket::gethostbyname(host)[0]
834:       rescue
835:         'localhost'
836:       end
837:     end

Create a new DRbTCPSocket instance.

uri is the URI we are connected to. soc is the tcp socket we are bound to. config is our configuration.

[Source]

     # File lib/drb/drb.rb, line 883
883:     def initialize(uri, soc, config={})
884:       @uri = uri
885:       @socket = soc
886:       @config = config
887:       @acl = config[:tcp_acl]
888:       @msg = DRbMessage.new(config)
889:       set_sockopt(@socket)
890:     end

Open a client connection to uri using configuration config.

[Source]

     # File lib/drb/drb.rb, line 822
822:     def self.open(uri, config)
823:       host, port, option = parse_uri(uri)
824:       host.untaint
825:       port.untaint
826:       soc = TCPSocket.open(host, port)
827:       self.new(uri, soc, config)
828:     end

Open a server listening for connections at uri using configuration config.

[Source]

     # File lib/drb/drb.rb, line 858
858:     def self.open_server(uri, config)
859:       uri = 'druby://:0' unless uri
860:       host, port, opt = parse_uri(uri)
861:       if host.size == 0
862:         host = getservername
863:         soc = open_server_inaddr_any(host, port)
864:       else
865:         soc = TCPServer.open(host, port)
866:       end
867:       port = soc.addr[1] if port == 0
868:       uri = "druby://#{host}:#{port}"
869:       self.new(uri, soc, config)
870:     end

[Source]

     # File lib/drb/drb.rb, line 839
839:     def self.open_server_inaddr_any(host, port)
840:       infos = Socket::getaddrinfo(host, nil, 
841:                                   Socket::AF_UNSPEC,
842:                                   Socket::SOCK_STREAM, 
843:                                   0,
844:                                   Socket::AI_PASSIVE)
845:       family = infos.collect { |af, *_| af }.uniq
846:       case family
847:       when ['AF_INET']
848:         return TCPServer.open('0.0.0.0', port)
849:       when ['AF_INET6']
850:         return TCPServer.open('::', port)
851:       else
852:         return TCPServer.open(port)
853:       end
854:     end

Parse uri into a [uri, option] pair.

[Source]

     # File lib/drb/drb.rb, line 873
873:     def self.uri_option(uri, config)
874:       host, port, option = parse_uri(uri)
875:       return "druby://#{host}:#{port}", option
876:     end

Private Class methods

[Source]

     # File lib/drb/drb.rb, line 807
807:     def self.parse_uri(uri)
808:       if uri =~ /^druby:\/\/(.*?):(\d+)(\?(.*))?$/
809:         host = $1
810:         port = $2.to_i
811:         option = $4
812:         [host, port, option]
813:       else
814:         raise(DRbBadScheme, uri) unless uri =~ /^druby:/
815:         raise(DRbBadURI, 'can\'t parse uri:' + uri)
816:       end
817:     end

Public Instance methods

On the server side, for an instance returned by open_server, accept a client connection and return a new instance to handle the server‘s side of this client-server session.

[Source]

     # File lib/drb/drb.rb, line 942
942:     def accept
943:       while true
944:         s = @socket.accept
945:         break if (@acl ? @acl.allow_socket?(s) : true) 
946:         s.close
947:       end
948:       self.class.new(nil, s, @config)
949:     end

Check to see if this connection is alive.

[Source]

     # File lib/drb/drb.rb, line 952
952:     def alive?
953:       return false unless @socket
954:       if IO.select([@socket], nil, nil, 0)
955:         close
956:         return false
957:       end
958:       true
959:     end

Close the connection.

If this is an instance returned by open_server, then this stops listening for new connections altogether. If this is an instance returned by open or by accept, then it closes this particular client-server session.

[Source]

     # File lib/drb/drb.rb, line 932
932:     def close
933:       if @socket
934:         @socket.close
935:         @socket = nil
936:       end
937:     end

Get the address of our TCP peer (the other end of the socket we are bound to.

[Source]

     # File lib/drb/drb.rb, line 897
897:     def peeraddr
898:       @socket.peeraddr
899:     end

On the client side, receive a reply from the server.

[Source]

     # File lib/drb/drb.rb, line 920
920:     def recv_reply
921:       @msg.recv_reply(stream)
922:     end

On the server side, receive a request from the client.

[Source]

     # File lib/drb/drb.rb, line 910
910:     def recv_request
911:       @msg.recv_request(stream)
912:     end

On the server side, send a reply to the client.

[Source]

     # File lib/drb/drb.rb, line 915
915:     def send_reply(succ, result)
916:       @msg.send_reply(stream, succ, result)
917:     end

On the client side, send a request to the server.

[Source]

     # File lib/drb/drb.rb, line 905
905:     def send_request(ref, msg_id, arg, b)
906:       @msg.send_request(stream, ref, msg_id, arg, b)
907:     end

Get the socket.

[Source]

     # File lib/drb/drb.rb, line 902
902:     def stream; @socket; end

[Validate]