Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. defmodule TermuxSmsCli.Fetcher do
  2. use GenServer
  3.  
  4. defp fetch_int(filter_fn, minimum) do
  5. fetch_int(filter_fn, minimum, minimum)
  6. end
  7.  
  8. defp fetch_int(filter_fn, minimum, count) do
  9. {reply, 0} = System.cmd("ssh", ["192.168.2.8", "-p", "8022", "termux-sms-list", "-l", "#{count}"])
  10. reply = Jason.decode!(reply)
  11. reply = Enum.filter(reply, filter_fn)
  12. if (Enum.count(reply) < minimum) do
  13. fetch_int(filter_fn, minimum, 2*count)
  14. else
  15. reply
  16. end
  17. end
  18.  
  19. def start_link(opts) do
  20. GenServer.start_link(__MODULE__, :ok, opts)
  21. end
  22.  
  23. def init(:ok) do
  24. {:ok, nil}
  25. end
  26.  
  27. def handle_call({:fetch, count}, _From, nil) do
  28. {:reply, fetch_int(fn _ -> true end, count), nil}
  29. end
  30.  
  31. def handle_call({:fetch_thread, count, tid}, _From, nil) do
  32. {:reply, fetch_int(fn %{"threadid" => tid_} -> tid == tid_ end, count), nil}
  33. end
  34.  
  35. def fetch(pid, count) do
  36. GenServer.call(pid, {:fetch, count})
  37. end
  38.  
  39. def fetch_thread(pid, count, tid) do
  40. GenServer.call(pid, {:fetch_thread, count, tid})
  41. end
  42. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement