1 package com.quikj.server.framework; 2 3 import java.io.*; 4 import java.net.*; 5 6 7 public class AceDatagram extends AceThread implements AceCompareMessageInterface 8 { 9 public AceDatagram(long user_parm, 10 String name, 11 AceThread cthread, 12 DatagramSocket socket, 13 int max_msg_size) 14 throws IOException, AceException 15 { 16 super(name, true); 17 18 Thread parent_thread; 19 20 if (cthread == null) 21 { 22 parent_thread = Thread.currentThread(); 23 } 24 else 25 { 26 parent_thread = cthread; 27 } 28 29 if ((parent_thread instanceof AceThread) == false) 30 { 31 throw new AceException("The thread supplied as a parameter is not an AceThread"); 32 } 33 34 parent = (AceThread)parent_thread; 35 this.socket = socket; 36 maxMsgSize = max_msg_size; 37 userParm = user_parm; 38 } 39 40 public AceDatagram(long user_parm, 41 String name, 42 DatagramSocket socket, 43 int max_msg_size) 44 throws IOException, AceException 45 { 46 this(user_parm, name, null, socket, max_msg_size); 47 } 48 49 public void dispose() 50 { 51 quit = true; 52 53 if (socket != null) 54 { 55 socket.close(); 56 socket = null; 57 } 58 59 flushMessage(); 60 super.dispose(); 61 } 62 63 public void run() 64 { 65 byte[] buffer = null; 66 67 while (true) 68 { 69 try 70 { 71 72 buffer = new byte[maxMsgSize]; 73 DatagramPacket dp = new DatagramPacket(buffer, buffer.length); 74 75 socket.receive(dp); 76 77 AceDatagramMessage msg = new AceDatagramMessage(this, 79 AceDatagramMessage.READ_COMPLETED, 80 buffer, 81 dp.getLength(), 82 dp.getAddress(), 83 dp.getPort(), 84 userParm); 85 if (parent.sendMessage(msg) == false) { 87 System.err.println(getName() 88 + ":AceDatagram.run() -- Could not send read completed message : " 89 + getErrorMessage()); 90 } 91 } 92 catch (IOException ex) 93 { 94 if (quit == true) 95 { 96 return; 97 } 98 else 99 { 100 System.err.println(getName() 101 + ":AceDatagram.run() -- IO error occured : " 102 + ex.getMessage()); 103 } 104 105 } 123 124 } 125 126 } 127 128 public boolean flushMessage() 129 { 130 return parent.removeMessage(new AceDatagramMessage(this, 0, null, 0, null, 0, userParm), 132 this); 133 } 134 135 public AceMessageInterface waitDatagramMessage() 136 { 137 Thread thr = Thread.currentThread(); 138 139 if ((thr instanceof AceThread) == false) 140 { 141 writeErrorMessage("This method is not being called from an object which is a sub-class of type AceThread"); 142 return null; 143 } 144 145 AceThread cthread = (AceThread)thr; 146 147 while (true) 148 { 149 AceMessageInterface msg_received = cthread.waitMessage(); 150 if ((msg_received instanceof AceDatagramMessage) == true) 151 { 152 if (((AceDatagramMessage)msg_received).getAceDatagram() == this) 153 { 154 return msg_received; 155 } 156 } 157 else if ((msg_received instanceof AceSignalMessage) == true) 158 { 159 return msg_received; 160 } 161 } 162 } 163 164 public boolean same(AceMessageInterface obj1, AceMessageInterface obj2) 166 { 167 boolean ret = false; 168 169 if (((obj1 instanceof AceDatagramMessage) == true) && 170 ((obj2 instanceof AceDatagramMessage) == true)) 171 { 172 if (((AceDatagramMessage)obj1).getAceDatagram() == 173 ((AceDatagramMessage)obj2).getAceDatagram()) 174 { 175 ret = true; 176 } 177 } 178 179 return ret; 180 } 181 182 183 private DatagramSocket socket = null; 184 private int maxMsgSize; 185 private long userParm; 186 private AceThread parent; 187 188 private boolean quit = false; 189 190 } 191 192 193 194 195 196 197 198 199 | Popular Tags |