Prechádzať zdrojové kódy

vcc: sessions are now usable.

intirain 4 rokov pred
rodič
commit
e82434e228
6 zmenil súbory, kde vykonal 151 pridanie a 11 odobranie
  1. 2 2
      Makefile
  2. 93 4
      cmd.c
  3. 14 2
      include/vcc/vcc.h
  4. 2 2
      include/vcc/version.h
  5. 1 1
      pretty.c
  6. 39 0
      vcc.c

+ 2 - 2
Makefile

@@ -1,6 +1,6 @@
 VCC_MAJOR 		= 1
-VCC_MINOR 		= 4
-VCC_PATCH 		= 1
+VCC_MINOR 		= 5
+VCC_PATCH 		= 0
 VCC_STR 		= -rc 
 
 VCC_COMPILE_TIME 	= $(shell date)

+ 93 - 4
cmd.c

@@ -25,6 +25,7 @@
 #include <fcntl.h>
 #include <errno.h>
 #include <string.h>
+#include <ctype.h>
 
 #include <sys/stat.h>
 #include <sys/types.h>
@@ -43,15 +44,22 @@
 int do_cmd_help(char *);
 int do_cmd_quit(char *);
 int do_cmd_ls(char *);
-int do_cmd_ls_bh(struct vcc_request *);
 int do_cmd_rate(char *);
+int do_cmd_newse(char *);
+int do_cmd_currs(char *);
+int do_cmd_swtch(char *);
+int do_cmd_lsse(char *);
 
 int read_msg(struct vcc_request *);
+int vcc_create_session(char *name);
+int do_cmd_ls_bh(struct vcc_request *);
 
 extern char usrname[];
 extern int rate;
+extern int current_sid;
 
 int get_users(void);
+int get_sessions(void);
 
 struct cmdtable {
 	char 		*cmd;
@@ -62,6 +70,10 @@ struct cmdtable {
 	{ "-quit", do_cmd_quit, "Disconnect to server and exit vcc. " }, 
 	{ "-ls", do_cmd_ls, "List all users online now. " }, 
 	{ "-rate", do_cmd_rate, "Change message refresh rate. " }, 
+	{ "-newse", do_cmd_newse, "Create a new session. " }, 
+	{ "-currs", do_cmd_currs, "Get current session id. " }, 
+	{ "-swtch", do_cmd_swtch, "Switch to another session. " }, 
+	{ "-lsse", do_cmd_lsse, "List all sessions created. " }, 
 	{ NULL, NULL, NULL }
 };
 
@@ -70,12 +82,17 @@ struct cmdtable {
 
 
 int do_cmd(char *s) {
-	struct cmdtable *t;
+	struct cmdtable 	*t;
+	char 			*p;
+	int 			len;
 
-	(void) s;
+	p = s;
+
+	for (len = 0; !isspace(*p); len++) 
+		p++;
 
 	for (t = cmdtab; t->cmd; t++) 
-		if (!strncmp(t->cmd, s, strlen(t->cmd))) 
+		if (!strncmp(t->cmd, s, len)) 
 			return t->do_cmd(s);
 	
 	pretty_unknow_cmd(s);
@@ -167,3 +184,75 @@ int do_cmd_ls_bh(struct vcc_request *req) {
 }
 
 
+int do_cmd_lsse(char *s) {
+	(void) s;
+
+	get_sessions();
+
+	return 0;
+}
+
+
+int do_cmd_lsse_bh(struct vcc_request *req) {
+	int n, i;
+
+	n = ntohl(req->uid);
+
+	clear_line();
+
+	if (unlikely(ntohl(req->reqtype) != REQ_CTL_SESS)) {
+		fprintf(stderr, "--- do_cmd_ls_bh: bad package. \n");
+
+		return 1;
+	}
+
+	for (i = 0; i < n; i++) 
+		printf("%s\n", req->msg + i * USRNAME_SIZE);
+
+	pretty_prompt(usrname);
+
+	return 0;
+}
+
+
+int do_cmd_newse(char *s) {
+	char sname[USRNAME_SIZE];
+
+	(void) s;
+
+	fprintf(stderr, "Name of new session: ");
+	scanf("%s", sname);
+
+	vcc_create_session(sname);
+	printf("Session create request sent. \n");
+
+	return 0;
+}
+
+
+int do_cmd_currs(char *s) {
+	(void) s;
+
+	printf("Current session id: %d\n", current_sid);
+
+	return 0;
+}
+
+
+int do_cmd_swtch(char *s) {
+	int sid;
+
+	(void) s;
+
+	printf("Old session id: %d\n", current_sid);
+	fprintf(stderr, "New session id: ");
+	scanf("%d", &sid);
+
+	current_sid = sid;
+
+	printf("We'll use this session from now on. \n");
+
+	return 0;
+}
+
+

+ 14 - 2
include/vcc/vcc.h

@@ -41,9 +41,19 @@
  * server -> client. login ack  */
 #define REQ_CTL_LOGIN 	4
 
+/* client -> server. create a new session */
+#define REQ_CTL_NEWSE 	5
+
+/* client -> server: request (get sessions present). 
+ * server -> client: response */
+#define REQ_CTL_SESS 	6
+
 #define REQ_SIZE 	512
 #define USRNAME_SIZE 	32
-#define MSG_SIZE 	(REQ_SIZE - 3 * sizeof(int) - USRNAME_SIZE)
+
+/* we'll not warn you anyway.  */
+#define PASSWD_SIZE 	64
+#define MSG_SIZE 	(REQ_SIZE - 4 * sizeof(int) - USRNAME_SIZE)
 
 struct vcc_request {
 	int 		magic;
@@ -51,9 +61,11 @@ struct vcc_request {
 
 	/* to make server easy and fast */
 	int 		uid;
+	int 		session;
+
 	char 		usrname[USRNAME_SIZE];
 	char 		msg[MSG_SIZE];
-} __attribute__ ((packed));
+};
 
 #define to_boolean(c) 	(!!(c))
 #define likely(c) 	__builtin_expect(to_boolean(c), 1)

+ 2 - 2
include/vcc/version.h

@@ -1,2 +1,2 @@
-#define VCC_VERSION "VCC 1.4.1 Sat Mar 26 14:16:55 CST 2022 root"
-#define VCC_SEMVER "vcc 1.4.1-rc "
+#define VCC_VERSION "VCC 1.5.0 Sat Mar 26 21:45:34 CST 2022 root"
+#define VCC_SEMVER "vcc 1.5.0-rc "

+ 1 - 1
pretty.c

@@ -157,7 +157,7 @@ int pretty_unknow_cmd(char *s) {
 
 int pretty_cmd_help(char *cmdname, char *descr) {
 	setcolor(stdout, curr_helpcmd);
-	printf("%s: ", cmdname);
+	printf("%s:", cmdname);
 
 	setcolor(stdout, curr_helpmsg);
 	printf("\t%s\n", descr);

+ 39 - 0
vcc.c

@@ -42,8 +42,11 @@
 
 extern int fd;
 
+int current_sid = 0;
+
 int vcc_login_bh(struct vcc_request *);
 int do_cmd_ls_bh(struct vcc_request *);
+int do_cmd_lsse_bh(struct vcc_request *);
 
 
 int init_socket(const char *server_addr) {
@@ -98,6 +101,7 @@ int send_msg(const char *msg, const char *usrname) {
 	req.magic = htonl(VCC_MAGIC);
 	req.reqtype = htonl(REQ_MSG_SEND);
 	req.uid = 0;
+	req.session = htonl(current_sid);
        
 	strncpy(req.usrname, usrname, USRNAME_SIZE);
 	strncpy(req.msg, msg, MSG_SIZE);
@@ -141,6 +145,20 @@ int get_users(void) {
 	return 0;
 }
 
+int get_sessions(void) {
+	struct vcc_request req;
+
+	clear_req(&req);
+
+	req.magic = htonl(VCC_MAGIC);
+	req.reqtype = htonl(REQ_CTL_SESS);
+	req.uid = 0;
+
+	write(fd, &req, REQ_SIZE);
+
+	return 0;
+}
+
 
 int do_bh(struct vcc_request *req) {
 	switch (ntohl(req->reqtype)) {
@@ -154,6 +172,11 @@ int do_bh(struct vcc_request *req) {
 
 		break;
 
+	case REQ_CTL_SESS:
+		do_cmd_lsse_bh(req);
+
+		break;
+
 	default:
 		fprintf(stderr, "*** unexpected packet type %d. \n", ntohl(req->reqtype));
 
@@ -203,3 +226,19 @@ int vcc_login_bh(struct vcc_request *req) {
 }
 
 
+int vcc_create_session(char *name) {
+	struct vcc_request req;
+
+	clear_req(&req);
+
+	req.magic = htonl(VCC_MAGIC);
+	req.reqtype = htonl(REQ_CTL_NEWSE);
+
+	strncpy(req.usrname, name, USRNAME_SIZE);
+
+	write(fd, &req, REQ_SIZE);
+
+	return 0;
+}
+
+