File: xIrcMsgDispatch.cpp

package info (click to toggle)
xirc 2.0-3
  • links: PTS
  • area: contrib
  • in suites: hamm, slink
  • size: 2,624 kB
  • ctags: 2,093
  • sloc: cpp: 21,094; makefile: 1,033
file content (160 lines) | stat: -rw-r--r-- 5,408 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
** Copyright (C) 1995, 1996  Joseph Croft <jcroft@unicomp.net>  
** 
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 1, or (at your option)
** any later version.
** 
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
** 
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "ircreply.h"
#include "xIrcMsgDispatch.h"

static int dbg = 0;

xIrcMsgDispatch::xIrcMsgDispatch()
{
   if (dbg) fprintf(stdout, "xIrcMsgDispatch::xIrcMsgDispatch():Enter\n");
   pSocket = NULL;
   if (dbg) fprintf(stdout, "xIrcMsgDispatch::xIrcMsgDispatch():Exit\n");
}

xIrcMsgDispatch::~xIrcMsgDispatch()
{
}

void xIrcMsgDispatch::setSocket(xIrcSocket *s)
{
   pSocket = s;
   if (pSocket == NULL && dspList.count() > 0)
      dspList.clear();      
}

bool xIrcMsgDispatch::outstanding(xIrcMessage *pMsg)
{
   bool rv = FALSE;
   xIrcMsgDispatchListIterator it(dspList);
   xIrcMsgDispatchEntry *pEnt;

   for (; (pEnt = it.current()) != NULL; ++it)
   {
      if (pEnt->sent() && pEnt->message()->rspCode == pMsg->rspCode)
      {
         rv = TRUE;
         break;
      }
   }
   return(rv);
}
   
void xIrcMsgDispatch::dispatchMsg(QObject *pObj, const char *pSlot, xIrcMessage *pMsg)
{
   xIrcMsgDispatchEntry *pEnt;
   int accepted;

   if (dbg) fprintf(stdout, "xIrcMsgDispatch::dispatchMsg():Enter\n");
   if (dbg) fflush(stdout);
   pEnt = new xIrcMsgDispatchEntry(pObj, pSlot, pMsg, accepted);
   if (dbg) fprintf(stdout, "xIrcMsgDispatch::dispatchMsg():accepted = %d\n", accepted);
   if (dbg) fflush(stdout);
   if (accepted == 1)
   {
      if (dbg) fprintf(stdout, "xIrcMsgDispatch::dispatchMsg():Adding Message to list\n");
      if (dbg) fflush(stdout);
      dspList.append(pEnt);
   }
   if (pSocket != NULL && (outstanding(pMsg) == FALSE || accepted == 0))
   {
      if (dbg) fprintf(stdout, "xIrcMsgDispatch::dispatchMsg():Sending Message\n");
      if (dbg) fflush(stdout);
      pEnt->setSent(TRUE);
      pSocket->sendIrcServerMessage(pMsg);
   }
   if (accepted <= 0)
   {
      if (dbg) fprintf(stdout, "xIrcMsgDispatch::dispatchMsg():Deleting Dispatch Entry\n");
      if (dbg) fflush(stdout);
      delete pEnt;
   }
   if (dbg) fprintf(stdout, "xIrcMsgDispatch::dispatchMsg():Exit- List Count = %d\n",
                            dspList.count());
   if (dbg) fflush(stdout);
}

bool xIrcMsgDispatch::proccessResponse(xIrcMessage *pMsg)
{
   xIrcMsgDispatchListIterator it(dspList);
   xIrcMsgDispatchEntry *pEnt, *pEnt1;

   if (dbg) fprintf(stdout, "xIrcMsgDispatch::proccessResponse():Enter\n");
   if (dbg) fflush(stdout);
   if (pMsg->rspCode >= 400 && pMsg->rspCode < 600)
   {
      if (dbg) fprintf(stdout, "xIrcMsgDispatch::proccessResponse():Have Error Response %d\n",
                              pMsg->rspCode);
      if (dbg) fflush(stdout);
      for (; (pEnt = it.current()) != NULL; ++it)
      {
         if (pEnt->sent() == TRUE)
         {
            if (dbg) fprintf(stdout, "xIrcMsgDispatch::proccessResponse():Dispatching error to first entry in list\n");
            if (dbg) fflush(stdout);
            pEnt->dispatchResponse(pMsg);
            if (dbg) fprintf(stdout, "xIrcMsgDispatch::proccessResponse():Removing 1st entry from list\n");
            if (dbg) fflush(stdout);
            dspList.remove(pEnt);
            break;
         }
      }
      if (dbg) fprintf(stdout, "xIrcMsgDispatch::proccessResponse():Exit- List Count = %d\n",
                               dspList.count());
      if (dbg) fflush(stdout);
      return(FALSE);
   }
      
   for (; (pEnt = it.current()) != NULL; ++it)
   {
      if (pEnt->sent() == TRUE && pEnt->is(pMsg))
      {
         if (pEnt->dispatchResponse(pMsg) == FALSE)
         {
            if (dbg) fprintf(stdout, "xIrcMsgDispatch::proccessResponse():Removing Entry from List\n");
            if (dbg) fflush(stdout);
            dspList.remove(pEnt);
            if (pSocket)
            {
               if (dbg) fprintf(stdout, "xIrcMsgDispatch::proccessResponse():Checking for next item in list to send\n");
               if (dbg) fflush(stdout);
               for (it.toFirst(); (pEnt1 = it.current()) != NULL; ++it)
               {
                  if (pEnt1->sent() == FALSE && outstanding(pEnt->message()) == FALSE)
                  {
                     if (dbg) fprintf(stdout, "xIrcMsgDispatch::proccessResponse():Found one! Sending it\n");
                     if (dbg) fflush(stdout);
                     pEnt1->setSent(TRUE);
                     pSocket->sendIrcServerMessage(pEnt1->message());
                     break;
                  }
               }
            }
         }
         break;
      }
   }
   if (dbg) fprintf(stdout, "xIrcMsgDispatch::proccessResponse():Exit- List Count = %d\n",
                            dspList.count());
   if (dbg) fflush(stdout);
   if (pEnt != NULL && pEnt->dispatch() == FALSE)
     return(FALSE);
   else
      return((pEnt == NULL) ? FALSE : TRUE);
}