Search in sources :

Example 6 with ProtobaseFutureImpl

use of com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl in project baseio by generallycloud.

the class TestSimple method main.

public static void main(String[] args) throws Exception {
    String serviceKey = "/test-simple";
    String param = "ttt";
    IoEventHandleAdaptor eventHandle = new IoEventHandleAdaptor() {

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            System.out.println("________________________" + future.getReadText());
        }
    };
    SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration(18300));
    SocketChannelConnector connector = new SocketChannelConnector(context);
    context.setProtocolFactory(new ProtobaseProtocolFactory());
    context.addSessionEventListener(new LoggerSocketSEListener());
    context.setIoEventHandleAdaptor(eventHandle);
    SocketSession session = connector.connect();
    ProtobaseFuture f = new ProtobaseFutureImpl(connector.getContext(), serviceKey);
    f.write(param);
    session.flush(f);
    ThreadUtil.sleep(500);
    CloseUtil.close(connector);
}
Also used : ProtobaseProtocolFactory(com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory) SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) SocketSession(com.generallycloud.baseio.component.SocketSession) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) ProtobaseFutureImpl(com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) Future(com.generallycloud.baseio.protocol.Future) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext)

Example 7 with ProtobaseFutureImpl

use of com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl in project baseio by generallycloud.

the class Consumer method push.

// FIXME push 失败时对message进行回收,并移除Consumer
public void push(Message message) throws IOException {
    this.message = message;
    TransactionSection section = attachment.getTransactionSection();
    if (section != null) {
        section.offerMessage(message);
    }
    int msgType = message.getMsgType();
    String content = message.toString();
    SocketSession session = this.session;
    ProtobaseFuture f = new ProtobaseFutureImpl(session.getContext(), future.getFutureId(), future.getFutureName());
    f.write(content);
    if (msgType == Message.TYPE_TEXT || msgType == Message.TYPE_MAP) {
        session.flush(f);
    } else if (msgType == Message.TYPE_TEXT_BYTE || msgType == Message.TYPE_MAP_BYTE) {
        BytedMessage byteMessage = (BytedMessage) message;
        byte[] bytes = byteMessage.getByteArray();
        f.writeBinary(bytes);
        session.flush(f);
    }
}
Also used : ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) SocketSession(com.generallycloud.baseio.component.SocketSession) ProtobaseFutureImpl(com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl) BytedMessage(com.generallycloud.baseio.container.jms.BytedMessage)

Example 8 with ProtobaseFutureImpl

use of com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl in project baseio by generallycloud.

the class TestListenSimpleServlet method doAccept.

@Override
protected void doAccept(SocketSession session, ParamedProtobaseFuture future) throws Exception {
    String test = future.getReadText();
    if (StringUtil.isNullOrBlank(test)) {
        test = "test";
    }
    future.write(test);
    future.write("$");
    session.flush(future);
    for (int i = 0; i < 5; i++) {
        ProtobaseFuture f = new ProtobaseFutureImpl(session.getContext(), future.getFutureId(), future.getFutureName());
        f.write(test);
        f.write("$");
        session.flush(f);
    }
}
Also used : ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) ParamedProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ParamedProtobaseFuture) ProtobaseFutureImpl(com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl)

Example 9 with ProtobaseFutureImpl

use of com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl in project baseio by generallycloud.

the class FileSendUtil method sendFile.

public void sendFile(SocketSession session, String serviceName, File file, int cacheSize) throws Exception {
    FileInputStream inputStream = new FileInputStream(file);
    int available = inputStream.available();
    int time = (available + cacheSize) / cacheSize - 1;
    byte[] cache = new byte[cacheSize];
    JSONObject json = new JSONObject();
    json.put(FileReceiveUtil.FILE_NAME, file.getName());
    json.put(FileReceiveUtil.IS_END, false);
    String jsonString = json.toJSONString();
    for (int i = 0; i < time; i++) {
        FileUtil.readInputStream(inputStream, cache);
        ProtobaseFuture f = new ProtobaseFutureImpl(session.getContext(), serviceName);
        f.write(jsonString);
        f.writeBinary(cache);
        session.flush(f);
    }
    int r = FileUtil.readInputStream(inputStream, cache);
    json.put(FileReceiveUtil.IS_END, true);
    ProtobaseFuture f = new ProtobaseFutureImpl(session.getContext(), serviceName);
    f.write(json.toJSONString());
    f.writeBinary(cache, 0, r);
    session.flush(f);
    CloseUtil.close(inputStream);
}
Also used : ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) JSONObject(com.alibaba.fastjson.JSONObject) ProtobaseFutureImpl(com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl) FileInputStream(java.io.FileInputStream)

Example 10 with ProtobaseFutureImpl

use of com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl in project baseio by generallycloud.

the class SimpleTestProtobaseClient method main.

public static void main(String[] args) throws Exception {
    IoEventHandleAdaptor eventHandleAdaptor = new IoEventHandleAdaptor() {

        @Override
        public void accept(SocketSession session, Future future) throws Exception {
            System.out.println();
            System.out.println("____________________" + future.getReadText());
            System.out.println();
        }
    };
    SocketChannelContext context = new NioSocketChannelContext(new ServerConfiguration("localhost", 18300));
    context.getServerConfiguration().setSERVER_ENABLE_MEMORY_POOL_DIRECT(true);
    SocketChannelConnector connector = new SocketChannelConnector(context);
    connector.setTimeout(99999999);
    context.setIoEventHandleAdaptor(eventHandleAdaptor);
    context.addSessionEventListener(new LoggerSocketSEListener());
    context.setProtocolFactory(new ProtobaseProtocolFactory());
    SocketSession session = connector.connect();
    ProtobaseFuture future = new ProtobaseFutureImpl(context, "test222");
    future.write("hello server!");
    session.flush(future);
    ThreadUtil.sleep(100);
    CloseUtil.close(connector);
}
Also used : ProtobaseProtocolFactory(com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory) SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) SocketSession(com.generallycloud.baseio.component.SocketSession) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) IoEventHandleAdaptor(com.generallycloud.baseio.component.IoEventHandleAdaptor) ProtobaseFutureImpl(com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl) ProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture) Future(com.generallycloud.baseio.protocol.Future) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext)

Aggregations

ProtobaseFutureImpl (com.generallycloud.baseio.codec.protobase.future.ProtobaseFutureImpl)12 ProtobaseFuture (com.generallycloud.baseio.codec.protobase.future.ProtobaseFuture)10 SocketChannelContext (com.generallycloud.baseio.component.SocketChannelContext)8 SocketSession (com.generallycloud.baseio.component.SocketSession)8 LoggerSocketSEListener (com.generallycloud.baseio.component.LoggerSocketSEListener)7 NioSocketChannelContext (com.generallycloud.baseio.component.NioSocketChannelContext)7 ServerConfiguration (com.generallycloud.baseio.configuration.ServerConfiguration)7 SocketChannelConnector (com.generallycloud.baseio.connector.SocketChannelConnector)7 Future (com.generallycloud.baseio.protocol.Future)7 ProtobaseProtocolFactory (com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory)6 IoEventHandleAdaptor (com.generallycloud.baseio.component.IoEventHandleAdaptor)6 JSONObject (com.alibaba.fastjson.JSONObject)1 BalanceClientSocketSession (com.generallycloud.baseio.balance.BalanceClientSocketSession)1 BalanceClientSocketSessionFactory (com.generallycloud.baseio.balance.BalanceClientSocketSessionFactory)1 ByteBuf (com.generallycloud.baseio.buffer.ByteBuf)1 ByteBufAllocator (com.generallycloud.baseio.buffer.ByteBufAllocator)1 ParamedProtobaseProtocolFactory (com.generallycloud.baseio.codec.protobase.ParamedProtobaseProtocolFactory)1 ParamedProtobaseFuture (com.generallycloud.baseio.codec.protobase.future.ParamedProtobaseFuture)1 ProtobaseBeatFutureFactory (com.generallycloud.baseio.codec.protobase.future.ProtobaseBeatFutureFactory)1 ProtobufUtil (com.generallycloud.baseio.codec.protobuf.ProtobufUtil)1