001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.midp.main;
028:
029: import com.sun.midp.events.EventTypes;
030: import com.sun.midp.events.EventQueue;
031: import com.sun.midp.events.EventListener;
032: import com.sun.midp.events.Event;
033: import com.sun.midp.events.NativeEvent;
034:
035: import com.sun.midp.log.Logging;
036: import com.sun.midp.log.LogChannels;
037:
038: /**
039: * Works as a simple (display) manager to test NAMS.
040: */
041: public class NamsNotifier implements EventListener {
042:
043: /**
044: * array of sync objects for midlet state change notifications -
045: * one per midlet/per state
046: */
047: private Object syncState[][];
048:
049: /**
050: * array of sync objects for display FG notifications -
051: * one per midlet
052: */
053: private Object syncFG[];
054:
055: /**
056: * array of sync objects for display BG notifications -
057: * one for all midlets
058: */
059: private Object syncBG;
060:
061: /**
062: * public constructor.
063: *
064: * registers itself as an event listener for TEST_EVENT
065: */
066: public NamsNotifier() {
067:
068: int i, j;
069: EventQueue eventQueue;
070:
071: // appId = 0 is reserved for NamsManager itself
072: initNamsNotifier(MIDletSuiteUtils.getIsolateId());
073:
074: // register self as the listener for
075: eventQueue = EventQueue.getEventQueue();
076: // if (eventQueue == null)
077: // Logging.report(Logging.ERROR, LogChannels.LC_CORE,
078: // "DEBUG: Notifier: constructor - event queue is null !!!");
079: eventQueue.registerEventListener(EventTypes.TEST_EVENT, this );
080:
081: syncState = new Object[NamsStorage.NAMS_STORAGE_SIZE][6];
082: syncFG = new Object[NamsStorage.NAMS_STORAGE_SIZE];
083: syncBG = new Object();
084:
085: for (i = 0; i < NamsStorage.NAMS_STORAGE_SIZE; ++i) {
086: for (j = 0; j < 6; ++j) {
087: syncState[i][j] = new Object();
088: }
089: syncFG[i] = new Object();
090: }
091: }
092:
093: /**
094: * initializes native part of NamsNotifier
095: *
096: * @param isolateId isolate where NamsNotifier is run
097: * (where to sent events)
098: */
099: private native void initNamsNotifier(int isolateId);
100:
101: /**
102: * event preprocessing event routine - empty
103: *
104: * @param event event to check
105: * @param waitingEvent event to compare with
106: *
107: * @return always returns true
108: */
109: public boolean preprocess(Event event, Event waitingEvent) {
110: return true;
111: }
112:
113: /**
114: * main event event processing event routine
115: *
116: * @param event event to process
117: *
118: */
119: public void process(Event event) {
120: NativeEvent e = (NativeEvent) event;
121: int appId = e.intParam1;
122: int callbackId = e.intParam2;
123: int state = e.intParam3;
124: int reason = e.intParam4;
125:
126: switch (callbackId) {
127: case 0:
128: /* BG callback - no parameters */
129: /*
130: Logging.report(Logging.WARNING, LogChannels.LC_CORE,
131: "DEBUG: Got Notification: {" +
132: "appId=-1, " +
133: "BG=" + callbackId + ", " +
134: "x, " +
135: "reason=" + reason + "}");
136: */
137: synchronized (syncBG) {
138: syncBG.notifyAll();
139: }
140: break;
141: case 1:
142: /* FG callback */
143: if (appId < 0 || appId > NamsStorage.NAMS_STORAGE_SIZE) {
144: /*
145: Logging.report(Logging.WARNING, LogChannels.LC_CORE,
146: "DEBUG: Notifier: FG callback - " +
147: "index out of bounds ..." +
148: " " + appId);
149: */
150: } else {
151: /*
152: Logging.report(Logging.WARNING, LogChannels.LC_CORE,
153: "DEBUG: Got Notification: {" +
154: " appId=" + appId + ", " +
155: "FG=" + callbackId + ", " +
156: "x, " +
157: "reason=" + reason + "}");
158: */
159: synchronized (syncFG[appId]) {
160: syncFG[appId].notifyAll();
161: }
162: }
163: break;
164: case 2:
165: /* State callback */
166: if (appId < 0 || appId >= NamsStorage.NAMS_STORAGE_SIZE
167: || state < 1 || state > 5) {
168: /*
169: Logging.report(Logging.WARNING, LogChannels.LC_CORE,
170: "DEBUG: Notifier: State callback - " +
171: "index out of bounds ..." +
172: " " + appId +
173: " " + state);
174: */
175: } else {
176: /*
177: Logging.report(Logging.WARNING, LogChannels.LC_CORE,
178: "DEBUG: Got Notification: {" +
179: " appId=" + appId + ", " +
180: "STATE=" + callbackId + ", " +
181: "state=" + state + ", " +
182: "reason=" + reason + "}");
183: */
184: synchronized (syncState[appId][state]) {
185: syncState[appId][state].notifyAll();
186: }
187: }
188: break;
189: }
190: }
191:
192: public boolean waitForBG(long timeout) {
193: boolean value = true;
194:
195: synchronized (syncBG) {
196: try {
197: /*
198: Logging.report(Logging.ERROR, LogChannels.LC_CORE,
199: "DEBUG: Enter Waiting for BG");
200: */
201: syncBG.wait(timeout);
202: } catch (InterruptedException ie) {
203: value = false;
204: } catch (IllegalMonitorStateException ime) {
205: Logging.report(Logging.ERROR, LogChannels.LC_CORE,
206: "Unexpected monitor exception");
207: }
208: }
209: return value;
210: }
211:
212: public boolean waitForFG(int appId, long timeout) {
213: boolean value = true;
214:
215: synchronized (syncFG[appId]) {
216: try {
217: /*
218: Logging.report(Logging.ERROR, LogChannels.LC_CORE,
219: "DEBUG: Enter Waiting for appId=" + appId + "FG");
220: */
221: syncFG[appId].wait(timeout);
222: } catch (InterruptedException ie) {
223: value = false;
224: } catch (IllegalMonitorStateException ime) {
225: Logging.report(Logging.ERROR, LogChannels.LC_CORE,
226: "Unexpected monitor exception");
227: }
228: }
229: return value;
230: }
231:
232: public boolean waitForState(int appId, int state, long timeout) {
233: boolean value = true;
234:
235: synchronized (syncState[appId][state]) {
236: try {
237: /*
238: Logging.report(Logging.ERROR, LogChannels.LC_CORE,
239: "DEBUG: Enter Waiting for appId=" + appId +
240: " state=" + state);
241: */
242: syncState[appId][state].wait(timeout);
243: } catch (InterruptedException ie) {
244: value = false;
245: } catch (IllegalMonitorStateException ime) {
246: Logging.report(Logging.ERROR, LogChannels.LC_CORE,
247: "Unexpected monitor exception");
248: }
249: }
250: return value;
251: }
252: }
|