-
Notifications
You must be signed in to change notification settings - Fork 255
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
Udp support #108
Merged
Merged
Udp support #108
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
rx-netty/src/examples/java/io/reactivex/netty/examples/java/HelloUdpClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.reactivex.netty.examples.java; | ||
|
||
import io.netty.channel.socket.DatagramPacket; | ||
import io.reactivex.netty.RxNetty; | ||
import io.reactivex.netty.channel.ObservableConnection; | ||
import rx.Observable; | ||
import rx.functions.Action1; | ||
import rx.functions.Func1; | ||
|
||
import java.nio.charset.Charset; | ||
|
||
/** | ||
* @author Nitesh Kant | ||
*/ | ||
public final class HelloUdpClient { | ||
|
||
public static void main(String[] args) { | ||
RxNetty.createUdpClient("localhost", HelloUdpServer.PORT).connect() | ||
.flatMap(new Func1<ObservableConnection<DatagramPacket, DatagramPacket>, | ||
Observable<DatagramPacket>>() { | ||
@Override | ||
public Observable<DatagramPacket> call(ObservableConnection<DatagramPacket, DatagramPacket> connection) { | ||
connection.writeStringAndFlush("Is there anybody out there?"); | ||
return connection.getInput(); | ||
} | ||
}).toBlockingObservable().forEach(new Action1<DatagramPacket>() { | ||
@Override | ||
public void call(DatagramPacket datagramPacket) { | ||
System.out.println("Received a new message: " | ||
+ datagramPacket.content().toString(Charset.defaultCharset())); | ||
} | ||
}); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
rx-netty/src/examples/java/io/reactivex/netty/examples/java/HelloUdpServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package io.reactivex.netty.examples.java; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.channel.ChannelPipeline; | ||
import io.netty.channel.socket.DatagramPacket; | ||
import io.netty.handler.logging.LogLevel; | ||
import io.netty.handler.logging.LoggingHandler; | ||
import io.reactivex.netty.RxNetty; | ||
import io.reactivex.netty.channel.ConnectionHandler; | ||
import io.reactivex.netty.channel.ObservableConnection; | ||
import io.reactivex.netty.pipeline.PipelineConfigurator; | ||
import rx.Observable; | ||
import rx.functions.Func1; | ||
|
||
import java.net.InetSocketAddress; | ||
import java.nio.charset.Charset; | ||
|
||
/** | ||
* @author Nitesh Kant | ||
*/ | ||
public final class HelloUdpServer { | ||
|
||
private static final byte[] WELCOME_MSG_BYTES = "Welcome to the broadcast world!".getBytes(Charset.defaultCharset()); | ||
public static final int PORT = 8000; | ||
|
||
public static void main(String[] args) { | ||
RxNetty.createUdpServer(PORT, new ConnectionHandler<DatagramPacket, DatagramPacket>() { | ||
@Override | ||
public Observable<Void> handle(final ObservableConnection<DatagramPacket, DatagramPacket> newConnection) { | ||
return newConnection.getInput().flatMap(new Func1<DatagramPacket, Observable<Void>>() { | ||
@Override | ||
public Observable<Void> call(DatagramPacket received) { | ||
InetSocketAddress sender = received.sender(); | ||
System.out.println("Received datagram. Sender: " + sender + ", data: " | ||
+ received.content().toString(Charset.defaultCharset())); | ||
ByteBuf data = newConnection.getChannelHandlerContext().alloc().buffer(WELCOME_MSG_BYTES.length); | ||
data.writeBytes(WELCOME_MSG_BYTES); | ||
return newConnection.writeAndFlush(new DatagramPacket(data, sender)); | ||
} | ||
}); | ||
} | ||
}).startAndWait(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
rx-netty/src/main/java/io/reactivex/netty/protocol/udp/client/UdpClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.reactivex.netty.protocol.udp.client; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing license headers ;) Also in other classes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! I created a different PR #109 for updating the copyright. |
||
|
||
import io.netty.bootstrap.Bootstrap; | ||
import io.reactivex.netty.channel.ObservableConnectionFactory; | ||
import io.reactivex.netty.client.ClientChannelFactory; | ||
import io.reactivex.netty.client.ClientChannelFactoryImpl; | ||
import io.reactivex.netty.client.RxClient; | ||
import io.reactivex.netty.client.RxClientImpl; | ||
import io.reactivex.netty.pipeline.PipelineConfigurator; | ||
|
||
import java.net.InetSocketAddress; | ||
|
||
/** | ||
* An implementation of {@link RxClient} for UDP/IP | ||
* | ||
* @author Nitesh Kant | ||
*/ | ||
public class UdpClient<I, O> extends RxClientImpl<I, O> { | ||
|
||
public UdpClient(ServerInfo serverInfo, Bootstrap clientBootstrap, ClientConfig clientConfig) { | ||
this(serverInfo, clientBootstrap, null, clientConfig); | ||
} | ||
|
||
public UdpClient(ServerInfo serverInfo, Bootstrap clientBootstrap, PipelineConfigurator<O, I> pipelineConfigurator, | ||
ClientConfig clientConfig) { | ||
super(serverInfo, clientBootstrap, pipelineConfigurator, clientConfig); | ||
} | ||
|
||
@Override | ||
protected ClientChannelFactory<O, I> _newChannelFactory(ServerInfo serverInfo, Bootstrap clientBootstrap, | ||
ObservableConnectionFactory<O, I> connectionFactory) { | ||
final InetSocketAddress receiverAddress = new InetSocketAddress(serverInfo.getHost(), serverInfo.getPort()); | ||
return new ClientChannelFactoryImpl<O, I>(clientBootstrap, | ||
new UdpClientConnectionFactory<O, I>(receiverAddress), serverInfo); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
rx-netty/src/main/java/io/reactivex/netty/protocol/udp/client/UdpClientBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2014 Netflix, Inc. | ||
* | ||
* 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. | ||
*/ | ||
package io.reactivex.netty.protocol.udp.client; | ||
|
||
import io.netty.bootstrap.Bootstrap; | ||
import io.reactivex.netty.client.AbstractClientBuilder; | ||
import io.reactivex.netty.client.RxClient; | ||
import io.reactivex.netty.client.RxClientImpl; | ||
|
||
/** | ||
* A builder to build an instance of {@link RxClientImpl} | ||
* | ||
* @author Nitesh Kant | ||
*/ | ||
public class UdpClientBuilder<I, O> extends AbstractClientBuilder<I,O, UdpClientBuilder<I, O>, RxClient<I, O>> { | ||
|
||
public UdpClientBuilder(String host, int port) { | ||
this(host, port, new Bootstrap()); | ||
} | ||
|
||
public UdpClientBuilder(String host, int port, Bootstrap bootstrap) { | ||
super(bootstrap, host, port); | ||
} | ||
|
||
@Override | ||
protected RxClient<I, O> createClient() { | ||
return new UdpClient<I, O>(serverInfo, bootstrap, pipelineConfigurator, clientConfig); | ||
} | ||
|
||
@Override | ||
protected boolean shouldCreateConnectionPool() { | ||
return false; // No connection pools are needed for UDP. | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpicking... the "....the tail of the pipeline". Writes always start at the tail (in fact all outbound operations).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aah, With head I was actually referring to the "start" of the pipeline for outbound operations, which actually is the tail! English is a funny language :)
I will change the comment anyways, thanks!