package io.scalecube.gateway.websocket;

import io.scalecube.services.ServiceCall;
import io.scalecube.services.codec.ServiceMessageDataCodec;

import reactor.core.publisher.Mono;

public class WebsocketGateway {

  public static class Builder {

    private ServiceCall call;
    private WebSocketAcceptor acceptor;
    private ServiceMessageDataCodec dataCodec = new ServiceMessageDataCodec();

    public Builder(ServiceCall call) {
      this.call = call;
    }

    public WebSocketServer build() {
      acceptor = new WebSocketAcceptor() {
        @Override
        public Mono<Void> onConnect(WebSocketSession session) {
          return session.send(
              call.invoke(session.receive()
                  .log("recv"))
                  .log("send")
                  .map(dataCodec::encode))
              .then();
        }

        @Override
        public Mono<Void> onDisconnect(WebSocketSession session) {
          return Mono.never();
        }
      };

      return new WebSocketServer(acceptor);
    }

  }

  public static Builder builder(ServiceCall call) {
    return new Builder(call);
  }

}

@@ -32,17 +32,19 @@
 bsocket;
-%0A
+%0D%0A%0D
 %0Aimport 
@@ -77,16 +77,17 @@
 iceCall;
+%0D
 %0Aimport 
@@ -138,17 +138,19 @@
 taCodec;
-%0A
+%0D%0A%0D
 %0Aimport 
@@ -177,17 +177,19 @@
 er.Mono;
-%0A
+%0D%0A%0D
 %0Apublic 
@@ -212,17 +212,19 @@
 ateway %7B
-%0A
+%0D%0A%0D
 %0A  publi
@@ -247,17 +247,19 @@
 uilder %7B
-%0A
+%0D%0A%0D
 %0A    pri
@@ -276,24 +276,25 @@
 ceCall call;
+%0D
 %0A    private
@@ -321,16 +321,17 @@
 cceptor;
+%0D
 %0A    pri
@@ -401,17 +401,19 @@
 Codec();
-%0A
+%0D%0A%0D
 %0A    pub
@@ -439,24 +439,25 @@
 Call call) %7B
+%0D
 %0A      this.
@@ -468,23 +468,26 @@
  = call;
+%0D
 %0A    %7D
-%0A
+%0D%0A%0D
 %0A    pub
@@ -511,24 +511,25 @@
 er build() %7B
+%0D
 %0A      accep
@@ -559,16 +559,17 @@
 ptor() %7B
+%0D
 %0A       
@@ -570,32 +570,33 @@
        @Override
+%0D
 %0A        public 
@@ -635,32 +635,33 @@
 ssion session) %7B
+%0D
 %0A          retur
@@ -675,16 +675,17 @@
 on.send(
+%0D
 %0A       
@@ -700,14 +700,28 @@
 all.
-invoke
+requestBidirectional
 (ses
@@ -734,16 +734,17 @@
 eceive()
+%0D
 %0A       
@@ -767,16 +767,17 @@
 %22recv%22))
+%0D
 %0A       
@@ -799,16 +799,17 @@
 (%22send%22)
+%0D
 %0A       
@@ -843,16 +843,17 @@
 encode))
+%0D
 %0A       
@@ -867,16 +867,17 @@
 .then();
+%0D
 %0A       
@@ -878,17 +878,19 @@
        %7D
-%0A
+%0D%0A%0D
 %0A       
@@ -899,16 +899,17 @@
 Override
+%0D
 %0A       
@@ -967,16 +967,17 @@
 ssion) %7B
+%0D
 %0A       
@@ -999,16 +999,17 @@
 never();
+%0D
 %0A       
@@ -1010,16 +1010,17 @@
        %7D
+%0D
 %0A      %7D
@@ -1020,17 +1020,19 @@
       %7D;
-%0A
+%0D%0A%0D
 %0A      r
@@ -1071,20 +1071,25 @@
 or);
+%0D
 %0A    %7D
-%0A
+%0D%0A%0D
 %0A  %7D
-%0A
+%0D%0A%0D
 %0A  p
@@ -1136,16 +1136,17 @@
  call) %7B
+%0D
 %0A    ret
@@ -1171,12 +1171,16 @@
 ll);
+%0D
 %0A  %7D
-%0A
+%0D%0A%0D
 %0A%7D
+%0D
 %0A

use request bidi