summaryrefslogtreecommitdiffstats
path: root/qtruby/rubylib/examples/network/clientserver/server/server.rb
blob: 2454115f0f28b5938fe0117665af94a182e8465e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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 < TQt::Socket
	def initialize(sock, parent=nil, name=nil)
		super( parent, name )
		@line = 0
		connect( self, TQ_SIGNAL('readyRead()'),
			TQ_SLOT('readClient()') )
		connect( self, TQ_SIGNAL('connectionClosed()'),
			TQ_SLOT('deleteLater()') )
		setSocket( sock )
    end

	signals 'logText(const TQString&)'

	slots 'readClient()'

    def readClient()
		ts = TQt::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 < TQt::ServerSocket
	def initialize( parent=nil )
		super( 4242, 1, parent )
		if !ok()
			tqWarning("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 'TQSocket*', not
	# 'ClientSocket*' as only types in the Smoke
	# library can be used for types in Signals
	signals 'newConnect(TQSocket*)'
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 < TQt::VBox
	def initialize()
		super
		@server = SimpleServer.new( self )
	
		itext = tr(
			"This is a small server example.\n" +
			"Connect with the client now."
			)
		lb = TQt::Label.new( itext, self )
		lb.setAlignment( AlignHCenter )
		@infoText = TQt::TextView.new( self )
		quit = TQt::PushButton.new( tr("Quit") , self )
	
		# See the comment above about why the 'ClientSocket*'
		# type cannot be used
		connect( @server, TQ_SIGNAL('newConnect(TQSocket*)'),
			TQ_SLOT('newConnect(TQSocket*)') )
		connect( quit, TQ_SIGNAL('clicked()'), $qApp,
			TQ_SLOT('quit()') )
    end

	slots 'newConnect(TQSocket*)', 'connectionClosed()'

    def newConnect( s )
		@infoText.append( tr("New connection\n") )
		connect( s, TQ_SIGNAL('logText(const TQString&)'),
			@infoText, TQ_SLOT('append(const TQString&)') )
		connect( s, TQ_SIGNAL('connectionClosed()'),
			TQ_SLOT('connectionClosed()') )
    end

    def connectionClosed()
		@infoText.append( tr("Client closed connection\n") )
    end
end


app = TQt::Application.new( ARGV )
info = ServerInfo.new
app.mainWidget = info
info.show
app.exec