brotorift 0.9.0 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 39731491bf8f75fd3020706e30be5bfeb20ce2ac29278a47c16554092d90724f
4
- data.tar.gz: eb54a4e1e1b8925458fe1f33943a595e566360b06d2838d404ac903cc798b7a6
3
+ metadata.gz: 9ea750ad59e5e8a3a45649decbfaa5e5d1d8626509f8a53d52fe07686e4bb776
4
+ data.tar.gz: fe20f3f62b599d1e654cc21424dd2fe1b80a63921ccfa7de5e26c93e620ba141
5
5
  SHA512:
6
- metadata.gz: 7dfae3292bf18d45826429a411f930773b0277fb62051880d10d580d61417362cebcb3dbb6d49a5763734879528aef574b8021646955295efa21ee3096c813ea
7
- data.tar.gz: fa60f4a94cc8a8cf060a922d4f3b4310e12c5f225a9a68770b896b6b4a6ee6669fca784cd039790f402139f1cd7917eb30df2d69e79fcd4aa8506e98d9895759
6
+ metadata.gz: 2eb09be1bc2de4cae9fcc30d56439b800d6160c0b2637a0a3a8c0524460714f31656c1c80f9da233b1b4e6a2d4b167a79a9a24d2b0f75d3d3513bd4d471bd3d6
7
+ data.tar.gz: bc66679e879bfa9d5bec86b01bf510d9f86346b8a230e5a9a64a85f8d11443fa861adc3c9d989fb95b097598991ab860e8e5288e7afb833fd7ad85fe28dc4b91
@@ -14,10 +14,16 @@ defmodule <%= node.namespace %>.<%= node.bot_client %> do
14
14
  GenServer.start_link(__MODULE__, args)
15
15
  end
16
16
 
17
+ @spec connect(client :: pid()) :: :ok
17
18
  def connect(client) do
18
19
  GenServer.call(client, :connect, :infinity)
19
20
  end
20
21
 
22
+ @spec sleep(client :: pid(), duration :: non_neg_integer()) :: :ok
23
+ def sleep(client, duration) do
24
+ GenServer.call(client, {:sleep, duration}, :infinity)
25
+ end
26
+
21
27
  <% for m in n.out_direction.messages.values %>
22
28
  @doc """
23
29
  <%= m.doc %>
@@ -51,42 +57,72 @@ defmodule <%= node.namespace %>.<%= node.bot_client %> do
51
57
  def init(_args) do
52
58
  {:ok, report} = BrotoriftBot.ReportManager.create_report()
53
59
  {:ok, tcp_client} = BrotoriftBot.TcpClientSupervisor.create_client(report, self())
54
- {:ok, {report, tcp_client}}
60
+ {:ok, {report, tcp_client, []}}
55
61
  end
56
62
 
57
- def handle_call(:connect, _from, {report, tcp_client}) do
63
+ def handle_call(:connect, _from, {report, tcp_client, []}) do
58
64
  BrotoriftBot.TcpClient.connect(tcp_client, @version)
59
- {:reply, :ok, {report, tcp_client}}
65
+ {:reply, :ok, {report, tcp_client, []}}
66
+ end
67
+
68
+ def handle_call({:sleep, duration}, _from, {report, tcp_client, queued_messages}) do
69
+ start_time = Time.utc_now()
70
+ Process.sleep(duration)
71
+ stop_time = Time.utc_now()
72
+ BrotoriftBot.Report.sleep(report, duration, start_time, stop_time)
73
+ {:reply, :ok, {report, tcp_client, queued_messages}}
60
74
  end
61
75
 
62
76
  <% for m in n.in_direction.messages.values %>
63
- def handle_call({:receive, :<%= m.bot_name %>}, _from, {report, tcp_client}) do
77
+ def handle_call({:receive, :<%= m.bot_name %>}, _from, {report, tcp_client, queued_messages}) do
64
78
  start_time = Time.utc_now()
65
- data = try_receive(<%= m.bot_header_name %>, tcp_client)
79
+ {data, messages_left} = try_receive(<%= m.bot_header_name %>, tcp_client, queued_messages)
66
80
  stop_time = Time.utc_now()
67
81
  BrotoriftBot.Report.receive(report, :<%= m.bot_name %>, start_time, stop_time)
68
82
 
69
83
  content = parse_packet(:<%= m.bot_name %>, data)
70
- {:reply, {:ok, content}, {report, tcp_client}}
84
+ {:reply, {:ok, content}, {report, tcp_client, messages_left}}
71
85
  end
72
86
  <% end %>
73
87
  <% for m in n.out_direction.messages.values %>
74
- def handle_cast({:send, {:<%= m.bot_send_name %><%= m.bot_params %>}}, {report, tcp_client}) do
88
+ def handle_cast({:send, {:<%= m.bot_send_name %><%= m.bot_params %>}}, {report, tcp_client, queued_messages}) do
75
89
  data = <<<%= m.bot_header_name %>::32-little>><% for p in m.members %>
76
90
  data = <%= p.type.bot_write node, p.bot_name %><% end %>
77
91
  time = Time.utc_now()
78
92
  :ok = BrotoriftBot.TcpClient.send_packet(tcp_client, data)
79
93
  BrotoriftBot.Report.send(report, :<%= m.bot_name %>, time)
80
- {:noreply, {report, tcp_client}}
94
+ {:noreply, {report, tcp_client, queued_messages}}
81
95
  end
82
96
  <% end %>
83
97
 
84
- defp try_receive(message, tcp_client) do
85
- receive do
86
- {^message, value} ->
87
- value
88
- _ ->
89
- try_receive(message, tcp_client)
98
+ def handle_info({message, value}, {report, tcp_client, queued_messages}) do
99
+ {:noreply, {report, tcp_client, queued_messages ++ [{message, value}]}}
100
+ end
101
+
102
+ defp try_receive(message, tcp_client, queued_messages) do
103
+ {value_found, messages_left_reversed} = queued_messages |> Enum.reduce({nil, []}, fn {m, v}, {value_found, messages_left} ->
104
+ if value_found != nil do
105
+ {value_found, [{m, v}] ++ messages_left}
106
+ else
107
+ if m == message do
108
+ {v, []}
109
+ else
110
+ {nil, []}
111
+ end
112
+ end
113
+ end
114
+ )
115
+
116
+ messages_left = Enum.reverse(messages_left_reversed)
117
+ if value_found != nil do
118
+ {value_found, messages_left}
119
+ else
120
+ receive do
121
+ {^message, value} ->
122
+ {value, messages_left}
123
+ _ ->
124
+ try_receive(message, tcp_client, messages_left)
125
+ end
90
126
  end
91
127
  end
92
128
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brotorift
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Ren