KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > PrimeNumberClient


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 import java.net.InetAddress JavaDoc;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33
34 import transport.AddPacketListenerCommand;
35 import transport.ConfigureLocalAddressCommand;
36 import transport.LocalAddress;
37 import transport.packet.Packet;
38 import jegg.EggBase;
39 import jegg.Port;
40 import jegg.PortException;
41 import jegg.timer.Timeout;
42 /**
43  *
44  *
45  * @author Bruce Lowery
46  */

47 public class PrimeNumberClient extends EggBase
48 {
49     private static final Log LOG = LogFactory.getLog(PrimeNumberClient.class);
50     
51     private int SERVICE_TCP_PORT;
52     private int MY_TCP_PORT;
53     private Port networkPort;
54     private int channelID;
55     
56     public void init()
57     {
58         MY_TCP_PORT = Integer.parseInt(getContext().getProperty("my-tcp-port"));
59         SERVICE_TCP_PORT = Integer.parseInt(getContext().getProperty("server-tcp-port"));
60         if (LOG.isInfoEnabled())
61         {
62             LOG.info("MY_TCP_PORT: " + MY_TCP_PORT);
63             LOG.info("SERVER_TCP_PORT: "+SERVICE_TCP_PORT);
64         }
65         
66         LocalAddress la = new LocalAddress(null,MY_TCP_PORT);
67         // Tell network what settings to use
68
try
69         {
70             networkPort.send(getContext().createMessage(new ConfigureLocalAddressCommand(la)));
71             // Register to get all packets.
72
networkPort.send(getContext().createMessage(new AddPacketListenerCommand(null,getContext().getPort())));
73         }
74         catch (PortException pe)
75         {
76             LOG.error("Failed to configure network address: ", pe);
77         }
78         getContext().createSingleShotTimer(5000);
79     }
80     
81     public void handle(Port p)
82     {
83         String JavaDoc name = (String JavaDoc) p.getId();
84         
85         if (LOG.isDebugEnabled())
86             LOG.debug("handle("+name+")");
87         
88         // TODO how to avoid hard-coded egg name...
89
if (name.endsWith("packet-transport"))
90         {
91             networkPort = p;
92         }
93     }
94     
95     /* (non-Javadoc)
96      * @see jegg.Egg#handle(java.lang.Object)
97      */

98     public void handle(Object JavaDoc message)
99     {
100         LOG.warn("Unexpected message: " + message);
101     }
102
103     public void handle(Packet p)
104     {
105         // When you get a packet, print the prime number and
106
// start a timer to trigger the next request.
107

108         Long JavaDoc el = (Long JavaDoc) p.getPayload();
109         LOG.info("Next prime number: " + el);
110         //System.out.println("Next prime number: " + el);
111
channelID = p.getChannelID();
112         getContext().createSingleShotTimer(1000/*1 secs*/);
113     }
114     
115     public void handle(Timeout t)
116     {
117         LOG.debug("TIMEOUT");
118         
119         // Time to request a new prime number
120
LOG.debug("Sending request packet");
121         
122         Packet p = new Packet(null,null);
123         
124         // The first time we send a packet, we don't
125
// know what the channel ID is, so have to
126
// fill in the destination address info
127
// the long way.
128
if (0 == channelID)
129         {
130             try
131             {
132             p.setIP(InetAddress.getLocalHost());
133             p.setPort(SERVICE_TCP_PORT);
134             }
135             catch (Throwable JavaDoc th)
136             {
137                 LOG.error("Unable to send request", th);
138                 return;
139             }
140         }
141         else
142         {
143             // But if we've gotten at least one packet
144
// from the server, then we've also gotten
145
// its channel ID from the packet it sent,
146
// so that can be used to set the destination
147
// on the packet we're sending.
148
p.setChannelID(channelID);
149         }
150         try
151         {
152             networkPort.send(getContext().createMessage(p));
153         }
154         catch (PortException e)
155         {
156             LOG.error("Failed to send request packet: ", e);
157         }
158     }
159 }
160
Popular Tags