]> Entropealabs - wampex_client.git/commitdiff
update states and resources, use module variables
authorChristopher <chris@entropealabs.com>
Sat, 22 Feb 2020 03:36:02 +0000 (21:36 -0600)
committerChristopher <chris@entropealabs.com>
Sat, 22 Feb 2020 03:36:02 +0000 (21:36 -0600)
lib/wampex/session.ex
priv/session.json

index 0d98e5d20011431ab6eb21c85bce84610222f435..cc6450ab1d66e4ff4c2616a9a480f4c31b1ac56c 100644 (file)
@@ -61,6 +61,37 @@ defmodule Wampex.Session do
           requests: []
         }
 
+  ## States
+  @established "Established"
+  @init "Init"
+  @challenge "Challenge"
+  @abort "Abort"
+  @goodbye "Goodbye"
+  @event "Event"
+  @invocation "Invocation"
+  @interrupt "Interrupt"
+  @handshake "Handshake"
+
+  ## Resources
+  @init_transport "InitTransport"
+  @wait_transport "WaitForTransport"
+  @hello "Hello"
+  @handle_challenge "HandleChallenge"
+  @handle_established "HandleEstablished"
+  @handle_abort "HandleAbort"
+  @handle_goodbye "HandleGoodbye"
+  @handle_event "HandleEvent"
+  @handle_invocation "HandleInvocation"
+  @handle_message "HandleMessage"
+  @handle_interrupt "HandleInterrupt"
+  @handle_handshake "HandleHandshake"
+
+  @spec cast_send_request(name :: atom() | pid(), request :: Wampex.message(), from :: pid()) ::
+          :ok
+  def cast_send_request(name, request, pid) do
+    __MODULE__.cast(name, {:send_request, request, pid})
+  end
+
   @spec cast_send_request(name :: atom() | pid(), request :: Wampex.message()) :: :ok
   def cast_send_request(name, request) do
     __MODULE__.cast(name, {:send_request, request})
@@ -76,7 +107,7 @@ defmodule Wampex.Session do
   def handle_call(
         {:send_request, request},
         from,
-        "Established",
+        @established,
         %SL{data: %Sess{request_id: r_id, transport: tt, transport_pid: t} = sess} = data
       ) do
     request_id = do_send(r_id, tt, t, request)
@@ -103,10 +134,29 @@ defmodule Wampex.Session do
     {:ok, %SL{data | data: %Sess{sess | message_queue: [{request, from} | mq]}}, []}
   end
 
+  @impl true
+  def handle_cast(
+        {:send_request, request, from},
+        @established,
+        %SL{data: %Sess{request_id: r_id, transport: tt, transport_pid: t} = sess} = data
+      ) do
+    request_id = do_send(r_id, tt, t, request)
+
+    {:ok,
+     %SL{
+       data
+       | data: %Sess{
+           sess
+           | request_id: request_id,
+             requests: [{request_id, from} | sess.requests]
+         }
+     }, []}
+  end
+
   @impl true
   def handle_cast(
         {:send_request, request},
-        "Established",
+        @established,
         %SL{data: %Sess{request_id: r_id, transport: tt, transport_pid: t} = sess} = data
       ) do
     request_id = do_send(r_id, tt, t, request)
@@ -130,9 +180,9 @@ defmodule Wampex.Session do
 
   @impl true
   def handle_resource(
-        "InitTransport",
+        @init_transport,
         _,
-        "WaitForTransport",
+        @wait_transport,
         data
       ) do
     Logger.debug("Waiting for transport to connect...")
@@ -141,9 +191,9 @@ defmodule Wampex.Session do
 
   @impl true
   def handle_resource(
-        "Hello",
+        @hello,
         _,
-        "Init",
+        @init,
         %SL{
           data: %Sess{
             transport: tt,
@@ -160,9 +210,9 @@ defmodule Wampex.Session do
 
   @impl true
   def handle_resource(
-        "HandleChallenge",
+        @handle_challenge,
         _,
-        "Challenge",
+        @challenge,
         %SL{
           data: %Sess{
             transport: tt,
@@ -179,9 +229,9 @@ defmodule Wampex.Session do
 
   @impl true
   def handle_resource(
-        "GoodBye",
+        @goodbye,
         _,
-        "GoodBye",
+        @handle_goodbye,
         %SL{data: %Sess{transport: tt, transport_pid: t, goodbye: goodbye}} = data
       ) do
     tt.send_message(t, Peer.goodbye(goodbye))
@@ -190,7 +240,7 @@ defmodule Wampex.Session do
 
   @impl true
   def handle_resource(
-        "HandleMessage",
+        @handle_message,
         _,
         _,
         %SL{data: %Sess{message: msg, requests: requests, roles: roles}} = data
@@ -204,16 +254,16 @@ defmodule Wampex.Session do
   end
 
   @impl true
-  def handle_resource("Abort", _, "Abort", data) do
+  def handle_resource(@handle_abort, _, @abort, data) do
     Logger.warn("Aborting: #{data.data.error}")
     {:ok, data, []}
   end
 
   @impl true
   def handle_resource(
-        "Established",
+        @handle_established,
         _,
-        "Established",
+        @established,
         %SL{
           data:
             %Sess{
@@ -236,15 +286,15 @@ defmodule Wampex.Session do
   end
 
   @impl true
-  def handle_resource("HandleHandshake", _, "Handshake", data) do
+  def handle_resource(@handle_handshake, _, @handshake, data) do
     {:ok, data, []}
   end
 
   @impl true
   def handle_resource(
-        "HandleEvent",
+        @handle_event,
         _,
-        "Event",
+        @event,
         %SL{data: %Sess{name: name, event: event}} = sl
       ) do
     Logger.debug("Received Event #{inspect(event)}")
@@ -260,9 +310,9 @@ defmodule Wampex.Session do
 
   @impl true
   def handle_resource(
-        "HandleInvocation",
+        @handle_invocation,
         _,
-        "Invocation",
+        @invocation,
         %SL{data: %Sess{name: name, invocation: invocation}} = sl
       ) do
     Logger.debug("Received Invocation #{inspect(invocation)}")
@@ -277,9 +327,9 @@ defmodule Wampex.Session do
 
   @impl true
   def handle_resource(
-        "HandleInterrupt",
+        @handle_interrupt,
         _,
-        "Interrupt",
+        @interrupt,
         %SL{data: %Sess{interrupt: {id, opts}, name: name}} = data
       ) do
     [{callee, _procedure}] = Registry.lookup(Wampex.callee_registry_name(name), id)
index bc25956cef7bf082aa9fb4baffc4ae91ad932231..b09b052d12b4c755837417b227ca0814f9b8ee61 100644 (file)
@@ -33,7 +33,7 @@
       "Choices": [
         {
           "StringEquals": ":message_received",
-          "Next": "HandleMessage"
+          "Next": "Message"
         },
         {
           "StringEquals": ":abort",
     },
     "Established": {
       "Type": "Choice",
-      "Resource": "Established",
+      "Resource": "HandleEstablished",
       "Choices": [
         {
           "StringEquals": ":message_received",
-          "Next": "HandleMessage"
+          "Next": "Message"
         },
         {
           "StringEquals": ":goodbye",
@@ -59,7 +59,7 @@
         }
       ]
     },
-    "HandleMessage": {
+    "Message": {
       "Type": "Choice",
       "Resource": "HandleMessage",
       "Choices": [
     },
     "Abort": {
       "Type": "Task",
-      "Resource": "Abort",
+      "Resource": "HandleAbort",
       "End": true
     }
   }