Class: CARPS::Mailbox

Inherits:
Object
  • Object
show all
Includes:
DRbUndumped
Defined in:
lib/carps/crypt/mailbox.rb

Overview

The mailbox’s responsibility is in sending messages and securely and robustly receiving them

It has knowledge is of the public keys of the Mailer s of the remote peers

Instance Method Summary collapse

Constructor Details

#initialize(sender, receiver, parser, manager) ⇒ Mailbox

Create the mailbox from a simple, synchronous mail sender and receiver.

The third parameter is a MessageParser.

The fourth parameter is a SessionManager.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/carps/crypt/mailbox.rb', line 41

def initialize sender, receiver, parser, manager
   @manager = manager 
   @receiver = receiver
   @parser = parser
   @sender = sender
   @mail = []
   @peers = {}
   # Semaphore to make sure only one thread can send mail at any one time
   @ssemaphore = Mutex.new
   # Semaphore to make sure only one thread can receive mail at any one time 
   @rsemaphore = Mutex.new
   # Load mails from the last session
   load_old_mails
   # Receive mail
   receive_forever
end

Instance Method Details

#add_peer(peer) ⇒ Object

Add a new peer



64
65
66
# File 'lib/carps/crypt/mailbox.rb', line 64

def add_peer peer
   @peers[peer.addr] = peer
end

#check(type, must_be_from = nil) ⇒ Object

Check for a new message. Don’t block



98
99
100
# File 'lib/carps/crypt/mailbox.rb', line 98

def check type, must_be_from=nil
   search type, must_be_from
end

#insecure_check(type, must_be_from = nil) ⇒ Object

Check for new messages insecurely. Don’t block.



115
116
117
# File 'lib/carps/crypt/mailbox.rb', line 115

def insecure_check type, must_be_from=nil
   insecure_search type, must_be_from
end

#insecure_read(type, must_be_from = nil) ⇒ Object

Insecurely read a message. Block until one comes.



103
104
105
106
107
108
109
110
111
112
# File 'lib/carps/crypt/mailbox.rb', line 103

def insecure_read type, must_be_from=nil
   msg = nil
   until msg
      msg = insecure_search type, must_be_from
      sleep 1
   end
   # Ding!
   puts "\a"
   return msg
end

#peer?(peer) ⇒ Boolean

Is this already a peer?

Returns:

  • (Boolean)


69
70
71
# File 'lib/carps/crypt/mailbox.rb', line 69

def peer? peer
   @peers.member? peer
end

#read(type, must_be_from = nil) ⇒ Object

Securely read a message. Block until one occurs.



86
87
88
89
90
91
92
93
94
95
# File 'lib/carps/crypt/mailbox.rb', line 86

def read type, must_be_from=nil
   msg = nil
   until msg
      msg = search type, must_be_from
      sleep 1
   end
   # Ding!
   puts "\a"
   return msg
end

#send(to, message) ⇒ Object

Send a message



74
75
76
77
78
# File 'lib/carps/crypt/mailbox.rb', line 74

def send to, message
   @ssemaphore.synchronize do
      @sender.send to, message
   end
end

#shutdownObject

Shutdown the mailbox



59
60
61
# File 'lib/carps/crypt/mailbox.rb', line 59

def shutdown
   @child.kill
end

#tag(text) ⇒ Object

Tag some text



81
82
83
# File 'lib/carps/crypt/mailbox.rb', line 81

def tag text
   @manager.tag text
end