add .gitignore
[remotetop.git] / td_lmsg.c
1 /*
2  * Copyright (c) 2004 Teodor Sigaev <teodor@sigaev.ru>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *      notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *      notice, this list of conditions and the following disclaimer in the
12  *      documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the author nor the names of any co-contributors
14  *      may be used to endorse or promote products derived from this software
15  *      without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "tlog.h"
33 #include "connection.h"
34 #include "top.h"   
35 #include "tmalloc.h"
36
37 static void 
38 getTopData(TC_Connection *conn) {
39         TCMsg *pmsg;
40
41         conn->len = TCMSGHDRSZ + MUHDRSZ + cfg.mu->number * sizeof(UnitInfo);
42         conn->buf = trealloc( conn->buf, conn->len );
43         pmsg = conn->buf;
44
45         pmsg->len = conn->len;
46         pmsg->type = TOPGETTYPE;
47         memcpy( pmsg->data, cfg.mu, pmsg->len - TCMSGHDRSZ );
48         ((MassiveUnit*)pmsg->data)->maxnumber = cfg.mu->number;
49         
50         TC_Send(conn);   
51 }
52         
53 static void 
54 gotLongMessage( TC_Connection *conn ) {
55         TCMsg *pmsg = conn->buf;
56
57         if ( conn->ptr -(char*) conn->buf < TCMSGHDRSZ ) {
58                 tlog(TL_ALARM,"Wrong size of long message (%d bytes)", conn->ptr - (char*)conn->buf);
59                 conn->state = CS_ERROR;
60                 return;
61         }
62         
63         switch(pmsg->type) {
64                 case TOPGETTYPE:
65                         getTopData(conn);
66                         break;
67                 default:
68                         tlog(TL_ALARM,"Got unknown packet type: %u (%08x)", pmsg->type, pmsg->type);
69                         conn->state = CS_ERROR;
70         }
71 }
72
73 static void
74 sendedLongMessage( TC_Connection *conn ) {
75         TCMsg *pmsg = conn->buf;
76         switch(pmsg->type) {
77                 case TOPGETTYPE:
78                         conn->state = CS_FINISH;
79                         break;
80                 default:
81                         tlog(TL_ALARM,"Send unknown packet type: %u(0x%08x)", pmsg->type, pmsg->type);
82                         conn->state = CS_ERROR;
83         }
84 }
85
86 void
87 ConnectionWorks() {
88         int i;
89         TC_Connection *conn;
90         for(i=TC_SERVER_CONN_START; i<cfg.pool.number; i++) {
91                 conn = cfg.pool.conn[i];
92                 switch( conn->state ) {
93                         case CS_INPROCESS:
94                                 TC_ServerConnect( conn, 0 );
95                                 break;
96                         case CS_CONNECTED: /* we have connected to remote host */
97                                 TC_Send( conn );
98                                 break;
99                         case CS_FINISHSEND:
100                                 sendedLongMessage( conn );
101                                 break;
102                         case CS_FINISHREAD:
103                                 gotLongMessage( conn );
104                                 break;
105                         case CS_READ:
106                                 if ( conn->readyio ) {
107                                         TC_Read( conn, TCMSGHDRSZ );
108                                         if ( conn->state != CS_READ ) i--; /* check it one more time */ 
109                                 }
110                                 break;
111                         case CS_SEND:
112                                 if ( conn->readyio ) {
113                                         TC_Send( conn );
114                                         if ( conn->state != CS_SEND ) i--; /* check it one more time */ 
115                                 }
116                                 break;
117                         /* ignore */
118                         case CS_OK: 
119                         case CS_AGAIN:
120                         case CS_WAIT: /* ??? */
121                                 break;
122                         /* out */
123                         case CS_TIMEOUT:
124                         case CS_CLOSED:
125                         case CS_NOTINITED:
126                         case CS_FINISH:
127                         case CS_ERROR:
128                         default:
129                                 tlog(TL_DEBUG, "Clear N%d connection", i);
130                                 TC_deleteConnectionByN(&cfg.pool,i);
131                                 i--;
132                 }
133         }
134 }