diff options
author | Simon Rettberg | 2014-03-28 17:51:24 +0100 |
---|---|---|
committer | Simon Rettberg | 2014-03-28 17:51:24 +0100 |
commit | fbbfee0a32ce83f5bfe36d78eddafed7226a041c (patch) | |
tree | f3d44e8b9e2bca678165cb3dd97ec1aa1380f244 /src/main/java/org/openslx/imagemaster/thrift/server | |
download | masterserver-fbbfee0a32ce83f5bfe36d78eddafed7226a041c.tar.gz masterserver-fbbfee0a32ce83f5bfe36d78eddafed7226a041c.tar.xz masterserver-fbbfee0a32ce83f5bfe36d78eddafed7226a041c.zip |
Initial Commit
Diffstat (limited to 'src/main/java/org/openslx/imagemaster/thrift/server')
3 files changed, 192 insertions, 0 deletions
diff --git a/src/main/java/org/openslx/imagemaster/thrift/server/BinaryListener.java b/src/main/java/org/openslx/imagemaster/thrift/server/BinaryListener.java new file mode 100644 index 0000000..8eeb7bc --- /dev/null +++ b/src/main/java/org/openslx/imagemaster/thrift/server/BinaryListener.java @@ -0,0 +1,36 @@ +package org.openslx.imagemaster.thrift.server; + +import org.apache.log4j.Logger; +import org.apache.thrift.protocol.TProtocolFactory; +import org.apache.thrift.server.TServer; +import org.apache.thrift.server.TThreadPoolServer; +import org.apache.thrift.transport.TServerSocket; +import org.apache.thrift.transport.TServerTransport; +import org.apache.thrift.transport.TTransportException; +import org.openslx.imagemaster.thrift.iface.ImageServer; +import org.apache.thrift.server.TThreadPoolServer.Args; + +public class BinaryListener implements Runnable +{ + private static Logger log = Logger.getLogger( BinaryListener.class ); + + @Override + public void run() + { + final ImageServerHandler handler = new ImageServerHandler(); + final ImageServer.Processor<ImageServerHandler> processor = new ImageServer.Processor<ImageServerHandler>( handler ); + final TProtocolFactory protFactory = new TBinaryProtocolSafe.Factory( true, true ); + final TServerTransport transport; + try { + transport = new TServerSocket( 9090 ); + } catch ( TTransportException e ) { + log.fatal( "Could not listen on port 9090" ); + return; + } + TServer server = new TThreadPoolServer( new Args( transport ).protocolFactory( protFactory ).processor( processor ) + .minWorkerThreads( 4 ).maxWorkerThreads( 8 ) ); + log.info( "Starting Binary Thrift" ); + server.serve(); + } + +} diff --git a/src/main/java/org/openslx/imagemaster/thrift/server/ImageServerHandler.java b/src/main/java/org/openslx/imagemaster/thrift/server/ImageServerHandler.java new file mode 100644 index 0000000..6be5d40 --- /dev/null +++ b/src/main/java/org/openslx/imagemaster/thrift/server/ImageServerHandler.java @@ -0,0 +1,35 @@ +package org.openslx.imagemaster.thrift.server; + +import org.apache.thrift.TException; +import org.openslx.imagemaster.server.ApiServer; +import org.openslx.imagemaster.thrift.iface.AuthenticationException; +import org.openslx.imagemaster.thrift.iface.ImageServer; +import org.openslx.imagemaster.thrift.iface.InvalidTokenException; +import org.openslx.imagemaster.thrift.iface.SessionData; +import org.openslx.imagemaster.thrift.iface.UserInfo; + +public class ImageServerHandler implements ImageServer.Iface +{ + + @Override + public boolean ping() throws TException + { + // TODO: Return false if service unavailable but running + return true; + } + + @Override + public SessionData authenticate( String username, String password ) + throws AuthenticationException + { + return ApiServer.authenticate( username, password ); + } + + @Override + public UserInfo getUserFromToken( String token ) + throws InvalidTokenException + { + return ApiServer.getUserFromToken( token ); + } + +} diff --git a/src/main/java/org/openslx/imagemaster/thrift/server/TBinaryProtocolSafe.java b/src/main/java/org/openslx/imagemaster/thrift/server/TBinaryProtocolSafe.java new file mode 100644 index 0000000..614be22 --- /dev/null +++ b/src/main/java/org/openslx/imagemaster/thrift/server/TBinaryProtocolSafe.java @@ -0,0 +1,121 @@ +package org.openslx.imagemaster.thrift.server; + +import java.io.UnsupportedEncodingException; +import java.nio.ByteBuffer; + +import org.apache.thrift.TException; +import org.apache.thrift.protocol.TBinaryProtocol; +import org.apache.thrift.protocol.TMessage; +import org.apache.thrift.protocol.TProtocol; +import org.apache.thrift.protocol.TProtocolException; +import org.apache.thrift.protocol.TProtocolFactory; +import org.apache.thrift.transport.TTransport; + +/** + * Binary protocol implementation for thrift. + * Will not read messages bigger than 12MiB. + * + */ +public class TBinaryProtocolSafe extends TBinaryProtocol +{ + + /** + * Factory + */ + public static class Factory implements TProtocolFactory + { + protected boolean strictRead_ = false; + protected boolean strictWrite_ = true; + + public Factory() + { + this( false, true ); + } + + public Factory(boolean strictRead, boolean strictWrite) + { + strictRead_ = strictRead; + strictWrite_ = strictWrite; + } + + public TProtocol getProtocol( TTransport trans ) + { + return new TBinaryProtocolSafe( trans, strictRead_, strictWrite_ ); + } + } + + private static final int maxLen = 12 * 1024 * 1024; // 12 MiB + + /** + * Constructor + */ + public TBinaryProtocolSafe(TTransport trans) + { + this( trans, false, true ); + } + + public TBinaryProtocolSafe(TTransport trans, boolean strictRead, boolean strictWrite) + { + super( trans ); + strictRead_ = strictRead; + strictWrite_ = strictWrite; + } + + /** + * Reading methods. + */ + + public TMessage readMessageBegin() throws TException + { + int size = readI32(); + if ( size > maxLen ) + throw new TProtocolException( TProtocolException.SIZE_LIMIT, "Payload too big." ); + if ( size < 0 ) { + int version = size & VERSION_MASK; + if ( version != VERSION_1 ) { + throw new TProtocolException( TProtocolException.BAD_VERSION, "Bad version in readMessageBegin" ); + } + return new TMessage( readString(), (byte)( size & 0x000000ff ), readI32() ); + } else { + if ( strictRead_ ) { + throw new TProtocolException( TProtocolException.BAD_VERSION, "Missing version in readMessageBegin, old client?" ); + } + return new TMessage( readStringBody( size ), readByte(), readI32() ); + } + } + + public String readString() throws TException + { + int size = readI32(); + if ( size > maxLen ) + throw new TProtocolException( TProtocolException.SIZE_LIMIT, "Payload too big." ); + if ( trans_.getBytesRemainingInBuffer() >= size ) { + try { + String s = new String( trans_.getBuffer(), trans_.getBufferPosition(), size, "UTF-8" ); + trans_.consumeBuffer( size ); + return s; + } catch ( UnsupportedEncodingException e ) { + throw new TException( "JVM DOES NOT SUPPORT UTF-8" ); + } + } + + return readStringBody( size ); + } + + public ByteBuffer readBinary() throws TException + { + int size = readI32(); + if ( size > maxLen ) + throw new TProtocolException( TProtocolException.SIZE_LIMIT, "Payload too big." ); + if ( trans_.getBytesRemainingInBuffer() >= size ) { + ByteBuffer bb = ByteBuffer.wrap( trans_.getBuffer(), trans_.getBufferPosition(), size ); + trans_.consumeBuffer( size ); + return bb; + } + + byte[] buf = new byte[size]; + trans_.readAll( buf, 0, size ); + return ByteBuffer.wrap( buf ); + } + +} |