IRC程序学习
%%聊天的中转站,将{chan,MM,Msg}形式的信息转化为 {mm, MM, Msg}形式
-module(mod_chat_controller).
-export([start/3]).
-import(lib_chan_mm, [send/2]). start(MM, _, _) ->
process_flag(trap_exit, true),
io:format("mod_chat_controller off we go ...~p~n",[MM]),
loop(MM). loop(MM) ->
receive
{chan, MM, Msg} ->
chat_server ! {mm, MM, Msg},
loop(MM);
{'EXIT', MM, _Why} ->
chat_server ! {mm_closed, MM};
Other ->
io:format("mod_chat_controller unexpected message =~p (MM=~p)~n",
[Other, MM]),
loop(MM)
end.
mod_chat_controller
%%聊天服务器
-module(chat_server).
-import(lib_chan_mm, [send/2, controller/2]).
-import(lists, [delete/2, foreach/2, map/2, member/2,reverse/2]). -compile(export_all). %%用chat.conf的配置启动服务器,每个连接到服务器的socket将调用mod_chat_controller模块的start函数
%%{port, 2223}.
%%{service, chat, password,"AsDT67aQ",mfa,mod_chat_controller,start,[]}.
start() ->
start_server(),
lib_chan:start_server("D:\\code\\erlcode\\socket_dist\\chat.conf"). start_server() ->
register(chat_server,
spawn(fun() ->
process_flag(trap_exit, true),
Val= (catch server_loop([])),
io:format("Server terminated with:~p~n",[Val])
end)). %%新启的进程,注册为chat_server,用来接收客户端用户的登录消息,当收到某一群组的登录信息时,如果该群组已经存在则传消息给该群组,如果不在则创建群组
server_loop(L) ->
receive
{mm, Channel, {login, Group, Nick}} ->
case lookup(Group, L) of
{ok, Pid} ->
Pid ! {login, Channel, Nick},
server_loop(L);
error ->
Pid = spawn_link(fun() ->
chat_group:start(Channel, Nick)
end),
server_loop([{Group,Pid}|L])
end;
{mm_closed, _} ->
server_loop(L);
{'EXIT', Pid, allGone} ->
L1 = remove_group(Pid, L),
server_loop(L1);
Msg ->
io:format("Server received Msg=~p~n",
[Msg]),
server_loop(L)
end. lookup(G, [{G,Pid}|_]) -> {ok, Pid};
lookup(G, [_|T]) -> lookup(G, T);
lookup(_,[]) -> error. remove_group(Pid, [{G,Pid}|T]) -> io:format("~p removed~n",[G]), T;
remove_group(Pid, [H|T]) -> [H|remove_group(Pid, T)];
remove_group(_, []) -> [].
chat_server
%%聊天群组,包含各个连接进该群组的进程id
-module(chat_group).
-import(lib_chan_mm, [send/2, controller/2]).
-import(lists, [foreach/2, reverse/2]). -export([start/2]). start(C, Nick) ->
process_flag(trap_exit, true),
controller(C, self()),
send(C, ack),
self() ! {chan, C, {relay, Nick, "I'm starting the group"}},
group_controller([{C,Nick}]). delete(Pid, [{Pid,Nick}|T], L) -> {Nick, reverse(T, L)};
delete(Pid, [H|T], L) -> delete(Pid, T, [H|L]);
delete(_, [], L) -> {"????", L}. group_controller([]) ->
exit(allGone);
group_controller(L) ->
receive
{chan, C, {relay, Nick, Str}} ->
foreach(fun({Pid,_}) -> send(Pid, {msg,Nick,C,Str}) end, L),
group_controller(L);
{login, C, Nick} ->
controller(C, self()),
send(C, ack),
self() ! {chan, C, {relay, Nick, "I'm joining the group"}},
group_controller([{C,Nick}|L]);
{chan_closed, C} ->
{Nick, L1} = delete(C, L, []),
self() ! {chan, C, {relay, Nick, "I'm leaving the group"}},
group_controller(L1);
Any ->
io:format("group controller received Msg=~p~n", [Any]),
group_controller(L)
end.
chat_group
-module(chat_client). -import(io_widget,
[get_state/1, insert_str/2, set_prompt/2, set_state/2,
set_title/2, set_handler/2, update_state/3]). -export([start/0, test/0, connect/5]). start() ->
connect("localhost", 2223, "AsDT67aQ", "general", "joe"). test() ->
connect("localhost", 2223, "AsDT67aQ", "general", "joe"),
connect("localhost", 2223, "AsDT67aQ", "general", "jane"),
connect("localhost", 2223, "AsDT67aQ", "general", "jim"),
connect("localhost", 2223, "AsDT67aQ", "general", "sue"). connect(Host, Port, HostPsw, Group, Nick) ->
spawn(fun() -> handler(Host, Port, HostPsw, Group, Nick) end). handler(Host, Port, HostPsw, Group, Nick) ->
process_flag(trap_exit, true),
Widget = io_widget:start(self()),
set_title(Widget, Nick),
set_state(Widget, Nick),
set_prompt(Widget, [Nick, " > "]),
set_handler(Widget, fun parse_command/1),
start_connector(Host, Port, HostPsw),
disconnected(Widget, Group, Nick). %%接收服务器的连接反馈信息,连接上后进行登录
disconnected(Widget, Group, Nick) ->
receive
{connected, MM} ->
insert_str(Widget, "connected to server\nsending data\n"),
lib_chan_mm:send(MM, {login, Group, Nick}),
wait_login_response(Widget, MM);
{Widget, destroyed} ->
exit(died);
{status, S} ->
insert_str(Widget, to_str(S)),
disconnected(Widget, Group, Nick);
Other ->
io:format("chat_client disconnected unexpected:~p~n",[Other]),
disconnected(Widget, Group, Nick)
end. %%接收服务器的登录反馈信息
wait_login_response(Widget, MM) ->
receive
{chan, MM, ack} ->
active(Widget, MM);
Other ->
io:format("chat_client login unexpected:~p~n",[Other]),
wait_login_response(Widget, MM)
end. %%登录后获取客户端的反馈,并接受传到客户端的信息
active(Widget, MM) ->
receive
{Widget, Nick, Str} ->
lib_chan_mm:send(MM, {relay, Nick, Str}),
active(Widget, MM);
{chan, MM, {msg, From, Pid, Str}} ->
insert_str(Widget, [From,"@",pid_to_list(Pid)," ", Str, "\n"]),
active(Widget, MM);
{'EXIT',Widget,windowDestroyed} ->
lib_chan_mm:close(MM);
{close, MM} ->
exit(serverDied);
Other ->
io:format("chat_client active unexpected:~p~n",[Other]),
active(Widget, MM)
end. %%链接chat服务器
start_connector(Host, Port, Pwd) ->
S = self(),
spawn_link(fun() -> try_to_connect(S, Host, Port, Pwd) end). try_to_connect(Parent, Host, Port, Pwd) ->
%% Parent is the Pid of the process that spawned this process
case lib_chan:connect(Host, Port, chat, Pwd, []) of
{error, _Why} ->
Parent ! {status, {cannot, connect, Host, Port}},
sleep(2000),
try_to_connect(Parent, Host, Port, Pwd);
{ok, MM} ->
lib_chan_mm:controller(MM, Parent),
Parent ! {connected, MM},
exit(connectorFinished)
end. sleep(T) ->
receive
after T -> true
end. to_str(Term) ->
io_lib:format("~p~n",[Term]). parse_command(Str) -> skip_to_gt(Str). skip_to_gt(">" ++ T) -> T;
skip_to_gt([_|T]) -> skip_to_gt(T);
skip_to_gt([]) -> exit("no >").
chat_client
-module(io_widget). -export([get_state/1,
start/1, test/0,
set_handler/2,
set_prompt/2,
set_state/2,
set_title/2, insert_str/2, update_state/3]). start(Pid) ->
gs:start(),
spawn_link(fun() -> widget(Pid) end). get_state(Pid) -> rpc(Pid, get_state).
set_title(Pid, Str) -> Pid ! {title, Str}.
set_handler(Pid, Fun) -> Pid ! {handler, Fun}.
set_prompt(Pid, Str) -> Pid ! {prompt, Str}.
set_state(Pid, State) -> Pid ! {state, State}.
insert_str(Pid, Str) -> Pid ! {insert, Str}.
update_state(Pid, N, X) -> Pid ! {updateState, N, X}. rpc(Pid, Q) ->
Pid ! {self(), Q},
receive
{Pid, R} ->
R
end. widget(Pid) ->
Size = [{width,500},{height,200}],
Win = gs:window(gs:start(),
[{map,true},{configure,true},{title,"window"}|Size]),
gs:frame(packer, Win,[{packer_x, [{stretch,1,500}]},
{packer_y, [{stretch,10,100,120},
{stretch,1,15,15}]}]),
gs:create(editor,editor,packer, [{pack_x,1},{pack_y,1},{vscroll,right}]),
gs:create(entry, entry, packer, [{pack_x,1},{pack_y,2},{keypress,true}]),
gs:config(packer, Size),
Prompt = " > ",
State = nil,
gs:config(entry, {insert,{0,Prompt}}),
loop(Win, Pid, Prompt, State, fun parse/1). loop(Win, Pid, Prompt, State, Parse) ->
receive
{From, get_state} ->
From ! {self(), State},
loop(Win, Pid, Prompt, State, Parse);
{handler, Fun} ->
loop(Win, Pid, Prompt, State, Fun);
{prompt, Str} ->
%% this clobbers the line being input ...
%% this could be fixed - hint
gs:config(entry, {delete,{0,last}}),
gs:config(entry, {insert,{0,Str}}),
loop(Win, Pid, Str, State, Parse);
{state, S} ->
loop(Win, Pid, Prompt, S, Parse);
{title, Str} ->
gs:config(Win, [{title, Str}]),
loop(Win, Pid, Prompt, State, Parse);
{insert, Str} ->
gs:config(editor, {insert,{'end',Str}}),
scroll_to_show_last_line(),
loop(Win, Pid, Prompt, State, Parse);
{updateState, N, X} ->
io:format("setelemtn N=~p X=~p State=~p~n",[N,X,State]),
State1 = setelement(N, State, X),
loop(Win, Pid, Prompt, State1, Parse);
{gs,_,destroy,_,_} ->
io:format("Destroyed~n",[]),
exit(windowDestroyed);
{gs, entry,keypress,_,['Return'|_]} ->
Text = gs:read(entry, text),
io:format("io_widget:input text:~p~n",[Text]),
io:format("io_widget:send text to pid:~p~n",[Pid]),
gs:config(entry, {delete,{0,last}}),
gs:config(entry, {insert,{0,Prompt}}),
try Parse(Text) of
Term ->
Pid ! {self(), State, Term}
catch
_:_ ->
self() ! {insert, "** bad input**\n** /h for help\n"}
end,
loop(Win, Pid, Prompt, State, Parse);
{gs,_,configure,[],[W,H,_,_]} ->
gs:config(packer, [{width,W},{height,H}]),
loop(Win, Pid, Prompt, State, Parse);
{gs, entry,keypress,_,_} ->
loop(Win, Pid, Prompt, State, Parse);
Any ->
io:format("Discarded:~p~n",[Any]),
loop(Win, Pid, Prompt, State, Parse)
end. scroll_to_show_last_line() ->
Size = gs:read(editor, size),
Height = gs:read(editor, height),
CharHeight = gs:read(editor, char_height),
TopRow = Size - Height/CharHeight,
if TopRow > 0 -> gs:config(editor, {vscrollpos, TopRow});
true -> gs:config(editor, {vscrollpos, 0})
end. test() ->
spawn(fun() -> test1() end). test1() ->
W = io_widget:start(self()),
io_widget:set_title(W, "Test window"),
loop(W). loop(W) ->
receive
{W, {str, Str}} ->
Str1 = Str ++ "\n",
io_widget:insert_str(W, Str1),
loop(W)
end. parse(Str) ->
{str, Str}.
io_widget
IRC程序学习的更多相关文章
- 通过反汇编C语言小程序学习Liunx汇编语言
大家好! 我是来自山东师范大学的吴乐. 今天在<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ...
- 微信小程序 学习资料
微信小程序 学习资料 资料名称 网址 官方教程 https://developers.weixin.qq.com/miniprogram/dev/index.html?t=18110517
- Tuxedo安装、配置、以及演示样例程序 (学习网址)
Tuxedo安装.配置.以及演示样例程序 (学习网址): 1.http://liu9403.iteye.com/blog/1415684 2.http://www.cnblogs.com/fnng/a ...
- Java开发桌面程序学习(一)——JavaFx+Jfoenix初始以及搭建
Java开发桌面程序学习(一)--JavaFx+Jfoenix初始以及搭建 前言 想做一个Java的桌面程序,但是,使用原生的Swing感觉又十分麻烦,那个布局都是拿代码设置,看着十分的乱,偶然的情况 ...
- 【微信小程序学习笔记】入门与了解
[微信小程序学习笔记(一)] IDE 下载安装 下载地址 官方工具:https://mp.weixin.qq.com/debug/w … tml?t=1476434678461 下载可执行文件后,可按 ...
- 微信小程序学习笔记二 数据绑定 + 事件绑定
微信小程序学习笔记二 1. 小程序特点概述 没有DOM 组件化开发: 具备特定功能效果的代码集合 体积小, 单个压缩包体积不能大于2M, 否则无法上线 小程序的四个重要的文件 *js *.wxml - ...
- 微信小程序学习笔记一 小程序介绍 & 前置知识
微信小程序学习笔记一 1. 什么是小程序? 2017年度百度百科十大热词之一 微信小程序, 简称小程序, 英文名 Mini Program, 是一种不需要下载安装即可使用的应用 ( 张小龙对其的定义是 ...
- 【C#】1.3 WPF应用程序学习要点
分类:C#.VS2015 创建日期:2016-06-14 使用教材:十二五国家级规划教材<C#程序设计及应用教程>(第3版) 一.要点概述 <C#程序设计及应用教程>(第3版) ...
- 微信小程序学习
官方网站 https://mp.weixin.qq.com/debug/wxadoc/dev/index.html 项目结构介绍 -- MINA框架 https://mp.weixin.qq.com/ ...
随机推荐
- Ubuntu 14.04 ThinkPad E431无线网卡驱动安装
Ubuntu 14.04下安装无线网卡驱动. sudo apt-get install linux-headers-generic build-essential dkms sudo apt-get ...
- Linux Platform驱动模型(二) _驱动方法【转】
转自:http://www.cnblogs.com/xiaojiang1025/archive/2017/02/06/6367910.html 在Linux设备树语法详解和Linux Platform ...
- python基础===trheading 模块
'''threading模块''' import threading import time def music(func): for i in range(2): print("[+]i ...
- HDU 5118 GRE Words Once More!
题目链接:HDU-5118 题意:给定一个有向无环图,每条边有一个权值.标定一些特定节点为“特殊节点”.从节点1出发到某“特殊节点”结束的路径,称为一个“GRE单词”.单词由路径上的权值组成.给定一组 ...
- 如何在本地用vs调试微信接口
这段时间在研究微信,看了网上很多都是把项目发布之后在服务器上调试,可以我想可以直接在vs上面设置断点调试 刚开始才用 http://www.cnblogs.com/hanzhaoxin/p/45186 ...
- ASPxCheckBoxList控件获取selected项的text和value的方法
设ASPxCheckBoxList的ClientInstanceName为list_ var needtext; for (var i = 0; i < list_.GetSelectedIte ...
- 修改centos地址连接为自动连接
1.进入目录/etc/sysconfig/network-scripts/ 2.修改ifcfg-etn0 文件 (即你的网卡标识命名的配置文件) 3.将ONBOOT=no改成yes 4.保存后重启 ...
- C# 笔记——覆盖和重写
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...
- JQuery实现AJAX实例
<script type="text/javascript" src="ReportServer?op=emb&resource=finereport.js ...
- ip和子网掩码的判断
只要记住B类IP的范围就好了(128以下的是A,128~191是B段,192以上是C段) 比如B类,网络地址为前两段,后面两段是主机地址,所以网络标识应该是255.255.0.0