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

org.noear.nami.channel.socketd.SocketdClientChannel Maven / Gradle / Ivy

There is a newer version: 3.0.5-M3
Show newest version
package org.noear.nami.channel.socketd;

import org.noear.nami.Channel;
import org.noear.nami.ChannelBase;
import org.noear.nami.Context;
import org.noear.nami.Result;
import org.noear.socketd.SocketD;
import org.noear.socketd.transport.core.Session;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Socketd 客户端通道
 *
 * @author noear
 * @since 1.3
 * @since 2.6
 */
public class SocketdClientChannel extends ChannelBase implements Channel {
    public static final SocketdClientChannel instance = new SocketdClientChannel();

    private final Map channelMap = new HashMap<>();
    private final ReentrantLock SYNC_LOCK = new ReentrantLock();

    private SocketdChannel get(String hostname, String url) {
        SocketdChannel channel = channelMap.get(hostname);

        if (channel == null) {
            SYNC_LOCK.lock();
            try {
                channel = channelMap.get(hostname);

                if (channel == null) {
                    try {
                        Session session = (Session) SocketD.createClient(url)
                                .listen(SocketdProxy.socketdToHandler)
                                .openOrThow();
                        channel = new SocketdChannel(() -> session);
                        channelMap.put(hostname, channel);
                    } catch (RuntimeException e) {
                        throw e;
                    } catch (Exception e) {
                        throw new IllegalStateException(e);
                    }
                }
            } finally {
                SYNC_LOCK.unlock();
            }
        }

        return channel;
    }

    @Override
    public Result call(Context ctx) throws Throwable {
        pretreatment(ctx);

        URI uri = URI.create(ctx.url);
        String hostname = uri.getAuthority();
        SocketdChannel channel = get(hostname, ctx.url);

        return channel.call(ctx);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy