ATTENTION: SKIP WHAT'S WRITTEN IN RED UNLESS YOU WANT TO KNOW EXACTLY WHAT'S GOING ON... NOT NEEDED THOUGH!
Well, let me first ask the general question/problem that's plaguing me in this "Coded Adventure"...
"Let's say I've opened a file for reading and the file that I'm reading contains: Hello World
How can I just add "World" to an array? I'm putting the file I'm reading into a buffer, but I don't know how to add only one word at a time..."
I hope I haven't already turned you off with this verbiage but, it is about to get a wee bit worse! (You can skip this if you want to just view my code and try to help me figure out just how to get a single word from a buffer) :$
Anyway, the program runs from the command line with four arguments, which we'll call "datafile," "logfile," "server" and "client," initiating a process we'll call the "broker." The time period from startup to termination of the broker process we'll call
"the trading session."
The general idea is that clients ask for stock quotes, either to buy or to sell, and the broker responds with price quotes which are valid for 3 minutes. If a client places an order for the quoted price per share within the 3 minute time window (to buy or to sell, as specified in the original request) then the order is accepted. An order placed after the window has expired is not accepted. A record of every transaction is written to the logfile (request for a quote, issue of a quote, placement of an order, and acceptance or nonacceptance of an order; also poorly formed client messages and resulting error messages sent back to clients).
The datafile has a line of the form "stock_id price1 price2" for each stock which can be traded, in no particular order. Here stock_id is like "U" or "ABCDE", that is, 1-5 uppercase letters. The format for price1 or price2 is like "1097.04", that is, decimal fixed point with two digits after the decimal point. This datafle represents the current price information for the broker's use, and to simplify testing is assumed to be unchanged for the duration of the broker's trading session. If the specified datafile does not exist or cannot be opened for reading at startup then the broker process should exit with a message to STDERR.
FIFOs are to be established by the broker to handle the messages to and from the clients. Clients write all requests for quotes to "server_fifo," where "server" is the third command line argument (here to be concatenated with "_fifo".) Subsequent communications about a quote are to go through "client_dddd_fifo" where "client" is the fourth command line argument and "dddd" is the client_id.
Customer id numbers, given as cust_id in the project description, are four digit numbers with leading 0s allowed, i.e., anything from 0000 to 9999.
A request for a quote should have the form "Request quote to buy stock_id by cust_id." or "Request quote to sell stock_id by cust_id."
**This is where my problem is... After I get what the client has written to server_fifo ("Request quote to buy stock_id by cust_id.") and it's read into my buffer... I don't know how to: Get the stock_id and cust_id**
Anyway, enough talking... here is my CODE:
int main(int argc, char *argv[])
{
char s[MAX_BUFF];
char stock[5];
char cust[4];
char *out;
int numBytes, fd, fd2;
unsigned output;
FILE *file;
FILE *ofile;
FILE *datafile;
mkfifo(strcat(argv[3], "_fifo"), S_IFIFO | 0666);
if(!fork())
{
fd = open(argv[3], O_WRONLY);
printf("Broker is waiting for request...\n");
gets(s);
if((numBytes = write(fd, s, strlen(s))) == -1)
perror("Write1");
else
{
printf("\nClient says, \"%s.\"\n", s);
ofile = fopen(argv[2], "a");
fputs(s, ofile);
fputc('\n', ofile);
fclose(ofile);
}
}
else
{
fd2 = open(argv[3], O_RDONLY);
signal(SIGALRM, handler);
do
{
if((numBytes = read(fd2, s, 300)) == -1)
perror("Read1");
else
{
s[numBytes] = '\0';
if(strstr(s, "quote") && strstr(s, "buy")) // Went the LOOONG way... had to use exact index.
{ // Could you tell me a better way of doing this? Thanks!
int i = 21;
int j = 29;
printf("Client wants to buy...\n");
while(i < 26)
{
stock[(i-21)] = s[i];
i++;
}
printf("The stock is: %s\n", stock);
while(j < 34)
{
cust[(j-29)] = s[j];
j++;
}
printf("The cust_id is: %s\n", cust);
printf("Broker is generating quote...\n");
mkfifo(strcat(argv[4], "_fifo"), S_IFIFO | 0666);
fd = open(argv[4], O_WRONLY | O_NDELAY);
out = "Buy at 50 per share of YADAD for 5555."; //This was just to test if it was writing to logfile..
write(fd, out, strlen(out));
// printf("\nBroker says, \"%s.\"\n", out);
ofile = fopen(argv[2], "a");
fputs(out, ofile);
fputc('\n', ofile);
fclose(ofile);
alarm(5);
}
else if (strstr(s, "quote") && strstr(s, "sell")) // Again, I'm going about this the WRONG way!
{
int i = 22;
printf("Client wants to sell...\n");
while(i < 27)
{
stock[(i-22)] = s[i];
i++;
}
printf("The stock is: %s\n", stock);
}
// printf("Broker read %d bytes.\n", numBytes);
}
}
while(numBytes > 0);
}
return 0;
}
Anyway, even if you can't help one bit... thanks for taking the time to read ALL of this! I love you for it! Really!