summaryrefslogtreecommitdiffstats
path: root/Kod/display/ClientServerApp/Client/Client.cpp
blob: efd08fe905ead306d084d2b421a35f86a235c7e2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//============================================================================
// Name        : Client.cpp
// Author      : Erik Hellström, hellstrom@isy.liu.se, 2008-12-14
// Modified    : Emil Larsson, lime@isy.liu.se, 2012-01-13
//
// Description : The client requests data to be sent by the server.
//
//============================================================================

#include <stdio.h>
#include <string>
#include "ComConduit.h"

//
// Show the command-line usage
//
void Usage(char* argv[]) {

	printf("\nUsage: %s <options>\n\n",argv[0]);
	printf("  -S=<message> Send message, ex -S=\"hello world\".\n");
	printf("  -X           Shutdown server.\n");
	printf("\n\n");
}





//
// Module main function
//
int main(int argc, char* argv[]) {

	// Conduit object
	ComConduit Link;
	// Key
	KeyType key[KEY_LEN] = COM_KEY;
	// Work variables
	wchar_t data[COMSTR_LEN];
	bool doShutdown;
	FlagType ret;
	int i,j;
	char c,*ptr;
	WaitResult wr;

	// Initialize data
	data[0] = '\0';
	doShutdown = false;

	//
	// Parse the command-line.
	//
	for (i=1; i<argc; i++) {
		ptr = argv[i];

		while(*ptr == '-') // Step forward
			ptr++;
		c = *ptr;          // Get option
		ptr++;
		if(*ptr == '=')   // Step forward
			ptr++;

		switch(c) {

	case 'S':
	case 's':
		// Read message
		j = 0;
		do {
			// add 1 to avoid the "null" character
//			data[j] = (*ptr)+1;
			data[j] = (*ptr);
			ptr++;
			j++;
		} while(j<COMSTR_LEN && *ptr != '\0');
		if(j >= COMSTR_LEN) {
			fprintf(stderr,"\nToo long message.\n");
			return EXIT_FAIL;
		}
		data[j] = '\0';
		break;

	case 'X':
	case 'x':
		// Shutdown
		doShutdown = true;
		break;
		}
	}

	// Check valid args
	if(!doShutdown && data[0] == '\0') {
		Usage(argv);
		return EXIT_FAIL;
	}

	//
	// Start client
	//
	if(!Link.StartClient(key)) {
		fprintf(stderr,"StartClient() failed.\n");
		return EXIT_FAIL;
	}

	ret = EXIT_OK;
	if(doShutdown) {
		// Tell server to shutdown
		if(!Link.CauseShutdown()) {
			fprintf(stderr,"\nCauseShutdown() failed.\n");
			ret = EXIT_FAIL;
		}
	}
	else {
		do {
			// Check if server is ready
			wr = Link.WaitForEvent(COM_READY,0);      
			if(wr != WAIT_OK) {
				fprintf(stderr,"Server is not ready.\n");
				ret = EXIT_FAIL;
				break;
			}

			// Set data in shm
			if(!Link.SetData(data)) {
				fprintf(stderr,"SetData() failed.\n");
				ret = EXIT_FAIL;
				break;
			}

			// Request that the server send the data
			if(!Link.CauseEvent(COM_REQUEST)) {
				fprintf(stderr,"CauseEvent() failed.\n");
				ret = EXIT_FAIL;
				break;
			}
		} while(false);
	}

	//
	// Shutdown
	//
	if(!Link.Shutdown()) {
		fprintf(stderr,"Shutdown() failed.\n");
		ret = EXIT_FAIL;
	}
	if(ret == EXIT_OK)
		fprintf(stdout, "\nClean exit.\n");

	return ret;
}