Permalink
Browse files

Initial spike of a netty integration module.

  • Loading branch information...
1 parent 4c05fed commit bc41ebc6f123a5b6382ee3255a0f6539300c0863 @chirino chirino committed Jan 15, 2013
View
@@ -0,0 +1,94 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ Copyright (C) 2012 FuseSource, Inc.
+ http://fusesource.com
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.fusesource.hawtdispatch</groupId>
+ <artifactId>hawtdispatch-project</artifactId>
+ <version>1.14-SNAPSHOT</version>
+ </parent>
+
+ <groupId>org.fusesource.hawtdispatch</groupId>
+ <artifactId>hawtdispatch-netty</artifactId>
+ <version>1.14-SNAPSHOT</version>
+ <packaging>bundle</packaging>
+
+ <description>HawtDispatch Transport: Transport abstractions for HawtDispatch</description>
+
+ <properties>
+ <junit-version>4.7</junit-version>
+ <asm-version>3.1</asm-version>
+ <log4j-version>1.2.14</log4j-version>
+ <osgi-version>4.2.0</osgi-version>
+ </properties>
+
+ <dependencies>
+
+ <dependency>
+ <groupId>org.fusesource.hawtdispatch</groupId>
+ <artifactId>hawtdispatch</artifactId>
+ <version>1.14-SNAPSHOT</version>
+ </dependency>
+ <dependency>
+ <groupId>io.netty</groupId>
+ <artifactId>netty-transport</artifactId>
+ <version>4.0.0.Beta1-SNAPSHOT</version>
+ </dependency>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>${junit-version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>log4j</groupId>
+ <artifactId>log4j</artifactId>
+ <version>${log4j-version}</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <source>1.6</source>
+ <target>1.6</target>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ <extensions>true</extensions>
+ <inherited>true</inherited>
+ <configuration>
+ <instructions>
+ </instructions>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
@@ -0,0 +1,114 @@
+/*
+ * Copyright 2012 The Netty Project
+ * Copyright 2013 Red Hat, Inc.
+ *
+ * The Netty Project licenses this file to you under the Apache License,
+ * version 2.0 (the "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+package org.fusesource.hawtdispatch.netty;
+
+import io.netty.channel.AbstractChannel;
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelPromise;
+import io.netty.channel.EventLoop;
+
+import java.net.ConnectException;
+import java.net.InetSocketAddress;
+import java.net.SocketAddress;
+import java.nio.channels.SocketChannel;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Abstract base class for {@link io.netty.channel.Channel} implementations that use
+ * HawtDispatch.
+ */
+abstract class HawtAbstractChannel extends AbstractChannel {
+
+ protected volatile java.nio.channels.Channel ch;
+
+ /**
+ * The future of the current connection attempt. If not null, subsequent
+ * connection attempts will fail.
+ */
+ protected ScheduledFuture<?> connectTimeoutFuture;
+ private ConnectException connectTimeoutException;
+
+ /**
+ * Creates a new instance.
+ *
+ * @param id
+ * the unique non-negative integer ID of this channel.
+ * Specify {@code null} to auto-generate a unique negative integer
+ * ID.
+ * @param parent
+ * the parent of this channel. {@code null} if there's no parent.
+ * @param ch
+ * the {@link SocketChannel} which will handle the IO or {@code null} if not created yet.
+ */
+ protected HawtAbstractChannel(Channel parent, Integer id, java.nio.channels.Channel ch) {
+ super(parent, id);
+ this.ch = ch;
+ }
+
+ @Override
+ public InetSocketAddress localAddress() {
+ return (InetSocketAddress) super.localAddress();
+ }
+
+ @Override
+ public InetSocketAddress remoteAddress() {
+ return (InetSocketAddress) super.remoteAddress();
+ }
+
+ /**
+ * Return the underlying {@link SocketChannel}. Be aware this should only be called after it was set as
+ * otherwise it will throw an {@link IllegalStateException}.
+ */
+ protected java.nio.channels.Channel javaChannel() {
+ if (ch == null) {
+ throw new IllegalStateException("Try to access Channel before eventLoop was registered");
+ }
+ return ch;
+ }
+
+ @Override
+ public boolean isOpen() {
+ if (ch == null) {
+ return true;
+ }
+ return ch.isOpen();
+ }
+
+ @Override
+ protected boolean isCompatible(EventLoop loop) {
+ return loop instanceof HawtEventLoop;
+ }
+
+ @Override
+ protected AbstractUnsafe newUnsafe() {
+ return new HawtAbstractUnsafe();
+ }
+
+ /**
+ * Connect to the remote peer using the given localAddress if one is specified or {@code null} otherwise.
+ */
+ protected abstract void doConnect(SocketAddress remoteAddress,
+ SocketAddress localAddress, ChannelPromise connectPromise);
+
+ class HawtAbstractUnsafe extends AbstractUnsafe {
+ @Override
+ public void connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) {
+ doConnect(remoteAddress, localAddress, promise);
+ }
+ }
+}
Oops, something went wrong.

0 comments on commit bc41ebc

Please sign in to comment.