-
Notifications
You must be signed in to change notification settings - Fork 0
/
psocket.erl
78 lines (70 loc) · 2.11 KB
/
psocket.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
-module(psocket).
-compile(export_all).
-define(OPCIONES,[{active,false},{mode,binary}]).
-import(pbalance,[getNode/0]).
getName(Socket)->
server:empty(),
case string:tokens(inbox(Socket)," ") of
["CON",Cid,Nombre] ->
dir!{add,self(),Nombre},
receive
ok ->
gen_tcp:send(Socket,"OK "++Cid),
P=spawn(?MODULE,listen,[Socket,Nombre]),
interfaz(Socket,Nombre,P);
error ->
gen_tcp:send(Socket, "ERROR "++Cid++" Used"),
getName(Socket)
end;
[_Com|[Id|_L]] ->
gen_tcp:send(Socket,"ERROR "++Id++" comanderror");
_X ->
clean(Socket)
end.
listen(Client,Nombre)->
receive
{res,X} ->
gen_tcp:send(Client,X),
listen(Client,Nombre);
{msg,Y} ->
gen_tcp:send(Client,"UPD "++Y),
listen(Client,Nombre);
close ->
gen_tcp:send(Client,"UPD Cerrando la conexion"),
clean(Client,Nombre)
end.
interfaz(Client,Nombre,Plis)->
case string:tokens(inbox(Client)," ") of
["LSG",Id]->spawn(getNode(),pcomando,comand,[Plis,Id,{lsg}]);
["NEW",Id]->spawn(getNode(),pcomando,comand,[Plis,Id,{new}]);
["HLP",Id]->spawn(getNode(),pcomando,comand,[Plis,Id,{help}]);
["LEA",Id,Game]->spawn(getNode(),pcomando,comand,[Plis,Id,{leave,Game}]);
["OBS",Id,Game]->spawn(getNode(),pcomando,comand,[Plis,Id,{obs,Game}]);
["ACC",Id,Game]->spawn(getNode(),pcomando,comand,[Plis,Id,{acc,Game}]);
["PLA",Id,Game,Lugar]->spawn(getNode(),pcomando,comand,[Plis,Id,{pla,Game,toint(Lugar)}]);
["BYE",Id]->spawn(getNode(),pcomando,comand,[Plis,Id,{bye}]);
["ERROR"] ->clean(Client,Nombre);
[_C|[Id|_Else]]-> gen_tcp:send(Client,"ERROR "++Id++" PARSE ERROR")
end,
interfaz(Client,Nombre,Plis).
clean(Sk) ->
gen_tcp:close(Sk),
exit(ok).
clean(Sk,Nombre)->
dir!{remove,self(),Nombre},
game!{removeall,self()},
clean(Sk).
%~ newName(Nombre)->
%~ dir!{add,self(),Nombre},
%~ receive
%~ ok -> ok;
%~ error -> error
%~ end.
inbox(Socket)->
case gen_tcp:recv(Socket,0) of
{ok,<<S/binary>>} -> binary:bin_to_list(S);
{error,_Reason} -> gen_tcp:send(Socket,"Error conexionerror"),"ERROR"
end.
toint(String)->
{A,_B}=string:to_integer(String),
A.