Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanups & JavaDocs #469

Merged
merged 1 commit into from
May 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/main/java/org/java_websocket/WebSocketFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,20 @@
import org.java_websocket.drafts.Draft;

public interface WebSocketFactory {
public WebSocket createWebSocket( WebSocketAdapter a, Draft d, Socket s );
public WebSocket createWebSocket( WebSocketAdapter a, List<Draft> drafts, Socket s );
/**
* Create a new Websocket with the provided listener, drafts and socket
* @param a The Listener for the WebsocketImpl
* @param d The draft which should be used
* @return A WebsocketImpl
*/
WebSocket createWebSocket( WebSocketAdapter a, Draft d);

/**
* Create a new Websocket with the provided listener, drafts and socket
* @param a The Listener for the WebsocketImpl
* @param drafts The drafts which should be used
* @return A WebsocketImpl
*/
WebSocket createWebSocket( WebSocketAdapter a, List<Draft> drafts);

}
13 changes: 11 additions & 2 deletions src/main/java/org/java_websocket/framing/CloseFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ public interface CloseFrame extends Framedata {
public static final int BUGGYCLOSE = -2;
public static final int FLASHPOLICY = -3;

public int getCloseCode() throws InvalidFrameException;
public String getMessage() throws InvalidDataException;
/**
* Getter for the close code used in this close frame
* @return the used close code
*/
int getCloseCode();

/**
* Getter for the close message used in this close frame
* @return the used close message
*/
String getMessage();
}
31 changes: 30 additions & 1 deletion src/main/java/org/java_websocket/framing/CloseFrameBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,52 @@
import java.nio.ByteBuffer;

public class CloseFrameBuilder extends FramedataImpl1 implements CloseFrame {

/**
* Attribute for just an empty ByteBuffer
*/
static final ByteBuffer emptybytebuffer = ByteBuffer.allocate( 0 );

/**
* The close code used in this close frame
*/
private int code;

/**
* The close message used in this close frame
*/
private String reason;

/**
* Constructor for a close frame
*
* Using opcode closing and fin = true
*/
public CloseFrameBuilder() {
super( Opcode.CLOSING );
setFin( true );
}

/**
* Constructor for a close frame
*
* Using opcode closing and fin = true
*
* @param code The close code causing this close frame
*/
public CloseFrameBuilder( int code ) throws InvalidDataException {
super( Opcode.CLOSING );
setFin( true );
setCodeAndMessage( code, "" );
}

/**
* Constructor for a close frame
*
* Using opcode closing and fin = true
*
* @param code The close code causing this close frame
* @param m The close message explaining this close frame a bit more
*/
public CloseFrameBuilder( int code, String m ) throws InvalidDataException {
super( Opcode.CLOSING );
setFin( true );
Expand Down
31 changes: 24 additions & 7 deletions src/main/java/org/java_websocket/framing/FrameBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,29 @@

public interface FrameBuilder extends Framedata {

public abstract void setFin( boolean fin );

public abstract void setOptcode( Opcode optcode );

public abstract void setPayload( ByteBuffer payload ) throws InvalidDataException;

public abstract void setTransferemasked( boolean transferemasked );
/**
* Setter for fin to indicate if this frame is the final fragment
* @param fin true, if this frame is the final fragment
*/
void setFin( boolean fin );

/**
* Setter for the opcode to use, how the provided "Payload data" should be interpreted
* @param optcode the interpretation as a Opcode
*/
void setOptcode( Opcode optcode );

/**
* Setter for the "Payload data" to use in this frame
* @param payload the "Payload data"
* @throws InvalidDataException indicates that the provided "Payload data" is not a valid data
*/
void setPayload( ByteBuffer payload ) throws InvalidDataException;

/**
* Setter for the transfermask to use in this frame
* @param transferemasked true, "Payload data" is masked
*/
void setTransferemasked( boolean transferemasked );

}
39 changes: 33 additions & 6 deletions src/main/java/org/java_websocket/framing/Framedata.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,40 @@ public interface Framedata {
/**
* Enum which contains the different valid opcodes
*/
public enum Opcode {
enum Opcode {
CONTINUOUS, TEXT, BINARY, PING, PONG, CLOSING
// more to come
}
public boolean isFin();
public boolean getTransfereMasked();
public Opcode getOpcode();
public ByteBuffer getPayloadData();// TODO the separation of the application data and the extension data is yet to be done
public abstract void append( Framedata nextframe ) throws InvalidFrameException;

/**
* Indicates that this is the final fragment in a message. The first fragment MAY also be the final fragment.
* @return true, if this frame is the final fragment
*/
boolean isFin();

/**
* Defines whether the "Payload data" is masked.
* @return true, "Payload data" is masked
*/
boolean getTransfereMasked();

/**
* Defines the interpretation of the "Payload data".
* @return the interpretation as a Opcode
*/
Opcode getOpcode();

/**
* The "Payload data" which was sent in this frame
* @return the "Payload data" as ByteBuffer
*/
ByteBuffer getPayloadData();// TODO the separation of the application data and the extension data is yet to be done

/**
* Appends an additional frame to the current frame
*
* This methods does not override the opcode, but does override the fin
* @param nextframe the additional frame
*/
void append( Framedata nextframe );
}
29 changes: 27 additions & 2 deletions src/main/java/org/java_websocket/framing/FramedataImpl1.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,40 @@
import org.java_websocket.util.Charsetfunctions;

public class FramedataImpl1 implements FrameBuilder {
protected static byte[] emptyarray = {};
/**
* Attribute for just an empty array
*/
private static byte[] emptyarray = {};

/**
* Indicates that this is the final fragment in a message.
*/
protected boolean fin;
/**
* Defines the interpretation of the "Payload data".
*/
protected Opcode optcode;

/**
* The unmasked "Payload data" which was sent in this frame
*/
private ByteBuffer unmaskedpayload;

/**
* Defines whether the "Payload data" is masked.
*/
protected boolean transferemasked;

/**
* Constructor for a FramedataImpl without any attributes set
*/
public FramedataImpl1() {
}

/**
* Constructor for a FramedataImpl without any attributes set apart from the opcode
* @param op the opcode to use
*/
public FramedataImpl1( Opcode op ) {
this.optcode = op;
unmaskedpayload = ByteBuffer.wrap( emptyarray );
Expand Down Expand Up @@ -75,7 +100,7 @@ public void setTransferemasked( boolean transferemasked ) {
}

@Override
public void append( Framedata nextframe ) throws InvalidFrameException {
public void append( Framedata nextframe ) {
ByteBuffer b = nextframe.getPayloadData();
if( unmaskedpayload == null ) {
unmaskedpayload = ByteBuffer.allocate( b.remaining() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ public ByteChannel wrapChannel( SocketChannel channel, SelectionKey key ) throws
}

@Override
public WebSocketImpl createWebSocket( WebSocketAdapter a, Draft d, Socket c ) {
public WebSocketImpl createWebSocket( WebSocketAdapter a, Draft d) {
return new WebSocketImpl( a, d );
}

@Override
public WebSocketImpl createWebSocket( WebSocketAdapter a, List<Draft> d, Socket s ) {
public WebSocketImpl createWebSocket( WebSocketAdapter a, List<Draft> d) {
return new WebSocketImpl( a, d );
}
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@

public class DefaultWebSocketServerFactory implements WebSocketServerFactory {
@Override
public WebSocketImpl createWebSocket( WebSocketAdapter a, Draft d, Socket s ) {
public WebSocketImpl createWebSocket( WebSocketAdapter a, Draft d) {
return new WebSocketImpl( a, d );
}
@Override
public WebSocketImpl createWebSocket( WebSocketAdapter a, List<Draft> d, Socket s ) {
public WebSocketImpl createWebSocket( WebSocketAdapter a, List<Draft> d) {
return new WebSocketImpl( a, d );
}
@Override
Expand Down
34 changes: 6 additions & 28 deletions src/main/java/org/java_websocket/server/WebSocketServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ public void run() {
channel.configureBlocking( false );
Socket socket = channel.socket();
socket.setTcpNoDelay( tcpNoDelay );
WebSocketImpl w = wsf.createWebSocket( this, drafts, socket );
WebSocketImpl w = wsf.createWebSocket( this, drafts );
w.key = channel.register( selector, SelectionKey.OP_READ, w );
try {
w.channel = wsf.wrapChannel( channel, w.key );
Expand Down Expand Up @@ -538,22 +538,6 @@ private void handleFatal( WebSocket conn, Exception e ) {
}
}

/**
* Gets the XML string that should be returned if a client requests a Flash
* security policy.
*
* The default implementation allows access from all remote domains, but
* only on the port that this WebSocketServer is listening on.
*
* This is specifically implemented for gitime's WebSocket client for Flash:
* http://github.com/gimite/web-socket-js
*
* @return An XML String that comforms to Flash's security policy. You MUST
* not include the null char at the end, it is appended automatically.
*/
protected String getFlashSecurityPolicy() {
return "<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"" + getPort() + "\" /></cross-domain-policy>";
}

@Override
public final void onWebsocketMessage( WebSocket conn, String message ) {
Expand Down Expand Up @@ -836,16 +820,10 @@ public void run() {
*/
public interface WebSocketServerFactory extends WebSocketFactory {
@Override
public WebSocketImpl createWebSocket( WebSocketAdapter a, Draft d, Socket s );
WebSocketImpl createWebSocket( WebSocketAdapter a, Draft d);

/**
* Create a new Websocket with the provided listener, drafts and socket
* @param a The Listener for the WebsocketImpl
* @param drafts The drafts which should be used
* @param s The socket which should be used
* @return A WebsocketImpl
*/
public WebSocketImpl createWebSocket( WebSocketAdapter a, List<Draft> drafts, Socket s );
@Override
WebSocketImpl createWebSocket( WebSocketAdapter a, List<Draft> drafts );

/**
* Allows to wrap the Socketchannel( key.channel() ) to insert a protocol layer( like ssl or proxy authentication) beyond the ws layer.
Expand All @@ -855,11 +833,11 @@ public interface WebSocketServerFactory extends WebSocketFactory {
* @return The channel on which the read and write operations will be performed.<br>
* @throws IOException may be thrown while writing on the channel
*/
public ByteChannel wrapChannel( SocketChannel channel, SelectionKey key ) throws IOException;
ByteChannel wrapChannel( SocketChannel channel, SelectionKey key ) throws IOException;

/**
* Allows to shutdown the websocket factory for a clean shutdown
*/
public void close();
void close();
}
}