KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > PrimeNumberServer


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

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

91     public void handle(Object JavaDoc message)
92     {
93         LOG.warn("Unexpected message: " + message);
94     }
95
96     public void handle(Packet p)
97     {
98         // Don't care about packet contents - just using it to
99
// trigger prime generation and send
100
Packet response = new Packet(PacketTypeEnum.APPLICATION,new Long JavaDoc(getNextPrime()));
101         response.setChannelID(p.getChannelID());
102         try
103         {
104             networkPort.send(getContext().createMessage(response));
105         }
106         catch (PortException e)
107         {
108             LOG.error("Unable to send response packet: ", e);
109         }
110     }
111     
112     private long getNextPrime()
113     {
114         for(;;)
115         {
116             nextPrime += 2;
117             boolean notPrime = false;
118             for (long el=2; el < nextPrime/2; ++el)
119             {
120                 if (0 == nextPrime%el)
121                 {
122                     notPrime = true;
123                     break;
124                 }
125             }
126             if (!notPrime) return nextPrime;
127         }
128     }
129 }
130
Popular Tags