1 18 19 package sync4j.framework.notification; 20 21 import java.io.ByteArrayOutputStream ; 22 import java.io.IOException ; 23 24 25 26 33 public class TriggerHeaderNotificationMessage { 34 35 public static final byte UI_MODE_NOT_SPECIFIED = 0; 37 public static final byte UI_MODE_BACKGROUND = 1; 38 public static final byte UI_MODE_INFORMATIVE = 2; 39 public static final byte UI_MODE_USER_INTERACTION = 3; 40 41 public static final byte INITIATOR_CLIENT = 0; 42 public static final byte INITIATOR_SERVER = 1; 43 44 private static final float MAX_VERSION_VALUE = 102.3f; 45 46 private static final int NUM_BIT_VERSION_FIELD = 10; 47 private static final int NUM_BIT_FUTURE_USE_FIELD = 27; 48 private static final int NUM_BIT_SESSION_ID_FIELD = 16; 49 private static final int NUM_BIT_SERVER_ID_LENGTH_FIELD = 8; 50 51 52 53 private float version; 55 private int uiMode; 56 private int initiator; 57 private int sessionId; 58 private String serverIdentifier; 59 60 62 public TriggerHeaderNotificationMessage() {} 63 64 73 public TriggerHeaderNotificationMessage(final float version, 74 final int uiMode, 75 final int initiator, 76 final int sessionId, 77 final String serverIdentifier) { 78 this.version = version; 79 this.uiMode = uiMode; 80 this.initiator = initiator; 81 this.sessionId = sessionId; 82 this.serverIdentifier = serverIdentifier; 83 } 84 85 91 public float getVersion() { 92 return version; 93 } 94 95 101 public void setVersion(float version) throws NotificationException { 102 103 if (version > MAX_VERSION_VALUE) { 104 throw new NotificationException("Value specified for version information must be small of " + MAX_VERSION_VALUE); 105 } 106 this.version = version; 107 } 108 109 114 public int getUiMode() { 115 return uiMode; 116 } 117 118 124 public void setUiMode(int uiMode) { 125 this.uiMode = uiMode; 126 } 127 128 133 public int getInitiator() { 134 return initiator; 135 } 136 137 143 public void setInitiator(int initiator) { 144 this.initiator = initiator; 145 } 146 147 152 public int getSessionId() { 153 return sessionId; 154 } 155 156 162 public void setSessionId(int sessionId) { 163 this.sessionId = sessionId; 164 } 165 166 167 168 173 public String getServerIdentifier() { 174 return serverIdentifier; 175 } 176 177 183 public void setServerIdentifier(String serverIdentifier) { 184 this.serverIdentifier = serverIdentifier; 185 } 186 187 188 193 public byte[] buildByteMessageValue() throws NotificationException { 194 195 byte[] toReturn = null; 196 197 ByteArrayOutputStream byteOut = new ByteArrayOutputStream (); 198 BitOutputStream bout = new BitOutputStream(byteOut); 199 200 201 byte[] bServerId = null; 202 try { 203 int newVersion = (int) (version * 10); 205 int[] intRepresentation = getIntBinaryRepresentation(newVersion, NUM_BIT_VERSION_FIELD); 206 for (int i = 0; i < NUM_BIT_VERSION_FIELD; i++) { 207 bout.writeBit(intRepresentation[i]); 208 } 209 210 switch (uiMode) { 212 case UI_MODE_NOT_SPECIFIED: 213 bout.writeBit(0); 214 bout.writeBit(0); 215 break; 216 case UI_MODE_BACKGROUND: 217 bout.writeBit(0); 218 bout.writeBit(1); 219 break; 220 case UI_MODE_INFORMATIVE: 221 bout.writeBit(1); 222 bout.writeBit(0); 223 break; 224 case UI_MODE_USER_INTERACTION: 225 bout.writeBit(1); 226 bout.writeBit(1); 227 break; 228 default: 229 bout.close(); 230 throw new NotificationException("User interaction mode '" + uiMode + 231 "' isn't valid"); 232 } 233 234 switch (initiator) { 236 case INITIATOR_CLIENT: 237 bout.writeBit(0); 238 break; 239 case INITIATOR_SERVER: 240 bout.writeBit(1); 241 break; 242 243 default: 244 bout.close(); 245 throw new NotificationException("Initiator value '" + uiMode + "' isn't valid"); 246 } 247 248 for (int i = 0; i < NUM_BIT_FUTURE_USE_FIELD; i++) { 251 bout.writeBit(0); 252 } 253 254 255 int[] intRepresentationSessionId = getIntBinaryRepresentation(sessionId, 256 NUM_BIT_SESSION_ID_FIELD); 257 258 for (int i = 0; i < NUM_BIT_SESSION_ID_FIELD; i++) { 259 bout.writeBit(intRepresentationSessionId[i]); 260 } 261 262 int length = serverIdentifier.length(); 264 int[] intRepresentationLength = getIntBinaryRepresentation(length, 265 NUM_BIT_SERVER_ID_LENGTH_FIELD); 266 267 for (int i = 0; i < NUM_BIT_SERVER_ID_LENGTH_FIELD; i++) { 268 bout.writeBit(intRepresentationLength[i]); 269 } 270 271 bServerId = serverIdentifier.getBytes("UTF-8"); 273 274 bout.close(); 275 } catch (IOException ex) { 276 throw new NotificationException("Error during message building", ex); 277 } 278 279 byte[] tmp = byteOut.toByteArray(); 280 281 toReturn = new byte[tmp.length + serverIdentifier.length()]; 282 283 System.arraycopy(tmp, 0, toReturn, 0, tmp.length); 284 System.arraycopy(bServerId, 0, toReturn, tmp.length, bServerId.length); 285 286 287 return toReturn; 288 } 289 290 291 293 298 private static int[] converteBinaryRepresentation(String binaryRepresentation) { 299 char[] c = binaryRepresentation.toCharArray(); 300 int[] intRepresentation = new int[c.length]; 301 302 for (int i = 0; i < c.length; i++) { 303 intRepresentation[i] = Integer.parseInt(String.valueOf(c[i])); 304 } 305 return intRepresentation; 306 } 307 308 309 317 private int[] getIntBinaryRepresentation(int value, int n) { 318 String sBinaryValue = Integer.toBinaryString(value); 319 sBinaryValue = paddingString(sBinaryValue, n, '0', true); 320 321 int[] intRepresentation = converteBinaryRepresentation(sBinaryValue); 322 323 return intRepresentation; 324 } 325 326 327 335 private static String paddingString(String s, int n, char c, boolean paddingLeft) { 336 StringBuffer str = new StringBuffer (s); 337 int strLength = str.length(); 338 if (n > 0 && n > strLength) { 339 for (int i = 0; i <= n; i++) { 340 if (paddingLeft) { 341 if (i < n - strLength)str.insert(0, c); 342 } else { 343 if (i > strLength)str.append(c); 344 } 345 } 346 } 347 return str.toString(); 348 } 349 350 } | Popular Tags |