Permalink
Browse files

DevSynch

  • Loading branch information...
1 parent d3efdfc commit f0b9b4cab019c62bf2e7a51954c5aa0ad9dbf2d2 @nickman committed Aug 12, 2012
View
@@ -47,11 +47,28 @@
</dependency>
<dependency>
- <groupId>org.jboss.netty</groupId>
+ <groupId>io.netty</groupId>
<artifactId>netty</artifactId>
<version>${netty.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ <version>${slf4j.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>ch.qos.logback</groupId>
+ <artifactId>logback-core</artifactId>
+ <version>${logback.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>ch.qos.logback</groupId>
+ <artifactId>logback-classic</artifactId>
+ <version>${logback.version}</version>
+ </dependency>
</dependencies>
View
@@ -0,0 +1,76 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<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/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.helios</groupId>
+ <artifactId>camel-nailgun</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ </parent>
+ <groupId>org.helios</groupId>
+ <artifactId>nailgun-core</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <packaging>jar</packaging>
+ <name>nailgun-core</name>
+ <url>http://www.heliosdev.org/projects/opentrace</url>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.3.2</version>
+ <configuration>
+ <source>1.6</source>
+ <target>1.6</target>
+ </configuration>
+ </plugin>
+ <plugin>
+ <artifactId>maven-release-plugin</artifactId>
+ <version>2.1</version>
+ <configuration>
+ <!--
+ During release:perform, enable the "release" profile
+ -->
+ <releaseProfiles>release</releaseProfiles>
+ <goals>deploy assembly:assembly</goals>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+
+
+ <dependencies>
+ <!-- Test Dependencies -->
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>org.jboss.netty</groupId>
+ <artifactId>netty</artifactId>
+ <version>${netty.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ <version>${slf4j.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>ch.qos.logback</groupId>
+ <artifactId>logback-core</artifactId>
+ <version>${logback.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>ch.qos.logback</groupId>
+ <artifactId>logback-classic</artifactId>
+ <version>${logback.version}</version>
+ </dependency>
+
+ </dependencies>
+
+
+</project>
@@ -27,6 +27,7 @@
import java.io.Serializable;
import java.net.InetAddress;
import java.net.InetSocketAddress;
+import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -35,7 +36,11 @@
import java.util.TreeMap;
import org.helios.nailgun.codecs.NailgunRequestDecoder;
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
+import org.jboss.netty.channel.Channels;
+import org.jboss.netty.channel.DownstreamMessageEvent;
/**
* <p>Title: DefaultNailgunRequestImpl</p>
@@ -58,6 +63,10 @@
private final List<String> arguments = new ArrayList<String>();
/** The netty channel through which the client is communicating */
private transient Channel channel = null;
+
+ /** The name of the response encoding channel handler */
+ public static final String RESP_HANDLER = "response-encoder";
+
/**
@@ -263,6 +272,59 @@ public void setChannel(Channel channel) {
* -- How do we timeout when the ng client is not sending any input ?
*/
+ /*
+ * writeInt(len);
+ writeByte(streamCode);
+ out.write(b, offset, len);
+
+ */
+
+ /**
+ * {@inheritDoc}
+ * @see org.helios.nailgun.NailgunRequest#out(java.lang.CharSequence)
+ */
+ @Override
+ public void out(CharSequence message) {
+ ChannelBuffer header = ChannelBuffers.buffer(5);
+ header.writeInt(message.length());
+ header.writeByte(NailgunConstants.CHUNKTYPE_STDOUT);
+
+ ChannelBuffer response = ChannelBuffers.wrappedBuffer(
+ header,
+ ChannelBuffers.copiedBuffer(message, Charset.defaultCharset())
+ );
+ channel.getPipeline().sendDownstream(new DownstreamMessageEvent(channel, Channels.future(channel), response, channel.getRemoteAddress()));
+ }
+
+ /**
+ * {@inheritDoc}
+ * @see org.helios.nailgun.NailgunRequest#err(java.lang.CharSequence)
+ */
+ @Override
+ public void err(CharSequence message) {
+ ChannelBuffer header = ChannelBuffers.buffer(5);
+ header.writeInt(message.length());
+ header.writeByte(NailgunConstants.CHUNKTYPE_STDERR);
+
+ ChannelBuffer response = ChannelBuffers.wrappedBuffer(
+ header,
+ ChannelBuffers.copiedBuffer(message, Charset.defaultCharset())
+ );
+ channel.getPipeline().sendDownstream(new DownstreamMessageEvent(channel, Channels.future(channel), response, channel.getRemoteAddress()));
+
+ }
+
+ /**
+ * {@inheritDoc}
+ * @see org.helios.nailgun.NailgunRequest#end()
+ */
+ @Override
+ public void end() {
+ ChannelBuffer header = ChannelBuffers.buffer(1);
+ header.writeByte(NailgunConstants.CHUNKTYPE_EXIT);
+ channel.getPipeline().sendDownstream(new DownstreamMessageEvent(channel, Channels.future(channel), header, channel.getRemoteAddress()));
+ channel.close();
+ }
@@ -274,6 +336,7 @@ public void setChannel(Channel channel) {
* @return a <code>String</code> representation
* of this object.
*/
+ @Override
public String toString() {
final String TAB = "\n\t";
StringBuilder retValue = new StringBuilder("DefaultNailgunRequestImpl [")
@@ -49,5 +49,22 @@
*/
public abstract int getRemotePort();
+ /**
+ * Returns a message back to the nail gun client's StdOut
+ * @param message The message to return
+ */
+ public abstract void out(CharSequence message);
+
+ /**
+ * Returns a message back to the nail gun client's StdErr
+ * @param message The message to return
+ */
+ public abstract void err(CharSequence message);
+
+ /**
+ * Terminates the response stream
+ */
+ public abstract void end();
+
}
@@ -30,6 +30,7 @@
import org.helios.nailgun.codecs.NailgunRequestDispatcher;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
+import org.jboss.netty.handler.codec.string.StringEncoder;
import org.jboss.netty.handler.execution.ExecutionHandler;
import org.jboss.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor;
import org.jboss.netty.handler.logging.LoggingHandler;
@@ -51,16 +52,20 @@
new OrderedMemoryAwareThreadPoolExecutor(16, 1048576, 1048576));
/** The shareable nailgun request decoder */
protected final NailgunRequestDecoder requestDecoder = new NailgunRequestDecoder();
+ /** The shareable response encoder */
+ protected final StringEncoder responseEncoder = new StringEncoder();
+
/**
* {@inheritDoc}
* @see org.jboss.netty.channel.ChannelPipelineFactory#getPipeline()
*/
@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = pipeline();
- //pipeline.addLast("nailgun-logger", new LoggingHandler(InternalLogLevel.INFO, true));
+ pipeline.addLast("nailgun-logger", new LoggingHandler(InternalLogLevel.INFO, true));
//pipeline.addLast("nailgun-logger", new LoggingHandler(InternalLogLevel.INFO, false));
//pipeline.addLast("nailgun-logger", new LoggingHandler(InternalLogLevel.INFO, false));
+ //pipeline.addLast("response-encoder", responseEncoder);
pipeline.addLast("nailgun-decoder", requestDecoder);
pipeline.addLast("nailgun-executor", executionHandler);
pipeline.addLast("nailgun-dispatcher", requestDispatcher);
@@ -128,6 +128,7 @@ protected void checkpoint(ChannelHandlerContext ctx, DecodingState state) {
if(log.isDebugEnabled()) log.debug("checkpoint:" + state);
}
+ @Override
protected void checkpoint(DecodingState state) {
super.checkpoint(state);
}
@@ -144,6 +145,7 @@ protected NailgunRequest decode(final ChannelHandlerContext ctx, final Channel c
if(context.getMessage().getRemoteAddress()==null) {
channelGroup.add(channel);
channel.getCloseFuture().addListener(new ChannelFutureListener(){
+ @Override
public void operationComplete(ChannelFuture future) throws Exception {
channelGroup.remove(future.getChannel());
}
@@ -25,7 +25,10 @@
package org.helios.nailgun.codecs;
import org.helios.nailgun.NailgunRequest;
+import org.helios.nailgun.handler.NailgunRequestHandler;
+import org.helios.nailgun.handler.RequestHandlerRegistry;
import org.jboss.netty.channel.ChannelHandlerContext;
+import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.logging.InternalLogger;
@@ -57,13 +60,27 @@
* {@inheritDoc}
* @see org.jboss.netty.channel.SimpleChannelUpstreamHandler#messageReceived(org.jboss.netty.channel.ChannelHandlerContext, org.jboss.netty.channel.MessageEvent)
*/
+ @Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
System.out.println("[" + Thread.currentThread().toString() + "] Processing NailgunRequest:\n" + e.getMessage());
Object message = e.getMessage();
if(message==null || !(message instanceof NailgunRequest)) {
log.warn("RequestDispatcher received invalid message [" + message + "]");
}
NailgunRequest request = (NailgunRequest)message;
+ NailgunRequestHandler handler = RequestHandlerRegistry.getInstance().lookup(request);
+ if(handler==null) log.warn("No handler found for request " + request);
+ handler.onNailgunRequest(request);
+ }
+
+ /**
+ * {@inheritDoc}
+ * @see org.jboss.netty.channel.SimpleChannelUpstreamHandler#exceptionCaught(org.jboss.netty.channel.ChannelHandlerContext, org.jboss.netty.channel.ExceptionEvent)
+ */
+ @Override
+ public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
+ log.error("Failed to dispatch request in ctx [" + ctx + "]", e.getCause());
+ //super.exceptionCaught(ctx, e);
}
}
@@ -0,0 +1,55 @@
+/**
+ * Helios, OpenSource Monitoring
+ * Brought to you by the Helios Development Group
+ *
+ * Copyright 2007, Helios Development Group and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ *
+ */
+package org.helios.nailgun.codecs;
+
+import java.nio.charset.Charset;
+
+import org.jboss.netty.handler.codec.string.StringEncoder;
+
+/**
+ * <p>Title: NailgunResponseEncoder</p>
+ * <p>Description: </p>
+ * <p>Company: Helios Development Group LLC</p>
+ * @author Whitehead (nwhitehead AT heliosdev DOT org)
+ * <p><code>org.helios.nailgun.codecs.NailgunResponseEncoder</code></p>
+ */
+
+public class NailgunResponseEncoder extends StringEncoder {
+
+ /**
+ * Creates a new NailgunResponseEncoder
+ */
+ public NailgunResponseEncoder() {
+ }
+
+ /**
+ * Creates a new NailgunResponseEncoder
+ * @param charset The character set for this encoder
+ */
+ public NailgunResponseEncoder(Charset charset) {
+ super(charset);
+ }
+
+}
Oops, something went wrong.

0 comments on commit f0b9b4c

Please sign in to comment.