summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--server.erl56
1 files changed, 39 insertions, 17 deletions
diff --git a/server.erl b/server.erl
index 7fcd6ab..2913999 100644
--- a/server.erl
+++ b/server.erl
@@ -1,12 +1,9 @@
-module(server).
--export([server/0, server/2, worker/2, create_workers/2, acceptor/2, listener/1, manager/1, broadcast/2, broadcast/3, handle_data/3]).
+-export([server/2, worker/2, create_workers/2, acceptor/2, listener/1, manager/1, broadcast/2, broadcast/3, handle_data/3]).
-server() ->
- server(1235, 10).
server(Port, NumWorkers) ->
ets:new(client_table, [named_table, public, {read_concurrency, true}]),
ets:insert(client_table, {counter, 0}),
- ets:insert(client_table, {server, "Server"}),
{ok, ServerSock} = gen_tcp:listen(Port, [list]),
spawn(?MODULE, create_workers, [NumWorkers, self()]),
@@ -32,6 +29,7 @@ supervisor(ServerSock, Pids) ->
message_all(lists:delete(From, Pids), {close, From}),
supervisor(ServerSock, lists:delete(From, Pids));
+
{announce, Data} ->
[Pid|_] = Pids,
Pid ! {broadcast, "Server", ok, Data},
@@ -60,12 +58,16 @@ manager(Socks) ->
broadcast(Socks, Data, norepeat),
manager(Socks);
{broadcast, FromIden, FromSock, Data} ->
- NewData = FromIden ++ ": " ++ Data,
- broadcast(lists:delete(FromSock, Socks), NewData),
+ broadcast(lists:delete(FromSock, Socks),
+ io_lib:format("~s: ~s\n", [FromIden, Data])),
manager(Socks);
{pm, FromIden, ToSock, Data} ->
- NewData = "(pm) " ++ FromIden ++ ": " ++ Data,
- broadcast([ToSock], NewData),
+ broadcast([ToSock],
+ io_lib:format("(pm) ~s: ~s\n", [FromIden, Data])),
+ manager(Socks);
+ {rename, _, OldIden, NewIden } ->
+ self() ! {broadcast, "Server", ok,
+ io_lib:format("~s renamed to ~s", [OldIden, NewIden])},
manager(Socks)
end.
@@ -94,18 +96,30 @@ acceptor(ListenerPid, ServerSock) ->
handle_data(ManagerPid, From, Data) ->
- case catch parse_data(ManagerPid, From, Data) of
+ case catch parse_data(ManagerPid, From, string:trim(Data)) of
ok -> ok;
_ ->
- ManagerPid ! {pm, "Server", From, "ERR: Transaction Failed\n"}
+ ManagerPid ! {pm, "Server", From, "ERR: Transaction Failed"}
end.
parse_data(ManagerPid, From, Data) ->
case string:tokens(Data, " ") of
+ ["/help" | _] ->
+ ManagerPid ! {pm, "Server", From, "..."};
["/pm" | [ToIden | _]] ->
NewData = string:slice(Data, string:length(ToIden) + 5),
ManagerPid ! {pm, client_iden(From), client_sock(ToIden), NewData};
- ["\n"] -> ok;
+ ["/pm" | _] ->
+ ManagerPid ! {pm, "Server", From, "/pm: ..."};
+ ["/rename" | [NewIden | []]] ->
+ {OldIden, _} = client_update(From, NewIden),
+ ManagerPid ! {rename, From, OldIden, NewIden};
+ ["/rename" | _] ->
+ ManagerPid ! {pm, "Server", From, "/rename: ..."};
+ [[$/ | _] | _] ->
+ ManagerPid ! {pm, "Server", From,
+ "Invalid Command, use /help for help"};
+ [] -> ok;
_ ->
ManagerPid ! {broadcast, client_iden(From), From, Data}
end, ok.
@@ -124,15 +138,23 @@ broadcast([Sock|Socks], Data, Opt) ->
broadcast([], _, _) -> ok.
client_add(Sock) ->
- [{_, Count}|_] = ets:lookup(client_table, counter),
- Iden = "Anon"++integer_to_list(Count),
- ets:insert(client_table, {Iden, Sock}),
- ets:insert(client_table, {Sock, Iden}),
- ets:update_counter(client_table, counter, 1).
+ Count = ets:update_counter(client_table, counter, 1),
+ Iden = "Mitko"++integer_to_list(Count),
+ true = ets:insert_new(client_table, {Iden, Sock}),
+ true = ets:insert_new(client_table, {Sock, Iden}),
+ Iden.
client_del(Sock) ->
[{_, Iden}|_] = ets:lookup(client_table, Sock),
ets:delete(client_table, Sock),
- ets:delete(client_table, Iden).
+ ets:delete(client_table, Iden),
+ Iden.
+client_update(Sock, NewIden) ->
+ [{_, OldIden}|_] = ets:lookup(client_table, Sock),
+ ets:delete(client_table, OldIden),
+ [] = ets:lookup(client_table, NewIden),
+ true = ets:update_element(client_table, Sock, {2, NewIden}),
+ true = ets:insert_new(client_table, {NewIden, Sock}),
+ {OldIden, NewIden}.
client_iden(Sock) ->
[{_, Iden}|_] = ets:lookup(client_table, Sock),
Iden.