All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.firefly.net.tcp.SecureTcpConnectionImpl Maven / Gradle / Ivy

There is a newer version: 5.0.0-dev6
Show newest version
package com.firefly.net.tcp;

import com.firefly.net.SecureSession;
import com.firefly.net.Session;
import com.firefly.net.buffer.FileRegion;
import com.firefly.utils.concurrent.Callback;
import com.firefly.utils.concurrent.Promise;
import com.firefly.utils.function.Action0;
import com.firefly.utils.function.Action1;
import com.firefly.utils.io.BufferUtils;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.concurrent.CompletableFuture;

public class SecureTcpConnectionImpl extends AbstractTcpConnection {

    SecureSession secureSession;

    public SecureTcpConnectionImpl(Session session, SecureSession secureSession) {
        super(session);
        this.secureSession = secureSession;
    }

    @Override
    public CompletableFuture writeToFuture(ByteBuffer byteBuffer) {
        Promise.Completable c = new Promise.Completable<>();
        try {
            secureSession.write(byteBuffer, new Callback() {
                public void succeeded() {
                    c.succeeded(null);
                }

                public void failed(Throwable x) {
                    c.failed(x);
                }
            });
        } catch (IOException e) {
            c.failed(e);
        }
        return c;
    }

    @Override
    public CompletableFuture writeToFuture(ByteBuffer[] byteBuffer) {
        Promise.Completable c = new Promise.Completable<>();
        try {
            secureSession.write(byteBuffer, new Callback() {
                public void succeeded() {
                    c.succeeded(null);
                }

                public void failed(Throwable x) {
                    c.failed(x);
                }
            });
        } catch (IOException e) {
            c.failed(e);
        }
        return c;
    }

    @Override
    public CompletableFuture writeToFuture(Collection byteBuffer) {
        return writeToFuture(byteBuffer.toArray(BufferUtils.EMPTY_BYTE_BUFFER_ARRAY));
    }

    @Override
    public CompletableFuture writeToFuture(String message) {
        return writeToFuture(message, DEFAULT_CHARSET);
    }

    @Override
    public CompletableFuture writeToFuture(String message, String charset) {
        ByteBuffer byteBuffer = BufferUtils.toBuffer(message, Charset.forName(charset));
        return writeToFuture(byteBuffer);
    }

    @Override
    public CompletableFuture writeToFuture(FileRegion file) {
        Promise.Completable c = new Promise.Completable<>();
        try {
            secureSession.transferFileRegion(file, new Callback() {
                public void succeeded() {
                    c.succeeded(null);
                }

                public void failed(Throwable x) {
                    c.failed(x);
                }
            });
        } catch (Throwable e) {
            c.failed(e);
        }
        return c;
    }

    @Override
    public TcpConnection write(ByteBuffer byteBuffer, Action0 succeeded, Action1 failed) {
        try {
            secureSession.write(byteBuffer, new Callback() {
                public void succeeded() {
                    succeeded.call();
                }

                public void failed(Throwable x) {
                    failed.call(x);
                }
            });
        } catch (Throwable e) {
            failed.call(e);
        }
        return this;
    }

    @Override
    public TcpConnection write(ByteBuffer[] byteBuffer, Action0 succeeded, Action1 failed) {
        try {
            secureSession.write(byteBuffer, new Callback() {
                public void succeeded() {
                    succeeded.call();
                }

                public void failed(Throwable x) {
                    failed.call(x);
                }
            });
        } catch (Throwable e) {
            failed.call(e);
        }
        return this;
    }

    @Override
    public TcpConnection write(Collection byteBuffer, Action0 succeeded, Action1 failed) {
        try {
            secureSession.write(byteBuffer.toArray(BufferUtils.EMPTY_BYTE_BUFFER_ARRAY), new Callback() {
                public void succeeded() {
                    succeeded.call();
                }

                public void failed(Throwable x) {
                    failed.call(x);
                }
            });
        } catch (Throwable e) {
            failed.call(e);
        }
        return this;
    }

    @Override
    public TcpConnection write(String message, Action0 succeeded, Action1 failed) {
        write(message, DEFAULT_CHARSET, succeeded, failed);
        return this;
    }

    @Override
    public TcpConnection write(String message, String charset, Action0 succeeded, Action1 failed) {
        ByteBuffer byteBuffer = BufferUtils.toBuffer(message, Charset.forName(charset));
        write(byteBuffer, succeeded, failed);
        return this;
    }

    @Override
    public TcpConnection write(FileRegion file, Action0 succeeded, Action1 failed) {
        try {
            secureSession.transferFileRegion(file, new Callback() {
                public void succeeded() {
                    succeeded.call();
                }

                public void failed(Throwable x) {
                    failed.call(x);
                }
            });
        } catch (Throwable e) {
            failed.call(e);
        }
        return this;
    }

    @Override
    public TcpConnection write(ByteBuffer byteBuffer, Action0 succeeded) {
        try {
            secureSession.write(byteBuffer, new Callback() {
                public void succeeded() {
                    succeeded.call();
                }
            });
        } catch (Throwable ignored) {
        }
        return this;
    }

    @Override
    public TcpConnection write(ByteBuffer[] byteBuffer, Action0 succeeded) {
        try {
            secureSession.write(byteBuffer, new Callback() {
                public void succeeded() {
                    succeeded.call();
                }
            });
        } catch (Throwable ignored) {
        }
        return this;
    }

    @Override
    public TcpConnection write(Collection byteBuffer, Action0 succeeded) {
        try {
            secureSession.write(byteBuffer.toArray(BufferUtils.EMPTY_BYTE_BUFFER_ARRAY), new Callback() {
                public void succeeded() {
                    succeeded.call();
                }
            });
        } catch (Throwable ignored) {
        }
        return this;
    }

    @Override
    public TcpConnection write(String message, Action0 succeeded) {
        return write(message, DEFAULT_CHARSET, succeeded);
    }

    @Override
    public TcpConnection write(String message, String charset, Action0 succeeded) {
        ByteBuffer byteBuffer = BufferUtils.toBuffer(message, Charset.forName(charset));
        try {
            secureSession.write(byteBuffer, new Callback() {
                public void succeeded() {
                    succeeded.call();
                }
            });
        } catch (Throwable ignored) {
        }
        return this;
    }

    @Override
    public TcpConnection write(FileRegion file, Action0 succeeded) {
        try {
            secureSession.transferFileRegion(file, new Callback() {
                public void succeeded() {
                    succeeded.call();
                }
            });
        } catch (Throwable ignored) {
        }
        return this;
    }

    @Override
    public TcpConnection write(ByteBuffer byteBuffer) {
        try {
            secureSession.write(byteBuffer, Callback.NOOP);
        } catch (Throwable ignored) {
        }
        return this;
    }

    @Override
    public TcpConnection write(ByteBuffer[] byteBuffer) {
        try {
            secureSession.write(byteBuffer, Callback.NOOP);
        } catch (Throwable ignored) {
        }
        return this;
    }

    @Override
    public TcpConnection write(Collection byteBuffer) {
        return write(byteBuffer.toArray(BufferUtils.EMPTY_BYTE_BUFFER_ARRAY));
    }

    @Override
    public TcpConnection write(String message) {
        return write(message, DEFAULT_CHARSET);
    }

    @Override
    public TcpConnection write(String message, String charset) {
        ByteBuffer byteBuffer = BufferUtils.toBuffer(message, Charset.forName(charset));
        try {
            secureSession.write(byteBuffer, Callback.NOOP);
        } catch (Throwable ignored) {
        }
        return this;
    }

    @Override
    public TcpConnection write(FileRegion file) {
        try {
            secureSession.transferFileRegion(file, Callback.NOOP);
        } catch (Throwable ignored) {
        }
        return this;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy