设为首页 加入收藏

TOP

Java AIO 服务器与客户端实现示例(六)
2015-02-02 14:44:13 来源: 作者: 【 】 浏览:80
Tags:Java AIO 服务器 客户端 实现 示例
se;
?
? ? ? ? synchronized(queue) {
? ? ? ? ? ? queue.add(buffer);
? ? ? ? ? ? // Currently no thread writing, make this thread dispatch a write
? ? ? ? ? ? if (!writing) {
? ? ? ? ? ? ? ? writing = true;
? ? ? ? ? ? ? ? threadShouldWrite = true;
? ? ? ? ? ? }
? ? ? ? }
?
? ? ? ? if (threadShouldWrite) {
? ? ? ? ? ? writeFromQueue();
? ? ? ? }
? ? }
?
? ? private void writeFromQueue() {
? ? ? ? ByteBuffer buffer;
?
? ? ? ? synchronized (queue) {
? ? ? ? ? ? buffer = queue.poll();
? ? ? ? ? ? if (buffer == null) {
? ? ? ? ? ? ? ? writing = false;
? ? ? ? ? ? }
? ? ? ? }
?
? ? ? ? // No new data in buffer to write
? ? ? ? if (writing) {
? ? ? ? ? ? writeBuffer(buffer);
? ? ? ? }
? ? }
?
? ? private void writeBuffer(ByteBuffer buffer) {
? ? ? ? channel.write(buffer, buffer, new CompletionHandler() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void completed(Integer result, ByteBuffer buffer) {
? ? ? ? ? ? ? ? if (buffer.hasRemaining()) {
? ? ? ? ? ? ? ? ? ? channel.write(buffer, buffer, this);
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? // Go back and check if there is new data to write
? ? ? ? ? ? ? ? ? ? writeFromQueue();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
?
? ? ? ? ? ? @Override
? ? ? ? ? ? public void failed(Throwable exc, ByteBuffer attachment) {
? ? ? ? ? ? }
? ? ? ? });
? ? }
?
? ? /**
? ? * Sends a message
? ? * @param string the message
? ? * @throws CharacterCodingException
? ? */
? ? public void writeStringMessage(String msg) throws CharacterCodingException {
? ? ? ? writeMessage(Charset.forName("UTF-8").newEncoder().encode(CharBuffer.wrap(msg)));
? ? }
}


Helper类


package com.stevex.app.aio;
?
import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
?
public class Helper {
? ? private static BlockingQueue words;
? ? private static Random random;
? ?
? ? public Helper() throws InterruptedException{
? ? ? ? words = new ArrayBlockingQueue(5);
? ? ? ? words.put("hi");
? ? ? ? words.put("who");
? ? ? ? words.put("what");
? ? ? ? words.put("where");
? ? ? ? words.put("bye");?
? ? ? ?
? ? ? ? random = new Random();
? ? }
? ?
? ? public String getWord(){
? ? ? ? return words.poll();
? ? }
?
? ? public void sleep() {
? ? ? ? try {
? ? ? ? ? ? TimeUnit.SECONDS.sleep(random.nextInt(3));
? ? ? ? } catch (InterruptedException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }?
? ?
? ? public static void sleep(long l) {
? ? ? ? try {
? ? ? ? ? ? TimeUnit.SECONDS.sleep(l);
? ? ? ? } catch (InterruptedException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ?
? ? public static String getAnswer(String question){
? ? ? ? String answer = null;
? ? ? ?
? ? ? ? switch(question){
? ? ? ? case "who":
? ? ? ? ? ? answer = "我是小娜\n";
? ? ? ? ? ? break;
? ? ? ? case "what":
? ? ? ? ? ? answer = "我是来帮你解闷的\n";
? ? ? ? ? ? break;
? ? ? ? case "where":
? ? ? ? ? ? answer = "我来自外太空\n";
? ? ? ? ? ? break;
? ? ? ? case "hi":
? ? ? ? ? ? answer = "hello\n";
? ? ? ? ? ? break;
? ? ? ? case "bye":
? ? ? ? ? ? answer = "88\n";
? ? ? ? ? ? break;
? ? ? ? default:
? ? ? ? ? ? ? ? answer = "请输入 who, 或者what, 或者where";
? ? ? ? }
? ? ? ?
? ? ? ? return answer;
? ? }
}


CharsetHelper类:


package com.stevex.app.nio;
?
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
?
public class CharsetHelper {
? ? private static final String UTF_8 = "UTF-8";
? ? private static CharsetEncoder encoder = Charset.forName(UTF_8).newEncoder();
? ? private static CharsetDecoder decoder = Charset.forName(UTF_8).newDecoder();
? ?
? ? public static ByteBuffer encode(CharBuffer in) throws CharacterCodingException{
? ? ? ? return encoder.encode(in);
? ? }

首页 上一页 3 4 5 6 下一页 尾页 6/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Java NIO操作类型 下一篇Java NIO 服务器与客户端实现示例

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: