-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from NiteshKant/udp
Udp support
- Loading branch information
Showing
18 changed files
with
606 additions
and
171 deletions.
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; | ||
|
||
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.