Search in sources :

Example 1 with FileReceiveUtil

use of com.generallycloud.baseio.container.FileReceiveUtil in project baseio by generallycloud.

the class TestDownload method main.

public static void main(String[] args) throws Exception {
    String serviceName = "TestDownloadServlet";
    String fileName = "upload-flashmail-2.4.exe";
    JSONObject j = new JSONObject();
    j.put(FileReceiveUtil.FILE_NAME, fileName);
    LoggerFactory.configure();
    SimpleIoEventHandle eventHandle = new SimpleIoEventHandle();
    ServerConfiguration configuration = new ServerConfiguration(8300);
    SocketChannelContext context = new NioSocketChannelContext(configuration);
    SocketChannelConnector connector = new SocketChannelConnector(context);
    context.setIoEventHandleAdaptor(eventHandle);
    context.setProtocolFactory(new ProtobaseProtocolFactory());
    context.addSessionEventListener(new LoggerSocketSEListener());
    connector.getContext().setProtocolFactory(new ProtobaseProtocolFactory());
    FixedSession session = new FixedSession(connector.connect());
    final FileReceiveUtil fileReceiveUtil = new FileReceiveUtil("download-");
    session.listen(serviceName, new OnFuture() {

        @Override
        public void onResponse(SocketSession session, Future future) {
            try {
                fileReceiveUtil.accept(session, (ParamedProtobaseFuture) future, false);
            } catch (Exception e) {
                DebugUtil.debug(e);
            }
        }
    });
    long old = System.currentTimeMillis();
    session.write(serviceName, j.toJSONString());
    System.out.println("Time:" + (System.currentTimeMillis() - old));
    ThreadUtil.sleep(5000);
    CloseUtil.close(connector);
}
Also used : SocketChannelConnector(com.generallycloud.baseio.connector.SocketChannelConnector) LoggerSocketSEListener(com.generallycloud.baseio.component.LoggerSocketSEListener) OnFuture(com.generallycloud.baseio.container.OnFuture) ServerConfiguration(com.generallycloud.baseio.configuration.ServerConfiguration) FixedSession(com.generallycloud.baseio.container.FixedSession) SimpleIoEventHandle(com.generallycloud.baseio.container.SimpleIoEventHandle) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) SocketChannelContext(com.generallycloud.baseio.component.SocketChannelContext) NioSocketChannelContext(com.generallycloud.baseio.component.NioSocketChannelContext) ProtobaseProtocolFactory(com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory) JSONObject(com.alibaba.fastjson.JSONObject) SocketSession(com.generallycloud.baseio.component.SocketSession) FileReceiveUtil(com.generallycloud.baseio.container.FileReceiveUtil) Future(com.generallycloud.baseio.protocol.Future) ParamedProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ParamedProtobaseFuture) OnFuture(com.generallycloud.baseio.container.OnFuture) ParamedProtobaseFuture(com.generallycloud.baseio.codec.protobase.future.ParamedProtobaseFuture)

Aggregations

JSONObject (com.alibaba.fastjson.JSONObject)1 ProtobaseProtocolFactory (com.generallycloud.baseio.codec.protobase.ProtobaseProtocolFactory)1 ParamedProtobaseFuture (com.generallycloud.baseio.codec.protobase.future.ParamedProtobaseFuture)1 LoggerSocketSEListener (com.generallycloud.baseio.component.LoggerSocketSEListener)1 NioSocketChannelContext (com.generallycloud.baseio.component.NioSocketChannelContext)1 SocketChannelContext (com.generallycloud.baseio.component.SocketChannelContext)1 SocketSession (com.generallycloud.baseio.component.SocketSession)1 ServerConfiguration (com.generallycloud.baseio.configuration.ServerConfiguration)1 SocketChannelConnector (com.generallycloud.baseio.connector.SocketChannelConnector)1 FileReceiveUtil (com.generallycloud.baseio.container.FileReceiveUtil)1 FixedSession (com.generallycloud.baseio.container.FixedSession)1 OnFuture (com.generallycloud.baseio.container.OnFuture)1 SimpleIoEventHandle (com.generallycloud.baseio.container.SimpleIoEventHandle)1 Future (com.generallycloud.baseio.protocol.Future)1