source: verona/phcpp/testphcpp.cpp @ 97:688b45026d07

Last change on this file since 97:688b45026d07 was 97:688b45026d07, checked in by Nikita Kozlov <nikita@…>, 3 years ago

adding subscribe/notify/publish

File size: 8.5 KB
Line 
1
2#include <string>
3#include <iostream>
4#include <time.h>
5#include <stdlib.h>
6#include <string.h>
7#include <cstdio>
8
9#ifdef _MSC_VER
10#include <windows.h>
11#define sleep(x) Sleep((x)*1000)
12#define snprintf _snprintf
13#endif
14
15using namespace std;
16
17#include "phapipp.h"
18
19
20int vl1, vl2, cid1, cid2;
21string gChatRoom;
22
23
24string extract_uri(const char *s)
25{
26  string tmp = s;
27  int lt = tmp.find('<');
28  int gt = tmp.find('>');
29 
30  return tmp.substr(lt+1, gt-lt-1);
31
32}
33
34class myphapi:public verona::stdphapi
35{
36
37  static const char *nz(const char *s) { return s ? s : ""; }
38
39  void  onRegProgress(int rid, int status)
40  {
41    printf("Reg: %d Status: %d\n", rid, status);
42  }
43
44
45  virtual void onMsgProgress(int mid,  const phMsgStateInfo_t *info)
46  {
47    if (info->event == phMsgNew)
48      {
49        printf("Got Message: to=%s from=%s type=%s\n======\n%s=======\n",
50               info->to, info->from, info->rawctt, info->content);
51      }
52  }
53
54  virtual void onNotifyProgress (const char* event, const char* from, const char *ctt, const char* content)
55  {
56    printf("Got notification: from=%s evt=%s ctt=%s content:\n====\n%s\n====\n", from, event, ctt, content);
57  }
58
59
60  void onCallProgress(int cid, const phCallStateInfo_t *info)
61  {
62    switch (info->event)
63      {
64      case phDIALING:
65        printf("DIALING line=%d cid=%d uri=%s\n", info->vlid, cid, info->u.remoteUri);
66        break;
67
68      case phRINGING:
69        printf("RINGING cid=%d uri=%s\n", cid, info->u.remoteUri);
70        break;
71
72      case phRINGandSTART:
73        printf("RINGINGandSTART cid=%d uri=%s\n", cid, info->u.remoteUri);
74        break;
75
76      case phRINGandSTOP:
77        printf("RINGandSTOP cid=%d uri=%s\n", cid, info->u.remoteUri);
78        break;
79
80
81      case phNOANSWER:
82        printf("NOANSWER cid=%d uri=%s\n", cid, info->u.remoteUri);
83        break;
84
85      case phCALLBUSY:
86        printf("BUSY cid=%d uri=%s\n", cid, info->u.remoteUri);
87        break;
88
89      case phCALLREDIRECTED:
90        printf("REDIRECTED cid=%d newcid=%d to=%s\n", cid, info->newcid, info->u.remoteUri);
91        break;
92
93      case phCALLOK:
94        printf("CALLOK cid=%d uri=%s\n", cid, info->u.remoteUri);
95        break;
96
97      case phCALLHELD:
98        printf("CALLHELD cid=%d  status=%d\n", cid, info->u.errorCode);
99        break;
100
101      case phCALLRESUMED:
102        printf("CALLRESUMED cid=%d  status=%d\n", cid, info->u.errorCode);
103        break;
104
105      case phHOLDOK:
106        printf("HOLDOK cid=%d  status=%d\n", cid, info->u.errorCode);
107        break;
108
109      case phRESUMEOK:
110        printf("RESUMEOK cid=%d  status=%d\n", cid, info->u.errorCode);
111        break;
112
113      case phINCALL:
114        printf("INCALL line=%d cid=%d to=%s from=%s streams=%x callinfo=%s\n", info->vlid, cid, 
115               info->localUri, info->u.remoteUri, info->streams, nz(info->callinfo));
116       
117        if (info->streams == PH_STREAM_DATA)
118          {
119            acceptCall(cid, 0, PH_STREAM_DATA);
120            cid2 = cid;
121          }
122
123        break;
124
125      case phCALLCLOSED:
126        printf("CALLCLOSED cid=%d  status=%d\n", cid, info->u.errorCode);
127        break;
128
129
130      case phCALLCLOSEDandSTOPRING:
131        printf("CALLCLOSEDandSTOPRING cid=%d  status=%d\n", cid, info->u.errorCode);
132        break;
133
134      case phCALLERROR:
135        printf("CALLERROR cid=%d  status=%d\n", cid, info->u.errorCode);
136        break;
137
138      case phDTMF:
139        printf("DTMF cid=%d  digit=%c\n", cid, info->u.dtmfDigit);
140        break;
141
142      case phXFERREQ:
143        printf("XFERREQ line=%d cid=%d newcid=%d to=%s", info->vlid, cid, info->newcid, info->u.remoteUri);
144        break;
145
146      case phXFERPROGRESS:
147        printf("XFERPROGRESS cid=%d status=%d\n", cid, info->u.errorCode);
148        break;
149
150      case phXFEROK:
151        printf("XFEROK cid=%d status=%d\n", cid, info->u.errorCode);
152        break;
153
154      case phXFERFAIL:
155        printf("XFERFAIL cid=%d status=%d\n", cid, info->u.errorCode);
156        break;
157
158      case phCALLREPLACED:
159        printf("CALLREPLACED cid=%d newcid=%d uri=%s streams=%x, callinfo=%s\n", cid, info->newcid, 
160               info->u.remoteUri, info->streams, nz(info->callinfo));
161
162        gChatRoom = extract_uri(info->u.remoteUri);
163        cid1 = info->newcid;
164
165        phCloseCall(cid);
166        break;
167
168      }
169
170    fflush(stdout);
171
172   
173  }
174
175};
176
177
178extern myphapi  api;
179
180
181static char * 
182maketimestamp(char buf[], int bsize)
183{
184    struct tm tmbuf;
185    time_t  now;
186    char  tmstr[64];
187
188    now = time(0);
189    tmbuf = *localtime(&now);
190
191    strftime(tmstr, 64, "%d-%b-%Y %I:%M:%S", &tmbuf);
192       
193    snprintf(buf, bsize, "%s.%03d %s", 
194             tmstr, now % 1000, 
195             ((tmbuf.tm_hour < 12) ? "AM" : "PM"));
196
197    return buf;
198}
199
200
201std::string gChatUri = "sip:CHAT@comverse.com";
202
203void sendSessionMessage(int vlid, const std::string &chatroom, const char *from, const char *message)
204{
205    char mid[64];
206    char cpimbuf[4096];
207    char timestamp[128];
208
209
210    maketimestamp(timestamp, sizeof(timestamp));
211
212    sprintf(mid, "%d", time(0));
213
214    snprintf(cpimbuf, sizeof(cpimbuf), 
215             "To: %s\r\n"
216             "From: %s\r\n"
217             "DateTime: %s\r\n"
218             "Subject: \r\n"
219             "Expires: %d\r\n"
220             "imdn.Message-ID: %s\r\n"
221             "imdn.Disposition-Notification: \r\n"
222             "Content-Type: text/html\r\n"
223             "\r\n",
224             chatroom.c_str(), from, timestamp, 3600, mid);
225
226    ::strcat(cpimbuf, message);
227
228    api.sendMessage(vlid, gChatUri, cpimbuf,  "message/cpim", gChatUri);
229 
230}
231
232
233
234const char * 
235getMobilityString(int vlid)
236{
237  switch(api.mobility(vlid))
238    {
239    case PH_LINE_MOBILITY_FIXED:
240      return "fixed";
241
242    case PH_LINE_MOBILITY_MOBILE:
243      return "mobile";
244
245    default:
246    case PH_LINE_MOBILITY_DEFAULT:
247      return "";
248    }
249
250}
251
252void remove_port(std::string& s)
253{
254  int colon = s.rfind(':');
255  int at = s.find('@');
256
257  if (colon > at && colon != s.npos)
258    s.replace(colon, s.length() - colon, "");
259
260}
261
262std::string
263getEntity(int vlid)
264{
265  std::string tmp = api.getSipAddress(vlid);
266  std::string sa = extract_uri(tmp.c_str());
267
268  remove_port(sa);
269  sa.replace(0,4, ""); // remove "sip:"
270  return sa;
271 
272}
273#define PUBLISH_MSG_TEMPLATE_WITH_DEVTYPE "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
274<presence xmlns=\"urn:ietf:params:xml:ns:pidf\"\n entity=\"%s\">\n\
275<tuple id=\"%s%s\">\n\
276<status><basic>%s</basic></status>\n\
277<activities>%s</activities>\n\
278<note>%s</note>\n\
279<contact priority=\"1\">%s</contact>\n\
280<time>%u</time>\n\
281</tuple>\n\
282</presence>\n"
283
284void publishPresence(int vlid, bool status,const std::string& activity, int timeout) 
285{
286
287    char *body = new char[4*1024];
288    std::string prefix = getMobilityString(vlid);
289    std::string uri = getEntity(vlid);
290    std::string realAct = activity;
291    const char *mood="1";
292
293 
294    if (prefix.length())
295      prefix += ".";
296
297    snprintf(body, 4*1024, PUBLISH_MSG_TEMPLATE_WITH_DEVTYPE, 
298             uri.c_str(), 
299             prefix.c_str(), 
300             uri.c_str(), 
301             (status ? "open" : "closed"),
302             realAct.c_str(),
303             mood,
304             uri.c_str(), 
305             time(0));
306
307
308    api.publish(vlid, "sip:" + uri, "presence" , "application/pidf+xml", body, timeout);
309
310    delete [] body;
311
312
313}
314
315
316
317
318void joinServerChat(const std::string& chatroom, int vlid, int cid)
319{
320  std::string cidstr  = api.getSipCallID(cid);
321
322  api.sendMessage(vlid, chatroom, "Join", "text/plain", gChatUri, cidstr);
323}
324 
325
326
327
328
329
330
331
332myphapi api;
333
334
335
336int main(int argc,  char *argv[])
337{
338
339  cout << "Intializing and creating virtual lines " << endl; 
340  api.init();
341 
342  api.addCustomHeader("INVITE", "Expires", "3600");
343
344  api.addAuthInfo(argv[1], argv[1], argv[2], NULL);
345 
346  vl1 = api.addVline(argv[1], argv[1], "voip.mbdsys.com", "voip.mbdsys.com:6060", 600);
347
348  string s;
349
350
351 // cout << "Placeing data call " << endl;
352 // cin >> s;
353
354  // place data call
355
356  ph_config_t* conf = api.getConfig();
357  strcpy(conf->audio_codecs, "PCMA,PCMU");
358  strcpy(conf->local_rtp_port, "27000");
359  /*
360  cid1 = api.placeCall(vl1, "sip:10001@sfrdroid", 0, 0, PH_STREAM_AUDIO, 0, "voip.mbdsys.com");
361  sleep(5);
362
363  cout << "netc1 joining chatroom: " << endl;
364  cin >> s;
365
366  joinServerChat(gChatRoom, vl1, cid1);
367  sleep(3);
368
369  cout << "netc2 joining chatroom: " << endl;
370  cin >> s;
371
372  joinServerChat(gChatRoom, vl2, cid2);
373  sleep(3);
374
375  cout << "netc1 sending message: " << endl;
376  cin >> s;
377
378  sendSessionMessage(vl1, gChatRoom, "sip:netc1@comverse.com", "hello you");
379  sleep(3);
380*/
381 // cout << "Closing data calls" << endl;
382//  cin >> s;
383
384//  api.closeCall(cid1);
385//  api.closeCall(cid2);
386
387  cout << "subscribing to nikita presence"  << endl;
388    cin >> s;
389
390    api.subscribe(vl1, "sip:nikita@voip.mbdsys.com:6060", PH_SUBS_PRESENCE);
391    sleep(3);
392    publishPresence(vl1, true, "online", 300);
393return 0;
394  sleep(3);
395
396  cout << "subscribing to netc2 presence"  << endl;
397  cin >> s;
398
399  api.subscribe(vl1, "sip:netc2@comverse.com", PH_SUBS_PRESENCE);
400  sleep(3);
401
402  cout << "Publishing netc2 presence"  << endl;
403  cin >> s;
404
405  publishPresence(vl2, true, "online", 300);
406  sleep(3);
407
408  cout << "Publishing changed netc2 presence"  << endl;
409  cin >> s;
410
411  publishPresence(vl2, true, "busy", 300);
412  sleep(3);
413
414
415  cout << "Terminating" << endl;
416  cin >> s;
417
418  api.terminate();
419}
Note: See TracBrowser for help on using the repository browser.