Skip to content

Commit

Permalink
#28 (#34)
Browse files Browse the repository at this point in the history
* #28 fix javadocs

* #28 javadocs

* #28 fix javadoc

* #28 javadocs & generics

* #28 fix javadocs

* #28 fix javadocs

* #28 fix javadocs

* #28 fix javadocs, update declared exceptions, update quickfix dependency

* #28 custom javadoc tags

* added javadocs

* #28 codacy badge
  • Loading branch information
kpavlov authored Jan 22, 2017
1 parent 7ae91c1 commit 2f9cef0
Show file tree
Hide file tree
Showing 22 changed files with 150 additions and 60 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fixio - FIX Protocol Support for Netty [![Build Status](https://travis-ci.org/kpavlov/fixio.png?branch=master)](https://travis-ci.org/kpavlov/fixio)
fixio - FIX Protocol Support for Netty [![Build Status](https://travis-ci.org/kpavlov/fixio.png?branch=master)](https://travis-ci.org/kpavlov/fixio) [![Codacy Badge](https://api.codacy.com/project/badge/Grade/7a6c7475813e44c5a96abe915ed60e73)](https://www.codacy.com/app/kpavlov/fixio?utm_source=github.com&utm_medium=referral&utm_content=kpavlov/fixio&utm_campaign=Badge_Grade)
=====

# Overview #
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/java/fixio/FixClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ public void setAuthenticationProvider(AuthenticationProvider authenticationProvi

/**
* Connect and start FIX session to specified host and port.
*
* @param host hostname
* @param port a TCP port
* @return ChannelFuture Channel close future which will resolve when the connection is closed.
* @throws InterruptedException when connection interrupted
*/
@SuppressWarnings("WeakerAccess")
public ChannelFuture connect(String host, int port) throws InterruptedException {
Expand Down
13 changes: 10 additions & 3 deletions core/src/main/java/fixio/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ public class Utils {
private Utils() {
}


/**
* @link http://www.javacodegeeks.com/2010/11/java-best-practices-char-to-byte-and.html
* Converts a String to ASCII byte array, provided that string contains only ASCII characters.
*
* @param str string to convert
* @return a byte array
* @see <a href="http://www.javacodegeeks.com/2010/11/java-best-practices-char-to-byte-and.html">Java Best Practices char to byte</a>
*/
public static byte[] stringToBytesASCII(String str) {
char[] buffer = str.toCharArray();
Expand All @@ -20,7 +23,11 @@ public static byte[] stringToBytesASCII(String str) {
}

/**
* @link http://www.javacodegeeks.com/2010/11/java-best-practices-char-to-byte-and.html
* Converts ASCII byte array to String, provided that string contains only ASCII characters.
*
* @param bytes a byte array tp convert
* @return converted String
* @see <a href="http://www.javacodegeeks.com/2010/11/java-best-practices-char-to-byte-and.html">Java Best Practices char to byte</a>
*/
public static String bytesToStringASCII(byte[] bytes) {
final int length = bytes.length;
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/fixio/fixprotocol/FieldListBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@

/**
* Represent a ordered list of fields.
* <p/>
* <p>
* Provides methods to add new fields.
* </p>
*/
interface FieldListBuilder<T> {

Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/fixio/fixprotocol/FixMessageBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@

/**
* Represents outgoing FIX Protocol message.
* <p/>
* <p>
* Messages of this type are passed into {@link fixio.netty.codec.FixMessageEncoder}.
* </p>
*/
public interface FixMessageBuilder extends FieldListBuilder<FixMessageBuilder> {
FixMessageHeader getHeader();
Expand Down
12 changes: 11 additions & 1 deletion core/src/main/java/fixio/fixprotocol/FixMessageBuilderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ public class FixMessageBuilderImpl implements FixMessage, FixMessageBuilder {

/**
* Creates FixMessageBuilderImpl with expected body field count.
* <p/>
* <p>
* Providing expected capacity eliminates unnecessary growing of internal ArrayList storing body fields.
* </p>
*
* @param expectedBodyFieldCount estimated maximum number of field in message body
*/
public FixMessageBuilderImpl(int expectedBodyFieldCount) {
this.header = new FixMessageHeader();
Expand All @@ -44,6 +47,9 @@ public FixMessageBuilderImpl(int expectedBodyFieldCount) {

/**
* Creates FixMessageBuilderImpl with specified FixMessageHeader and FixMessageTrailer.
*
* @param header message header
* @param trailer message trailer
*/
public FixMessageBuilderImpl(FixMessageHeader header, final FixMessageTrailer trailer) {
assert (header != null) : "FixMessageHeader is expected";
Expand All @@ -66,6 +72,7 @@ public FixMessageBuilderImpl() {
* Creates FixMessageBuilderImpl with specified message type (tag 35)
* and default expected body field count.
*
* @param messageType Value of message type (tag 35)
* @see #DEFAULT_BODY_FIELD_COUNT
*/
public FixMessageBuilderImpl(String messageType) {
Expand All @@ -76,6 +83,9 @@ public FixMessageBuilderImpl(String messageType) {
/**
* Creates FixMessageBuilderImpl with specified message type (tag 35)
* and expected body field count.
*
* @param messageType Value of message type (tag 35)
* @param expectedBodyFieldCount estimated maximum number of field in message body
*/
public FixMessageBuilderImpl(String messageType, int expectedBodyFieldCount) {
this(expectedBodyFieldCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import fixio.fixprotocol.FieldType;
import fixio.fixprotocol.FixMessageFragment;

public abstract class AbstractField<T> implements FixMessageFragment {
public abstract class AbstractField<T> implements FixMessageFragment<T> {

private final int tagNum;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package fixio.fixprotocol.fields;

public abstract class AbstractTemporalField extends AbstractField<Long> {
public abstract class AbstractTemporalField extends AbstractField {

protected final long value;

Expand All @@ -13,7 +13,7 @@ public AbstractTemporalField(int tagNum, long timestampMillis) {
* Returns timestamp value in milliseconds.
* Use with caution, since it causes value autoboxing.
*
* @return timestamp value in milliseconds.
* @return value in milliseconds as {@link java.lang.Long}. Prefer {@link #timestampMillis()} to avoid autoboxing.
* @see #timestampMillis()
*/
@Override
Expand All @@ -23,6 +23,8 @@ public final Long getValue() {

/**
* Useful when you need to get a milliseconds without autoboxing
*
* @return value in milliseconds
*/
public final long timestampMillis() {
return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@

/**
* High performance thread-safe Fixed-Point Number implementation.
* <p/>
* <p>
* This class should be used as a replacement for {@link java.math.BigDecimal} when dealing with monetary data in FIX protocol API.
* <p/>
* </p>
* <p>
* This implementation is <strong>thread-safe.</strong>
* </p>
*/
public class FixedPointNumber extends Number {

/**
* Negative or positive scaled value decimal value: <code>scaledValue := value * (10^scale)</code>
*/
private final long scaledValue;

/**
* If zero or positive, the scale is the number of digits to the right of the decimal point.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@

/**
* Field representing Date represented in UTC (Universal Time Coordinated, also known as "GMT") in YYYYMMDD format.
* <p/>
* <p>
* This special-purpose field is paired with UTCTimeOnly to form a proper UTCTimestamp for bandwidth-sensitive messages.
* <p/>
* </p>
* <p>
* Valid values:
* YYYY = 0000-9999, MM = 01-12, DD = 01-31.
* <p/>
* YYYY = 0000-9999,
* MM = 01-12,
* DD = 01-31.
* </p>
* Example(s): <code>MDEntryDate="20030910"</code>
*/
public class UTCDateOnlyField extends AbstractTemporalField {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import fixio.fixprotocol.FixMessage;
import fixio.fixprotocol.FixMessageBuilder;
import fixio.validator.BusinessRejectException;
import fixio.validator.FixMessageValidator;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
Expand All @@ -41,16 +42,14 @@ public CompositeFixApplicationAdapter(List<FixMessageValidator> validators, List
}

@Override
public void onMessage(ChannelHandlerContext ctx, FixMessage msg, List<Object> out) throws Exception {
public void onMessage(ChannelHandlerContext ctx, FixMessage msg, List<Object> out) throws BusinessRejectException {
assert (msg != null) : "Message can't be null";
LOGGER.info("Received : {}", msg);

//Validate
if (validators != null) {
for (FixMessageValidator validator : validators) {
validator.validate(ctx, msg);
}
}

//Business handler
if (handlers != null) {
Expand All @@ -71,7 +70,7 @@ public void onMessage(ChannelHandlerContext ctx, FixMessage msg, List<Object> ou
}

@Override
public void beforeSendMessage(ChannelHandlerContext ctx, FixMessageBuilder msg) throws Exception {
public void beforeSendMessage(ChannelHandlerContext ctx, FixMessageBuilder msg) {
for (FixMessageHandler handler : handlers) {
handler.beforeSendMessage(ctx, msg);
}
Expand Down
21 changes: 19 additions & 2 deletions core/src/main/java/fixio/handlers/FixApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import fixio.events.LogoutEvent;
import fixio.fixprotocol.FixMessage;
import fixio.fixprotocol.FixMessageBuilder;
import fixio.validator.BusinessRejectException;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;

Expand All @@ -28,21 +29,37 @@ public interface FixApplication extends ChannelHandler {

/**
* Invoked after FIX session was successfully established.
*
* @param ctx current {@link ChannelHandlerContext}
* @param msg {@link LogonEvent} message to handle
*/
void onLogon(ChannelHandlerContext ctx, LogonEvent msg);

/**
* Invoked after FIX session was closed.
*
* @param ctx current {@link ChannelHandlerContext}
* @param msg {@link LogonEvent} message to handle
*/
void onLogout(ChannelHandlerContext ctx, LogoutEvent msg);

/**
* Invoked when message arrived
*
* @param ctx current {@link ChannelHandlerContext}
* @param msg a {@link FixMessage} to handle
* @param out a {@link List} where decoded messages should be added
* @throws BusinessRejectException when message can't be accepted
* @throws InterruptedException if interrupted while processing a message
*/
void onMessage(ChannelHandlerContext ctx, FixMessage msg, List<Object> out) throws Exception;
void onMessage(ChannelHandlerContext ctx, FixMessage msg, List<Object> out)
throws BusinessRejectException, InterruptedException;

/**
* Invoked before {@link FixMessageBuilder} is sent.
*
* @param ctx current {@link ChannelHandlerContext}
* @param messageBuilder a message builder
*/
void beforeSendMessage(ChannelHandlerContext ctx, FixMessageBuilder messageBuilder) throws Exception;
void beforeSendMessage(ChannelHandlerContext ctx, FixMessageBuilder messageBuilder);
}
24 changes: 20 additions & 4 deletions core/src/main/java/fixio/handlers/FixApplicationAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import fixio.events.LogoutEvent;
import fixio.fixprotocol.FixMessage;
import fixio.fixprotocol.FixMessageBuilder;
import fixio.validator.BusinessRejectException;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
Expand Down Expand Up @@ -48,25 +49,40 @@ protected void decode(ChannelHandlerContext ctx, Object msg, List<Object> out) t
}
}

/**
* @implNote This implementation does nothing.
*/
@Override
public void onLogon(ChannelHandlerContext ctx, LogonEvent msg) {
}

/**
* @implSpec This implementation does nothing.
*/
@Override
public void onLogout(ChannelHandlerContext ctx, LogoutEvent msg) {
}

/**
* @implSpec This implementation does nothing.
*/
@Override
public void onMessage(ChannelHandlerContext ctx, FixMessage msg, List<Object> out) throws Exception {
public void onMessage(ChannelHandlerContext ctx, FixMessage msg, List<Object> out) throws BusinessRejectException, InterruptedException {
}

/**
* @implSpec This implementation does nothing.
*/
@Override
public void beforeSendMessage(ChannelHandlerContext ctx, FixMessageBuilder msg) throws Exception {
public void beforeSendMessage(ChannelHandlerContext ctx, FixMessageBuilder msg) {
}

/**
* @implSpec This implementation initiates closing the {@link ChannelHandlerContext} (connection)
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
LOGGER.error("Uncaught application exception.", cause);
ctx.close().sync();
ctx.close();
}
}
2 changes: 2 additions & 0 deletions core/src/main/java/fixio/handlers/FixMessageHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public interface FixMessageHandler {
/**
* Performs some processing on incoming {@link FixMessage}.
*
* @param ctx current ChannelHandlerContext
* @param msg a FixMessage to handle
* @return true if message should be handled by following {@link FixMessageHandler} in a chain.
*/
boolean handle(ChannelHandlerContext ctx, FixMessage msg);
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/java/fixio/netty/codec/FixMessageDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@

/**
* Decodes series of {@link ByteBuf}s into FixMessages.
* <p/>
* <p>
* Use following code to configure {@link io.netty.channel.ChannelPipeline}:
* </p>
* <pre><code>
* ChannelPipeline pipeline = ch.pipeline();
* pipeline.addLast("tagDecoder", new DelimiterBasedFrameDecoder(1024, Unpooled.wrappedBuffer(new byte[]{1})));
* pipeline.addLast("fixMessageDecoder", new FixMessageDecoder());
* pipeline.addLast("fixMessageEncoder", new FixMessageEncoder());
* </code></pre>
* <p/>
* FixMessageDecoder should be preceded in pipeline with {@link io.netty.handler.codec.DelimiterBasedFrameDecoder}, which is responsible for detecting FIX Protocol tags.
* <p>
* FixMessageDecoder should be preceded in pipeline with {@link io.netty.handler.codec.DelimiterBasedFrameDecoder},
* which is responsible for detecting FIX Protocol tags.
* </p>
* <p/>
* <p>
* <strong>This class is not thread safe!</strong>
* It should be a separate instance per {@link io.netty.channel.Channel}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ protected void prepareMessageToSend(ChannelHandlerContext ctx, FixSession sessio
/**
* Retrieves {@link FixSession} from context.
*
* @return null if session not established.
* @return FixSession or null, if session not established.
* @param ctx context
*/
protected FixSession getSession(ChannelHandlerContext ctx) {
Attribute<FixSession> fixSessionAttribute = ctx.channel().attr(FIX_SESSION_KEY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@

/**
* Simple java bean FixSessionSettingsProvider implementation.
* <p/>
* <p>
* <strong>Thread-safety:</strong> This implementation is immutable and thread-safe.
* </p>
*/
public class FixSessionSettingsProviderImpl implements FixSessionSettingsProvider {

Expand Down
4 changes: 2 additions & 2 deletions core/src/test/java/fixio/FixConversationIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void testBusinessMessage() throws InterruptedException {
private static class ServerLogicHandler extends FixApplicationAdapter {

@Override
public void onMessage(ChannelHandlerContext ctx, FixMessage msg, List<Object> out) throws Exception {
public void onMessage(ChannelHandlerContext ctx, FixMessage msg, List<Object> out) {
if (MessageTypes.USER_REQUEST.equals(msg.getMessageType())) {
conversation.add(msg);
ctx.writeAndFlush(createUserStatusReport());
Expand All @@ -124,7 +124,7 @@ public void onMessage(ChannelHandlerContext ctx, FixMessage msg, List<Object> ou
private class ClientApp extends FixApplicationAdapter {

@Override
public void onMessage(ChannelHandlerContext ctx, FixMessage msg, List<Object> out) throws Exception {
public void onMessage(ChannelHandlerContext ctx, FixMessage msg, List<Object> out) throws InterruptedException {
if ("BF".equals(msg.getMessageType())) {
conversation.add(msg);
client.disconnect();
Expand Down
Loading

0 comments on commit 2f9cef0

Please sign in to comment.