PageRenderTime 41ms CodeModel.GetById 15ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

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