journeta 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History.txt CHANGED
@@ -1,5 +1,14 @@
1
+ == 0.0.4 2008-08-22
2
+
3
+ * Graphical instant messenger example.
4
+ * examples/instant_messenger_gui.rb (requires the wxruby gem)
5
+ * Will talk to the command line example seemlessly.
6
+ * Linux bug fixes.
7
+ * Ruby 1.8.6 support
8
+
1
9
  == 0.0.3 2008-08-21
2
10
 
11
+ * Linux support.
3
12
  * Added peer 'groups' support to avoid peer message spamming.
4
13
  * Major refactoring to make outbound messages sent asynchonously to the sending thread.
5
14
  * Example and documentation updates.
data/Manifest.txt CHANGED
@@ -5,6 +5,7 @@ README.txt
5
5
  Rakefile
6
6
  lib/journeta.rb
7
7
  examples/instant_messenger.rb
8
+ examples/instant_messenger_gui.rb
8
9
  examples/queue_client.rb
9
10
  examples/queue_server.rb
10
11
  lib/journeta/logger.rb
@@ -21,6 +22,7 @@ lib/journeta/journeta_engine.rb
21
22
  lib/journeta/common/dummy_peer_handler.rb
22
23
  lib/journeta/common/job.rb
23
24
  lib/journeta/common/shutdown.rb
25
+ lib/journeta/common/basic_message.rb
24
26
  scripts/txt2html
25
27
  setup.rb
26
28
  test/test_journeta.rb
@@ -6,20 +6,13 @@ $LOAD_PATH.unshift lib_path
6
6
  # Load up the library!
7
7
  require 'journeta'
8
8
  include Journeta
9
-
10
-
11
- # Any arbitrary object can be sent to peers as long as it's serializable to YAML.
12
- # We'll create an ordinary class with a couple typical-looking fields to send to our peers.
13
- class ExampleMessage
14
- attr_accessor :name
15
- attr_accessor :text
16
- end
9
+ include Journeta::Common
17
10
 
18
11
  # A message handler will be called by the engine every time a message is received.
19
12
  # This code will be customized for your application-specific needs.
20
13
  class ExampleHandler < Journeta::DefaultPeerHandler
21
14
  def handle(message)
22
- if message.class == ExampleMessage
15
+ if message.class == BasicMessage
23
16
  puts "#{message.name.chop}: #{message.text}"
24
17
  else
25
18
  putsd("Unsupported message type received from peer. (#{message})")
@@ -79,7 +72,7 @@ begin
79
72
  loop do
80
73
  begin
81
74
  input = gets
82
- m = ExampleMessage.new
75
+ m = BasicMessage.new
83
76
  m.name = name
84
77
  m.text = input
85
78
  journeta.send_to_known_peers(m)
@@ -0,0 +1,166 @@
1
+ #!/usr/bin/env ruby
2
+ current_dir = File.dirname(File.expand_path(__FILE__))
3
+ lib_path = File.join(current_dir, '..', 'lib')
4
+ $LOAD_PATH.unshift lib_path
5
+
6
+ puts 'A GUI instant messenger demo which can also talk to the command-line version! (Requires the "wxruby" gem.)'
7
+ # Load up the library!
8
+ require 'journeta'
9
+ include Journeta
10
+ include Journeta::Common
11
+
12
+ require 'rubygems'
13
+
14
+
15
+ # Use UTF-8.
16
+ $KCODE = 'u'
17
+
18
+ # Library to get character lengths (as opposed to byte lengths) from Ruby strings.
19
+ require 'jcode'
20
+
21
+ # Load the wxruby GUI library.
22
+ require 'wx'
23
+
24
+ class JournetaGUIHandler
25
+
26
+ def initialize(control)
27
+ @control = control
28
+ end
29
+
30
+ def handle(message)
31
+ if message.class == BasicMessage
32
+ text = @control.get_value
33
+ text.chop!
34
+ text += "\n#{message.name.chop}: #{message.text}"
35
+ @control.set_value text
36
+ else
37
+ putsd("Unsupported message type received from peer. (#{message})")
38
+ end
39
+ end
40
+ end
41
+
42
+
43
+ class PeerTextControl < Wx::TextCtrl
44
+
45
+ DEFAULT_TEXT = "(joined chat at #{Time.now})\n\n"
46
+
47
+ def initialize(parent, text = DEFAULT_TEXT)
48
+ super(parent, -1, text,
49
+ Wx::DEFAULT_POSITION, Wx::DEFAULT_SIZE, Wx::TE_MULTILINE)
50
+ end
51
+
52
+ end
53
+
54
+ # A read-only text ctrl useful for displaying output
55
+ class MessageTextControl < Wx::TextCtrl
56
+ STYLE = Wx::TE_READONLY|Wx::TE_MULTILINE
57
+ def initialize(parent)
58
+ super(parent, -1, '', Wx::DEFAULT_POSITION, Wx::DEFAULT_SIZE, STYLE)
59
+ end
60
+ end
61
+
62
+ class IConvFrame < Wx::Frame
63
+
64
+ @name = 'anonymous'
65
+
66
+ # Ruby stdlib for converting strings between encodings
67
+ begin
68
+ require 'iconv'
69
+ ICONV_LOADED = 1
70
+ rescue LoadError
71
+ end
72
+
73
+
74
+
75
+ def initialize(title, pos, size)
76
+ super(nil, -1, title, pos, size)
77
+ panel = Wx::Panel.new(self)
78
+
79
+ # TODO Ask user for name.
80
+ dialog = Wx::TextEntryDialog.new(self, "What's your name?",
81
+ "Please enter text",
82
+ "anonymous",
83
+ Wx::OK ,
84
+ Wx::DEFAULT_POSITION)
85
+ dialog.show_modal
86
+ @name = dialog.get_value
87
+
88
+ sizer = Wx::BoxSizer.new(Wx::VERTICAL)
89
+
90
+ # The text input and display
91
+ @peer_control = PeerTextControl.new(panel)
92
+ sizer.add(@peer_control, 3, Wx::GROW|Wx::ALL, 2)
93
+ @peer_control.set_editable(false)
94
+ # TODO set background color so it's obviously read-only.
95
+ # @peer_control.set_background_colour('888888')
96
+
97
+ @message_control = MessageTextControl.new(panel)
98
+ @message_control.set_editable(true)
99
+ sizer.add(@message_control, 1, Wx::GROW|Wx::ALL, 2)
100
+
101
+ # The button to show what's selected
102
+ button = Wx::Button.new(panel, -1, 'Send')
103
+ sizer.add(button, 0, Wx::ADJUST_MINSIZE|Wx::ALL, 2 )
104
+ evt_button(button.get_id) { | e | on_click(e) }
105
+
106
+ panel.set_sizer_and_fit( sizer )
107
+
108
+ peer_port = (2048 + rand( 2 ** 8))
109
+ @journeta = Journeta::JournetaEngine.new(:peer_port => peer_port, :peer_handler => JournetaGUIHandler.new(@peer_control), :groups => ['im_example'])
110
+ @journeta.start
111
+ end
112
+
113
+
114
+ def construct_menus()
115
+ menu_bar = Wx::MenuBar.new()
116
+
117
+ menu_file = Wx::Menu.new()
118
+ menu_file.append(Wx::ID_EXIT, "E&xit\tAlt-X", "Quit this program")
119
+ evt_menu(Wx::ID_EXIT) { on_quit() }
120
+ menu_bar.append(menu_file, "&File")
121
+ if self.class.const_defined?(:ICONV_LOADED)
122
+ construct_import_export_menus(menu_bar)
123
+ end
124
+
125
+ menu_help = Wx::Menu.new()
126
+ menu_help.append(Wx::ID_ABOUT, "&About...\tF1", "Show about dialog")
127
+ evt_menu(Wx::ID_ABOUT) { on_about() }
128
+ menu_bar.append(menu_help, "&Help")
129
+
130
+ set_menu_bar(menu_bar)
131
+ end
132
+
133
+ def on_click(e)
134
+ text = @message_control.get_value
135
+ msg = BasicMessage.new
136
+ msg.name = @name
137
+ msg.text = text
138
+ @journeta.send_to_known_peers(msg)
139
+ @message_control.set_value('')
140
+ @peer_control.set_value "#{@peer_control.get_value}\n#{@name}: #{text}"
141
+ end
142
+
143
+ def on_quit()
144
+ @journeta.stop
145
+ close(TRUE)
146
+ end
147
+
148
+ def on_about()
149
+ msg = "A GUI demo. Wx version #{Wx::VERSION_STRING}"
150
+ Wx::message_box(msg, "About Minimal", Wx::OK|Wx::ICON_INFORMATION, self)
151
+ end
152
+ end
153
+
154
+
155
+ class InstantMessengerApplication < Wx::App
156
+
157
+ def on_init()
158
+ frame = IConvFrame.new("Journeta Instant Messenger",
159
+ Wx::Point.new(50, 50),
160
+ Wx::Size.new(450, 450) )
161
+
162
+ frame.show(true)
163
+ end
164
+ end
165
+
166
+ InstantMessengerApplication.new().main_loop()
data/lib/journeta.rb CHANGED
@@ -16,6 +16,7 @@ require 'journeta/peer_registry'
16
16
  require 'journeta/peer_connection'
17
17
  require 'journeta/journeta_engine'
18
18
 
19
+ require 'journeta/common/basic_message'
19
20
  require 'journeta/common/job'
20
21
  require 'journeta/common/shutdown'
21
22
  require 'journeta/common/dummy_peer_handler'
@@ -0,0 +1,12 @@
1
+ module Journeta
2
+
3
+ module Common
4
+
5
+ class BasicMessage
6
+ attr_accessor :name
7
+ attr_accessor :text
8
+ end
9
+
10
+ end
11
+
12
+ end
@@ -68,7 +68,7 @@ module Journeta
68
68
  @mutex.synchronize do
69
69
  # Grab all peers in relevant groups.
70
70
  group = all_do
71
- n= group.count
71
+ n= group.length
72
72
  if n > 0
73
73
  putsd "Sending payload to #{n} peers."
74
74
  group.each do |uuid, conn|
@@ -18,8 +18,13 @@ module Journeta
18
18
  socket = UDPSocket.new
19
19
  # Remember how i said this was fucked up? yeaahhhhhh. i hope you like C.
20
20
  # `man setsockopt` for details.
21
- # SO_REUSEPORT is needed so multiple peers can be run on the same machine.
22
- socket.setsockopt( Socket::SOL_SOCKET, Socket::SO_REUSEPORT, [1].pack("i_") )
21
+ if PLATFORM[/linux/i]
22
+ socket.setsockopt( Socket::SOL_SOCKET, Socket::SO_REUSEADDR, [1].pack("i_") )
23
+ else
24
+ # SO_REUSEPORT is needed so multiple peers can be run on the same machine.
25
+ # socket.setsockopt(Socket::IPPROTO_IP, Socket::IP_TTL, [1].pack('i')) # Preston's original config for OS X.
26
+ socket.setsockopt( Socket::SOL_SOCKET, Socket::SO_REUSEPORT, [1].pack("i_") ) # Remi's suggested default.
27
+ end
23
28
  # socket.setsockopt( Socket::SOL_SOCKET, Socket::SO_REUSEADDR, [1].pack("i_") )
24
29
  socket.setsockopt(Socket::IPPROTO_IP, Socket::IP_ADD_MEMBERSHIP, addresses)
25
30
  socket.bind(Socket::INADDR_ANY, port)
@@ -2,7 +2,7 @@ module Journeta #:nodoc:
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 0
5
- TINY = 3
5
+ TINY = 4
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: journeta
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Preston Lee
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-08-21 00:00:00 -07:00
12
+ date: 2008-08-22 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -42,6 +42,7 @@ files:
42
42
  - Rakefile
43
43
  - lib/journeta.rb
44
44
  - examples/instant_messenger.rb
45
+ - examples/instant_messenger_gui.rb
45
46
  - examples/queue_client.rb
46
47
  - examples/queue_server.rb
47
48
  - lib/journeta/logger.rb
@@ -58,6 +59,7 @@ files:
58
59
  - lib/journeta/common/dummy_peer_handler.rb
59
60
  - lib/journeta/common/job.rb
60
61
  - lib/journeta/common/shutdown.rb
62
+ - lib/journeta/common/basic_message.rb
61
63
  - scripts/txt2html
62
64
  - setup.rb
63
65
  - test/test_journeta.rb