📄 dce_agent.c
字号:
/***************************************Ho Chi Minh City University of Technology Computer Science Department Distributed Computing Environment (DCE)Written by Nguyen Van Noi -59406114File dce_agent.c on Mon Dec 21 12:50:18 1998****************************************/#include "dce_agent.h"#include "dce_sinfo.h"#define MAXTIME 300AgentDes Agent;MSG msg;int dce_agent_init (int, char **);void dce_agent_exit ();int dce_agent_mcastsend (char *buf, int len);void dce_agent_alive (void *p);void dce_agent_chooseserver (void *p);void TreaBothAgent ();void dce_msg_admin_request_agent_exist ();void dce_msg_admin_request_agent_connect ();void dce_msg_admin_request_agent_max_order ();void dce_msg_admin_requet_agent_seronhost ();void dce_msg_admin_request_agent_listserver ();void dce_msg_admin_request_agent_howserver ();void dce_msg_admin_request_agent_listperuser ();void dce_admin_request_agent_howserveralive ();void dce_msg_server_request_agent_broadcastdata ();void dce_msg_admin_request_agent_addservice ();void dce_msg_admin_request_agent_listservice ();void dce_msg_client_request_agent_service ();void dce_msg_all_reply_client_pleasewait ();void dce_msg_admin_request_agent_listcuruserservice ();void dce_msg_service_reply_all_finish ();void dce_msg_all_reply_alluserservice ();void dce_agent_treat_for_election ();void dce_msg_admin_request_agent_confirm_connect();voidmain (int argc, char *argv[]){ if (!dce_diamon ()) exit (-1); if (!dce_agent_init (argc, argv)) exit (-2); while (!Agent.bExit) { if (dce_agent_mcastrecv ((char *) &msg, sizeof (MSG)) != sizeof (MSG)) continue; switch (msg.who) { case ADMIN: if (Agent.AdminTime > 0) if ((Agent.AdminAddr == msg.saddr) && (Agent.pid == msg.nextra)) Agent.AdminTime = MAXTIME; break; case AGENT: TreaBothAgent (); break; case SERVER: dce_agent_update_newmsg (); break; case SERVICE: break; } switch (msg.type) { case DCE_MSG_ADMIN_REQUEST_AGENT_CONFIRM_CONNECT: dce_msg_admin_request_agent_confirm_connect(); break; case DCE_MSG_SERVER_ELECTION_AGENT: case DCE_MSG_SERVER_ELECTED_AGENT: dce_agent_treat_for_election (); break; case DCE_MSG_SERVER_REQUEST_AGENT_BROADCASTDATA: dce_msg_server_request_agent_broadcastdata (); break; case DCE_MSG_SERVICE_REPLY_ALL_FINISH: dce_msg_service_reply_all_finish (); break; case DCE_MSG_ADMIN_REQUEST_AGENT_LISTCURUSERSERVICE: dce_msg_admin_request_agent_listcuruserservice (); break; case DCE_MSG_ALL_REPLY_ALL_USERSERVICE: dce_msg_all_reply_alluserservice (); break; case DCE_MSG_ALL_REPLY_CLIENT_PLEASEWAIT: dce_msg_all_reply_client_pleasewait (); break; case DCE_MSG_CLIENT_REQUEST_AGENT_SERVICE: dce_msg_client_request_agent_service (); break; case DCE_MSG_ADMIN_REQUEST_AGENT_LISTSERVICE: dce_msg_admin_request_agent_listservice (); break; case DCE_MSG_ADMIN_REQUEST_AGENT_ADDSERVICE: dce_msg_admin_request_agent_addservice (); break; case DCE_MSG_ADMIN_REQUEST_AGENT_HOWMANYSERVERALIVE: dce_admin_request_agent_howserveralive (); break; case DCE_MSG_ADMIN_REQUEST_ALL_DELUSER: DelUid (Agent.pUserList, msg.lextra); break; case DCE_MSG_ADMIN_REQUEST_AGENT_LISTUSER: dce_msg_admin_request_agent_listperuser (); break; case DCE_MSG_ADMIN_REQUEST_ALL_ADDUSER: AddUid (Agent.pUserList, msg.lextra); break; case DCE_MSG_ADMIN_REQUEST_AGENT_HOWSERVER: dce_msg_admin_request_agent_howserver (); break; case DCE_MSG_ADMIN_REQUEST_ALL_KILLSERVER: dce_agent_delete_server (msg.aaddr); break; case DCE_MSG_ADMIN_REQUEST_AGENT_LISTSERVER: dce_msg_admin_request_agent_listserver (); break; case DCE_MSG_ADMIN_REQUEST_AGENT_SERONHOST: dce_msg_admin_requet_agent_seronhost (); break; case DCE_MSG_ADMIN_REQUEST_AGENT_MAXORDER: dce_msg_admin_request_agent_max_order (); break; case DCE_MSG_ADMIN_REQUEST_AGENT_CLEAR_CONNECT: case DCE_MSG_ADMIN_EXIT: case DCE_MSG_ADMIN_REQUEST_AGENT_DISCONNECT: Agent.AdminAddr = 0; Agent.AdminTime = -1; break; case DCE_MSG_ADMIN_REQUEST_AGENT_CONNECT: dce_msg_admin_request_agent_connect (); break; case DCE_MSG_ADMIN_REQUEST_AGENT_EXIST: dce_msg_admin_request_agent_exist (); break; case DCE_MSG_ADMIN_REQUEST_AGENT_KILLAGENT: case DCE_MSG_KILLALL: Agent.bExit = 1; break; } }}intdce_agent_init (int argc, char **argv){ int i, n; u_long u; if (argc < 7) return 0; atexit (dce_agent_exit); TIMEOUT_IAMALIVE= atoi (argv[7]); Agent.Msg.order = atoi (argv[6]); Agent.szDCE_ROOT = argv[1]; Agent.maddr = gethostaddr (argv[2]); Agent.mport = atoi (argv[3]); u = gethostaddr (argv[4]); i = atoi (argv[5]); Agent.Msg.who = AGENT; Agent.Msg.saddr = getmyaddr (); Agent.AdminAddr = 0; if (!(Agent.pListServer = NewList ())) return 0; if (!(Agent.pUserList = NewList ())) return 0; if (!(Agent.pService = NewList ())) return 0; if (!(Agent.pCurUserService = NewList ())) return 0; if ((Agent.nMSockSend = dce_mcastsocksend ()) < 0) return 0; if ((Agent.nMSockRecv = dce_mcastsockrecv (Agent.maddr, Agent.mport)) < 0) return 0; if ((Agent.nSocket = dce_socket (&Agent.Msg.sport)) < 0) return 0; if ((n = dce_connect (u, i)) > 0) { Agent.Msg.type = DCE_MSG_AGENT_REPLY_ADMIN_FORK_SUCCESS; write (n, (void *) &Agent.Msg, sizeof (MSG)); close (n); } else return 0; dce_signal (); if (!dce_timer_init ()) return 0; dce_agent_alive (NULL); Agent.Msg.type = DCE_MSG_AGENT_REQUEST_SERVER_BROADCASTDATA; dce_agent_mcastsend ((char *) &Agent.Msg, sizeof (MSG)); return 1;}voiddce_agent_exit (){ FreeList (Agent.pUserList, NULL); FreeList (Agent.pListServer, NULL); FreeList (Agent.pService, NULL); FreeList (Agent.pCurUserService, NULL); dce_timer_free (); dce_mcastcloserecv (Agent.nMSockRecv, Agent.maddr); close (Agent.nSocket); close (Agent.nMSockSend); _cleanup (); _exit ();}intdce_agent_mcastsend (char *buf, int len){ return dce_mcastsendsend (Agent.nMSockSend, Agent.maddr, Agent.mport, buf, len);}intdce_agent_mcastrecv (char *buf, int len){ return dce_mcastrecvrecv (Agent.nMSockRecv, Agent.maddr, Agent.mport, buf, len);}voiddce_agent_alive (void *p){ char c = Agent.Msg.type; Agent.Msg.type = DCE_MSG_IAMALIVE; dce_agent_mcastsend ((char *) &Agent.Msg, sizeof (MSG)); Agent.Msg.type = c; dce_settimer (TIMEOUT_IAMALIVE, dce_agent_alive, NULL); dce_agent_dcetime (); DecTimeUserService (Agent.pCurUserService); Agent.AdminTime -= TIMEOUT_IAMALIVE; if (Agent.AdminTime < 0) Agent.AdminAddr = 0;/*Modify code here */}voiddce_msg_admin_request_agent_exist (){ int n; if ((n = dce_connect (msg.saddr, msg.sport)) > 0) { Agent.Msg.type = DCE_MSG_AGENT_REPLY_ADMIN_EXIST; write (n, (void *) &Agent.Msg, sizeof (MSG)); close (n); }}voidTreaBothAgent (){ if (msg.saddr != Agent.Msg.saddr) dce_msg_server_request_agent_broadcastdata (); if (Agent.Msg.order > msg.order) exit (0);}voiddce_msg_admin_request_agent_connect (){ int n; if ((n = dce_connect (msg.saddr, msg.sport)) <= 0) return; if (Agent.AdminAddr == 0) { Agent.AdminAddr = msg.saddr; Agent.pid = msg.nextra; Agent.AdminTime = MAXTIME; Agent.Msg.type = DCE_MSG_AGENT_REPLY_ADMIN_ACCEPT_CONNECT; } else { Agent.Msg.aaddr = Agent.AdminAddr; Agent.Msg.nextra = Agent.AdminTime; Agent.Msg.type = DCE_MSG_AGENT_REPLY_ADMIN_REFUSE_CONNECT; } write (n, (void *) &Agent.Msg, sizeof (MSG)); close (n);}voiddce_msg_admin_request_agent_max_order (){ int n; Agent.Msg.type = DCE_MSG_AGENT_REPLY_ADMIN_MAXORDER; Agent.Msg.nextra = dce_agent_find_maxorder (); if ((n = dce_connect (msg.saddr, msg.sport)) <= 0) return; write (n, (void *) &Agent.Msg, sizeof (MSG)); close (n);}voiddce_msg_admin_requet_agent_seronhost (){ int n; if (dce_agent_findserverinlist (msg.aaddr)) Agent.Msg.type = DCE_MSG_AGENT_REPLY_ADMIN_SERVERREADYRUN; else Agent.Msg.type = DCE_MSG_AGENT_REPLY_ADMIN_NOSERVERONHOST; if ((n = dce_connect (msg.saddr, msg.sport)) <= 0) return; write (n, (void *) &Agent.Msg, sizeof (MSG)); close (n);}voiddce_msg_admin_request_agent_listserver (){ int n; if ((n = dce_connect (msg.saddr, msg.sport)) <= 0) return; Agent.Msg.type = DCE_MSG_AGENT_REPLY_ADMIN_SERVERINFO; dce_agent_writesinfosock (n); Agent.Msg.type = DCE_MSG_END_OF_TRANSACTION; write (n, (void *) &Agent.Msg, sizeof (MSG)); close (n);}voiddce_msg_admin_request_agent_howserver (){ int n; Agent.Msg.type = DCE_MSG_AGENT_REPLY_ADMIN_HOWSERVER; Agent.Msg.nextra = CountList (Agent.pListServer); if ((n = dce_connect (msg.saddr, msg.sport)) <= 0) return; write (n, (void *) &Agent.Msg, sizeof (MSG)); close (n);}voiddce_msg_admin_request_agent_listperuser (){ int n; if ((n = dce_connect (msg.saddr, msg.sport)) <= 0) return; Agent.Msg.type = DCE_MSG_AGENT_REPLY_ADMIN_USERINFO; dce_agent_writeuserlist (n); Agent.Msg.type = DCE_MSG_END_OF_TRANSACTION; write (n, (void *) &Agent.Msg, sizeof (MSG)); close (n);}voiddce_admin_request_agent_howserveralive (){ int n; Agent.Msg.type = DCE_MSG_AGENT_REPLY_ADMIN_HOWMANYSERVERALIVE; Agent.Msg.nextra = dce_agent_findhowserveralive (); if ((n = dce_connect (msg.saddr, msg.sport)) <= 0) return; write (n, (void *) &Agent.Msg, sizeof (MSG)); close (n);}voiddce_msg_server_request_agent_broadcastdata (){ Agent.Msg.type = DCE_MSG_ADMIN_REQUEST_ALL_ADDUSER; dce_agent_userbroadcast (); Agent.Msg.type = DCE_MSG_ADMIN_REQUEST_AGENT_ADDSERVICE; dce_agent_servicebroadcast (); dce_agent_userservicebroadcast ();}voiddce_msg_admin_request_agent_addservice (){ AddService (Agent.pService, msg.nextra, msg.sername);}voiddce_msg_admin_request_agent_listservice (){ int n; if ((n = dce_connect (msg.saddr, msg.sport)) <= 0) return; Agent.Msg.type = DCE_MSG_AGENT_REPLY_ADMIN_SERVICEINFO; dce_agent_writeservicelist (n); Agent.Msg.type = DCE_MSG_END_OF_TRANSACTION; write (n, (void *) &Agent.Msg, sizeof (MSG)); close (n);}voiddce_msg_client_request_agent_service (){ int n; MSG *pMsg; PSERVICEINFO ps; if (!dce_agent_findhowserveralive ()) Agent.Msg.type = DCE_MSG_AGENT_REPLY_CLIENT_NOSERVER; else if (!FindUid (Agent.pUserList, msg.uid)) Agent.Msg.type = DCE_MSG_AGENT_REPLY_CLIENT_USERNOTFOUND; else if (!(ps = (PSERVICEINFO) FindService (Agent.pService, msg.sername))) Agent.Msg.type = DCE_MSG_AGENT_REPLY_CLIENT_NOSERVICE; else if (ps->param > msg.nextra) Agent.Msg.type = DCE_MSG_ALL_REPLY_CLIENT_ERROR; else Agent.Msg.type = DCE_MSG_ALL_REPLY_CLIENT_PLEASEWAIT; if ((n = dce_connect (msg.saddr, msg.sport)) <= 0) return; write (n, (void *) &Agent.Msg, sizeof (MSG)); close (n); if (Agent.Msg.type != DCE_MSG_ALL_REPLY_CLIENT_PLEASEWAIT) return; if (!(pMsg = (MSG *) malloc (sizeof (MSG)))) return; memcpy (pMsg, &msg, sizeof (MSG)); dce_settimer (2 * TIMEOUT_IAMALIVE - 1, dce_agent_chooseserver, (void *) pMsg);}voiddce_agent_chooseserver (void *p){ int n; MSG *pMsg = (MSG *) p; if (!dce_agent_findhowserveralive ()) { if ((n = dce_connect (pMsg->saddr, pMsg->sport)) <= 0) return; memcpy (pMsg, &(Agent.Msg), sizeof (MSG)); pMsg->type = DCE_MSG_ALL_REPLY_CLIENT_ERROR; write (n, (void *) pMsg, sizeof (MSG)); close (n); return; } pMsg->type = DCE_MSG_AGENT_REQUEST_SERVER_SERVE_CLIENT; pMsg->aaddr = dce_agent_findminload (); dce_agent_mcastsend ((char *) pMsg, sizeof (MSG)); free (pMsg);}voiddce_msg_all_reply_client_pleasewait (){ PUSERSERVICE p = (PUSERSERVICE) FindUserService (Agent.pCurUserService, msg.pid, msg.saddr); if (p) { p->time = 2 * TIMEOUT_IAMALIVE + 1; p->status = 1; return; } if (p = (PUSERSERVICE) NewUserService (msg.pid, msg.saddr, msg.uid, msg.nextra, msg.aaddr, msg.lextra)) InsertList (Agent.pCurUserService, (void *) p);}voiddce_msg_admin_request_agent_listcuruserservice (){ int n; if ((n = dce_connect (msg.saddr, msg.sport)) <= 0) return; Agent.Msg.type = DCE_MSG_AGENT_REPLY_ADMIN_LISTCURUSERSERVICE; dce_agent_writecuruserservice (n); Agent.Msg.type = DCE_MSG_END_OF_TRANSACTION; write (n, (void *) &Agent.Msg, sizeof (MSG)); close (n);}voiddce_msg_service_reply_all_finish (){ PUSERSERVICE p = (PUSERSERVICE) FindUserService (Agent.pCurUserService, msg.pid, msg.saddr); if (p) { p->tEnd = msg.lextra1; p->time = -1; p->status = 0; return; }}voiddce_msg_all_reply_alluserservice (){ PUSERSERVICE p = (PUSERSERVICE) FindUserService (Agent.pCurUserService, msg.pid, msg.saddr); if (p) return; if (!(p = (PUSERSERVICE) NewUserService (msg.pid, msg.saddr, msg.uid, msg.nextra, msg.aaddr, msg.lextra))) return; p->tEnd = msg.lextra1; p->time = msg.sport; p->status = (p->time > 0); InsertList (Agent.pCurUserService, (void *) p);}voiddce_agent_treat_for_election (){ char c = Agent.Msg.type; Agent.Msg.type = DCE_MSG_IAMALIVE; dce_agent_mcastsend ((char *) &Agent.Msg, sizeof (MSG)); Agent.Msg.type = c;}void dce_msg_admin_request_agent_confirm_connect(){ int n; if ((n = dce_connect (msg.saddr, msg.sport)) <= 0) return; if ((Agent.AdminAddr == msg.saddr) && (Agent.pid == msg.nextra)) { Agent.Msg.type = DCE_MSG_AGENT_REPLY_ADMIN_ACCEPT_CONNECT; } else { Agent.Msg.aaddr = Agent.AdminAddr; Agent.Msg.nextra = Agent.AdminTime; Agent.Msg.type = DCE_MSG_AGENT_REPLY_ADMIN_REFUSE_CONNECT; } write (n, (void *) &Agent.Msg, sizeof (MSG)); close (n);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -