Project

General

Profile

Revision 477

Removing colonetclient because we are not using it.

View differences:

trunk/code/projects/colonet/ColonetClient/Commands.cpp
1
/**
2
 *
3
 *  @author Jason Knichel
4
 *
5
 *  3/22/07
6
 */
7

  
8
#include <stdio.h>
9
#include <string.h>
10
#include <ctype.h>
11
#include "includes/Client.h"
12
#include "includes/Commands.h"
13

  
14
void Commands::printUsage()
15
{
16
  printf("\nAvailable commands:\n");
17
  printf("buzzer\n");
18
  printf("lcd\n");
19
  printf("type \"help <command>\" for more information\n\n");
20
}
21

  
22
int Commands::parseCommand(char * command, Server server)
23
{
24
  if (!command)
25
    return -1;
26

  
27
  if (strlen(command) > MAX_INPUT_LENGTH)
28
    return -1;
29

  
30
  char tempCommand[MAX_INPUT_LENGTH];
31
  strcpy(tempCommand, command);
32
  char tokens[MAX_TOKENS][MAX_TOKEN_SIZE];
33
  int numTokens = 0;
34
  if ((numTokens = tokenizeCommand(tempCommand, tokens)) < 1)
35
    {
36
      return -1;
37
    }
38

  
39
  if (strcmp(tokens[0], "help") == 0)
40
    {
41
      if (numTokens == 1)
42
        {
43
          printUsage();
44
          return 0;
45
        }
46
      
47
      if (strcmp(tokens[1], "buzzer") == 0)
48
        {
49
          buzzer.printUsage();
50
        }
51
      else if (strcmp(tokens[1], "lcd") == 0)
52
        {
53
          lcd.printUsage();
54
        }
55
    }
56
  else if (strcmp(tokens[0], "buzzer") == 0)
57
    {
58
      if (buzzer.parseCommand(tokens, numTokens, server) < 0)
59
        {
60
          return -1;
61
        }
62
    }
63
  else if (strcmp(tokens[0], "lcd") == 0)
64
    {
65
      if (lcd.parseCommand(tokens, numTokens, server) < 0)
66
        {
67
          return -1;
68
        }
69
    }
70
  else
71
    {
72
      printf("Invalid command.\n");
73
    }
74

  
75
  return 0;
76
}
77

  
78
int tokenizeCommand(char* command, 
79
                           char tokens[MAX_TOKENS][MAX_TOKEN_SIZE])
80
{
81
  char* nextToken = command;
82
  char* endToken = NULL;
83
  int numTokens = 0;
84

  
85
  if (!command) {
86
    return -1;
87
  }
88

  
89
  while ((endToken = strstr(nextToken, " "))) {
90
    *endToken = '\0';
91

  
92
    if (strlen(nextToken) > MAX_TOKEN_SIZE-1) {
93
      return -1;
94
    }
95

  
96
    strcpy(tokens[numTokens], nextToken);
97

  
98
    numTokens++;
99
    nextToken = endToken + 1;
100

  
101
    while (isspace(*nextToken) && *nextToken != '\0') {
102
      nextToken++;
103
    }
104
  }
105

  
106
  if (endToken == NULL && *nextToken != '\0') {
107
    int nextTokenLen = strlen(nextToken);
108
    if (nextTokenLen > MAX_TOKEN_SIZE-1) {
109
      return -1;
110
    }
111
    if (nextToken[nextTokenLen-1] == '\n')
112
      nextToken[nextTokenLen-1] = '\0';
113

  
114
    strcpy(tokens[numTokens], nextToken);
115

  
116
    numTokens++;
117
  }
118

  
119
  return numTokens;
120
}
trunk/code/projects/colonet/ColonetClient/Buzzer.cpp
1
/**
2
 *
3
 *  @author Jason Knichel
4
 *
5
 *  3/22/07
6
 */
7

  
8
#include <stdio.h>
9
#include <string.h>
10
#include <stdlib.h>
11
#include "includes/Buzzer.h"
12

  
13
Buzzer::Buzzer()
14
{
15
}
16

  
17
void Buzzer::printUsage()
18
{
19
  printf("\nUsage of the \"buzzer\" command:\n");
20
  printf("buzzer init\n");
21
  printf("buzzer setValue <value>\n");
22
  printf("buzzer setFreq <frequency>\n");
23
  printf("buzzer chirp\n");
24
  printf("buzzer off\n");
25
  printf("\n");
26
}
27

  
28

  
29
int Buzzer::parseCommand(char tokens[MAX_TOKENS][MAX_TOKEN_SIZE], int numTokens, Server server)
30
{
31
  if (numTokens < 2)
32
    {
33
      printf("Error: The buzzer command did not contain enough fields.\n");
34
      return -1;
35
    }
36

  
37
  char buffer[MAX_COMMAND_LEN];
38
  buffer[0] = '\0';
39
  snprintf(buffer,MAX_COMMAND_LEN, "%d ", SEND_TO_ROBOT);
40
  int len = strlen(buffer);
41

  
42
  if (strcmp(tokens[1], "init") == 0)
43
    {
44
      snprintf(buffer+len, MAX_COMMAND_LEN - len, "%d", BUZZER_INIT);
45
    }
46
  else if (strcmp(tokens[1], "setValue") == 0 || strcmp(tokens[1], "setFreq") == 0)
47
    {
48
      if (numTokens < 3)
49
        {
50
          printf("Error: That buzzer command needs arguments.\n");
51
          return -1;
52
        }
53

  
54
      char * endPtr = NULL;
55
      int value = strtol(tokens[2], &endPtr, 10);
56
      if (tokens[2][0] == '\0' || *endPtr != '\0')
57
        {
58
          printf("Error: The specified value was invalid.\n");
59
          return -1;
60
        }
61
      
62
      if (strcmp(tokens[1], "setValue") == 0)
63
        {
64
          snprintf(buffer+len, MAX_COMMAND_LEN - len, "%d %d", BUZZER_SET_VAL, value);
65
        }
66
      else
67
        {
68
          snprintf(buffer+len, MAX_COMMAND_LEN - len, "%d %d", BUZZER_SET_FREQ, value);
69
        }
70
    }
71
  else if (strcmp(tokens[1], "chirp") == 0)
72
    {
73
      snprintf(buffer+len, MAX_COMMAND_LEN - len, "%d", BUZZER_CHIRP);
74
    }
75
  else if (strcmp(tokens[1], "off") == 0)
76
    {
77
      snprintf(buffer+len, MAX_COMMAND_LEN - len, "%d", BUZZER_OFF);
78
    }
79
  else
80
    {
81
      printf("Error: invalid buzzer command.\n");
82
      return -1;
83
    }
84

  
85

  
86
  //TODO: remove this print later
87
  printf("Command being sent to server:\n%s\n", buffer);
88

  
89
  server.sendMsg(buffer);
90

  
91
  return 0;
92
}
trunk/code/projects/colonet/ColonetClient/Lcd.cpp
1
/**
2
 *
3
 *  @author Jason Knichel
4
 *
5
 *  4/12/07
6
 */
7

  
8
#include <stdio.h>
9
#include <string.h>
10
#include <stdlib.h>
11
#include "includes/Lcd.h"
12
#include "includes/Client.h"
13

  
14
Lcd::Lcd()
15
{
16
}
17

  
18
void Lcd::printUsage()
19
{
20
  printf("\nUsage of the \"lcd\" command:\n");
21
  printf("lcd init\n");
22
  printf("lcd clearScreen\n");
23
  printf("lcd putByte <byteValue>\n");
24
  printf("lcd putChar <character>\n");
25
  printf("lcd putString <string>\n");
26
  printf("lcd goto <x> <y>\n");
27
  printf("lcd putInt <integer>\n");
28
  printf("\n");
29
}
30

  
31

  
32
int Lcd::parseCommand(char tokens[MAX_TOKENS][MAX_TOKEN_SIZE], int numTokens, Server server)
33
{
34
  if (numTokens < 2)
35
    {
36
      printf("Error: The lcd command did not contain enough fields.\n");
37
      return -1;
38
    }
39

  
40
  char buffer[MAX_COMMAND_LEN];
41
  buffer[0] = '\0';
42
  snprintf(buffer, MAX_COMMAND_LEN, "%d ", SEND_TO_ROBOT);
43
  int len = strlen(buffer);
44

  
45
  if (strcmp(tokens[1], "init") == 0)
46
    {
47
      snprintf(buffer+len, MAX_COMMAND_LEN - len, "%d", LCD_INIT);
48
    }
49
  else if (strcmp(tokens[1], "clearScreen") == 0)
50
    {
51
      snprintf(buffer+len, MAX_COMMAND_LEN - len, "%d", LCD_CLEAR_SCREEN);
52
    }
53
  else if (numTokens < 3)
54
    {
55
      printf("Error: The lcd %s command was invalid or did not contain enough fields.\n", tokens[1]);
56
      return -1;
57
    }
58
  else
59
    {
60
      if (strcmp(tokens[1], "putByte") == 0)
61
        {
62
          char * endPtr = NULL;
63
          int value = strtol(tokens[2], &endPtr, 10);
64
          if (tokens[2][0] == '\0' || *endPtr != '\0')
65
            {
66
              printf("Error: The specified value was invalid.\n");
67
              return -1;
68
            }
69
          
70
          if (value < 0 || value > 255)
71
            {
72
              printf("Error: The specified byte value was too large.\n");
73
              return -1;
74
            }
75

  
76
          snprintf(buffer+len, MAX_COMMAND_LEN - len, "%d %d", LCD_PUTBYTE, value);
77
        }
78
      else if (strcmp(tokens[1], "putChar") == 0)
79
        {
80
          int charLength = strlen(tokens[2]);
81
          if (charLength <= 0 || charLength > 1)
82
            {
83
              printf("Error: The specified character was invalid.\n");
84
              return -1;
85
            }
86
          
87
          char character = tokens[2][0];
88
          
89
          snprintf(buffer+len, MAX_COMMAND_LEN - len, "%d %c", LCD_PUTCHAR, character);
90
        }
91
      else if (strcmp(tokens[1], "putString") == 0)
92
        {
93
          //TODO: make this faster
94
          char stringBuffer[MAX_INPUT_LENGTH];
95
          stringBuffer[0] = '\0';
96
          for (int i = 2; i < numTokens; i++)
97
            {
98
              strncat(stringBuffer, tokens[i], MAX_INPUT_LENGTH - strlen(stringBuffer));
99
              strcat(stringBuffer, " ");
100
            }
101
          if ((signed)(strlen(stringBuffer)) > MAX_COMMAND_LEN - len - 3)
102
            {
103
              printf("Error: The string you want to send is too long.\n");
104
              return -1;
105
            }
106

  
107
          snprintf(buffer+len, MAX_COMMAND_LEN - len, "%d %s", LCD_PUTSTR, stringBuffer);
108
        }
109
      else if (strcmp(tokens[1], "goto") == 0)
110
        {
111
          if (numTokens < 4)
112
            {
113
              printf("Error: Not enough parameters for the goto command.\n");
114
              return -1;
115
            }
116

  
117
          char * endPtr = NULL;
118
          int xValue = strtol(tokens[2], &endPtr, 10);
119
          if (tokens[2][0] == '\0' || *endPtr != '\0')
120
            {
121
              printf("Error: The specified value was invalid.\n");
122
              return -1;
123
            }
124
          endPtr = NULL;
125
          int yValue = strtol(tokens[2], &endPtr, 10);
126
          if (tokens[2][0] == '\0' || *endPtr != '\0')
127
            {
128
              printf("Error: The specified value was invalid.\n");
129
              return -1;
130
            }
131
          
132
          snprintf(buffer+len, MAX_COMMAND_LEN - len, "%d %d %d", LCD_GOTOXY, xValue, yValue);
133
        }
134
      else if (strcmp(tokens[1], "putInt") == 0)
135
        {
136
          char * endPtr = NULL;
137
          int value = strtol(tokens[2], &endPtr, 10);
138
          if (tokens[2][0] == '\0' || *endPtr != '\0')
139
            {
140
              printf("Error: The specified value was invalid.\n");
141
              return -1;
142
            }
143
          
144
          snprintf(buffer+len, MAX_COMMAND_LEN - len, "%d %d", LCD_PUTINT, value);
145
        }
146
      else
147
        {
148
          printf("Error: invalid lcd command.\n");
149
          return -1;
150
        }
151
    }
152

  
153
  //TODO: remove this print later
154
  printf("Command being sent to server:\n%s\n", buffer);
155
  
156
  server.sendMsg(buffer);
157

  
158
  return 0;
159
}
trunk/code/projects/colonet/ColonetClient/includes/Lcd.h
1
/**
2
 *
3
 *  @author Jason Knichel
4
 *
5
 *  4/12/07
6
 */
7

  
8
#ifndef LCD_H
9
#define LCD_H
10

  
11
#include "command_defines.h"
12
#include "Server.h"
13

  
14
class Lcd
15
{
16
 public:
17
  Lcd();
18
  void printUsage();
19
  int parseCommand(char tokens[MAX_TOKENS][MAX_TOKEN_SIZE], int numTokens, Server server);
20

  
21
};
22

  
23
#endif
trunk/code/projects/colonet/ColonetClient/includes/Server.h
1
/**
2
 *
3
 * @author Jason Knichel
4
 *
5
 * 2/15/07
6
 */
7

  
8
#ifndef SERVER_H
9
#define SERVER_H
10

  
11
#include "../../lib/colonet_defs.h"
12

  
13
#define MAX_ERROR_MSG_LEN 256
14
#define MIN_PORT 1024
15
#define MAX_PORT 65536
16
#define MAX_RESPONSES_QUEUE 8
17
#define READ_BUFFER_SIZE (MAX_RESPONSE_LEN * MAX_RESPONSES_QUEUE)
18

  
19
//Error codes
20
#define ERROR_SERVER_PARAMETER_NULL -1
21
#define ERROR_SERVER_MESSAGE_LENGTH -2
22
#define ERROR_SERVER_SOCKET         -3
23
#define ERROR_SERVER_SENDING        -4
24
#define ERROR_SERVER_READING        -5
25
#define ERROR_SERVER_BUFFER_LENGTH  -6
26
#define ERROR_SERVER_MESSAGE_TOO_LONG -7
27
#define ERROR_SERVER_NOT_ENOUGH_ROOM -8
28
#define ERROR_NO_NEWLINE            -9
29

  
30

  
31
class Server
32
{
33
 public:
34
  Server(char * serverAddress, char * port);
35
  Server(int ipAdress, unsigned short port);
36

  
37
  //sends a message to the server you are connected to
38
  int sendMsg(char * message);
39
  //try to read something from the server you are connected to and buffer it
40
  int recvMessagesFromServer();
41
  //getMsg - returns the next message in the readBuffer
42
  int getMsg(char * buffer, int bufferLen);
43

  
44
  int isError();
45
  char * getErrorMsg();
46
  void clearError();
47

  
48
 private:
49
  void initializeServer(int ipAddress, unsigned short port);
50

  
51
  int serverSocket;
52
  int error;
53
  char errorMsg[MAX_ERROR_MSG_LEN];
54

  
55
  char readBuffer[READ_BUFFER_SIZE];
56
  int readBufferLength;
57
};
58

  
59

  
60

  
61
#endif
trunk/code/projects/colonet/ColonetClient/includes/command_defines.h
1
/**
2
 *
3
 *  @author Jason Knichel
4
 *
5
 *  3/22/07
6
 */
7

  
8
#ifndef COMMAND_DEFINES_H
9
#define COMMAND_DEFINES_H
10

  
11
#define MAX_TOKENS 15
12
#define MAX_TOKEN_SIZE 30
13

  
14

  
15
#endif
trunk/code/projects/colonet/ColonetClient/includes/Client.h
1
/**
2
 *
3
 *  @author Jason Knichel
4
 *
5
 * 3/1/07
6
 */
7

  
8

  
9
#ifndef CLIENT_H
10
#define CLIENT_H
11

  
12
#define MAX_INPUT_LENGTH 128
13

  
14
#endif
trunk/code/projects/colonet/ColonetClient/includes/Commands.h
1
/**
2
 *
3
 *  @author Jason Knichel
4
 *
5
 *  3/22/07
6
 */
7

  
8
#ifndef COMMANDS_H
9
#define COMMANDS_H
10

  
11
#include "command_defines.h"
12
#include "Server.h"
13
#include "Buzzer.h"
14
#include "Lcd.h"
15

  
16
class Commands
17
{
18
 public:
19
  void printUsage();
20
  int parseCommand(char * command, Server colonetServer);
21

  
22
 private:
23
  Buzzer buzzer;
24
  Lcd lcd;
25
};
26

  
27
int tokenizeCommand(char * command, char tokens[MAX_TOKENS][MAX_TOKEN_SIZE]);
28

  
29

  
30
#endif
trunk/code/projects/colonet/ColonetClient/includes/Buzzer.h
1
/**
2
 *
3
 *  @author Jason Knichel
4
 *
5
 *  3/22/07
6
 */
7

  
8
#ifndef BUZZER_H
9
#define BUZZER_H
10

  
11
#include "command_defines.h"
12
#include "Server.h"
13

  
14
class Buzzer
15
{
16
 public:
17
  Buzzer();
18
  void printUsage();
19
  int parseCommand(char tokens[MAX_TOKENS][MAX_TOKEN_SIZE], int numTokens, Server server);
20
};
21

  
22
#endif
trunk/code/projects/colonet/ColonetClient/Server.cpp
1
/**
2
 *
3
 *  @author Jason Knichel
4
 *
5
 * 2/15/07
6
 */
7

  
8
#include "includes/Server.h"
9

  
10
#include <sys/types.h>
11
#include <sys/socket.h>
12
#include <stdio.h>
13
#include <string.h>
14
#include <netinet/in.h>
15
#include <unistd.h>
16
#include <netdb.h>
17
#include <stdlib.h>
18
#include "../lib/colonet_defs.h"
19

  
20

  
21
Server::Server(char * serverAddress, char * port)
22
{
23
  if (!serverAddress)
24
    {
25
      //TODO: log error message here
26
      error = 1;
27
      snprintf(errorMsg, MAX_ERROR_MSG_LEN, "Server address was null.");
28
      return;
29
    }
30
 
31
  struct hostent * host = gethostbyname(serverAddress);
32
  if (!host)
33
    {
34
      //TODO: log error message here
35
      error = 1;
36
      snprintf(errorMsg, MAX_ERROR_MSG_LEN, "Could not look up IP address from server address.");
37
      return;
38
    }
39

  
40
  int portNum = 0;
41
  char * endPtr = NULL;
42
  portNum = strtol(port, &endPtr, 10);
43
  if (port[0] == '\0' || *endPtr != '\0')
44
    {
45
      //TODO: log error message here
46
      error = 1;
47
      snprintf(errorMsg, MAX_ERROR_MSG_LEN, "\nThe specified port was invalid.\n");
48
      return;
49
    }
50
  if (portNum < MIN_PORT || portNum > MAX_PORT)
51
    {
52
      //TODO: log error message here
53
      error = 1;
54
      snprintf(errorMsg, MAX_ERROR_MSG_LEN, "\nThe port must be between %d and %d inclusive.\n", MIN_PORT, MAX_PORT);
55
      return;
56
    }
57

  
58
  //TODO: test this to make sure the string host->h_addr is the 4 byte ip
59
  // and not a string in dot notation
60
  int ipAddress = ntohl(atoi(host->h_addr));
61

  
62
  initializeServer(ipAddress, (unsigned short)portNum);
63
}
64

  
65
Server::Server (int ipAddress, unsigned short port)
66
{
67
  initializeServer(ipAddress, (unsigned short)port);
68
}
69

  
70

  
71
int Server::sendMsg(char * message)
72
{
73
  if (!message)
74
    {
75
      error = 1;
76
      snprintf(errorMsg, MAX_ERROR_MSG_LEN, "Message was null when trying to send a message.");
77
      return ERROR_SERVER_PARAMETER_NULL;
78
    }
79

  
80
  if (strlen(message) > MAX_COMMAND_LEN)
81
    {
82
      error = 1;
83
      snprintf(errorMsg, MAX_ERROR_MSG_LEN, "Message was too long.");
84
      return ERROR_SERVER_MESSAGE_LENGTH;
85
    }
86

  
87
  if (serverSocket < 0)
88
    {
89
      error = 1;
90
      snprintf(errorMsg, MAX_ERROR_MSG_LEN, "The socket was not a valid file descriptor.");
91
      return ERROR_SERVER_SOCKET;
92
    }
93

  
94
  char * sentPoint = message;
95
  int needToSend = strlen(sentPoint);
96

  
97
  if (sentPoint[needToSend-1] != '\n')
98
    {
99
      if (needToSend == MAX_COMMAND_LEN-1)
100
        {
101
          error = 1;
102
          snprintf(errorMsg, MAX_ERROR_MSG_LEN, "The message was not terminated by a newline and there was no room to add one.");
103
          return ERROR_NO_NEWLINE;
104
        }
105
      else
106
        {
107
          sentPoint[needToSend] = '\n';
108
          sentPoint[needToSend+1] = '\0';
109
          needToSend++;
110
        }
111
    }
112
  while(needToSend > 0)
113
    {
114
      int sent = write(serverSocket, sentPoint, needToSend);
115
      if (sent < 0)
116
        {
117
          error = 1;
118
          snprintf(errorMsg, MAX_ERROR_MSG_LEN, "There was an error when trying to send the message to the server.");
119
          return ERROR_SERVER_SENDING;
120
        }
121
      needToSend -= sent;
122
    }
123

  
124
  return 1;
125
}
126

  
127
int Server::recvMessagesFromServer()
128
{
129
  char message[READ_BUFFER_SIZE];
130
  int maxCanRead = READ_BUFFER_SIZE - readBufferLength;
131
  
132
  if (maxCanRead > 0)
133
    {
134
      fd_set readSet;
135
      FD_ZERO(&readSet);
136
      FD_SET(serverSocket, &readSet);
137

  
138
      struct timeval t;
139
      memset(&t, 0, sizeof(struct timeval));
140

  
141
      select (serverSocket+1, &readSet, NULL, NULL, &t);
142

  
143
      if (!(FD_ISSET(serverSocket, &readSet)))
144
        {
145
          return 1;
146
        }
147

  
148
      printf("Before read.\n");
149
      int numRead = read(serverSocket, message, maxCanRead);
150
      printf("After read.\n");
151

  
152
      if (numRead < 0)
153
        {
154
          error = 1;
155
          snprintf(errorMsg, MAX_ERROR_MSG_LEN, "There was an error reading from the server.");
156
          return ERROR_SERVER_READING;
157
        }
158
      memcpy(readBuffer+readBufferLength, message, numRead);
159
      readBufferLength += numRead;
160
    }
161

  
162
  return 1;
163
}
164

  
165
int Server::getMsg(char * buffer, int bufferLen)
166
{
167
  if (!buffer)
168
    {
169
      error = 1;
170
      snprintf(errorMsg, MAX_ERROR_MSG_LEN, "The buffer given to getMsg was null.");
171
      return ERROR_SERVER_PARAMETER_NULL;
172
    }
173
  if (bufferLen < 0 || bufferLen > MAX_RESPONSE_LEN)
174
    {
175
      error = 1;
176
      snprintf(errorMsg, MAX_ERROR_MSG_LEN, "The buffer length given to getMsg was invalid.");
177
      return ERROR_SERVER_BUFFER_LENGTH;
178
    }
179

  
180
  int len = -1;
181
  int i;
182
  for (i = 0; i < readBufferLength; i++)
183
    {
184
      if (readBuffer[i] == '\n')
185
        {
186
          len = i+1;
187
          break;
188
        }
189
    }
190
  
191
  if (len == -1)
192
    {
193
      //no message found in buffer
194
      return 0;
195
    }
196

  
197
  if (len > MAX_RESPONSE_LEN)
198
    {
199
      //there was a message in the buffer but it was too long
200
      memmove(readBuffer, readBuffer+len, readBufferLength - len);
201
      readBufferLength -= len;
202
      error = 1;
203
      snprintf(errorMsg, MAX_ERROR_MSG_LEN, "The next message in the buffer was too long.  Removing message from buffer.");
204
      return ERROR_SERVER_MESSAGE_TOO_LONG;
205
    }
206

  
207
  if (len > bufferLen)
208
    {
209
      error = 1;
210
      snprintf(errorMsg, MAX_ERROR_MSG_LEN, "The next message is longer than the buffer it is trying to be stored in.  Leaving message in queue.");
211
      return ERROR_SERVER_NOT_ENOUGH_ROOM;
212
    }
213

  
214
  //this copies the newline over also into the buffer
215
  memcpy(buffer, readBuffer, len);
216
  memmove(readBuffer, readBuffer+len, readBufferLength - len);
217
  readBufferLength -= len;
218
  
219
  return 1;
220
}
221

  
222
int Server::isError()
223
{
224
  return error;
225
}
226

  
227
char * Server::getErrorMsg()
228
{
229
  return errorMsg;
230
}
231

  
232
void Server::clearError()
233
{
234
  memset(errorMsg, 0, MAX_ERROR_MSG_LEN);
235
  error = 0;
236
}
237

  
238

  
239

  
240
/******************
241
 *  Private Methods
242
 *****************/
243

  
244
//assumes ipAddress and port are in host format
245
void Server::initializeServer(int ipAddress, unsigned short port)
246
{
247
  serverSocket = socket(AF_INET, SOCK_STREAM, 0);
248
  if (serverSocket < 0)
249
    {
250
      //TODO: implement logging system and log error message here
251
      error = 1;
252
      snprintf(errorMsg, MAX_ERROR_MSG_LEN, "Could not create a socket.");
253
      return;
254
    }
255

  
256
  struct sockaddr_in addr;
257
  memset (&addr, 0, sizeof(struct sockaddr_in));
258
  addr.sin_family = AF_INET;
259
  addr.sin_addr.s_addr = htonl(ipAddress);
260
  addr.sin_port = htons(port);
261
  
262
  if (connect(serverSocket, (struct sockaddr*)&addr, sizeof(addr)) < 0)
263
    {
264
      //TODO: log error message here
265
      close(serverSocket);
266
      error = 1;
267
      snprintf(errorMsg, MAX_ERROR_MSG_LEN, "Could not connect to server.");
268
      return;
269
    }
270

  
271
  readBufferLength = 0;
272

  
273
  clearError();
274
}
275

  
trunk/code/projects/colonet/ColonetClient/Makefile
1
CC = g++
2
LD = g++
3
FLAGS = -Wall -Wshadow -Wextra
4
CONNECTION_FILES = Server.cpp
5
CONNECTION_OBJECTS = $(CONNECTION_FILES:.cpp=.o)
6
CLIENT_FILES = Client.cpp
7
CLIENT_OBJECTS = $(CLIENT_FILES:.cpp=.o)
8
COMMANDS_FILES = Commands.cpp Buzzer.cpp Lcd.cpp
9
COMMANDS_OBJECTS = $(COMMANDS_FILES:.cpp=.o)
10

  
11
all: client
12

  
13

  
14
connection: $(CONNECTION_FILES)
15
	$(CC) $(FLAGS) -c $(CONNECTION_FILES)
16

  
17
commands: 
18
	$(CC) $(FLAGS) -c $(COMMANDS_FILES)
19

  
20
client: connection commands $(CLIENT_FILES)
21
	$(LD) -o $@ $(CONNECTION_OBJECTS) $(COMMANDS_OBJECTS) $(CLIENT_FILES) 
22

  
23

  
24
clean:
25
	rm -rf *.o
trunk/code/projects/colonet/ColonetClient/Client.cpp
1
/**
2
 *
3
 *  @author Jason Knichel
4
 *
5
 * 3/1/07
6
 */
7

  
8
#include <stdlib.h>
9
#include <stdio.h>
10
#include <string.h>
11
#include "includes/Server.h"
12
#include "includes/Client.h"
13
#include "includes/Commands.h"
14
#include "includes/Buzzer.h"
15

  
16

  
17

  
18
//TODO: figure out why it thinks its connected to the server even if we pick some random IP address
19

  
20

  
21
int main(int argc, char * argv[])
22
{
23
  Buzzer buzzer;
24
  if (argc < 3)
25
    {
26
      printf("\nUsage: %s <server address> <port number>\n", argv[0]);
27
      exit(0);
28
    }
29

  
30
  Server colonetServer(argv[1], argv[2]);
31
  if (colonetServer.isError())
32
    {
33
      printf("There was an error initializing a connection to the server.\nError message: %s\n", colonetServer.getErrorMsg());
34
      exit(0);
35
    }
36
  
37
  Commands commands;
38

  
39
  char msgBuffer[MAX_RESPONSE_LEN];
40
  char inputBuffer[MAX_INPUT_LENGTH+1];
41
  
42
  printf("Type \"help\" for information on available commands\n");
43
  while(1)
44
    {
45
      colonetServer.recvMessagesFromServer();
46

  
47
      while(colonetServer.getMsg(msgBuffer, MAX_RESPONSE_LEN) > 0)
48
        {
49
          //buffer now contains a message the server sent to the client.  do something with it
50
        }
51
      if (colonetServer.isError())
52
        {
53
          printf("There was an error:\n%s\n", colonetServer.getErrorMsg());
54
          colonetServer.clearError();
55
        }
56

  
57
      printf("Enter a command: ");
58
      if (fgets(inputBuffer, MAX_INPUT_LENGTH, stdin) == NULL)
59
        {
60
          printf("There was an error reading from standard input.\n");
61
          continue;
62
        }
63
      if(inputBuffer[0] == '\n')
64
        continue;
65

  
66
      if (commands.parseCommand(inputBuffer, colonetServer))
67
        {
68
          printf("There was an error parsing a command.\n");
69
          continue;
70
        }
71
    }
72
  printf("Done.\n");
73
}

Also available in: Unified diff