001// ______________________________________________________
002// Generated by codegen - https://gitee.com/l0km/codegen 
003// template: thrift/client/perservice/client.service.decorator.class.vm
004// ______________________________________________________
005
006
007package net.gdface.sdk.fse.thrift;
008import java.nio.ByteBuffer;
009import net.gdface.sdk.fse.CodeBean;
010import net.gdface.sdk.fse.FeatureSe;
011import net.gdface.thrift.TypeTransformer;
012import net.gdface.thrift.ClientFactory;
013
014import com.facebook.swift.service.RuntimeTApplicationException;
015import com.google.common.net.HostAndPort;
016
017import static com.google.common.base.Preconditions.*;
018
019/**
020 * 基于thrift/swift框架生成的client端代码提供{@link FeatureSe}接口的RPC实现(线程安全)<br>
021 * 转发所有{@link FeatureSe}接口方法到{@link #delegate()}指定的实例<br>
022 * 所有服务端抛出的{@link RuntimeException}异常被封装到{@link ServiceRuntimeException}中抛出<br>
023 * Example:<br>
024 * <pre>
025 *        FeatureSeThriftClient thriftInstance = ClientFactory
026 *            .builder()
027 *            .setHostAndPort("127.0.0.1",26413)
028 *            .build(FeatureSe.class, FeatureSeThriftClient.class);
029 * </pre>
030 * 计算机生成代码(generated by automated tools ThriftServiceDecoratorGenerator @author guyadong)<br>
031 * @author guyadong
032 *
033 */
034public class FeatureSeThriftClient implements FeatureSe {
035    private final ClientFactory factory;
036
037    public ClientFactory getFactory() {
038        return factory;
039    }
040    public FeatureSeThriftClient(ClientFactory factory) {
041        super();
042        this.factory = checkNotNull(factory,"factory is null");
043    }
044    /**
045     * @param host RPC service host
046     * @param port RPC service port 
047     */
048    public FeatureSeThriftClient(String host,int port) {
049        this(ClientFactory.builder().setHostAndPort(host,port));
050    }
051    /**
052     * @param hostAndPort RPC service host and port 
053     */
054    public FeatureSeThriftClient(HostAndPort hostAndPort) {
055        this(ClientFactory.builder().setHostAndPort(hostAndPort));
056    }
057    /**
058     * test if connectable for RPC service
059     * @return return {@code true} if connectable ,otherwise {@code false}
060     */
061    public boolean testConnect(){
062        return factory.testConnect();
063    }
064
065    /**
066     * @return 返回{@link net.gdface.sdk.fse.thrift.client.FeatureSe}实例
067     */
068    protected net.gdface.sdk.fse.thrift.client.FeatureSe delegate() {
069        return factory.applyInstance(net.gdface.sdk.fse.thrift.client.FeatureSe.class);
070    }
071    @Override
072    public String toString() {
073        StringBuilder builder = new StringBuilder();
074        builder.append("FeatureSeThriftClient [factory=");
075        builder.append(factory);
076        builder.append(",interface=");
077        builder.append(FeatureSe.class.getName());
078        builder.append("]");
079        return builder.toString();
080    }
081    @Override
082    public boolean addFeature(byte[] featureId,
083        byte[] feature,
084        String imgMD5) 
085        {
086        net.gdface.sdk.fse.thrift.client.FeatureSe instance = delegate();
087        try{
088            return instance.addFeatureToFse(featureId,
089                feature,
090                imgMD5);
091        }
092        catch(net.gdface.sdk.fse.thrift.client.ServiceRuntimeException e){
093            throw new ServiceRuntimeException(e);
094        }
095        finally{
096            factory.releaseInstance(instance);
097        }
098    }
099    @Override
100    public boolean addFeature(byte[] featureId,
101        byte[] feature,
102        long appid) 
103        {
104        net.gdface.sdk.fse.thrift.client.FeatureSe instance = delegate();
105        try{
106            return instance.addFeatureToFseWithAppId(featureId,
107                feature,
108                appid);
109        }
110        catch(net.gdface.sdk.fse.thrift.client.ServiceRuntimeException e){
111            throw new ServiceRuntimeException(e);
112        }
113        finally{
114            factory.releaseInstance(instance);
115        }
116    }
117    @Override
118    public void clearAll() 
119        {
120        net.gdface.sdk.fse.thrift.client.FeatureSe instance = delegate();
121        try{
122             instance.clearAllOfFse();
123        }
124        catch(net.gdface.sdk.fse.thrift.client.ServiceRuntimeException e){
125            throw new ServiceRuntimeException(e);
126        }
127        finally{
128            factory.releaseInstance(instance);
129        }
130    }
131    @Override
132    public CodeBean getFeature(byte[] featureId) 
133        {
134        net.gdface.sdk.fse.thrift.client.FeatureSe instance = delegate();
135        try{
136            return TypeTransformer.getInstance().to(
137                    instance.getFeatureFromFse(featureId),
138                    net.gdface.sdk.fse.thrift.client.CodeBean.class,
139                    CodeBean.class);
140        }
141        catch(net.gdface.sdk.fse.thrift.client.ServiceRuntimeException e){
142            throw new ServiceRuntimeException(e);
143        }
144        catch(RuntimeTApplicationException e){
145            return net.gdface.thrift.ThriftUtils.returnNull(e);
146        }
147        finally{
148            factory.releaseInstance(instance);
149        }
150    }
151    @Override
152    public CodeBean getFeatureByHex(String featureId) 
153        {
154        net.gdface.sdk.fse.thrift.client.FeatureSe instance = delegate();
155        try{
156            return TypeTransformer.getInstance().to(
157                    instance.getFeatureByHexFromFse(featureId),
158                    net.gdface.sdk.fse.thrift.client.CodeBean.class,
159                    CodeBean.class);
160        }
161        catch(net.gdface.sdk.fse.thrift.client.ServiceRuntimeException e){
162            throw new ServiceRuntimeException(e);
163        }
164        catch(RuntimeTApplicationException e){
165            return net.gdface.thrift.ThriftUtils.returnNull(e);
166        }
167        finally{
168            factory.releaseInstance(instance);
169        }
170    }
171    @Override
172    public boolean removeFeature(byte[] featureId) 
173        {
174        net.gdface.sdk.fse.thrift.client.FeatureSe instance = delegate();
175        try{
176            return instance.removeFeatureFromFse(featureId);
177        }
178        catch(net.gdface.sdk.fse.thrift.client.ServiceRuntimeException e){
179            throw new ServiceRuntimeException(e);
180        }
181        finally{
182            factory.releaseInstance(instance);
183        }
184    }
185    @Override
186    public boolean removeFeatureByHex(String featureId) 
187        {
188        net.gdface.sdk.fse.thrift.client.FeatureSe instance = delegate();
189        try{
190            return instance.removeFeatureByHexFromFse(featureId);
191        }
192        catch(net.gdface.sdk.fse.thrift.client.ServiceRuntimeException e){
193            throw new ServiceRuntimeException(e);
194        }
195        finally{
196            factory.releaseInstance(instance);
197        }
198    }
199    @Override
200    public CodeBean[] searchCode(byte[] code,
201        double sim,
202        int rows) 
203        {
204        net.gdface.sdk.fse.thrift.client.FeatureSe instance = delegate();
205        try{
206            return TypeTransformer.getInstance().toArray(
207                    instance.searchCodeFromFse(code,
208                sim,
209                rows),
210                    net.gdface.sdk.fse.thrift.client.CodeBean.class,
211                    CodeBean.class);
212        }
213        catch(net.gdface.sdk.fse.thrift.client.ServiceRuntimeException e){
214            throw new ServiceRuntimeException(e);
215        }
216        catch(RuntimeTApplicationException e){
217            return net.gdface.thrift.ThriftUtils.returnNull(e);
218        }
219        finally{
220            factory.releaseInstance(instance);
221        }
222    }
223    @Override
224    public int size() 
225        {
226        net.gdface.sdk.fse.thrift.client.FeatureSe instance = delegate();
227        try{
228            return instance.sizeOfFse();
229        }
230        catch(net.gdface.sdk.fse.thrift.client.ServiceRuntimeException e){
231            throw new ServiceRuntimeException(e);
232        }
233        finally{
234            factory.releaseInstance(instance);
235        }
236    }
237}