📄 session.java
字号:
/* * Copyright 1999-2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistribution in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * Neither the name of Sun Microsystems, Inc. or the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * This software is provided "AS IS," without a warranty of any * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that Software is not designed, licensed or intended * for use in the design, construction, operation or maintenance of * any nuclear facility. */package st;import java.io.*;import javax.microedition.io.*;import javax.microedition.lcdui.*;import javax.microedition.rms.*;import shared.MessageConstants;/** * Helper for managing the shopping session with the server. */public class Session implements Runnable { /* * Note: Each method that connects with the server calls back to * the SmartTicket MIDlet: * * 1) updateGauge() as needed to update the progress bar for the user. * 2) stopLoading() or stopLoading(Exception) to indicate the operation * is done. */ byte message; // The name of the MIDlet's record store. static final String RECORD_STORE_NAME = "SmartTicket"; static final String MESSAGE_STORE_NAME = "l10n_data"; SmartTicket smartTicket; RecordStore recordStore; // Cache variable for preview mode. byte previewMode; // Cache variable for size of rows in seating plan. int rowSize; public Session(SmartTicket st) { smartTicket = st; } /** * run method for this session - from here we call the individual * methods that service the requests */ public void run() { switch (message) { case MessageConstants.CREATE_USER: createUser(smartTicket.accountForm.getUsername(), smartTicket.accountForm.getPassword(), smartTicket.accountForm.getZipCode(), smartTicket.accountForm.getCreditCard(), smartTicket.accountForm.getPreviewMode()); break; case MessageConstants.LOGIN_USER: login(); break; case MessageConstants.DISPLAY_LOCALES: loadLocales(smartTicket.localeList); break; case MessageConstants.LOAD_MESSAGES: loadMessages(smartTicket.localeList.getLocaleID()); break; case MessageConstants.DISPLAY_MOVIES: loadMovies(smartTicket.movieList); break; case MessageConstants.DISPLAY_LOCATIONS: loadLocations(smartTicket.locationList, smartTicket.movieList.getMovieID()); break; case MessageConstants.DISPLAY_SHOWTIMES: loadShowtimes(smartTicket.showtimeList, smartTicket.movieList.getMovieID(), smartTicket.locationList.getLocationID()); break; case MessageConstants.DISPLAY_SEATINGPLAN: loadSeatingPlan(smartTicket.seatingCanvas, smartTicket.showtimeList.getShowID(), smartTicket.movieList.getMovieTitle(), smartTicket.showtimeList.getShowtime()); break; case MessageConstants.DISPLAY_POSTER: loadPoster(smartTicket.posterCanvas, smartTicket.posterURL + smartTicket.movieList.getPosterUrl()); break; case MessageConstants.RESERVE_SEATS: reserveSeats(smartTicket.confirmForm, smartTicket.seatingCanvas.getSelectedSeats(), smartTicket.movieList.getMovieTitle(), smartTicket.showtimeList.getShowtime()); break; case MessageConstants.CONFIRM_SEATS: confirmSeats(smartTicket.confirmForm.getCreditCardCheck()); break; case MessageConstants.CANCEL_SEATS: cancelSeats(); break; default: } } /** * Open the session. * * Remember to call close() when you are done. */ public void open() { try { recordStore = RecordStore.openRecordStore(RECORD_STORE_NAME, true); } catch (RecordStoreException rse) { // Do nothing. } } /** * Indicate whether the session is for a new user (one who doesn't have * an account). */ public boolean isNewUser() { try { return (recordStore.getNumRecords() == 0); } catch (RecordStoreException rse) { return true; } } /** * Close the session. * * The session should have been opened using open(). */ public void close() { try { recordStore.closeRecordStore(); } catch (RecordStoreException rse) { // Do nothing. } } /* Messages to server. */ /** * Login to server. */ public void login() { HttpConnection conn = null; DataInputStream in = null; try { if (recordStore.getNumRecords() == 0) { throw new ApplicationException( MessageConstants.USER_NOT_FOUND_ERROR); } // Load the account from local store. byte[] record = recordStore.getRecord(1); in = new DataInputStream(new ByteArrayInputStream(record)); String userID = in.readUTF(); String password = in.readUTF(); int zipCode = in.readInt(); previewMode = (byte)in.read(); // Send the login information to the server. smartTicket.updateGauge(GaugeForm.GAUGE_MAX/3); String message = MessageConstants.LOGIN_USER + "^" + userID + "," + password; conn = open(smartTicket.getBaseServletURL(), message); in = conn.openDataInputStream(); smartTicket.updateGauge(2*GaugeForm.GAUGE_MAX/3); smartTicket.servlet = in.readUTF(); smartTicket.updateGauge(GaugeForm.GAUGE_MAX); smartTicket.stopLoading(); } catch (Exception e) { smartTicket.stopLoading(e); } finally { close(conn, in); } } /** * Load the list of locales available for this application. * * @param ll the list to populate with locale data. */ public void loadLocales(LocaleList ll) { HttpConnection conn = null; DataInputStream in = null; try { smartTicket.updateGauge(GaugeForm.GAUGE_MAX/3); String message = MessageConstants.DISPLAY_LOCALES + "^"; conn = open(smartTicket.servlet, message); in = conn.openDataInputStream(); smartTicket.updateGauge(2*GaugeForm.GAUGE_MAX/3); int count = in.readInt(); ll.init(count); for (int i = 0; i != count; i++) { ll.addLocale(in.readInt(), in.readUTF()); } smartTicket.updateGauge(GaugeForm.GAUGE_MAX); smartTicket.stopLoading(); } catch (Exception e) { smartTicket.stopLoading(e); } finally { close(conn, in); } } /** * Load into the record store the localized messages for the * locale with the given ID. */ public void loadMessages(int localeID) { HttpConnection conn = null; DataInputStream in = null; RecordStore rs = null; try { rs = openMessageStore(true); byte [] record = smartTicket.localeList.getLocale().getBytes(); if (rs.getNumRecords() > 0) { if (smartTicket.localeList.getLocale().equals( new String(rs.getRecord(1)))) { smartTicket.updateGauge(GaugeForm.GAUGE_MAX); smartTicket.stopLoading(); return; } else { rs.setRecord(1, record, 0, record.length); } } else { rs.addRecord(record, 0, record.length); } /* * If the selected localed is not the default and not * the current locale download the messages */ if (localeID > 0) { String message = MessageConstants.LOAD_MESSAGES + "^" + localeID; conn = open(smartTicket.servlet, message); in = conn.openDataInputStream(); smartTicket.updateGauge(2*GaugeForm.GAUGE_MAX/3); // read the messages record = in.readUTF().getBytes(); if (rs.getNumRecords() > 1) { rs.setRecord(2, record, 0, record.length); } else { rs.addRecord(record, 0, record.length); } } smartTicket.updateGauge(GaugeForm.GAUGE_MAX); smartTicket.stopLoading(); } catch (Exception e) { smartTicket.stopLoading(e); } finally { close(conn, in); if (rs != null) { try { rs.closeRecordStore(); } catch (RecordStoreException rse) { // ignore } } } } /** * Create a new user account. * * This method saves some information locally and some information on the * server. * * @param userID the user name. * @param password the user's password. * @param zipCode the user's zipCode. * @param creditCard the user's credit card number. * @param previewMode the user's preview mode preference. */ public void createUser(String userID, String password, int zipCode, String creditCard, byte previewMode) { HttpConnection conn = null; DataInputStream in = null; try { // Send the account information to the server. smartTicket.updateGauge(GaugeForm.GAUGE_MAX/3); String message = MessageConstants.CREATE_USER + "^" + userID + "," + password + "," + zipCode + "," + creditCard; conn = open(smartTicket.servlet, message); in = conn.openDataInputStream(); smartTicket.updateGauge(2*GaugeForm.GAUGE_MAX/3); smartTicket.servlet = in.readUTF(); // Save the account to the local store. ByteArrayOutputStream bout = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(bout); out.writeUTF(userID); out.writeUTF(password); out.writeInt(zipCode); out.write((byte)previewMode); byte[] record = bout.toByteArray(); if (recordStore.getNumRecords() > 0) { recordStore.setRecord(1, record, 0, record.length); } else { recordStore.addRecord(record, 0, record.length); } smartTicket.updateGauge(GaugeForm.GAUGE_MAX); smartTicket.stopLoading(); } catch (Exception e) { smartTicket.stopLoading(e); } finally { close(conn, in); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -