Murga-Projects Forums
the fltk loop and socket loop? - Printable Version

+- Murga-Projects Forums (http://www.murga-projects.com/forum)
+-- Forum: Project Forums (/forumdisplay.php?fid=1)
+--- Forum: MurgaLua - General (/forumdisplay.php?fid=2)
+--- Thread: the fltk loop and socket loop? (/showthread.php?tid=344)


the fltk loop and socket loop? - jyf1987 - 09-16-2008 02:45 PM

as you know i want to make an simple irc client recently,
with your help i can draw the widget and do with the event
but i dont know how to make the fltk loop and socket loop together

this is my code ,it can display the msg from irc server
but it often freez(i mean do not work and leave a dead state)

Code:
dofile("cfg.ini")

function quit_callback(msg)
   if fltk.fl_choice("Are you sure you want to EXIT ?", "No", "Yes", nil) >= 1 then
      w:hide()
      os.exit()
   end
end

function iput(msg)
  rd:send("PRIVMSG "..cfg.chanel.." "..msg.."\n")
end

ww,wh,wt=500,480,"Simple IRC Client - Powered by jyf1987"

w= fltk:Fl_Window(ww,wh,wt)
w:callback(quit_callback)

msgout= fltk:Fl_Browser(5, 5, 490, 440)


msgin= fltk:Fl_Input(5, 450, 490, 25)
msgin:callback(
function()
  iput(msgin:value())
  msgout:bottomline(msgout:size())
  msgin:value("")
end
)
msgin:when(fltk.FL_WHEN_ENTER_KEY_ALWAYS)
w:show()
Fl:flush() -- these two lines are necessary to draw before run() is called
Fl:check()
w:make_current()
-----------------------utils-------------------------------

split=function(str, pat)
   local t = {} -- NOTE: use {n = 0} in Lua-5.0
   local fpat = "(.-)" .. pat
   local last_end = 1
   local s, e, cap = str:find(fpat, 1)
   while s do
      if s ~= 1 or cap ~= "" then
table.insert(t,cap)
      end
      last_end = e+1
      s, e, cap = str:find(fpat, last_end)
   end
   if last_end <= #str then
      cap = str:sub(last_end)
      table.insert(t, cap)
   end
   return t
end

function ipong(sock)
    sock:send('PONG 123456')
end

function iquit(sock)
    sock:send('QUIT')
    sock:close()
end


function main()
while s do
  local i=string.find(s,"PING")
  if i==1 then
    print(s)
    ipong(rd)
    s=rd:receive('*l')
  end
  
  local k=string.find(s,":",2)
  if k==nil then
    k=1
  end
  local headstr=string.sub(s,1,k-2)
  print(headstr)
  local header=split(headstr," ")
  print(table.getn(header))
  local j=string.find(headstr,"PRIVMSG")
  --print(j)
  if j then
    if string.find(headstr,cfg.nick,j+7) then
        if string.sub(s,string.find(s,":",2)+1,string.len(s))=="quit" then
            iquit(rd)
        else
            print("personal msg: "..s)
        end
    else
        print("group msg: "..s)
    end
  end
  s=rd:receive('*l')
  --Fl:wait()
  Fl:flush()
  Fl:check()
  murgaLua.sleep(700)
end

end

print=function(str)
  msgout:add(str)
  msgout:bottomline(msgout:size())
end
-------------------------my process------------------------
rd=socket.connect(cfg.host,cfg.port)
rd:send("USER "..cfg.user.." orrr2.cn "..cfg.host.." :"..cfg.user.."\n")
rd:send("NICK "..cfg.nick.."\n")
if (cfg.pwd and cfg.pwd~="") then
  rd:send("PRIVMSG /msg nickserv identify "..cfg.pwd.."\n")
end
rd:send("JOIN "..cfg.chanel.."\n")
s=rd:receive('*l')
main()


can someone show me anyother good method for doing this thing?


RE: the fltk loop and socket loop? - jyf1987 - 09-16-2008 02:46 PM

to run this code,you need to make a new file named cfg.ini with the content

Code:
cfg={
  ["host"]="irc.freenode.net",
  ["port"]=6667,
  ["user"]="yman8",
  ["nick"]="botjyf1987s",
  ["pwd"]="",
  ["chanel"]="#puppylinux"
}




RE: the fltk loop and socket loop? - jyf1987 - 09-16-2008 04:00 PM

ok,i have solve the problem
and now i can chat with my own client,Big Grin

but it strange when the code run,it used almost 7mb ram
and after i minimize the main window and restore it to normal again
it used only <2mb ram and 700k when minimize


RE: the fltk loop and socket loop? - JohnMurga - 09-16-2008 07:37 PM

jyf1987 Wrote:
ok,i have solve the problem
and now i can chat with my own client,Big Grin

but it strange when the code run,it used almost 7mb ram
and after i minimize the main window and restore it to normal again
it used only <2mb ram and 700k when minimize


Cool !

Can you post the final version ?

As far as memory usage ... I'll look into it.

Cheers
John de Murga


RE: the fltk loop and socket loop? - jyf1987 - 09-16-2008 08:07 PM

ok,here is the code

Code:
dofile("cfg.ini")

function quit_callback(msg)
   if fltk.fl_choice("Are you sure you want to EXIT ?", "No", "Yes", nil) >= 1 then
      w:hide()
      os.exit()
   end
end

function iput(msg)
  rd:send("PRIVMSG "..cfg.chanel.." "..msg.."\n")
end

ww,wh,wt=500,480,"Simple IRC Client - Powered by jyf1987"

w= fltk:Fl_Window(ww,wh,wt)
w:callback(quit_callback)

msgout= fltk:Fl_Browser(5, 5, 490, 440)


msgin= fltk:Fl_Input(5, 450, 490, 25)
msgin:callback(
function()
  iput(msgin:value())
  msgout:bottomline(msgout:size())
  msgin:value("")
end
)
msgin:when(fltk.FL_WHEN_ENTER_KEY_ALWAYS)
w:show()
Fl:flush() -- these two lines are necessary to draw before run() is called
Fl:check()
w:make_current()
-----------------------utils-------------------------------

split=function(str, pat)
   local t = {} -- NOTE: use {n = 0} in Lua-5.0
   local fpat = "(.-)" .. pat
   local last_end = 1
   local s, e, cap = str:find(fpat, 1)
   while s do
      if s ~= 1 or cap ~= "" then
table.insert(t,cap)
      end
      last_end = e+1
      s, e, cap = str:find(fpat, last_end)
   end
   if last_end <= #str then
      cap = str:sub(last_end)
      table.insert(t, cap)
   end
   return t
end

function ipong(sock)
    sock:send('PONG 123456')
end

function iquit(sock)
    sock:send('QUIT')
    sock:close()
end




print=function(str)
  msgout:add(str)
  msgout:bottomline(msgout:size())
end
-------------------------my process------------------------
rd=socket.connect(cfg.host,cfg.port)
rd:settimeout(0.10)
rd:send("USER "..cfg.user.." orrr2.cn "..cfg.host.." :"..cfg.user.."\n")
rd:send("NICK "..cfg.nick.."\n")
if (cfg.pwd and cfg.pwd~="") then
  rd:send("PRIVMSG /msg nickserv identify "..cfg.pwd.."\n")
end
rd:send("JOIN "..cfg.chanel.."\n")
s=rd:receive('*l')
while 1 do
  if s then
  local i=string.find(s,"PING")
  if i==1 then
    print(s)
    ipong(rd)
    s=rd:receive('*l')
  end
  
  local k=string.find(s,":",2)
  if k==nil then
    k=1
  end
  local headstr=string.sub(s,1,k-2)
  print(headstr)
  local header=split(headstr," ")
  print(table.getn(header))
  local j=string.find(headstr,"PRIVMSG")
  --print(j)
  if j then
    if string.find(headstr,cfg.nick,j+7) then
        if string.sub(s,string.find(s,":",2)+1,string.len(s))=="quit" then
            iquit(rd)
        else
            print("personal msg: "..s)
        end
    else
        print("group msg: "..s)
    end
  end
  end
  s=rd:receive('*l')
  
  Fl:check()
  murgaLua.sleep(10)
end


also need a file named cfg.ini and with content
cfg={
["host"]="irc.freenode.net",
["port"]=6667,
["user"]="yman8",
["nick"]="jyf19872",
["pwd"]="",
["chanel"]="#puppylinux"
}


RE: the fltk loop and socket loop? - jyf1987 - 09-16-2008 08:08 PM

btw,do you have an IM accout,my lord? Big Grin