Rename localtime to worldtime so that a user thinks it's a local transport example

- Fixes #1003
This commit is contained in:
Trustin Lee 2013-01-31 12:27:57 +09:00
parent 39357f3835
commit 073517dc63
8 changed files with 47 additions and 2701 deletions

View File

@ -13,7 +13,7 @@
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.example.localtime;
package io.netty.example.worldclock;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
@ -27,16 +27,16 @@ import java.util.List;
import java.util.regex.Pattern;
/**
* Sends a list of continent/city pairs to a {@link LocalTimeServer} to
* Sends a list of continent/city pairs to a {@link WorldClockServer} to
* get the local times of the specified cities.
*/
public class LocalTimeClient {
public class WorldClockClient {
private final String host;
private final int port;
private final Collection<String> cities;
public LocalTimeClient(String host, int port, Collection<String> cities) {
public WorldClockClient(String host, int port, Collection<String> cities) {
this.host = host;
this.port = port;
this.cities = new ArrayList<String>();
@ -48,14 +48,14 @@ public class LocalTimeClient {
try {
b.group(new NioEventLoopGroup())
.channel(NioSocketChannel.class)
.handler(new LocalTimeClientInitializer());
.handler(new WorldClockClientInitializer());
// Make a new connection.
Channel ch = b.connect(host, port).sync().channel();
// Get the handler instance to initiate the request.
LocalTimeClientHandler handler =
ch.pipeline().get(LocalTimeClientHandler.class);
WorldClockClientHandler handler =
ch.pipeline().get(WorldClockClientHandler.class);
// Request and get the response.
List<String> response = handler.getLocalTimes(cities);
@ -89,15 +89,15 @@ public class LocalTimeClient {
return;
}
new LocalTimeClient(host, port, cities).run();
new WorldClockClient(host, port, cities).run();
}
private static void printUsage() {
System.err.println(
"Usage: " + LocalTimeClient.class.getSimpleName() +
"Usage: " + WorldClockClient.class.getSimpleName() +
" <host> <port> <continent/city_name> ...");
System.err.println(
"Example: " + LocalTimeClient.class.getSimpleName() +
"Example: " + WorldClockClient.class.getSimpleName() +
" localhost 8080 America/New_York Asia/Seoul");
}

View File

@ -13,16 +13,16 @@
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.example.localtime;
package io.netty.example.worldclock;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundMessageHandlerAdapter;
import io.netty.example.localtime.LocalTimeProtocol.Continent;
import io.netty.example.localtime.LocalTimeProtocol.LocalTime;
import io.netty.example.localtime.LocalTimeProtocol.LocalTimes;
import io.netty.example.localtime.LocalTimeProtocol.Location;
import io.netty.example.localtime.LocalTimeProtocol.Locations;
import io.netty.example.worldclock.WorldClockProtocol.Continent;
import io.netty.example.worldclock.WorldClockProtocol.LocalTime;
import io.netty.example.worldclock.WorldClockProtocol.LocalTimes;
import io.netty.example.worldclock.WorldClockProtocol.Location;
import io.netty.example.worldclock.WorldClockProtocol.Locations;
import java.util.ArrayList;
import java.util.Collection;
@ -34,10 +34,10 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
public class LocalTimeClientHandler extends ChannelInboundMessageHandlerAdapter<LocalTimes> {
public class WorldClockClientHandler extends ChannelInboundMessageHandlerAdapter<LocalTimes> {
private static final Logger logger = Logger.getLogger(
LocalTimeClientHandler.class.getName());
WorldClockClientHandler.class.getName());
private static final Pattern DELIM = Pattern.compile("/");

View File

@ -13,7 +13,7 @@
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.example.localtime;
package io.netty.example.worldclock;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
@ -23,16 +23,17 @@ import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;
public class LocalTimeServerInitializer extends ChannelInitializer<SocketChannel> {
public class WorldClockClientInitializer extends ChannelInitializer<SocketChannel> {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast("frameDecoder", new ProtobufVarint32FrameDecoder());
p.addLast("protobufDecoder", new ProtobufDecoder(LocalTimeProtocol.Locations.getDefaultInstance()));
p.addLast("protobufDecoder", new ProtobufDecoder(WorldClockProtocol.LocalTimes.getDefaultInstance()));
p.addLast("frameEncoder", new ProtobufVarint32LengthFieldPrepender());
p.addLast("protobufEncoder", new ProtobufEncoder());
p.addLast("handler", new LocalTimeServerHandler());
p.addLast("handler", new WorldClockClientHandler());
}
}

View File

@ -13,11 +13,12 @@
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.example.localtime;
package io.netty.example.worldclock;
// How to generate
// ===============
// $ protoc src/main/java/io/netty/example/localtime/LocalTimeProtocol.proto \
// $ cd example
// $ protoc src/main/java/io/netty/example/worldclock/WorldClockProtocol.proto \
// --java_out=src/main/java
//
// Add @SuppressWarnings("all") to the generated code not to pollute IDE task list.

View File

@ -13,21 +13,21 @@
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.example.localtime;
package io.netty.example.worldclock;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.socket.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
/**
* Receives a list of continent/city pairs from a {@link LocalTimeClient} to
* Receives a list of continent/city pairs from a {@link WorldClockClient} to
* get the local times of the specified cities.
*/
public class LocalTimeServer {
public class WorldClockServer {
private final int port;
public LocalTimeServer(int port) {
public WorldClockServer(int port) {
this.port = port;
}
@ -36,7 +36,7 @@ public class LocalTimeServer {
try {
b.group(new NioEventLoopGroup(), new NioEventLoopGroup())
.channel(NioServerSocketChannel.class)
.childHandler(new LocalTimeServerInitializer());
.childHandler(new WorldClockServerInitializer());
b.bind(port).sync().channel().closeFuture().sync();
} finally {
@ -51,6 +51,6 @@ public class LocalTimeServer {
} else {
port = 8080;
}
new LocalTimeServer(port).run();
new WorldClockServer(port).run();
}
}

View File

@ -13,27 +13,28 @@
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.example.localtime;
package io.netty.example.worldclock;
import static java.util.Calendar.*;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundMessageHandlerAdapter;
import io.netty.example.localtime.LocalTimeProtocol.Continent;
import io.netty.example.localtime.LocalTimeProtocol.DayOfWeek;
import io.netty.example.localtime.LocalTimeProtocol.LocalTime;
import io.netty.example.localtime.LocalTimeProtocol.LocalTimes;
import io.netty.example.localtime.LocalTimeProtocol.Location;
import io.netty.example.localtime.LocalTimeProtocol.Locations;
import io.netty.example.worldclock.WorldClockProtocol.Continent;
import io.netty.example.worldclock.WorldClockProtocol.DayOfWeek;
import io.netty.example.worldclock.WorldClockProtocol.LocalTime;
import io.netty.example.worldclock.WorldClockProtocol.LocalTimes;
import io.netty.example.worldclock.WorldClockProtocol.Location;
import io.netty.example.worldclock.WorldClockProtocol.Locations;
import java.util.Calendar;
import java.util.TimeZone;
import java.util.logging.Level;
import java.util.logging.Logger;
public class LocalTimeServerHandler extends ChannelInboundMessageHandlerAdapter<Locations> {
import static java.util.Calendar.*;
public class WorldClockServerHandler extends ChannelInboundMessageHandlerAdapter<Locations> {
private static final Logger logger = Logger.getLogger(
LocalTimeServerHandler.class.getName());
WorldClockServerHandler.class.getName());
@Override
public void messageReceived(ChannelHandlerContext ctx, Locations locations) throws Exception {

View File

@ -13,7 +13,7 @@
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.example.localtime;
package io.netty.example.worldclock;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
@ -23,17 +23,16 @@ import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;
public class LocalTimeClientInitializer extends ChannelInitializer<SocketChannel> {
public class WorldClockServerInitializer extends ChannelInitializer<SocketChannel> {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast("frameDecoder", new ProtobufVarint32FrameDecoder());
p.addLast("protobufDecoder", new ProtobufDecoder(LocalTimeProtocol.LocalTimes.getDefaultInstance()));
p.addLast("protobufDecoder", new ProtobufDecoder(WorldClockProtocol.Locations.getDefaultInstance()));
p.addLast("frameEncoder", new ProtobufVarint32LengthFieldPrepender());
p.addLast("protobufEncoder", new ProtobufEncoder());
p.addLast("handler", new LocalTimeClientHandler());
p.addLast("handler", new WorldClockServerHandler());
}
}