1 /*
2 * Copyright (c) 2004, Bruce Lowery
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * - Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * - Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * - Neither the name of JEGG nor the names of its contributors may be used
14 * to endorse or promote products derived from this software without
15 * specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29 package jegg;
30
31 import java.util.Collection;
32 import java.util.HashSet;
33 import java.util.Iterator;
34 import java.util.Set;
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38 /***
39 * Used to deliver a message to an egg. Each Egg
40 * instance has just one message port instance that other eggs can use to
41 * send it messages. Any message that is written to an egg port will be
42 * delivered, asynchronously, to the egg that the port belongs to.
43 * <p>
44 * An egg can publish its port in the JEgg port registry so that other
45 * eggs will be able to send it messages (see {@link jegg.Egg.publishPort(Port)
46 * Egg.publishPort()}).
47 */
48 public class Port
49 {
50 /*** Class logger */
51 private static final Log LOG = LogFactory.getLog(Port.class);
52
53 /*** The egg that this port belongs to. */
54 private Egg _owner;
55
56 /*** Other ports connected to this one */
57 private final Collection _otherPorts = new HashSet();
58
59 /***
60 * Construct a port for the specified egg.
61 * @param egg the egg that will own the new port.
62 */
63 Port (final Egg egg)
64 {
65 super();
66 _owner = egg;
67 }
68
69 /***
70 * Deliver an ob ject to the egg that this port belongs to.
71 * @param o the object to deliver to the egg.
72 * @param p the prioroty of the message.
73 */
74 public final void send(Message m)
75 {
76 _owner.enqueue(m);
77 }
78
79 /***
80 * Deliver a message to all clients connected to this
81 * port. The message will be send with a medium priority
82 * level assigned to it.
83 * @param message
84 */
85 void broadcast(final Object msg)
86 {
87 broadcast(msg,Priority.MEDIUM);
88 }
89
90 /***
91 * Deliver a message with specific priority to all
92 * clients connected to this port.
93 * @param msg
94 * @param p
95 */
96 void broadcast(final Object msg, final Priority p)
97 {
98 Message message = new Message(msg,this,p);
99 for (Iterator it = _otherPorts.iterator(); it.hasNext(); )
100 {
101 Port po = (Port) it.next();
102 po.send(message);
103 }
104 }
105
106 /***
107 * Add a port to the list of ports that broadcast message will
108 * be sent to.
109 * @param port xx
110 */
111 public final void connect (final Port client)
112 {
113 _otherPorts.add(client);
114 }
115
116 /***
117 * Get list of ports connected to this one
118 */
119 final Port[] getConnectedPorts()
120 {
121 return (Port[]) _otherPorts.toArray(new Port[_otherPorts.size()]);
122 }
123
124 } // END OF CLASS Port
This page was automatically generated by Maven