CoCreate User Forum  

Go Back   CoCreate User Forum > Support > Customization

Reply
 
Thread Tools Search this Thread Rating: Thread Rating: 3 votes, 3.67 average. Display Modes
  #1  
Old 01-28-2003, 11:26 PM
fabrizio fabrizio is offline
Registered User
 
Join Date: Jan 2003
Location: Parma - Italy
Posts: 34
Implementing socket communication in Lisp

Hi all,
I have a question about Lisp programming in OSDM: I need to implement a socket connection from OSDM to another application using Lisp. Is there a way to do this? I don't think that any socket functionality exists in standard Lisp, but maybe there is another way to achieve the same result. Maybe loading some common lisp libary into OSDM? Do you think I have a chanche? Any ideas?
Any suggestion would be precious...

Thanks in advance.
Fabrizio
Reply With Quote
  #2  
Old 01-29-2003, 05:35 AM
fabrizio fabrizio is offline
Registered User
 
Join Date: Jan 2003
Location: Parma - Italy
Posts: 34
an idea

Hi all.
After a brief serach, it seems there's no way to implement a direct socket communication in a Lisp program... I haven't found any documentation about the existence of any socket functionality in Lisp.
So, first of all, thanx to "someone" who suggested me to use a named pipe. According to this idea, the lisp code would be able to communicate to the named pipe as a client and the pipe server could be, for instance, a C program that would create the real socket connection to the other application and would work just as a repeater... everything read from the pipe would be redirected to the socket and viceversa. But, since I am not very expert about named pipes (and I don't know how I could connect to them in Lisp) I thought to another idea... which is only a modification of the one I was suggested:
I thought I could write a program (Java program in the specific)that starts OSDM as a child program and then connects to its stdin and stdout to communicate with it. So I could have Lisp code writing to *standard-output* and Java code reading from that stream and viceversa. Then the Java program would create a socket connection (maybe RMI...) to the other Java app.
But I discovered a strange issue. When Lisp code writes to *standard-output* the Java program reads the messages correctly. But when I try to read from *standard-input* in the Lisp program, two problems occur:
1- All OSD commands are in some way put in the *standard-input* before being executed. And if I perform a (read-line) on that stream i get every command as a string, and then such command is no more executed. It seems that some "command processor" in OSD reads the commands to execute from stdin and that when I perform a (read-line) I actually remove those commands from the stream thus avoiding such commands to be executed
2- If the java program writes strings to the standard input of the OSD instance it has launched, such strings are not available from the *standard-input* stream in my Lisp code. It seems that OSD standard input and *standard-input* were 2 different streams.

Can anyone explain me the reasons of those 2 problems?
Of course, any other idea/suggestion is welcome!!

Thanx (especially for the time it took to read this LONG post :-(
Fabrizio
Reply With Quote
  #3  
Old 01-29-2003, 08:19 AM
clausb's Avatar
clausb clausb is offline
Registered User
 
Join Date: Nov 2002
Posts: 1,168
Re: an idea

Fabrizio,

I think you're moving into dangerous territory here. After all, the redirected stdin and stdout handles might be passed on to processes started from OSDM, such as the external helper programs to pack and unpack package files, and I'm not sure what the impact of this might be. And as you already found out, there is more between stdin and stdout than is dreamt of in your philosophy

I'd really recommend to at least try the named pipes. From LISP code, named pipes can be used like a normal file; unlike normal pipes, they have a filename and a path. The only slightly unusual thing with named pipes is how to create them. On an HP-UX system, you would probably use the mknod() or mkfifo() system calls. On Windows, try CreateNamedPipe(). (The MSDN library has example code, see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ipc/base/createnamedpipe.asp.)

Claus
Reply With Quote
  #4  
Old 01-30-2003, 12:08 AM
fabrizio fabrizio is offline
Registered User
 
Join Date: Jan 2003
Location: Parma - Italy
Posts: 34
named pipes

Claus,
I will follow what you recommend. After all, you are the expert....
I am already working on a C program that creates a full duplex named pipe. I was looking for another solution because I've read that named pipes are much slower than sockets... but it looks there's no other solution.
There's still an issue in my mind, even if I haven't dealed with it yet: to listen to the named pipe from lisp code, I should write a function and subscribe it to some OSDM event which is processed frequently. But, thinking to this model, I don't think there's any IKit event which is processed when OSDM is doing NOTHING. So I guess I won't be able to write a request on the socket from my program and have OSDM noticing it if the user is not interacting with OSDM itself... but I hope I am making a wrong assumption.

When I have some code running, I will let you know.
Thank you.
Fabrizio
Reply With Quote
  #5  
Old 01-30-2003, 05:12 AM
fabrizio fabrizio is offline
Registered User
 
Join Date: Jan 2003
Location: Parma - Italy
Posts: 34
tried named pipes

So, at the end, I tried with named pipes. On Win2K platform, I created a C console program (Redirector.exe), which creates a named pipe using CreateNamedPipe() and then starts listening to it.
Whenever any other program connects to the pipe, Redirector creates a new thread which serves that particular client. The new thread start listening to the new instance of the pipe using ReadFile(), waiting for the client writes something on it. Once the client writes something, the C program writes in on stdout and then tries to "calculate" an answer based on the request and writes it to the pipe, so the client should be able to read it.
I have then written some Lisp test code to open a connection to the pipe, write smthg on it, read from it and close the pipe itself.
Well, testing it all toghether the result is that:
1- When redirector.exe is running, I am able to connect to its pipe from my lisp code.
2- When I try to write to the pipe from the lisp code, redirector is able to read the request and prints it out (there's a problem on the termination of the string read from the server, I guess it's due to the different terminator char between C and LISP )
3- After that the C program writes the answer to the pipe, and everything seems to work fine here.
4- But then it seems to be impossible to read the answer from the pipe in my LISP code: the (read-line) function always finds an eof at the beginning of the pipe
5- At the end, the pipe is closed correctly from my lisp code.

This is all what I was able to do... I really can't understand why no line is read from the pipe in the lisp code... Bah!

Anyway, I'll attach here my Lisp and C source files: if anyone of you will ever need to work with named pipes in Lisp(quite improbable), you could give them a look...

Thanx.
Fabrizio
Attached Files
File Type: zip namedpipe-connection.zip (2.2 KB, 492 views)
Reply With Quote
  #6  
Old 01-30-2003, 09:10 AM
John Scheffel's Avatar
John Scheffel John Scheffel is offline
Administrator
 
Join Date: Sep 2002
Location: San Jose, CA
Posts: 1,288
While most of this is over my head, I would like to thank Fabrizio for posting his experiences and code. I know how hard it is to take time to document what you have done for the benefit others. The only reward for doing this is the thanks of the people you help. This is one example of the type of info I would like to see people posting here so that many people might benefit. An of course thanks to Claus and all the other people who take time to answer people's questions.
__________________
John Scheffel
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 05:29 PM.



Hosted by SureServer    Forums   Modeling FAQ   Macro Site   Vendor/Contractors   Software Resellers   CoCreate   Gallery   Home   Board Members   Regional User Groups  By-Laws  

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
You Rated this Thread: