






Hotspots: Admin Pages | Turn-in Site |
Current Links: Case Final Project Summer 2007
Step 3
Sending a message is a simple one line of code if you have all the information.
code:
/result /
result := broker sendMessage: (Message selector: #symbol) to: reciver.
result := broker sendMessage: (Message selector: #method) to: reciver.
result := broker sendMessage: (Message selector: #method: argument: arg) to: reciver.
result := broker sendMessage: (Message selector: #method:to: arguments: array) to: reciver.
These are 4 different Messages that the broker can send. The first one is just a simple symbol and then next three are ways to call a method with
varing amounts of arguments. As you can see they all expect a return result from the call.
The reciver has to be a ObjRef though.
code: reciver := ObjRef newOnHostName: reciverIp port: reciverPort oid: id.
so to send a message to somebody you need to know their ip and port.
To generate your own ObjRef to pass to other user to use is a little easier to code.
code: /userRef / userRef := ObjRef newOn: (broker accessPoint) oid: id.
Below is some example code so that you can see how it all comes together
code
method initialize
broker := RequestBroker newStstTcpAtPort: 12345.
userInfo := ObjRef newOn: (broker accessPoint) oid: #clientObj.
broker objectAdaptor export: self oid: #clientObj.
broker start.
method connectToServer
/ result server /
server := ObjRef newOnHostName: 128.0.0.1 port: 25252 oid: #serverObj.
result := broker sendMessage: (Message selector: #notifyNewConnection: argument: userinfo) to: server.
self userList: result
method initialize
broker := RequestBroker newStstTcpAtPort: 25252.
broker objectAdaptor export: self oid: #serverObj.
userList := OrderedCollection new.
broker start.
method notifyNewConnection: userInfo
userList add: userInfo.
^userList
As you can see we have a client and server setup. The client knows the ip and port that the server will be at already. The first thing that happens is the client sends a message to the server with oid #serverObj and a message to call a notifyNewConnection method and pass it the userinfo. Since the server has it set up so that any message with oid of #serverObj get sent to "self" it calls it's notifyNewConnection: method. This adds the userinfo to the userList and returns the userList to the client which then adds it to its userList.
Link to this Page