@@ -4,44 +4,39 @@ class InstantMessengerApplication usingPlatform: platform = Value (
44 private TransferArray = platform kernel TransferArray.
55 private Dictionary = platform collections Dictionary.
66 private TransferObject = platform kernel TransferObject.
7- |
8- )(
7+ |)(
98
10- public class InstantMessenger new: name total: size = (
11- | private buddyMap :: = Dictionary new: size.
9+ public class InstantMessenger new: name total: size = (
10+ | private buddyMap = Dictionary new: size.
1211 private name = name.
1312 private textMessage ::= nil.
14- |
15- )(
13+ |)(
1614
1715 public startChat: remoteMessenger = (
1816 | pDiscover pName pSend msg pp array |
1917
20- pp:: actors createPromisePair.
18+ pp:: actors createPromisePair.
2119
22- pDiscover:: self addMessenger: remoteMessenger. (* returns a far reference of the remote messenger *)
20+ (* returns a far reference of the remote messenger *)
21+ pDiscover:: self addMessenger: remoteMessenger.
2322
24- pName:: pDiscover <-: getName.
25- pName whenResolved: [:remoteName |
26- msg:: ( 'Hello ' + remoteName) .
27- pSend:: sendMessage: remoteName contentMsg: msg.
23+ pName:: pDiscover <-: getName.
24+ pName whenResolved: [:remoteName |
25+ msg:: 'Hello ' + remoteName.
26+ pSend:: sendMessage: remoteName contentMsg: msg.
2827
29- pSend whenResolved:[:result |
30- (result = 'ok')
31- ifTrue:[
32- array:: TransferArray new: 2. (* array with the msg to display in the sender actor screen *)
33- array at: 1 put: name.
34- array at: 2 put: msg.
35- pp resolver resolve: array.
36- ]
37- ifFalse:[
38- pp resolver resolve: nil.
39- ]
40- ].
41- ].
28+ pSend whenResolved: [:result |
29+ result = 'ok'
30+ ifTrue: [
31+ (* array with the msg to display in the sender actor screen *)
32+ array:: TransferArray new: 2.
33+ array at: 1 put: name.
34+ array at: 2 put: msg.
35+ pp resolver resolve: array ]
36+ ifFalse:[
37+ pp resolver resolve: nil ] ] ].
4238
43-
44- ^ pp promise
39+ ^ pp promise
4540 )
4641
4742 public getName = (
@@ -65,26 +60,23 @@ public class InstantMessenger new: name total: size = (
6560
6661 public addMessenger: messenger = (
6762 | p name pName buddy |
68-
69- p:: actors createPromisePair.
70-
71- pName:: messenger <-: getName.
72- pName whenResolved:[:n |
73- (buddyMap containsKey: n)
74- ifTrue:[
75- buddyMap at: n put: messenger. (* update el far reference *)
76- (getName + ' updated the far reference to buddy: '+ n) println.
77- ]
78- ifFalse:[
79- buddyMap at: n put: messenger.
80- (getName + ' discovered a new messenger buddy: '+ n) println.
81- ].
82- p resolver resolve: (buddyMap at: n).
83-
84- ] onError:[:e|
85- ('-Error adding the messenger: ' + e) println.
86- p resolver resolve: nil.
87- ].
63+ p:: actors createPromisePair.
64+
65+ pName:: messenger <-: getName.
66+ pName whenResolved: [:n |
67+ (buddyMap containsKey: n)
68+ ifTrue: [
69+ buddyMap at: n put: messenger. (* update el far reference *)
70+ (getName + ' updated the far reference to buddy: '+ n) println. ]
71+ ifFalse: [
72+ buddyMap at: n put: messenger.
73+ (getName + ' discovered a new messenger buddy: '+ n) println ].
74+
75+ p resolver resolve: (buddyMap at: n).
76+ ] onError: [:e |
77+ ('-Error adding the messenger: ' + e) println.
78+ p resolver resolve: nil.
79+ ].
8880
8981 ^ p promise
9082 )
@@ -96,70 +88,57 @@ public class InstantMessenger new: name total: size = (
9688 sender:: textMessage getSender.
9789
9890 (* print message to the screen *)
99- self displayMessage: sender +': '+ content.
91+ self displayMessage: sender + ': ' + content.
10092
10193 (* acknowledgement notification of message received *)
10294 ^ 'ok'.
10395 )
104- )
96+ )
10597
106- class TextMessage new: content sender: senderName = TransferObject (
98+ class TextMessage new: content sender: senderName = TransferObject (
10799 | private msg = content.
108100 private sender = senderName.
109- |
110- )(
111-
112- public getContent = (
113- ^ msg.
114- )
101+ |)(
102+ public getContent = (
103+ ^ msg.
104+ )
115105
116- public getSender = (
117- ^ sender
106+ public getSender = (
107+ ^ sender
108+ )
118109 )
119110
120- )
121-
122111 public main: args = (
123- | completionPP1 completionPP2 users messenger1 messenger2 pResult1 pResult2 |
124-
125- completionPP1:: actors createPromisePair.
126-
127- completionPP2:: actors createPromisePair.
128-
129- '[INSTANT MESSENGER APPLICATION] Starting' println.
112+ | completionPP1 completionPP2 users messenger1 messenger2 pResult1 pResult2 |
130113
131- users:: Array new: 2.
132- users at: 1 put: 'Joe'.
133- users at: 2 put: 'Marie'.
114+ completionPP1:: actors createPromisePair.
115+ completionPP2:: actors createPromisePair.
134116
135- messenger1:: (actors createActorFromValue: InstantMessenger) <-: new: (users at: 1) total: users size.
136- messenger2:: (actors createActorFromValue: InstantMessenger) <-: new: (users at: 2) total: users size.
117+ '[INSTANT MESSENGER APPLICATION] Starting' println.
137118
138- pResult1:: messenger1 <-: startChat: messenger2.
139- pResult1 whenResolved:[:result |
140- result
141- ifNotNil:[
142- messenger1 <-: displayMessage: (result at: 1) +': '+ (result at: 2).
143- completionPP1 resolver resolve: 0. (* end application *)
144- ]
145- ].
119+ users:: Array new: 2.
120+ users at: 1 put: 'Joe'.
121+ users at: 2 put: 'Marie'.
146122
147- pResult2:: messenger2 <-: startChat: messenger1.
148- pResult2 whenResolved:[: result|
149- result
150- ifNotNil:[
151- messenger2 <-: displayMessage: (result at: 1) +': '+ (result at: 2).
152- completionPP2 resolver resolve: 0. (* end application *)
153- ]
154- ].
123+ messenger1:: (actors createActorFromValue: InstantMessenger) <-: new: (users at: 1) total: users size.
124+ messenger2:: (actors createActorFromValue: InstantMessenger) <-: new: (users at: 2) total: users size.
155125
156- ^ completionPP1 promise whenResolved: [:result1 |
126+ pResult1:: messenger1 <-: startChat: messenger2.
127+ pResult1 whenResolved: [:result |
128+ result ifNotNil: [
129+ messenger1 <-: displayMessage: (result at: 1) + ': ' + (result at: 2).
130+ completionPP1 resolver resolve: 0. (* end application *)
131+ ] ].
157132
158- completionPP2 promise whenResolved: [:result2 |
133+ pResult2:: messenger2 <-: startChat: messenger1.
134+ pResult2 whenResolved: [:result |
135+ result ifNotNil:[
136+ messenger2 <-: displayMessage: (result at: 1) + ': ' + (result at: 2).
137+ completionPP2 resolver resolve: 0. (* end application *)
138+ ] ].
159139
160- '\n\n''[INSTANT MESSENGER APPLICATION] Ending' println.
161- ].
162- ].
163- )
164-
165- )
140+ ^ completionPP1 promise whenResolved: [:result1 |
141+ completionPP2 promise whenResolved: [:result2 |
142+ '\n\n[INSTANT MESSENGER APPLICATION] Ending' println ] ].
143+ )
144+ )
0 commit comments