reassignement TCPSocket::new

in my model, I have:

attr_accessor :socket

in my controller I have

def toServer(s)     begin       if(@socket == nil)         @socket = TCPSocket::new(@channel.serverip, @channel.port)       end      @socket.send(s,0)     rescue       return     end   end

I have a dropdown list in the view that lets the user select a different channel, thus, the socket is created in the view based on the ip and port of the selected channel.

Usually, however, the channel info will remain the same. So I am wondering how I can make the socket persist from one POST/GET operation to the other, unless the channel has changed.

And my second question is, if I someone does tell me how I can make it persist, do I run the risk of some sort of memory leaks here with this? I wonder because I have a solitary TCPSocket object which would then be reassigned -- does the un-assigned old reference that socket contained go automatically out of scope and get garbage collected? Thanks, Janna B repeatedly in my controller, naturally,

Usually, however, the channel info will remain the same. So I am wondering how I can make the socket persist from one POST/GET operation to the other, unless the channel has changed.

And my second question is, if I someone does tell me how I can make it persist, do I run the risk of some sort of memory leaks here with this? I wonder because I have a solitary TCPSocket object which would then be reassigned -- does the un-assigned old reference that socket contained go automatically out of scope and get garbage collected? Thanks, Janna B

If you reassign a variable then its old contents will eventually be garbage collected (assuming no other references to it). You are likely to run into other problems though here: have you thought about concurrent users using the app at the same time, or what happens when you have multiple mongrels / passenger instances ?

Lastly it looks like @socket is an instance variable of your controller, since every request is served by a new controller instance @socket will always be nil.

Fred

Thanks Fred,

Yes, multiple users are not a problem as part of what is passed through the socket is the reference to the user. I have numerous channels that can be run, however, each with a different port assignement, by various users. Thus, it would seem I want to create a hash of

channels => sockets

but how can I make that be persistent?

-Janna B

Then would the following be a problem in a controller if concurrent users use the app at the same time ?

      socket = TCPSocket::new(@channel.serverip, @channel.port)       socket.send(s+"\n",0)       socket.close

(multiple servers I am not concerned about -- one glassfish gem instance roars with all the horsepower this particular app ever needs -- but what WOULD happen if I ran this under multiple mongrel instances? Can't the same port be shared by multiple instances?)

The various instances would just be completely unaware that another instance already had a socket.

if you had multiple instances inside a single glassfish process and if you were storing socket in somewhere that persisted across requests then it could potentially go wrong. No one's going to be very happy if two different threads try to write to the same socket at the same time.

Fred