Skip to content

Commit 64f8fec

Browse files
author
suyh
committed
读写消息
1 parent 7988270 commit 64f8fec

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

tmp-flink-queue/backup/opt_long.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
示例:在struct option long_options[]数组中,如果有一个元素{"help", 0, 0, 'h'},这里的"help"就是长选项的名称。这意味着在命令行中,用户可以使用--help来表示这个选项。长选项名称通常是具有明确语义的单词,方便用户记忆和使用,比如"version"、"input - file"等,这样用户通过--version或--input - file这样的命令行参数就能很直观地理解选项的用途。
1515
has_arg属性
1616
定义:has_arg用于表示长选项是否需要参数以及参数的性质,它是一个整数,有以下几种取值:
17-
0:表示长选项不需要参数。例如,{"verbose", 0, 0, 'v'}中的verbose选项可能只是一个开关,用于开启或关闭详细输出模式,不需要额外的参数跟在后面。
18-
1:表示长选项需要一个参数。比如{"input - file", 1, 0, 'i'},当用户在命令行输入--input - file filename.txt时,filename.txt就是这个长选项input - file的参数,用于指定输入文件的名称。
19-
2:表示长选项的参数是可选的。这种情况相对复杂一些,例如{"output - file", 2, 0, 'o'},用户可以选择提供一个输出文件名称作为参数,如--output - file result.txt,也可以不提供参数,程序可能会使用默认的输出文件名或者有其他的处理方式。
17+
0(no_argument):表示长选项不需要参数。例如,{"verbose", 0, 0, 'v'}中的verbose选项可能只是一个开关,用于开启或关闭详细输出模式,不需要额外的参数跟在后面。
18+
1(required_argument):表示长选项需要一个参数。比如{"input - file", 1, 0, 'i'},当用户在命令行输入--input - file filename.txt时,filename.txt就是这个长选项input - file的参数,用于指定输入文件的名称。
19+
2(optional_argument):表示长选项的参数是可选的。这种情况相对复杂一些,例如{"output - file", 2, 0, 'o'},用户可以选择提供一个输出文件名称作为参数,如--output - file result.txt,也可以不提供参数,程序可能会使用默认的输出文件名或者有其他的处理方式。
2020
*flag属性
2121
定义:*flag是一个指针,通常用于和val属性配合来改变一个变量的值。如果flag为NULL,getopt_long函数返回val的值作为选项的返回值;如果flag不为NULL,当找到该选项时,*flag指向的变量会被设置为val的值。
2222
示例:假设我们有一个变量int option_enabled = 0;,并且有一个选项{"enable - option", 0, &option_enabled, 1}。当在命令行中发现--enable - option这个选项时,option_enabled的值会被设置为1,用于表示这个选项被启用。

tmp-flink-queue/v002/ipcmqs.c

+30-10
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,19 @@ struct msgbuf {
2020

2121
// 打印使用帮助信息
2222
void usage() {
23-
printf("Usage:./ipc_msg_queue [-r|-w] [--dates <date>] [--pns <pns>] [--channelList <channels>]\n");
23+
printf("Usage:./ipc_msg_queue [-r|-w] [--dates <date>] [--pns <pns>] [--channelList <channels>] [--jobName <jobName>]\n");
2424
printf("Options:\n");
2525
printf(" -r, --read Read from the message queue\n");
2626
printf(" -w, --write Write to the message queue\n");
2727
printf(" -d, --dates Specify date\n");
2828
printf(" -p, --pns Specify pns\n");
2929
printf(" -c, --channelList Specify channel list\n");
30+
printf(" -j, --jobName Specify job name\n");
3031
exit(EXIT_FAILURE);
3132
}
3233

3334
// 格式化消息并返回消息长度
34-
int formatMessage(char *buffer, const char *date, const char *pns, const char *channelList) {
35+
int formatMessage(char *buffer, const char *date, const char *pns, const char *channelList, const char *jobName) {
3536
int offset = 0;
3637
int dateLen = strlen(date);
3738
*((int*)(buffer + offset)) = dateLen;
@@ -51,11 +52,17 @@ int formatMessage(char *buffer, const char *date, const char *pns, const char *c
5152
strcpy(buffer + offset, channelList);
5253
offset += channelListLen;
5354

55+
int jobNameLen = strlen(jobName);
56+
*((int*)(buffer + offset)) = jobNameLen;
57+
offset += sizeof(int);
58+
strcpy(buffer + offset, jobName);
59+
offset += jobNameLen;
60+
5461
return offset;
5562
}
5663

5764
// 解析读取到的消息
58-
void parseReceivedMessage(const char *buffer, char *date, char *pns, char *channelList) {
65+
void parseReceivedMessage(const char *buffer, char *date, char *pns, char *channelList, char *jobName) {
5966
int offset = 0;
6067
int dateLen = *((int*)(buffer + offset));
6168
offset += sizeof(int);
@@ -73,21 +80,30 @@ void parseReceivedMessage(const char *buffer, char *date, char *pns, char *chann
7380
offset += sizeof(int);
7481
strncpy(channelList, buffer + offset, channelListLen);
7582
channelList[channelListLen] = '\0';
83+
offset += channelListLen;
84+
85+
int jobNameLen = *((int*)(buffer + offset));
86+
offset += sizeof(int);
87+
strncpy(jobName, buffer + offset, jobNameLen);
88+
jobName[jobNameLen] = '\0';
89+
offset += channelListLen;
7690
}
7791

78-
// ./ipcmqs -w --pns hy --date 20241102
92+
// ./ipcmqs -w --pns hy --date 20241102 --jobName cohort
7993
int main(int argc, char *argv[]) {
8094
int opt;
8195
int operation = OTHER_OPERATION;
82-
char date[9] = "";
83-
char pns[800] = "";
96+
char date[20] = "";
97+
char pns[1024] = "";
8498
char channelList[2048] = "";
99+
char jobName[1024] = "";
85100
struct option long_options[] = {
86101
{"read", no_argument, 0, 'r'},
87102
{"write", no_argument, 0, 'w'},
88103
{"dates", required_argument, 0, 'd'},
89104
{"pns", required_argument, 0, 'p'},
90105
{"channelList", no_argument, 0, 'c'},
106+
{"jobName", required_argument, 0, 'j'},
91107
{0, 0, 0, 0}
92108
};
93109

@@ -108,6 +124,9 @@ int main(int argc, char *argv[]) {
108124
case 'c':
109125
strcpy(channelList, optarg);
110126
break;
127+
case 'j':
128+
strcpy(jobName, optarg);
129+
break;
111130
default:
112131
usage();
113132
}
@@ -144,18 +163,19 @@ int main(int argc, char *argv[]) {
144163
exit(EXIT_SUCCESS);
145164
}
146165

147-
parseReceivedMessage(buffer.mdata, date, pns, channelList);
166+
parseReceivedMessage(buffer.mdata, date, pns, channelList, jobName);
148167
printf("dates: %s\n", date);
149168
printf("pns: %s\n", pns);
150169
printf("channelList: %s\n", channelList);
170+
printf("jobName: %s\n", jobName);
151171
} else if (operation == WRITE_OPERATION) {
152-
if (strlen(date) == 0 || strlen(pns) == 0) {
153-
fprintf(stderr, "Dates and PNs cannot be empty when writing to the message queue.\n");
172+
if (strlen(date) == 0 || strlen(pns) == 0 || strlen(jobName) == 0) {
173+
fprintf(stderr, "Dates, pns jobName cannot be empty when writing to the message queue.\n");
154174
exit(EXIT_FAILURE);
155175
}
156176
struct msgbuf buffer;
157177
buffer.mtype = MSG_TYPE;
158-
int msgLength = formatMessage(buffer.mdata, date, pns, channelList);
178+
int msgLength = formatMessage(buffer.mdata, date, pns, channelList, jobName);
159179
if (msgsnd(msgid, &buffer, msgLength, 0) == -1) {
160180
perror("msgsnd");
161181
exit(EXIT_FAILURE);

0 commit comments

Comments
 (0)