// Path: app/src/main/java/com/jilk/ros/rosbridge/FullMessageHandler.java
// public interface FullMessageHandler<T extends Message> {
//     public void onMessage(String id, T message);
// }
// 
// Path: app/src/main/java/com/jilk/ros/rosbridge/operation/CallService.java
// @MessageType(string = "call_service")
// public class CallService extends Operation {
//     @Indicator public String service;
//     @Indicated @AsArray public Message args;  
//     public Integer fragment_size; // use Integer for optional items
//     public String compression;
// 
//     public CallService() {}
//     
//     public CallService(String service, Message args) {
//         this.service = service;
//         this.args = args;
//     }    
// }
// 
// Path: app/src/main/java/com/jilk/ros/rosbridge/operation/ServiceResponse.java
// @MessageType(string = "service_response")
// public class ServiceResponse extends Operation {
//     @Indicator public String service;
//     public boolean result;
//     @Indicated public Message values;
// 
//     public ServiceResponse() {}
//     
//     public ServiceResponse(String service) {
//         this.service = service;
//     }    
// }


import com.jilk.ros.message.Message;
import com.jilk.ros.rosbridge.FullMessageHandler;
import com.jilk.ros.rosbridge.operation.CallService;
import com.jilk.ros.rosbridge.operation.ServiceResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
/**
 * Copyright (c) 2014 Jilk Systems, Inc.
 * 
 * This file is part of the Java ROSBridge Client.
 *
 * The Java ROSBridge Client is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * The Java ROSBridge Client is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Java ROSBridge Client.  If not, see http://www.gnu.org/licenses/.
 * 
 */
package com.jilk.ros;



public class Service<CallType extends Message, ResponseType extends Message> extends Message implements FullMessageHandler {
    private String service;
    private Class<? extends ResponseType> responseType;
    private Class<? extends CallType> callType;
    private ROSClient client;
    private Map<String, CallRecord> calls;
    
    public Service(String service, Class<? extends CallType> callType,
            Class<? extends ResponseType> responseType, ROSClient client) {
        this.service = service;
        this.client = client;
        this.responseType = responseType;
        this.callType = callType;
        calls = new HashMap<String, CallRecord>();
    }

    // A result can only be returned once; it is cleared from the hash before 
    //   being sent/returned. This is to ensure that results do not accumulate
    //   indefinitely.  If callers need to keep these around they can set up their 
    //   own hash.
    
    @Override
    public void onMessage(String id, Message response) {
        //System.out.print("Service.onMessage: ");
        //response.print();
        CallRecord call = calls.get(id);
        if(call == null) {
            System.out.print("No caller service response");
// Path: app/src/main/java/com/jilk/ros/rosbridge/FullMessageHandler.java
// public interface FullMessageHandler<T extends Message> {
//     public void onMessage(String id, T message);
// }
// 
// Path: app/src/main/java/com/jilk/ros/rosbridge/operation/CallService.java
// @MessageType(string = "call_service")
// public class CallService extends Operation {
//     @Indicator public String service;
//     @Indicated @AsArray public Message args;  
//     public Integer fragment_size; // use Integer for optional items
//     public String compression;
// 
//     public CallService() {}
//     
//     public CallService(String service, Message args) {
//         this.service = service;
//         this.args = args;
//     }    
// }
// 
// Path: app/src/main/java/com/jilk/ros/rosbridge/operation/ServiceResponse.java
// @MessageType(string = "service_response")
// public class ServiceResponse extends Operation {
//     @Indicator public String service;
//     public boolean result;
//     @Indicated public Message values;
// 
//     public ServiceResponse() {}
//     
//     public ServiceResponse(String service) {
//         this.service = service;
//     }    
// }


// Path: app/src/main/java/com/jilk/ros/Service.java
import com.jilk.ros.message.Message;
import com.jilk.ros.rosbridge.FullMessageHandler;
import com.jilk.ros.rosbridge.operation.CallService;
import com.jilk.ros.rosbridge.operation.ServiceResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

/**
 * Copyright (c) 2014 Jilk Systems, Inc.
 * 
 * This file is part of the Java ROSBridge Client.
 *
 * The Java ROSBridge Client is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * The Java ROSBridge Client is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Java ROSBridge Client.  If not, see http://www.gnu.org/licenses/.
 * 
 */
package com.jilk.ros;



public class Service<CallType extends Message, ResponseType extends Message> extends Message implements FullMessageHandler {
    private String service;
    private Class<? extends ResponseType> responseType;
    private Class<? extends CallType> callType;
    private ROSClient client;
    private Map<String, CallRecord> calls;
    
    public Service(String service, Class<? extends CallType> callType,
            Class<? extends ResponseType> responseType, ROSClient client) {
        this.service = service;
        this.client = client;
        this.responseType = responseType;
        this.callType = callType;
        calls = new HashMap<String, CallRecord>();
    }

    // A result can only be returned once; it is cleared from the hash before 
    //   being sent/returned. This is to ensure that results do not accumulate
    //   indefinitely.  If callers need to keep these around they can set up their 
    //   own hash.
    
    @Override
    public void onMessage(String id, Message response) {
        //System.out.print("Service.onMessage: ");
        //response.print();
        CallRecord call = calls.get(id);
        if(call == null) {
            System.out.print("No caller service response");
            return;