/razpubs/test_src/com/razie/upnp/clock/ClockDevice.java

http://razpub.googlecode.com/ · Java · 121 lines · 78 code · 22 blank · 21 comment · 6 complexity · 83f7a23c18ec5e6e8f8a8c5ecb419f60 MD5 · raw file

  1. /******************************************************************
  2. *
  3. * CyberUPnP for Java
  4. *
  5. * Copyright (C) Satoshi Konno 2002
  6. *
  7. * File : ClockDevice.java
  8. *
  9. ******************************************************************/
  10. package com.razie.upnp.clock;
  11. import org.cybergarage.http.HTTPRequest;
  12. import org.cybergarage.http.HTTPResponse;
  13. import org.cybergarage.http.HTTPStatus;
  14. import org.cybergarage.upnp.Action;
  15. import org.cybergarage.upnp.Argument;
  16. import org.cybergarage.upnp.Device;
  17. import org.cybergarage.upnp.Service;
  18. import org.cybergarage.upnp.ServiceList;
  19. import org.cybergarage.upnp.StateVariable;
  20. import org.cybergarage.upnp.control.ActionListener;
  21. import org.cybergarage.upnp.control.QueryListener;
  22. import org.cybergarage.upnp.device.InvalidDescriptionException;
  23. import com.razie.pub.FileUtils;
  24. public class ClockDevice extends Device implements ActionListener, QueryListener
  25. {
  26. private final static String DESCRIPTION_FILE_NAME = "com/razie/upnp/clock/description/description.xml";
  27. private final static String PRESENTATION_URI = "/presentation";
  28. private StateVariable timeVar;
  29. public ClockDevice() throws InvalidDescriptionException
  30. {
  31. super(FileUtils.fileFromUrl(ClockDevice.class.getClassLoader().getResource(DESCRIPTION_FILE_NAME)));
  32. Action getTimeAction = getAction("GetTime");
  33. getTimeAction.setActionListener(this);
  34. Action setTimeAction = getAction("SetTime");
  35. setTimeAction.setActionListener(this);
  36. ServiceList serviceList = getServiceList();
  37. Service service = serviceList.getService(0);
  38. service.setQueryListener(this);
  39. timeVar = getStateVariable("Time");
  40. setLeaseTime(60);
  41. }
  42. ////////////////////////////////////////////////
  43. // ActionListener
  44. ////////////////////////////////////////////////
  45. public boolean actionControlReceived(Action action)
  46. {
  47. String actionName = action.getName();
  48. if (actionName.equals("GetTime") == true) {
  49. Clock clock = Clock.getInstance();
  50. String dateStr = clock.getDateString();
  51. Argument timeArg = action.getArgument("CurrentTime");
  52. timeArg.setValue(dateStr);
  53. return true;
  54. }
  55. if (actionName.equals("SetTime") == true) {
  56. Argument timeArg = action.getArgument("NewTime");
  57. String newTime = timeArg.getValue();
  58. Argument resultArg = action.getArgument("Result");
  59. resultArg.setValue("Not implemented (" + newTime + ")");
  60. return true;
  61. }
  62. return false;
  63. }
  64. ////////////////////////////////////////////////
  65. // QueryListener
  66. ////////////////////////////////////////////////
  67. public boolean queryControlReceived(StateVariable stateVar)
  68. {
  69. Clock clock = Clock.getInstance();
  70. stateVar.setValue(clock.getDateString());
  71. return true;
  72. }
  73. ////////////////////////////////////////////////
  74. // HttpRequestListner
  75. ////////////////////////////////////////////////
  76. public void httpRequestRecieved(HTTPRequest httpReq)
  77. {
  78. String uri = httpReq.getURI();
  79. if (uri.startsWith(PRESENTATION_URI) == false) {
  80. super.httpRequestRecieved(httpReq);
  81. return;
  82. }
  83. Clock clock = Clock.getInstance();
  84. String contents = "<HTML><BODY><H1>" + clock.toString() + "</H1></BODY></HTML>";
  85. HTTPResponse httpRes = new HTTPResponse();
  86. httpRes.setStatusCode(HTTPStatus.OK);
  87. httpRes.setContent(contents);
  88. httpReq.post(httpRes);
  89. }
  90. ////////////////////////////////////////////////
  91. // update
  92. ////////////////////////////////////////////////
  93. public void update()
  94. {
  95. Clock clock = Clock.getInstance();
  96. String timeStr = clock.toString();
  97. timeVar.setValue(timeStr);
  98. }
  99. }