
#define LOG_TAG "MyClient"

#include <sys/types.h>
#include <unistd.h>
#include <grp.h>

#include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <utils/Log.h>
#include <utils/RefBase.h>

#include <../MyBinder/IMyBinder.h>

#define ALOGD printf
#define ALOGW printf
#define ALOGE printf

using namespace android;

static void help() {
	printf("USAGE:\n\tMyClient fire\n\tMyClient loadBomb <bomb number>\n\tMyClient checkBomb\n");
}

int main(int argc, char** argv) {

    //ALOGD("argv[0]=%s, argv[1] = %s, argv[2] = %s\n", *argv, *(argv+1), *(argv+2));
	if(argc < 2) {
		help();
		exit(0);
	}
    char *cmd = *(argv+1);
    char *arg = *(argv+2);

    sp<IBinder> binder =
        defaultServiceManager()->getService(String16("service.MyBinder"));
    sp<IMyBinder> service = interface_cast<IMyBinder>(binder);
    if (service.get() == NULL) {
        ALOGW("Cannot connect to the service.MyBinder");
        return -1;
    }
    //ALOGD("get service.MyBinder sucess.\n");

    if(!strcmp(cmd, "fire")) {
        if(true == service->fire()) {
            ALOGD("Bang!\n");
        } else {
            ALOGW("no bomb..\n");
        }
    } else if(!strcmp(cmd, "loadBomb")) {
        if(NULL != arg) {
            if(true == service->loadBomb((char)*arg-48)) {
                ALOGD("loadBomb sucess.\n");
            } else {
                ALOGE("loadBomb error.\n");
            }
        } else {
			ALOGE("bomb number should be given.\n");
		}
    } else if(!strcmp(cmd, "checkBomb")) {
        ALOGD("rest bomb:%d\n", service->checkBomb());
    } else {
		help();
	}
    
    return 0;
}

