def _datagram_connect(self): # FIXME raise exception if not of the right family if not self.connected: print "Connecting datagram socket to", self.bind_addr self.connected = True self.python_inbound_handler = PythonInboundHandler(self) bootstrap = Bootstrap().group(NIO_GROUP).channel(NioDatagramChannel) bootstrap.handler(self.python_inbound_handler) # add any options # such as .option(ChannelOption.SO_BROADCAST, True) future = bootstrap.bind(_get_inet_addr(self.bind_addr)) self._handle_channel_future(future, "bind") self.channel = future.channel() print "Completed _datagram_connect on {}".format(self)
def __init__(self, host, port): self.host = host self.port = port group = NioEventLoopGroup() try: #Create the eventLoopGroup and the channel self.handler = CommHandler(self) bootstrap = Bootstrap().group(group).channel(NioSocketChannel).handler(self.handler) bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS,10000) bootstrap.option(ChannelOption.TCP_NODELAY, True) bootstrap.option(ChannelOption.SO_KEEPALIVE, True) #bootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) channel = bootstrap.connect(host, port).syncUninterruptibly() channel.awaitUninterruptibly(5000) if channel is None: print "Could not connect to the Server" else: print "Channel created" pipeline = channel.channel().pipeline() pipeline.addLast("frameDecoder", LengthFieldBasedFrameDecoder(67108864, 0, 4, 0, 4)) pipeline.addLast("protobufDecoder", ProtobufDecoder(Request.getDefaultInstance())) pipeline.addLast("frameEncoder", LengthFieldPrepender(4)) pipeline.addLast("protobufEncoder", ProtobufEncoder()) pipeline.addLast("handler", CommHandler(self)) self.handler.setChannel(channel.channel()) except: print sys.exc_info()[0] finally: print "finally!!" group.shutdownGracefully()
def _connect(self, addr): print "Begin _connect" self._init_client_mode() self.connected = True self.python_inbound_handler = PythonInboundHandler(self) bootstrap = Bootstrap().group(NIO_GROUP).channel(NioSocketChannel) # add any options # FIXME really this is just for SSL handling if self.connect_handlers: for handler in self.connect_handlers: print "Adding connect handler", handler bootstrap.handler(handler) else: print "Adding read adapter", self.python_inbound_handler bootstrap.handler(self.python_inbound_handler) # FIXME also support any options here def completed(f): self._notify_selectors() print "Connection future - connection completed", f host, port = addr future = bootstrap.connect(host, port) future.addListener(completed) self._handle_channel_future(future, "connect") self.channel = future.channel() print "Completed _connect on {}".format(self)
def make_request(group, host, port, req): bootstrap = Bootstrap().\ group(group).\ channel(NioSocketChannel).\ handler(SSLInitializer()).\ option(ChannelOption.TCP_NODELAY, True).\ option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) channel = bootstrap.connect(host, port).sync().channel() data = [None] cv = Condition() class ReadAdapter(ChannelInboundHandlerAdapter): def channelRead(self, ctx, msg): try: length = msg.writerIndex() print "length=", length data[0] = buf = jarray.zeros(length, "b") msg.getBytes(0, buf) cv.acquire() cv.notify() cv.release() finally: msg.release() def channelReadComplete(self, ctx): print "Partial read" # partial reads; this seems to be seen in SSL handshaking/wrap/unwrap pass channel.pipeline().addLast(ReadAdapter()) channel.writeAndFlush(Unpooled.wrappedBuffer(req)).sync() channel.read() # block until we get one full read; note that any real usage would # require parsing the HTTP header (Content-Length) for the number # of bytes to be read cv.acquire() while not data[0]: cv.wait() cv.release() channel.close().sync() return data[0].tostring()