RE: multi-thread: http and IO port
"Kelly Smith" <[email protected]>
| Newsgroups | gmane.comp.hardware.microcontrollers.tini |
|---|---|
| Message-ID | <[email protected]> |
Dear Charles - Re: "My current problem: I want to start a multi-thread program. it should listen to port 80 as a HTTP server , and by the mean while , it check an IO port and do some IO command." OK; Here is the body of code for a a "Device Controller Servlet", that controls an LED on a TINI with the BitPort class. This servlet is used by Shawn Silverman's bullet-proof TINI Tynamo Web-Server. Also, take a look at Ted Kosan's on-line (FREE!) Java course, Chapter 7 - "Lecture 7 - Arrays, Threads and Polymorphism (V1.02)"; The URL is: http://javadevices.org/javacourses/index.html Also refer to: "Lecture 6 - Exceptions, Sockets, Streams and Web Servers (V1.04) " Best regards, Kelly Smith *** Watch out for line-wrap on the code below, since your email will probably wrap long text lines! *** package com.qindesign.servlet.example; import com.dalsemi.system.BitPort; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; /** * DeviceControllerServlet. * * Shows how a system can display a Web page and respond when a user * clicks a link that sends information to the servlet. * * The Web page enables users to monitor and control two LEDs on a Dallas Semiconductor * DSTINI390, Dallas Semiconductor DSTINIm400, or a Systronix TStik400 module. * * Tested with the Tynamo Web server (http://tynamo.qindesign.com) and DSTINI390, DSTINIm400 * module (http://www.dalsemi.com), as well as the Systronix TStik400 (http://www.systronix.com). * * The servlet's Web page contains links to the following image files: * red-led-on.gif * red-led-off.gif * green-led-on.gif * green-led-off.gif * red-button-right.gif * green-button-right.gif * * Before deploying the servlet to the DSTINI390, DSTINIm400 or TStik400, YOU MUST do the * following: * * If using the default webserver.props file, store the image files in the * http-root directory under Tynamo's home directory on the development * computer. (Or edit webserver.props to specify a different directory and store * the files there.) * * Edit the provided build.properties file to include the locations of this * DeviceControllerServlet.java file and the DSTINI390, DSTINIm400 or TStik400 * installation on your development computer. * * For more on compiling the servlet and deploying it to a DSTINI390, DSTINIm400 or TStik400, * see http://tynamo.qindesign.com. * * Other embedded Ethernet and Internet code and resources are available from www.Lvr.com. * * @author Jan Axelson (Adapted from the original DeviceController). * http://www.Lvr.com (Lakeview Research) * @author Shawn Silverman (Adapted from the original DeviceController). * http://tynamo.qindesign.com/ (QIN Design) * @version 1.2 - Modified December 20, 2003 by Kelly Smith. Added display of Telnet * session "ON/OFF" LED state. * @version 1.1 - Modified December 12, 2003 by Kelly Smith. Added support for DSTINI390. * @author Kelly Smith.Email: [email protected] * * References: * * http://tynamo.qindesign.com/docs/ajile/index.html * http://www.lvr.com/eeccode.htm */ public class DeviceControllerServlet extends HttpServlet { // led1 is included on the Dallas Semiconductor DSTINIm400 and Systronix TStik400 board. // led2 is an optional LED to be added and controlled in a similar way at port 5, bit 3. // DSTINI390: P5.2, C1RX, RXD1, DS2480 RX, "serial 1", pin 19. DSTINIm400: P5.2, T3, LED BitPort led1 = new BitPort(BitPort.Port5Bit2); // DSTINI390: P5.3, C1TX, TXD1, DS2480 TX, "serial 1", pin 18. DSTINIm400: P5.3: available, connect for Net Boot jumper BitPort led2 = new BitPort(BitPort.Port5Bit3); // led1 is included on the Dallas Semiconductor DSTINI390 board. // DSTINI390: P3.5, T1, INTOW (Internal 1-Wire), LED pin 11. DSTINIm400400: P3.5, T1, CLKO BitPort led1_TINI = new BitPort(BitPort.Port3Bit5); /** * The initial state for the LEDS is off. * * @throws ServletException */ public void init() throws ServletException { // A logic high (set) turns the corresponding LED off for a DSTINIm400400/TStik400 board. led1.set(); // A logic low (clear) turns the corresponding LED off for a DSTINI390 board. led1_TINI.clear(); // A logic high (set) turns the corresponding LED off for a DSTINIm400400/TStik400 board. led2.set(); } // end init /** * Responds to HTTP GET requests. * * @param request the HTTPServletRequest object * @param response the HttpServletResponse object * * @throws ServletException * @throws IOException */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //The query string sent by the client tells which button was clicked on the Web page. String query = request.getQueryString(); // Initialize the LED states. boolean led1On; // DSTINIm400400/TStik400 board. boolean led1Toggled; led1Toggled=false; boolean led1On_TINI; // DSTINI390 board. boolean led2On; // DSTINIm400400/TStik400 board. boolean led2Toggled; led2Toggled=false; // Read the current state of the LEDs (0=on, 1=off). // and toggle the LED named in the query string. if ("button1".equals(query)) { // 1.2 - Modified December 18, 2003 by Kelly Smith. Added Telnet "ON/OFF" LED state. System.out.print("The Red Button 1 was clicked:"); // Toggle Red LED 1. // A logic low turns the LED 1 on. led1On = toggle(led1); // DSTINIm400400/TStik400 board. // A logic low turns the LED 1 off. led1On = toggle(led1_TINI); // DSTINI390 board. led1Toggled=true; } else { // Don't toggle Red LED 1. led1Toggled=false; led1On_TINI = (led1_TINI.readLatch() != 0); } if ("button2".equals(query)) { // 1.2 - Modified December 18, 2003 by Kelly Smith. Added Telnet "ON/OFF" LED state. System.out.print("The Green Button 2 was clicked:"); // Toggle Green LED 2. // A logic low turns the LED 2 on. led2On = toggle(led2); // DSTINIm400400/TStik400 board. led2Toggled=true; } else { // Don't toggle Green LED 2. led2Toggled=false; } // Set the images and text to match the current LEDs' 1 state. // If led1State is true, LED1 is off. String led1Image; String led1State; // Read the current LED1 hardware state and test the On/Off state. led1On = (led1.readLatch() == 0); if (led1On) { led1Image= "/red-led-on.gif"; led1State = "ON"; if (led1Toggled) { System.out.println(" ON"); } else { } } else { led1Image = "/red-led-off.gif"; led1State = "OFF"; if (led1Toggled) { System.out.println(" OFF"); } else { } } // Set the images and text to match the current LEDs' 2 state. // If led2State is false, LED2 is off. String led2Image; String led2State; // Read the current LED2 hardware state and test the On/Off state. led2On = (led2.readLatch() == 0); if (led2On) { led2Image= "/green-led-on.gif"; led2State = "ON"; if (led2Toggled) { System.out.println(" ON"); } else { } } else { led2Image = "/green-led-off.gif"; led2State = "OFF"; if (led2Toggled) { System.out.println(" OFF"); } else { } } // Return the Web page to the client. SendWebPage (response, led1Image, led2Image, led1State, led2State); } //end doGet /** * Toggles the specified bitPort bit and returns the new state. * * @param bitPort a bit that controls an LED. */ private static boolean toggle(BitPort bitPort) { if (bitPort.readLatch() == 0) { // If it's 0, set the bit to turn the LED off and return true. bitPort.set(); return true; } else { // If it's 1, clear the bit to turn the LED on and return false. bitPort.clear(); return false; } } // end toggle /** * Sends a Web page to the output stream. * * @param response the HttpServletResponse object * @param led1Image name of an image file * @param led2Image name of an image file * @param led1State "on" or "off" * @param led2State "on" or "off" * * @throws IOException */ private void SendWebPage (HttpServletResponse response, String led1Image, String led2Image, String led1State, String led2State) throws IOException { // Write the Web page to the client. // Add an escape character (\) before all quotes (") within the HTML code // (but not before the quotes required at the beginning and end // of each out.print statement). // When writing to the output stream, concatenate string constants with "+", // to minimize the number of writes to the output stream while keeping the code // readable and because there is no performance penalty. // Set the Content-Type field in the HTML header. response.setContentType("text/html"); //Use a ServletOutputStream object to write the page. ServletOutputStream out = response.getOutputStream(); // Write the page's contents to the output stream. // Place the LED and button images in a table so they line up. // The variables led1Image and led2Image each contain a file name, // either "ledon.gif" or "ledoff.gif", as appropriate. out.print("<html>" + "<head><title>DSTINI390/DSTINIm400/TStik400 LED1 and LED2 Device Controller</title></head>" + "<style>body{font-family: Verdana,sans-serif}</style></head>" + "<body>" + "<h1>LED1 and LED2 Device Controller</h1>" + "<table><tr><td>" +"<img src=\""); out.print(led1Image); out.print("\">" + "</td>" + "<td>" + "<img src=\""); out.print(led2Image); out.print("\">" + "</td>" + "</tr>"); // /servlet/DeviceControllerServlet is the mapping that identifies // DeviceControllerServlet as the DeviceControllerServlet servlet, as // specified in the servlets.props file. // button1 or button2 is the query string returned to the DSTINI390, DSTINIm400 // or TStik400 when the user clicks corresponding Red or Green LED button. out.print("<tr>" + "<td>" + "<a href=\"/servlet/DeviceControllerServlet?button1\"><img SRC=\"/red-button-right.gif\"></a>" + "</td>" + "<td>" + "<a href=\"/servlet/DeviceControllerServlet?button2\"><img SRC=\"/green-button-right.gif\" ></a>" + "</td>" + "</tr>" + "</table>" + "<p>Red LED 1 is "); out.print (led1State); // Send text that matches the LEDs' states. out.print(".</p>" + "<p>Green LED 2 is "); out.print (led2State); out.print(".</p>" + "<p>Click a button to turn an LED on or off.</p>" + "<p>The web page will update to show the current states of the LEDs.</p>" + "</body>" + "</html>"); // The servlet container closes the output stream when the request has been // serviced, so there's no need to do it here. } // end SendWebPage /** * Gets information about this servlet. */ public String getServletInfo() { return "Device ControllerServlet"; } // end getServletInfo } // end DeviceControllerServlet -----Original Message----- From: tini-admin-6tN4nzCoH/[email protected] [mailto:[email protected]]On Behalf Of Charles Chao Wang Sent: Thursday, April 15, 2004 3:44 PM To: tini-6tN4nzCoH/[email protected] Subject: [TINI]multi-thread: http and IO port Hi, Thank you guys for the response of CAN baud rate problem. Now CAN bus is working. :) My current problem: I want to start a multi-thread program. it should listen to port 80 as a HTTP server , and by the mean while , it check an IO port and do some IO command. I have finished both parts of the code and tested it, they are working. But I do not know how to put them together by multi-thread way. Is there any code I can read to learn something? BTW, I am using TiniM400 and TiniS400 and Java. Thanks in advance. Charles __________________________________ Do you Yahoo!? Yahoo! Tax Center - File online by April 15th http://taxes.yahoo.com/filing.html _______________________________________________ TINI mailing list TINI-6tN4nzCoH/[email protected] To UNSUBSCRIBE, edit your profile, or see list archives: http://lists.dalsemi.com/mailman/listinfo/tini _______________________________________________ TINI mailing list TINI-6tN4nzCoH/[email protected] To UNSUBSCRIBE, edit your profile, or see list archives: http://lists.dalsemi.com/mailman/listinfo/tini