all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Re: Elegance in elisp, need advices
       [not found] <874r7r7v1d.fsf@noos.fr>
@ 2003-01-29 19:04 ` Stefan Monnier <foo@acm.com>
  2003-01-30  1:36   ` Kim F. Storm
  2003-02-03  0:30   ` Luke Gorrie
  0 siblings, 2 replies; 3+ messages in thread
From: Stefan Monnier <foo@acm.com> @ 2003-01-29 19:04 UTC (permalink / raw)


[ redirecting follow-ups to gnu.emacs.help ]

> For instance, the connection is set up with open-network-stream and
> set-process-filter. I keep a buffer-like string (buffer as in C not as
> in emacs), to which I concatenate every received string. When I am
[...]
> What would be the 'good' way to do that ?

Other than details of the layout of your code (more than 80 cols, docstring
on the same line as the `defun', ...) it looks OK.  I don't know of any way
to make it much better.  You could of course use a real Emacs buffer rather
than a string, so you can use `insert / delete-region' rather than
`concat / substring', but fundamentally, it can't be very different.

Admittedly, many process filters do such things, so it might be worth it
to provide a generic way to solve it, but I don't know of anybody who's
done that yet.

> Also, to decode/encode int8 int16 and int32 into mldonkey format, I
> use this kind of infamous functions :

I remember someone posting some generic code to do packing/unpacking of
binary data (maybe it was called struct.el or something like that).

>>> (defun int32-to-proto (n) (string (mod n 256) (mod (/ n 256) 256) (mod (/ n 65536) 256) (mod (/ n 16777216) 256)))

BTW, on 32bit machines, Emacs' integers only use 28bits (i.e. from -2^17
to 2^17-1).


        Stefan

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Elegance in elisp, need advices
  2003-01-29 19:04 ` Elegance in elisp, need advices Stefan Monnier <foo@acm.com>
@ 2003-01-30  1:36   ` Kim F. Storm
  2003-02-03  0:30   ` Luke Gorrie
  1 sibling, 0 replies; 3+ messages in thread
From: Kim F. Storm @ 2003-01-30  1:36 UTC (permalink / raw)


"Stefan Monnier <foo@acm.com>" <monnier+gnu.emacs.sources/news/@flint.cs.yale.edu> writes:

> 
> > Also, to decode/encode int8 int16 and int32 into mldonkey format, I
> > use this kind of infamous functions :
> 
> I remember someone posting some generic code to do packing/unpacking of
> binary data (maybe it was called struct.el or something like that).

It is now called bindat.el and is available in CVS emacs.

-- 
Kim F. Storm  http://www.cua.dk

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Elegance in elisp, need advices
  2003-01-29 19:04 ` Elegance in elisp, need advices Stefan Monnier <foo@acm.com>
  2003-01-30  1:36   ` Kim F. Storm
@ 2003-02-03  0:30   ` Luke Gorrie
  1 sibling, 0 replies; 3+ messages in thread
From: Luke Gorrie @ 2003-02-03  0:30 UTC (permalink / raw)


"Stefan Monnier <foo@acm.com>" <monnier+gnu.emacs.sources/news/@flint.cs.yale.edu> writes:

> [ redirecting follow-ups to gnu.emacs.help ]
> 
> > For instance, the connection is set up with open-network-stream and
> > set-process-filter. I keep a buffer-like string (buffer as in C not as
> > in emacs), to which I concatenate every received string. When I am
> [...]
> > What would be the 'good' way to do that ?
> 
> Other than details of the layout of your code (more than 80 cols, docstring
> on the same line as the `defun', ...) it looks OK.  I don't know of any way
> to make it much better.  You could of course use a real Emacs buffer rather
> than a string, so you can use `insert / delete-region' rather than
> `concat / substring', but fundamentally, it can't be very different.
> 
> Admittedly, many process filters do such things, so it might be worth it
> to provide a generic way to solve it, but I don't know of anybody who's
> done that yet.

I have something like this, it's a simple library for writing
network-attached state machines. It basically gives your process
buffer a "state", which is the name of a function to call when
something happens (initialization, data arrives, disconnection.) Like
a sentinel+filter, but I also accumulate data in the buffer itself
(optionally.)

The state machine also has a function to call if it terminates
successfully and one to call if it fails (explicitly or via lisp
error.) This is nice if you want to deliver the final result of your
connection to some other code.

When data arrives, the state function will typically check the buffer
to see if a whole message is there, and if not just return (to be
reinvoked when more data arrives), otherwise consume the message and
possibly change states.

It also has some debugging support, via an *fsm-debug* buffer that
logs events and state changes.

It's not properly documented, but the code is here (net-fsm.el):

http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/distel/distel/elisp/net-fsm.el?rev=1.6&content-type=text/vnd.viewcvs-markup

It was mostly written to support an implementation of the Distributed
Erlang state machine (derl.el):

http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/distel/distel/elisp/derl.el?rev=1.16&content-type=text/vnd.viewcvs-markup

A simpler machine talks to the Erlang Port Mapper Daemon (epmd.el):

http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/distel/distel/elisp/epmd.el?rev=1.1&content-type=text/vnd.viewcvs-markup

If this sounds interesting but the code is too opaque, let me know and
I'll document it when I have a chance.

BTW, Emacs buffers (rather than strings) make exquisite network
buffers I reckon, since you can interactively switch into them and
call your data-reading functions via M-: or the debugger, and see the
point advance through the input, etc. I love it a lot for debugging.

Cheers,
Luke

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2003-02-03  0:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <874r7r7v1d.fsf@noos.fr>
2003-01-29 19:04 ` Elegance in elisp, need advices Stefan Monnier <foo@acm.com>
2003-01-30  1:36   ` Kim F. Storm
2003-02-03  0:30   ` Luke Gorrie

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.