/******************************************************************
 *
 *	CyberUPnP for Java
 *
 *	Copyright (C) Satoshi Konno 2002
 *
 *	File : ClockDevice.java
 *
 ******************************************************************/

package controlPoint;

import java.io.*;

import org.cybergarage.upnp.*;
import org.cybergarage.upnp.device.*;
import org.cybergarage.upnp.control.*;
import org.cybergarage.http.*;
import org.cybergarage.upnp.ssdp.*;

public class Client extends ControlPoint implements NotifyListener {

    public static void main(String[] args) {
	new Client();
    }

    public Client()
    {
	org.cybergarage.util.Debug.on();
	addNotifyListener(this);
	start();
	loop();
    }

    public void loop() {
        for (int n = 0; n < 10000; n++) {
            System.out.println("Sending: hello " + n);
	    queryAll();
            try {
                Thread.currentThread().sleep(1000);
            } catch(Exception e) {
            }
        }
        System.exit(0);
    }

    private void queryAll() {
	DeviceList devList = getDeviceList();
	// System.out.println("Discovered devices: " + devList.size());

	Device dev = null;
	for (int n = 0; n < devList.size(); n++) {
	    dev = devList.getDevice(n);
	    // System.out.println("  Type: " +  dev.getDeviceType());
	    if (dev.getDeviceType().equals("urn:schemas-upnp-org:device:echo:1")) {
		ServiceList services = dev.getServiceList();
		for (int m = 0; m < services.size(); m++) {
		    Service svc = services.getService(m);
		    if (svc.getServiceType().equals("urn:schemas-upnp-org:service:echo:1")) {
			queryToOne(svc);;
		    }
		}
	    }
	}
    }	    

    private void queryToOne(Service svc) {
	StateVariable vbl = svc.getStateVariable("Text");
	vbl.postQuerylAction();
	String value = vbl.getValue();
	System.out.println("Text value " + value);


	/*
	Action echoAction = svc.getAction("Echo");
	Argument inArg = echoAction.getArgument("InText");
	inArg.setValue(txt);
	if (echoAction.postControlAction()) {
	    Argument arg = echoAction.getArgument("ReturnText");
	    String value = arg.getValue();
	    System.out.println(value);
	}
	*/
    }

    public void deviceNotifyReceived(SSDPPacket ssdpPacket) { 
	// uncomment this to see all discovery packets
	// System.out.println("New SSDPPacket, all " + ssdpPacket);
    } 
}

