libthrift0.9.0解析(二)之TSimpleServer

时间:2023-01-20 23:03:19

TSimpleServer简单实现Tserver,代码如下。

/**
* Simple singlethreaded server for testing.
*
*/
public class TSimpleServer extends TServer { private static final Logger LOGGER = LoggerFactory.getLogger(TSimpleServer.class.getName()); private boolean stopped_ = false; public TSimpleServer(AbstractServerArgs args) {
super(args);
} public void serve() {
stopped_ = false;
try {
serverTransport_.listen(); // 开启监听
} catch (TTransportException ttx) {
LOGGER.error("Error occurred during listening.", ttx);
return;
} // Run the preServe event
if (eventHandler_ != null) {
eventHandler_.preServe(); // 前处理切入
} setServing(true); while (!stopped_) {
TTransport client = null;
TProcessor processor = null;
TTransport inputTransport = null;
TTransport outputTransport = null;
TProtocol inputProtocol = null;
TProtocol outputProtocol = null;
ServerContext connectionContext = null;
try {
client = serverTransport_.accept(); // 获得一个client transport
if (client != null) { // 以下代码根据获取的client transport层层封装
processor = processorFactory_.getProcessor(client); // 根据client获取processor
inputTransport = inputTransportFactory_.getTransport(client); // 根据client获取input transport
outputTransport = outputTransportFactory_.getTransport(client); // 根据client获取output transport
inputProtocol = inputProtocolFactory_.getProtocol(inputTransport); // 根据input transport获取input protocol
outputProtocol = outputProtocolFactory_.getProtocol(outputTransport); // 根据output transport获取output protocol
if (eventHandler_ != null) {// 处理前切入eventHandler根据input/output protocol生成connectionContext
connectionContext = eventHandler_.createContext(inputProtocol, outputProtocol);
}
while (true) {// 一直循环处理同一个用户的请求,processor.process永远返回true,只有抛出异常时(即用户端主动关闭连接)才会继续接收下一个用户的请求。
if (eventHandler_ != null) {// 切入eventHandler处理前面生成的connectionContext
eventHandler_.processContext(connectionContext, inputTransport, outputTransport);
}
if(!processor.process(inputProtocol, outputProtocol)) {// 调用processor对input/output protocol进行处理
break;
}
}
}
} catch (TTransportException ttx) {
// Client died, just move on
} catch (TException tx) {
if (!stopped_) {
LOGGER.error("Thrift error occurred during processing of message.", tx);
}
} catch (Exception x) {
if (!stopped_) {
LOGGER.error("Error occurred during processing of message.", x);
}
} if (eventHandler_ != null) {
eventHandler_.deleteContext(connectionContext, inputProtocol, outputProtocol);
} if (inputTransport != null) {
inputTransport.close();
} if (outputTransport != null) {
outputTransport.close();
} }
setServing(false);
} public void stop() {
stopped_ = true;
serverTransport_.interrupt();
}
}

首先,开启监听,然后在处理之前面向切面切入前处理流程 eventHandler_.preServe();最后进入接收处理流程。

处理时关键看第54行processor.process()函数。

TProcessor为一单接口,仅含process方法。其子类thrift不再提供,而是根据用户自定义的thrift IDL文件自动生成。然而thrift提供了一个抽象类TBaseProcessor<I>封装了一些常用的实现,该类实现了TProcessor接口,框架自动生成的Processor都继承了该基类。

TBaseProcessor<I>代码如下:

public abstract class TBaseProcessor<I> implements TProcessor {
private final I iface; // 真正的处理函数实现
private final Map<String,ProcessFunction<I, ? extends TBase>> processMap; protected TBaseProcessor(I iface, Map<String, ProcessFunction<I, ? extends TBase>> processFunctionMap) {
this.iface = iface;
this.processMap = processFunctionMap;
} public Map<String,ProcessFunction<I, ? extends TBase>> getProcessMapView() {
return Collections.unmodifiableMap(processMap);
} @Override
public boolean process(TProtocol in, TProtocol out) throws TException {
TMessage msg = in.readMessageBegin(); // 获取请求信息,主要包含函数名信息。
ProcessFunction fn = processMap.get(msg.name); // 根据函数名信息获取相应的处理函数。
if (fn == null) { // 若没有相关的处理函数,写入异常并返回。
TProtocolUtil.skip(in, TType.STRUCT);
in.readMessageEnd();
TApplicationException x = new TApplicationException(TApplicationException.UNKNOWN_METHOD, "Invalid method name: '"+msg.name+"'");
out.writeMessageBegin(new TMessage(msg.name, TMessageType.EXCEPTION, msg.seqid));
x.write(out);
out.writeMessageEnd();
out.getTransport().flush();
return true;
}
fn.process(msg.seqid, in, out, iface); // 处理请求
return true;
}
}

ProcessFunction为一抽象类,其子类也是根据IDL自动生成。与IDL中的函数一一对应,为代理处理器。

TBase为IDL中定义的types的基类。T封装了处理函数的参数信息,其子类也是框架根据IDL自动生成。

public abstract class ProcessFunction<I, T extends TBase> {
private final String methodName; private static final Logger LOGGER = LoggerFactory.getLogger(ProcessFunction.class.getName()); public ProcessFunction(String methodName) {
this.methodName = methodName;
} public final void process(int seqid, TProtocol iprot, TProtocol oprot, I iface) throws TException {
T args = getEmptyArgsInstance(); // 获取一个空的参数封装
try {
args.read(iprot); // 从input protol中读取所有参数到args中
} catch (TProtocolException e) {
iprot.readMessageEnd();
TApplicationException x = new TApplicationException(TApplicationException.PROTOCOL_ERROR, e.getMessage());
oprot.writeMessageBegin(new TMessage(getMethodName(), TMessageType.EXCEPTION, seqid));
x.write(oprot);
oprot.writeMessageEnd();
oprot.getTransport().flush();
return;
}
iprot.readMessageEnd(); // 读取参数完毕
TBase result = null; try {
result = getResult(iface, args); // 获取处理结果
} catch(Throwable th) {
LOGGER.error("Internal error processing " + getMethodName(), th);
TApplicationException x = new TApplicationException(TApplicationException.INTERNAL_ERROR,
"Internal error processing " + getMethodName());
oprot.writeMessageBegin(new TMessage(getMethodName(), TMessageType.EXCEPTION, seqid));
x.write(oprot);
oprot.writeMessageEnd();
oprot.getTransport().flush();
return;
} if(!isOneway()) {// true表示单向操作不许要返回信息,即函数为void类型。false表示需要返回值,则启用output protol写入结果。
oprot.writeMessageBegin(new TMessage(getMethodName(), TMessageType.REPLY, seqid));
result.write(oprot);
oprot.writeMessageEnd();
oprot.getTransport().flush();
}
} protected abstract boolean isOneway(); public abstract TBase getResult(I iface, T args) throws TException; public abstract T getEmptyArgsInstance(); public String getMethodName() {
return methodName;
}
}

到此为止,基本了解了libthrift的大致框架。在处理函数中主要是与protocol打交道,protocol又是根据input/output transport生成,而input/output transport又是根据client transport生成。总形成三层结构,底层是client transport由server transport的accept函数获得,中层是input/output transport ,顶层是input/output protocol。然而三层之间并不是完全透明不可见的,典型如oprot.getTransport().flush() 对上层暴露了底层,也无伤大雅。

下节介绍protocol以及transport。

libthrift0.9.0解析(二)之TSimpleServer的更多相关文章

  1. libthrift0&period;9&period;0解析(四)之TThreadPoolServer&amp&semi;ServerContext

    TThreadPoolServer直接继承自TServer,实现类serve和stop操作. 在serve中可以接受多个连接,每个连接单独开一个线程进行处理,在每个线程中,按顺序处理该线程所绑定连接的 ...

  2. libthrift0&period;9&period;0解析(三)之TProtocol&amp&semi;TTransport

       以上是transport和protocol的类图和结构图. transport封装了底层的传输通道,主要有read/write.open/close等基本的读写方法,而且都是对于二进制数据. p ...

  3. libthrift0&period;9&period;0解析(五)之TNonblockingServer&amp&semi;THsHaServer

    本文是一边看代码一边写的,是真随笔,随看随下笔. 看TNonblockingServer,先看其父类AbstractNonblockingServer.一般来说,父类封装的都是通用的东西,具体的底层实 ...

  4. libthrift0&period;9&period;0解析(一)之TServer

    TServer 属性serverTransport 为TServerTransport类型,类图如下: 构造函数,简单根据args设置几个成员,大部分是工厂类: protected TServer(A ...

  5. Fixflow引擎解析&lpar;二&rpar;&lpar;模型&rpar; - BPMN2&period;0读写

    Fixflow引擎解析(四)(模型) - 通过EMF扩展BPMN2.0元素 Fixflow引擎解析(三)(模型) - 创建EMF模型来读写XML文件 Fixflow引擎解析(二)(模型) - BPMN ...

  6. C&num;使用zxing,zbar,thoughtworkQRcode解析二维码,附源代码

    最近做项目需要解析二维码图片,找了一大圈,发现没有人去整理下开源的几个库案例,花了点时间 做了zxing,zbar和thoughtworkqrcode解析二维码案例,希望大家有帮助. zxing是谷歌 ...

  7. Java生成与解析二维码

    1.下载支持二维码的jar包qrcode.jar和qrcode_swetake.jar, 其中qrcode_swetake.jar用于生成二维码,rcode.jar用于解析二维码,jar包下载地址(免 ...

  8. java 生成和解析二维码

    public class QRCode { /** * 解析二维码(QRCode) * @param imgPath * @return */ public static String decoder ...

  9. java代码生成二维码以及解析二维码

    package com.test; import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedI ...

随机推荐

  1. 逐行扫描型Memory LCD显存管理与emWin移植

    因为Memory LCD 的特性,不能设置像素坐标,只能用缓存整体刷新. 所以对于Memory LCD来说,emWin移植仅与打点函数有关,这里用Sharp Memory LCD(ls013b7dh0 ...

  2. PhpStorm&sol;Xdebug安装使用

    安装环境:XAMPP;phpStorm版本10; windows 7 64bit. XAMPP.phpStorm 都直接安装在了D盘根目录,9999m目录建在D:\xampp\htocts下,即目录工 ...

  3. 【转】selenium学习路线

    selenium学习路线 配置你的测试环境,真对你所学习语言,来配置你相应的selenium 测试环境.selenium 好比定义的语义---“问好”,假如你使用的是中文,为了表术问好,你的写法是“你 ...

  4. 用c&num;开发微信(2)扫描二维码,用户授权后获取用户基本信息 (源码下载)

    本文将介绍基于Senparc.Weixin微信开发框架来实现网页授权来获取用户基本信息.先生成包含授权及回调url信息的二维码:用户用微信扫描之后,被要求授权以获取Ta的用户基本信息:用户授权后,通过 ...

  5. Java Session 介绍&semi;

    为什么需要Session 这是为了填补 Http 协议的局限,当用户去访问一个页面,服务端返回完了请求(如,你访问完一个网页,这个页面将页面内容,界面UI呈现给你),就算是结束了,就断开了,服务端不再 ...

  6. 书评&lt&semi;&lt&semi;剑指offer 名企面试官精讲典型编程题&gt&semi;&gt&semi;

      前前后后阅读了一周, 感慨很多, 面试考察的是一个人的综合能力, 这一点从面试官的角度去解读, 确实对面试的理解更立体. *) 具体考察的点1) 扎实的基础2) 高质量的代码3) 清晰的思路4) ...

  7. 在asp&period;net前台页面中引入命名空间 和连接数据库

    例如:<%@ Import Namespace="System.Data" %> 连接数据库 <% string strconn = "Data Sou ...

  8. poj2750Potted Flower &lpar;线段树)

    http://poj.org/problem?id=2750 之前做过类似的题 把一段的左连续最大.最小 右连续最大及最小及中间的连续更新出 就可以算出这段最大的连续和 注意不能全部加上 加上一特判 ...

  9. HDU 5701 中位数计数 暴力

    老题了,附上黄学长链接一发,直接改改就AC了,http://hzwer.com/1216.html #include <cstdio> #include <iostream> ...

  10. 用js实现选项卡切换效果

    这是要实现的效果图: 一.HTML页面布局 <!-- HTML页面布局 --><ul class="tab_menu"> <li class=&quo ...