This commit is contained in:
Timofey Khoruzhii 2023-04-17 22:49:46 +03:00
commit 5b3d8ad0bc
9 changed files with 276 additions and 0 deletions

4
.formatter.exs Normal file
View file

@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]

26
.gitignore vendored Normal file
View file

@ -0,0 +1,26 @@
# The directory Mix will write compiled artifacts to.
/_build/
# If you run "mix test --cover", coverage assets end up here.
/cover/
# The directory Mix downloads your dependencies sources to.
/deps/
# Where third-party dependencies like ExDoc output generated docs.
/doc/
# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch
# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump
# Also ignore archive artifacts (built via "mix archive.build").
*.ez
# Ignore package tarball (built via "mix hex.build").
chat_server-*.tar
# Temporary files, for example, from tests.
/tmp/

21
README.md Normal file
View file

@ -0,0 +1,21 @@
# ChatServer
**TODO: Add description**
## Installation
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `chat_server` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:chat_server, "~> 0.1.0"}
]
end
```
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at <https://hexdocs.pm/chat_server>.

31
lib/chat_client.ex Normal file
View file

@ -0,0 +1,31 @@
defmodule ChatClient do
use GenServer
require Logger
def start_link(socket) do
GenServer.start_link(__MODULE__, socket)
end
def init(socket) do
:inet.setopts(socket, active: :once)
{:ok, %{socket: socket}}
end
def handle_info({:tcp, socket, data}, state) do
Logger.info("Received message: #{inspect(data)}")
ChatServer.broadcast(data)
:inet.setopts(socket, active: :once)
{:noreply, state}
end
def handle_info({:tcp_closed, _socket}, state) do
Logger.info("Client disconnected: #{inspect(state.socket)}")
ChatServer.remove_client(self())
{:stop, :normal, state}
end
def handle_info({:send_data, data}, %{socket: socket} = state) do
:gen_tcp.send(socket, data)
{:noreply, state}
end
end

74
lib/chat_server.ex Normal file
View file

@ -0,0 +1,74 @@
defmodule ChatServer do
use GenServer
require Logger
def start_link(_) do
GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
end
def init(:ok) do
{:ok, %{clients: %{}}}
end
# Run in main proccess
def start_listening(port \\ 0) do
Logger.info("Starting chat server on port #{port}")
{:ok, socket} =
:gen_tcp.listen(port, [:binary, packet: :line, active: false, reuseaddr: true])
# Add this line
{:ok, port} = :inet.port(socket)
{:ok, _} = Task.start(fn -> loop_acceptor(socket) end)
{:ok, port}
end
# New clients
# infinity loop for accept new clients
defp loop_acceptor(socket) do
{:ok, client_socket} = :gen_tcp.accept(socket)
Logger.info("New client connected: #{inspect(client_socket)}")
{:ok, client_pid} = ChatClient.start_link(client_socket)
:gen_tcp.controlling_process(client_socket, client_pid)
add_client(client_pid, client_socket)
loop_acceptor(socket)
end
def add_client(client_pid, client_socket) do
Logger.info("Add client: #{inspect(client_socket)}")
GenServer.cast(__MODULE__, {:add_client, client_pid, client_socket})
end
def handle_cast({:add_client, client_pid, client_socket}, %{clients: clients} = state) do
Logger.info("Registering client: #{inspect(client_pid)}")
new_clients = Map.put(clients, client_pid, {client_pid, client_socket})
{:noreply, %{state | clients: new_clients}}
end
# remove client
def handle_cast({:remove_client, client_pid}, %{clients: clients} = state) do
Logger.info("Unregistering client: #{inspect(client_pid)}")
new_clients = Map.delete(clients, client_pid)
{:noreply, %{state | clients: new_clients}}
end
def remove_client(client_pid) do
Logger.info("Remove client")
GenServer.cast(__MODULE__, {:remove_client, client_pid})
end
# broadcast
def broadcast(message) do
clients = GenServer.call(__MODULE__, :get_clients)
Enum.each(clients, fn {client_pid, _client_socket} ->
send(client_pid, {:send_data, message})
end)
end
# get clients
def handle_call(:get_clients, _from, state) do
Logger.info("Get clients")
{:reply, Map.values(state.clients), state}
end
end

View file

@ -0,0 +1,19 @@
defmodule ChatServer.Application do
use Application
require Logger
def start(_type, _args) do
Logger.info("Start application")
children = [
{ChatServer, []}
]
opts = [strategy: :one_for_one, name: ChatServer.Supervisor]
Supervisor.start_link(children, opts)
ChatServer.start_listening(8080)
{:ok, self()}
end
end

35
mix.exs Normal file
View file

@ -0,0 +1,35 @@
defmodule ChatServer.MixProject do
use Mix.Project
def project do
[
app: :chat_server,
version: "0.1.0",
elixir: "~> 1.14",
start_permanent: Mix.env() == :prod,
deps: deps(),
test_coverage: [tool: ExUnit.Coverage],
preferred_cli_env: [coveralls: :test, "coveralls.detail": :test, "coveralls.post": :test]
]
end
# Run "mix help compile.app" to learn about applications.
def application do
result = [extra_applications: [:logger]]
result ++
if Mix.env() == :test do
[]
else
[mod: {ChatServer.Application, []}]
end
end
# Run "mix help deps" to learn about dependencies.
defp deps do
[
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
]
end
end

65
test/chat_server_test.exs Normal file
View file

@ -0,0 +1,65 @@
defmodule ChatServerTest do
use ExUnit.Case, async: true
alias ChatServer
alias ChatClient
setup do
{:ok, server} = ChatServer.start_link([])
{:ok, %{server: server}}
end
test "server starts with an empty client list", %{server: server} do
clients = GenServer.call(server, :get_clients)
assert length(clients) == 0
end
test "adding and removing clients", %{server: server} do
# Add a fake client
GenServer.cast(server, {:add_client, self(), :fake_socket})
clients = GenServer.call(server, :get_clients)
assert length(clients) == 1
# Remove the fake client
GenServer.cast(server, {:remove_client, self()})
clients = GenServer.call(server, :get_clients)
assert length(clients) == 0
end
end
defmodule ChatClientTest do
use ExUnit.Case, async: true
alias ChatServer
alias ChatClient
setup do
{:ok, server} = ChatServer.start_link([])
{:ok, %{server: server}}
end
test "client connects and disconnects", %{server: server} do
# Start a fake server
{:ok, port} = ChatServer.start_listening()
# Connect a client
{:ok, client_socket} =
:gen_tcp.connect({127, 0, 0, 1}, port, [:binary, packet: :line, active: false])
{:ok, client} = ChatClient.start_link(client_socket)
Process.sleep(100)
# Check if the client is added to the server
clients = GenServer.call(server, :get_clients)
assert length(clients) == 1
# Disconnect the client
:gen_tcp.close(client_socket)
GenServer.stop(client)
Process.sleep(100)
# Check if the client is removed from the server
clients = GenServer.call(server, :get_clients)
assert length(clients) == 0
end
end

1
test/test_helper.exs Normal file
View file

@ -0,0 +1 @@
ExUnit.start()