vendor/bundle/ruby/1.8/gems/Ruby-MemCache-0.0.1/lib/memcache.rb
def send( pairs, multiline=false )
		raise TypeError, "type mismatch: #{pairs.class.name} given" unless
			pairs.is_a?( Hash )
		buffers = {}
		rval = {}

		# Fetch the Method object for the IO handler
		handler = self.method( :handle_line_io )

		# Set up the buffers and reactor for the exchange
		pairs.each do |server,cmds|
			unless server.alive?
				rval[server] = nil
				pairs.delete( server )
				next
			end

			# Handle either Arrayish or Stringish commandsets
			wbuf = cmds.respond_to?( :join ) ? cmds.join( CRLF ) : cmds.to_s
			self.debug_msg( "Created command %p for %p", wbuf, server )
			wbuf += CRLF

			# Make a buffer tuple (read/write) for the server
			buffers[server] = { :rbuf => '', :wbuf => wbuf }

			# Register the server's socket with the reactor
			@reactor.register( server.socket, :write, :read, server,
							   buffers[server], multiline, &handler )
		end

		# Do all the IO at once
		self.debug_msg( "Reactor starting for %d IOs", @reactor.handles.length )
		@reactor.poll until @reactor.empty?
		self.debug_msg( "Reactor finished." )

		# Build the return value, delegating the processing to a block if one
		# was given.
		pairs.each {|server,cmds|

			# Handle protocol errors if they happen.  I have no idea if this is
			# desirable/correct behavior: none of the other clients react to
			# CLIENT_ERROR or SERVER_ERROR at all; in fact, I think they'd all
			# hang on one like this one did before I added them to the
			# terminator pattern in #handle_line_io. So this may change in the
			# future if it ends up being better to just ignore errors, try to
			# cache/fetch what we can, and hope returning nil will suffice in
			# the face of error conditions
			self.handle_protocol_error( buffers[server][:rbuf], server ) if
				ANY_ERROR.match( buffers[server][:rbuf] )

			# If the caller is doing processing on the reply, yield each buffer
			# in turn. Otherwise, just use the raw buffer as the return value
			if block_given?
				self.debug_msg( "Yielding value/s %p for %p",
					buffers[server][:rbuf], server )
				rval[server] = yield( server, buffers[server][:rbuf] )
			else
				rval[server] = buffers[server][:rbuf]
			end
		}

		return rval
	end
### Given +pairs+ of MemCache::Server objects and Strings or Arrays of
### commands for each server, do multiplexed IO between all of them, reading
### single-line responses.
Given +pairs+ of MemCache::Server objects and Strings or Arrays of
commands for each server, do multiplexed IO between all of them, reading
single-line responses.
Given + pairs + of MemCache :: Server objects and Strings or Arrays of commands for each server do multiplexed IO between all of them reading single - line responses .
def send( pairs, multiline=false )
		raise TypeError, "type mismatch: #{pairs.class.name} given" unless
			pairs.is_a?( Hash )
		buffers = {}
		rval = {}
		handler = self.method( :handle_line_io )
		pairs.each do |server,cmds|
			unless server.alive?
				rval[server] = nil
				pairs.delete( server )
				next
			end
			wbuf = cmds.respond_to?( :join ) ? cmds.join( CRLF ) : cmds.to_s
			self.debug_msg( "Created command %p for %p", wbuf, server )
			wbuf += CRLF
			buffers[server] = { :rbuf => '', :wbuf => wbuf }
			@reactor.register( server.socket, :write, :read, server,
							   buffers[server], multiline, &handler )
		end
		self.debug_msg( "Reactor starting for %d IOs", @reactor.handles.length )
		@reactor.poll until @reactor.empty?
		self.debug_msg( "Reactor finished." )
		pairs.each {|server,cmds|
			self.handle_protocol_error( buffers[server][:rbuf], server ) if
				ANY_ERROR.match( buffers[server][:rbuf] )
			if block_given?
				self.debug_msg( "Yielding value/s %p for %p",
					buffers[server][:rbuf], server )
				rval[server] = yield( server, buffers[server][:rbuf] )
			else
				rval[server] = buffers[server][:rbuf]
			end
		}
		return rval
	end
def send ( pairs , multiline = false ) raise TypeError , "type mismatch: #{pairs.class.name} given" unless pairs . is_a? ( Hash ) buffers = { } rval = { } handler = self . method ( :handle_line_io ) pairs . each do | server , cmds | unless server . alive? rval [ server ] = nil pairs . delete ( server ) next end wbuf = cmds . respond_to? ( :join ) ? cmds . join ( CRLF ) : cmds . to_s self . debug_msg ( "Created command %p for %p" , wbuf , server ) wbuf += CRLF buffers [ server ] = { :rbuf => '' , :wbuf => wbuf } @reactor . register ( server . socket , :write , :read , server , buffers [ server ] , multiline , & handler ) end self . debug_msg ( "Reactor starting for %d IOs" , @reactor . handles . length ) @reactor . poll until @reactor . empty? self . debug_msg ( "Reactor finished." ) pairs . each { | server , cmds | self . handle_protocol_error ( buffers [ server ] [ :rbuf ] , server ) if ANY_ERROR . match ( buffers [ server ] [ :rbuf ] ) if block_given? self . debug_msg ( "Yielding value/s %p for %p" , buffers [ server ] [ :rbuf ] , server ) rval [ server ] = yield ( server , buffers [ server ] [ :rbuf ] ) else rval [ server ] = buffers [ server ] [ :rbuf ] end } return rval end
Given +pairs+ of MemCache::Server objects and Strings or Arrays of
commands for each server, do multiplexed IO between all of them, reading
single-line responses.
Given + pairs + of MemCache :: Server objects and Strings or Arrays of commands for each server do multiplexed IO between all of them reading single - line responses .
# Fetch the Method object for the IO handler # Set up the buffers and reactor for the exchange # Handle either Arrayish or Stringish commandsets # Make a buffer tuple (read/write) for the server # Register the server's socket with the reactor # Do all the IO at once # Build the return value, delegating the processing to a block if one # was given. # Handle protocol errors if they happen.  I have no idea if this is # desirable/correct behavior: none of the other clients react to # CLIENT_ERROR or SERVER_ERROR at all; in fact, I think they'd all # hang on one like this one did before I added them to the # terminator pattern in #handle_line_io. So this may change in the # future if it ends up being better to just ignore errors, try to # cache/fetch what we can, and hope returning nil will suffice in # the face of error conditions # If the caller is doing processing on the reply, yield each buffer # in turn. Otherwise, just use the raw buffer as the return value
pairs
multiline