summaryrefslogtreecommitdiffstats
path: root/qtruby/rubylib/examples/network/clientserver
diff options
context:
space:
mode:
Diffstat (limited to 'qtruby/rubylib/examples/network/clientserver')
-rw-r--r--qtruby/rubylib/examples/network/clientserver/client/client.rb88
-rw-r--r--qtruby/rubylib/examples/network/clientserver/server/server.rb115
2 files changed, 203 insertions, 0 deletions
diff --git a/qtruby/rubylib/examples/network/clientserver/client/client.rb b/qtruby/rubylib/examples/network/clientserver/client/client.rb
new file mode 100644
index 00000000..1f16b0ca
--- /dev/null
+++ b/qtruby/rubylib/examples/network/clientserver/client/client.rb
@@ -0,0 +1,88 @@
+require 'Qt'
+
+class Client < Qt::VBox
+
+ def initialize( host, port )
+ super()
+ # GUI layout
+ @infoText = Qt::TextView.new( self )
+ hb = Qt::HBox.new( self )
+ @inputText = Qt::LineEdit.new( hb )
+ send = Qt::PushButton.new( tr("Send") , hb )
+ close = Qt::PushButton.new( tr("Close connection") , self )
+ quit = Qt::PushButton.new( tr("Quit") , self )
+
+ connect( send, SIGNAL('clicked()'), SLOT('sendToServer()') )
+ connect( close, SIGNAL('clicked()'), SLOT('closeConnection()') )
+ connect( quit, SIGNAL('clicked()'), $qApp, SLOT('quit()') )
+
+ # create the socket and connect various of its signals
+ @socket = Qt::Socket.new( self )
+ connect( @socket, SIGNAL('connected()'),
+ SLOT('socketConnected()') )
+ connect( @socket, SIGNAL('connectionClosed()'),
+ SLOT('socketConnectionClosed()') )
+ connect( @socket, SIGNAL('readyRead()'),
+ SLOT('socketReadyRead()') )
+ connect( @socket, SIGNAL('error(int)'),
+ SLOT('socketError(int)') )
+
+ # connect to the server
+ @infoText.append( tr("Trying to connect to the server\n") )
+ @socket.connectToHost( host, port )
+ end
+
+ slots 'closeConnection()', 'sendToServer()',
+ 'socketReadyRead()', 'socketConnected()',
+ 'socketConnectionClosed()', 'socketClosed()',
+ 'socketError(int)'
+
+ def closeConnection()
+ @socket.close()
+ if @socket.state() == Qt::Socket::Closing
+ # We have a delayed close.
+ connect( @socket, SIGNAL('delayedCloseFinished()'),
+ SLOT('socketClosed()') )
+ else
+ # The socket is closed.
+ socketClosed()
+ end
+ end
+
+ def sendToServer()
+ # write to the server
+ os = Qt::TextStream.new(@socket)
+ os << @inputText.text() << "\n"
+ @inputText.setText( "" )
+ os.dispose()
+ end
+
+ def socketReadyRead()
+ # read from the server
+ while @socket.canReadLine() do
+ @infoText.append( @socket.readLine() )
+ end
+ end
+
+ def socketConnected()
+ @infoText.append( tr("Connected to server\n") )
+ end
+
+ def socketConnectionClosed()
+ @infoText.append( tr("Connection closed by the server\n") )
+ end
+
+ def socketClosed()
+ @infoText.append( tr("Connection closed\n") )
+ end
+
+ def socketError( e )
+ @infoText.append( tr("Error number %d occurred\n" % e) )
+ end
+end
+
+app = Qt::Application.new( ARGV )
+client = Client.new( ARGV.length < 1 ? "localhost" : ARGV[0], 4242 )
+app.mainWidget = client
+client.show
+app.exec
diff --git a/qtruby/rubylib/examples/network/clientserver/server/server.rb b/qtruby/rubylib/examples/network/clientserver/server/server.rb
new file mode 100644
index 00000000..d8a937f4
--- /dev/null
+++ b/qtruby/rubylib/examples/network/clientserver/server/server.rb
@@ -0,0 +1,115 @@
+require 'Qt'
+
+=begin
+ The ClientSocket class provides a socket that is connected with a client.
+ For every client that connects to the server, the server creates a new
+ instance of this class.
+=end
+class ClientSocket < Qt::Socket
+ def initialize(sock, parent=nil, name=nil)
+ super( parent, name )
+ @line = 0
+ connect( self, SIGNAL('readyRead()'),
+ SLOT('readClient()') )
+ connect( self, SIGNAL('connectionClosed()'),
+ SLOT('deleteLater()') )
+ setSocket( sock )
+ end
+
+ signals 'logText(const QString&)'
+
+ slots 'readClient()'
+
+ def readClient()
+ ts = Qt::TextStream.new( self )
+ while canReadLine() do
+ str = ts.readLine()
+ emit logText( tr("Read: '%s'\n" % str) )
+
+ ts << @line << ": " << str
+ # 'endl' needs to be called like this in ruby
+ endl(ts)
+ emit logText( tr("Wrote: '%d: %s'\n" % [@line, str]) )
+
+ @line += 1
+ end
+ ts.dispose()
+ end
+end
+
+
+=begin
+ The SimpleServer class handles new connections to the server. For every
+ client that connects, it creates a new ClientSocket -- that instance is now
+ responsible for the communication with that client.
+=end
+class SimpleServer < Qt::ServerSocket
+ def initialize( parent=nil )
+ super( 4242, 1, parent )
+ if !ok()
+ qWarning("Failed to bind to port 4242")
+ exit(1)
+ end
+ end
+
+ def newConnection( socket )
+ s = ClientSocket.new( socket, self )
+ emit newConnect( s )
+ end
+
+ # The type of the argument is 'QSocket*', not
+ # 'ClientSocket*' as only types in the Smoke
+ # library can be used for types in Signals
+ signals 'newConnect(QSocket*)'
+end
+
+
+=begin
+ The ServerInfo class provides a small GUI for the server. It also creates the
+ SimpleServer and as a result the server.
+=end
+class ServerInfo < Qt::VBox
+ def initialize()
+ super
+ @server = SimpleServer.new( self )
+
+ itext = tr(
+ "This is a small server example.\n" +
+ "Connect with the client now."
+ )
+ lb = Qt::Label.new( itext, self )
+ lb.setAlignment( AlignHCenter )
+ @infoText = Qt::TextView.new( self )
+ quit = Qt::PushButton.new( tr("Quit") , self )
+
+ # See the comment above about why the 'ClientSocket*'
+ # type cannot be used
+ connect( @server, SIGNAL('newConnect(QSocket*)'),
+ SLOT('newConnect(QSocket*)') )
+ connect( quit, SIGNAL('clicked()'), $qApp,
+ SLOT('quit()') )
+ end
+
+ slots 'newConnect(QSocket*)', 'connectionClosed()'
+
+ def newConnect( s )
+ @infoText.append( tr("New connection\n") )
+ connect( s, SIGNAL('logText(const QString&)'),
+ @infoText, SLOT('append(const QString&)') )
+ connect( s, SIGNAL('connectionClosed()'),
+ SLOT('connectionClosed()') )
+ end
+
+ def connectionClosed()
+ @infoText.append( tr("Client closed connection\n") )
+ end
+end
+
+
+app = Qt::Application.new( ARGV )
+info = ServerInfo.new
+app.mainWidget = info
+info.show
+app.exec
+
+