Source code: proxy/bearer/PreparedMessages.java
1 /**
2 * TODO: set beter initial capacity for HashMaps
3 * TODO: who sets id ?
4 */
5
6 package proxy.bearer;
7
8 import java.util.*;
9 import java.lang.*;
10
11 /**
12 * <p>Description: This is data structere that stores messages prepared
13 * for delivery. </p>
14 * @author Julian Kania
15 * @version 1.0
16 */
17
18 public class PreparedMessages {
19
20 /**
21 * my collection of waiting messages
22 */
23 private HashMap wm;
24
25 /**
26 * my acknowledges
27 */
28 private HashMap ackns;
29
30
31 /**
32 * default constructor
33 */
34 public PreparedMessages(){
35 wm = new HashMap();
36 ackns = new HashMap();
37 };
38
39 /**
40 * insert new message into data structere
41 * @param ci pair IMEI -- Application identifier determining to whom this message should be sent
42 * @param pm message prepared for delivery, that is jms4j2me-serialized byte array with given code
43 */
44 public synchronized void put(ClientIdentifier ci, PreparedMessage pm){
45 log("putting message for mobile " + ci.mobile() + ", owner " + ci.owner());
46 if (!wm.containsKey(ci)){
47 wm.put(ci, new LinkedList());
48 }
49 ((LinkedList)wm.get(ci)).addLast(pm);
50 log("for mobile " + ci.mobile() + ", owner " + ci.owner() + " i have " + ((LinkedList)wm.get(ci)).size() + " messages");
51 }
52
53 /**
54 * get first message for given destination.
55 * @param to whom this message should be sent, as pair IMEI -- application identifier
56 * @return message prepared for delivery, that is jms4j2me-serialized
57 * byte array with given code, or null if no data found
58 */
59 public synchronized PreparedMessage get(ClientIdentifier ci){
60 // log("get: rying to get message for mobile " + ci.mobile() + ", owner " + ci.owner() );
61 // log("get: number of mobiles messages are stored for: " + wm.size());
62 if ( !wm.containsKey(ci) ){
63 log("get: no messages found, returning null");
64 return null;
65 }
66
67 if ( ((LinkedList)wm.get(ci)).isEmpty() ){
68 // log("list is empty, returning null");
69 return null;
70 }
71 return (PreparedMessage) ( (LinkedList) wm.get(ci)).getFirst();
72 }
73
74 /**
75 * get pointed message for given destination.
76 * @param to whom this message should be sent, as pair IMEI -- application identifier
77 * @param id what is the identifier of awaiting message
78 * @return message prepared for delivery, that is jms4j2me-serialized byte array with given code
79 * @obsolete you should not use this
80 */
81 /*
82 public synchronized PreparedMessage get(ClientIdentifier ci, int id){
83 return (PreparedMessage)((LinkedList)wm.get(ci)).get(id);
84 }
85
86 /**
87 * deletes first message in the queue for given client. Check message id to ensure
88 * right message is deleted
89 *
90 * @param ci pair IMEI -- Application identifier determining to whom this message should be sent
91 * @param id message id
92 */
93 public synchronized void delete(ClientIdentifier ci, int id){
94 if (this.get(ci).id() != id){
95 log("JASNA CHOLERA!!! kazali mi kasować wiadomosc id = " + id + ", a u mnie pierwsza ma id = " + get(ci).id());
96 } else {
97 ((LinkedList)wm.get(ci)).removeFirst();
98 }
99
100 }
101
102 /**
103 * saves number of int representing messages received from given mobile
104 * append them to previous acknowledges if there are any
105 *
106 * @param ci author of messages that are acknowledged
107 * @param arr that messages' identifiers
108 */
109 public synchronized void putAcknowledges(ClientIdentifier ci, int[] arr){
110 log("putting acknowledges " + arr.length + " for mobile " + ci.mobile());
111 if (ackns.get(ci) == null){
112 ackns.put(ci, new Vector());
113 }
114 /* ugly. how to convert int[] to Collection? */
115 for (int i = 0; i < arr.length; i++)
116 ((Vector)ackns.get(ci)).add(new Integer(arr[i]));
117 }
118
119
120 /**
121 * returns all all acknowledges for give client, and deletes those acknowledges
122 *
123 * @param ci author acknowledged messages
124 *@return messages identifiers
125 */
126 public synchronized Integer[] getAcknowledges(ClientIdentifier ci){
127 log("get acknowledge for mobile " + ci.mobile() + ", owner " + ci.owner());
128 synchronized(ackns){
129 if (ackns.get(ci) == null)
130 return null;
131 // log("ackns.get(ci).class daje " + ackns.get(ci).getClass());
132 // log("((Vector)ackns.get(ci)).toArray().class daje " + ( (Vector) ackns.get(ci)).toArray().getClass());
133
134 Object[] tmp = ((Vector) ackns.get(ci)).toArray();
135 Integer[] result = new Integer[ ( (Vector) ackns.get(ci)).size()];
136 for (int i = 0; i < result.length; i++) {
137 if (tmp[i] == null)
138 log("WIELKI BLAD");
139 else
140 log("tmp[i] = " + tmp[i]);
141 result[i] = (Integer) tmp[i];
142 }
143 // Integer[] result = (Integer[])((Vector)ackns.get(ci)).toArray();
144 ( (Vector) ackns.get(ci)).removeAllElements();
145 return result;
146 }
147 }
148 private void log(String s){
149 System.out.println("PMs:" + s);
150 }
151
152 } /* class PreparedMessages */