Read about our upcoming Code of Conduct on this issue

URpcDecoderCommon.cpp 14.2 KB
Newer Older
1
2
3
4
5
6
7
8
#ifndef URpcDecoderCommon_CPP
#define URpcDecoderCommon_CPP

#include "../../Deploy/Include/rdk_cpp_initdll.h"
#include "../../Deploy/Include/rdk_rpc.h"
#include "../../Deploy/Include/myrdk.h"
#include "URpcDecoderCommon.h"
#include "URpcCommandInternal.h"
9
#include "UApplication.h"
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

namespace RDK {

// --------------------------
//   
// --------------------------
URpcDecoderCommon::URpcDecoderCommon(void)
{

}

URpcDecoderCommon::~URpcDecoderCommon(void)
{

}
// --------------------------

// --------------------------
//   
// --------------------------
///    
URpcDecoderCommon* URpcDecoderCommon::New(void)
{
 return new URpcDecoderCommon;
}

/// ,    
/// ,        false
bool URpcDecoderCommon::IsCmdSupported(const UEPtr<URpcCommand> &command) const
{
 if(!command || !command->IsDecoded)
  return false;

 std::string cmd=command->FunctionName;
 if(cmd == "RpcPing")
 {
  return true;
 }
 else
 if(cmd == "SetNumChannels")
 {
  return true;
 }
 else
 if(cmd == "GetNumChannels")
 {
  return true;
 }
 else
 if(cmd == "GetServerName")
 {
  return true;
 }
 else
 if(cmd == "GetServerId")
 {
  return true;
 }
 else
 if(cmd == "GetChannelName")
 {
  return true;
 }
 else
 if(cmd == "SetChannelName")
 {
  return true;
 }
 else
 if(cmd == "GetChannelVideoSource")
 {
  return true;
 }
 else
 if(cmd == "SetChannelVideoSource")
 {
  return true;
 }
 else
 if(cmd == "CheckChannelVideoSourceConnection")
 {
  return true;
 }
 else
 if(cmd == "GetChannelBroacaster")
 {
  return true;
 }
 else
 if(cmd == "SetChannelBroadcaster")
 {
  return true;
 }
 else
 if(cmd == "ResetChannel")
 {
  return true;
 }
 else
 if(cmd == "StartChannel")
 {
  return true;
 }
 else
 if(cmd == "StopChannel")
 {
  return true;
 }
 else
 if(cmd == "RegisterMetadataReceiver")
 {
  return true;
 }
 else
 if(cmd == "UnRegisterMetadataReceiver")
 {
  return true;
 }
 else
 if(cmd == "LoadProject")
 {
131
  return true;
132
133
134
135
 }
 else
 if(cmd == "SaveProject")
 {
136
  return true;
137
138
139
140
141
142
143
144
145
146
147
148
 }
 else
 if(cmd == "ReadImageFromVideoSource")
 {
  return true;
 }
 else
 if(cmd == "StartVideoSource")
 {
  return true;
 }
 else
149
150
151
152
153
154
155
156
157
 if(cmd == "DeployProject")
 {
  return true;
 }
 else
 if(cmd == "GetDeploymentState")
 {
  return true;
 }
158
159
160
161
162
 else
 if(cmd == "GetLastError")
 {
  return true;
 }
Aivan_2008's avatar
Aivan_2008 committed
163
 else
Aivan_2008's avatar
Aivan_2008 committed
164
165
166
167
168
 if(cmd == "PrepareProject")
 {
  return true;
 }
 else
169
170
171
172
173
 if(cmd == "GetPreparationResult")
 {
  return true;
 }
 else
Aivan_2008's avatar
Aivan_2008 committed
174
 if(cmd == "OpenPreparedProject")
Aivan_2008's avatar
Aivan_2008 committed
175
176
177
 {
  return true;
 }
Aivan_2008's avatar
Aivan_2008 committed
178
179
 else
 if(cmd == "RunPreparedProject")
180
181
182
183
 {
  return true;
 }
 else
Aivan_2008's avatar
Aivan_2008 committed
184
 if(cmd == "GetCalculationState")
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
 {
  return true;
 } 
 else
 if(cmd == "FinishCalculation")
 {
  return true;
 }
 else
 if(cmd == "UploadCalculationResults")
 {
  return true;
 }
 else
 if(cmd == "GetUploadState")
 {
  return true;
 }
 else
 if(cmd == "CloseSolver")
205
206
 {
  return true;
Aivan_2008's avatar
Aivan_2008 committed
207
 }
208
 else
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
 if(cmd == "StopVideoSource")
  return true;

 return false;
}

///        
///  false    
bool URpcDecoderCommon::AProcessCommand(const UEPtr<URpcCommand> &command)
{
 if(!command)
 {
  //  -   
  return false;
 }

 UEPtr<URpcCommandInternal> cmd=dynamic_pointer_cast<URpcCommandInternal>(command);
 if(!cmd)
 {
  //  -    
229
  MLog_LogMessage(command->ChannelIndex, RDK_EX_WARNING, (std::string("RPC Common Decoder : Command internal structure not supported. CmdId=")+sntoa(command->GetCmdId())+std::string(" Command=")+command->FunctionName).c_str());
230
231
232
233
234
235
236
237
238
239
240
241
242
243
  return false;
 }

 const char* response=RemoteCall(cmd->Request.c_str(), cmd->ResponseStatus, cmd->ChannelIndex);

 if(response)
  cmd->Response=response;
 else
  cmd->Response=RDK::sntoa(cmd->ResponseStatus);

 MEngine_FreeBufString(cmd->ChannelIndex, response);

 if(cmd->ResponseStatus == 2001)
 {
244
  MLog_LogMessage(command->ChannelIndex, RDK_EX_WARNING, (std::string("RPC Common Decoder: Unknown command. CmdId=")+sntoa(command->GetCmdId())+std::string(" Command=")+command->FunctionName).c_str());
245
246
247
248
249
250
251
  return false;
 }

 cmd->IsProcessed=true;
 return true;
}

252
const char* URpcDecoderCommon::RemoteCall(const char *request, int &return_value, int &res_channel_index)
253
254
255
256
257
258
259
{
 return_value=INT_MAX;

 RDK::USerStorageXML xml,xml_data;

 xml.Load(request,"RpcRequest");

260
 int channel_index=-1;
261
262
 std::string cmd;
 std::string component_name;
263
 res_channel_index=0;
264
265
266
267
268
269
270
271


 if(!ExtractCmd(xml,cmd) || cmd.empty())
 {
  return_value=2000;
  return 0;
 }

272
 if(!ExtractCC(xml,channel_index,component_name))
273
 {
274
  ExtractChannel(xml,channel_index);
275
276
277
  ExtractComponent(xml,component_name);
 }
  /*
278
 if((channel_index < 0 || channel_index >= GetNumEngines()) &&
279
280
281
282
283
284
	(cmd != "GetNumChannels" && cmd !="SetNumChannels"))
 {
  return_value=2001;
  return 0;
 }  */

285
 res_channel_index=channel_index;
286
287
288
289
290
291
292
293
294
295
296
 std::string response,temp;
 std::string name;

 RDK::USerStorageXML result;
 result.Create("RpcResponse");
 result.WriteString("Id", xml.ReadString("Id",""));
// if(cmd == "EngineInit")
// {
// }
// else
// {
297
 response=ARemoteCall(cmd,xml,component_name, channel_index, return_value);
298
299
300
301
302
303
304
305
306
 if(return_value == 2001)
 {
  if(cmd == "RpcPing")
  {
   return_value=0;
  }
  else
  if(cmd == "SetNumChannels")
  {
307
308
   int num_engines=xml.ReadInteger("NumChannels",Core_GetNumChannels());
   if(GetApplication()->SetNumChannels(num_engines))
309
310
311
312
313
314
315
	return_value=0;//UServerControlForm->SetNumChannels(num_engines);
   else
	return_value=112112;//UServerControlForm->SetNumChannels(num_engines);
  }
  else
  if(cmd == "GetNumChannels")
  {
316
   response=RDK::sntoa(GetApplication()->GetNumChannels());
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
   return_value=0;
  }
  else
  if(cmd == "GetServerName")
  {
   response=GetApplication()->GetServerControl()->GetServerName();
   return_value=0;
  }
  else
  if(cmd == "GetServerId")
  {
   response=GetApplication()->GetServerControl()->GetServerId();
   return_value=0;
  }
  else
  if(cmd == "GetChannelName")
  {
334
   response=GetApplication()->GetProjectConfig().ChannelsConfig[channel_index].ChannelName;
335
336
337
338
339
340
341
342
   return_value=0;
  }
  else
  if(cmd == "SetChannelName")
  {
   std::string ch_name=xml.ReadString("Name","");
   if(!ch_name.empty())
   {
Aleksandr Bakhshiev's avatar
Aleksandr Bakhshiev committed
343
	TProjectConfig config=GetApplication()->GetProjectConfig();
344
	config.ChannelsConfig[channel_index].ChannelName=ch_name;
Aleksandr Bakhshiev's avatar
Aleksandr Bakhshiev committed
345
	GetApplication()->SetProjectConfig(config);
346
347
348
349
350
351
   }
   return_value=0;
  }
  else
  if(cmd == "ResetChannel")
  {
352
   GetApplication()->ResetChannel(channel_index);
353
354
355
356
357
   return_value=0;
  }
  else
  if(cmd == "StartChannel")
  {
358
   GetApplication()->StartChannel(channel_index);
359
360
361
362
363
   return_value=0;
  }
  else
  if(cmd == "StopChannel")
  {
364
   GetApplication()->PauseChannel(channel_index);
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
   return_value=0;
  }
  else
  if(cmd == "RegisterMetadataReceiver")
  {
   string address=xml.ReadString("Address","");
   int port=xml.ReadInteger("Port",8888);
   return_value=GetApplication()->GetServerControl()->RegisterMetadataReceiver(address, port);
  }
  else
  if(cmd == "UnRegisterMetadataReceiver")
  {
   string address=xml.ReadString("Address","");
   int port=xml.ReadInteger("Port",1000);
   return_value=GetApplication()->GetServerControl()->UnRegisterMetadataReceiver(address, port);
  }
  else
  if(cmd == "LoadProject")
  {
   std::string file_name=xml.ReadString("FileName","");
   if(!file_name.empty())
   {
387
388
389
       GetApplication()->OpenProject(file_name);
       RDK::UIVisualControllerStorage::UpdateInterface(true);
       return_value=0;//UServerControlForm->LoadProject(channel_index,file_name);
390
391
392
393
394
   }
  }
  else
  if(cmd == "SaveProject")
  {
395
396
   GetApplication()->SaveProject();
   return_value=0;//UServerControlForm->SaveProject();
397
  }
398
399
400
401
402
403
404
405
406
407
408
409
410
411
  else
  if(cmd == "DeployProject")
  {
   int task_id=xml.ReadInteger("TaskId",-1);
   int resp = GetApplication()->GetProjectDeployer()->StartProjectDeployment(task_id);
   std::stringstream ss;
   ss<<resp;
   response = ss.str();
   return_value=0;
  }
  else
  if(cmd == "GetDeploymentState")
  {
   //GetApplication()->SaveProject();
412
   int d_state = GetApplication()->GetProjectDeployer()->GetDeploymentState();
413
414
415
416
   int stage_max = GetApplication()->GetProjectDeployer()->GetStageCap();
   int stage_pos = GetApplication()->GetProjectDeployer()->GetStageProgress();

   std::stringstream ss;
417
   ss<<d_state<<" "<<stage_pos<<" "<<stage_max;
418
419
420
   response=ss.str();
   return_value=0;//UServerControlForm->SaveProject();
  }
421
422
423
424
  else
  if(cmd == "GetLastError")
  {
   std::string last_error=GetApplication()->GetProjectDeployer()->GetLastError();
Aivan_2008's avatar
Aivan_2008 committed
425
426
427
428
   response=last_error;
   return_value=0;
  }
  else
Aivan_2008's avatar
Aivan_2008 committed
429
  if(cmd == "PrepareProject")
Aivan_2008's avatar
Aivan_2008 committed
430
  {
Aivan_2008's avatar
Aivan_2008 committed
431
    std::string resp="";
432
    int rs = GetApplication()->GetProjectDeployer()->PrepareProject(resp);
Aivan_2008's avatar
Aivan_2008 committed
433
434
435
436
    response=resp;
    return_value=0;
  }
  else
437
438
439
440
441
442
443
444
445
446
  if(cmd == "GetPreparationResult")
  {
    std::string resp="";
    int rs = GetApplication()->GetProjectDeployer()->GetPreparationResult(resp);
    std::stringstream ss;
    ss<<rs<<"|"<<resp.c_str();
    response=ss.str();
    return_value=0;
  }
  else
Aivan_2008's avatar
Aivan_2008 committed
447
448
449
450
451
452
  if(cmd=="OpenPreparedProject")
  {
    std::string resp="";
    int rs = GetApplication()->GetProjectDeployer()->OpenPreparedProject(resp);
    response=resp;
    return_value=0;
453
  }
454
  else
Aivan_2008's avatar
Aivan_2008 committed
455
  if(cmd=="RunPreparedProject")
456
  {
Aivan_2008's avatar
Aivan_2008 committed
457
458
459
460
461
    int rs = GetApplication()->GetProjectDeployer()->RunPreparedProject();
    std::stringstream ss;
    ss<<rs;
    response = ss.str();
    return_value=0;
462
463
  }
  else
Aivan_2008's avatar
Aivan_2008 committed
464
  if(cmd == "GetCalculationState")
465
  {
Aivan_2008's avatar
Aivan_2008 committed
466
467
468
469
470
471
472
473
474
475
476
477
478
    /*std::string resp="";
    int rs = GetApplication()->GetProjectDeployer()->GetPreparationResult(resp);
    std::stringstream ss;
    ss<<rs<<"|"<<resp.c_str();
    response=ss.str();*/
    //  :
    //"<calculation_state>|<capture_state>|<capture_frid>|<capture_maxfrid>|<capture_finished>|<message>"
    int calculation_state=-1;
    int capture_state=-1;
    int capture_frid=-1;
    int capture_maxfrid=-1;
    std::string message = "test state message";

479

Aivan_2008's avatar
Aivan_2008 committed
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
    //   -   
    ///   
    /// 0 - 
    /// 1 -  
    /// 2 -  ,   
    int calc_state = GetApplication()->GetProjectDeployer()->GetCalculationState();
    if(calc_state>=0)
    {
        calculation_state = calc_state;
        // ,     :
        std::string log_err="";
        bool process_log_res = GetApplication()->GetProjectDeployer()->ProcessCalculationLog(log_err);
        if(!process_log_res)
        {
            message = "Log error: "+log_err;
        }
        else
        {
            if(calc_state==2)
            {
                message = "Calculation started but not active (2).";
            }
            else if(calc_state==1 || calc_state==0)
            {
504
505
506
507
                unsigned long long capture_frame_id=0;
                unsigned long long capture_max_frame_id=0;
                int cap_state_out = -1;
                bool res_capstate = GetApplication()->GetProjectDeployer()->GetCaptureState(cap_state_out, capture_frame_id, capture_max_frame_id);
Aivan_2008's avatar
Aivan_2008 committed
508
509
510
511
512
513
514
515
                if(!res_capstate)
                {
                    std::string err="";
                    err = GetApplication()->GetProjectDeployer()->GetLastError();
                    message = "Get capture state error: "+err;
                }
                else
                {
516
                    capture_state = cap_state_out;
Aivan_2008's avatar
Aivan_2008 committed
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
                    capture_frid = capture_frame_id;
                    capture_maxfrid = capture_max_frame_id;
                    message = "Calculation is active";
                }
            }
            else
            {
                message = "Error: Wrong calculation state number";
            }
        }

    }
    else
    {
        std::string err="";
        err = GetApplication()->GetProjectDeployer()->GetLastError();
        message = "GetCalculationState() error: "+err;
534
    }
Aivan_2008's avatar
Aivan_2008 committed
535
536
537
538
539

    std::stringstream res_ss;
    res_ss<<calculation_state<<"|"<<capture_state<<"|"<<capture_frid<<"|"<<capture_maxfrid<<"|"<<message.c_str();
    response = res_ss.str();
    return_value=0;
540
  }
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
  else
  if(cmd=="FinishCalculation")
  {
    bool rs = GetApplication()->GetProjectDeployer()->FinishCalculation();
    std::string le = GetApplication()->GetProjectDeployer()->GetLastError();
    std::stringstream ss;
    ss<<rs<<"|"<<le;
    response = ss.str();
    return_value=0;
  }
  else
  if(cmd=="UploadCalculationResults")
  {
    bool rs = GetApplication()->GetProjectDeployer()->UploadCalculationResults();
    std::string le = GetApplication()->GetProjectDeployer()->GetLastError();
    std::stringstream ss;
    ss<<rs<<"|"<<le;
    response = ss.str();
    return_value=0;
  }
  else
  if(cmd=="GetUploadState")
  {
    int us = GetApplication()->GetProjectDeployer()->GetUploadState();
    std::string le = GetApplication()->GetProjectDeployer()->GetLastError();
    std::stringstream ss;
    ss<<us<<"|"<<le;
    response = ss.str();
    return_value=0;
  }
  else
  if(cmd=="CloseSolver")
  {
    bool rs = GetApplication()->GetProjectDeployer()->CloseSolver();
    std::string le = GetApplication()->GetProjectDeployer()->GetLastError();
    std::stringstream ss;
    ss<<rs<<"|"<<le;
    response = ss.str();
    return_value=0;
  }
581
582
583
584
585
586
 }

 result.SelectRoot();
 if(!response.empty())
 {
  result.WriteString("Data",response);
587
//  MEngine_FreeBufString(channel_index,response);
588
589
590
591
 }
 else
  result.WriteString("Data","");
 result.WriteInteger("Res",return_value);
592
 MCore_LockChannel(0);
593
594
 std::string &buffer=GetEngine(0)->CreateTempString();
 result.Save(buffer);
595
 MCore_UnLockChannel(0);
596
597
598
599

 return buffer.c_str();
}

600
std::string URpcDecoderCommon::ARemoteCall(const std::string &cmd, RDK::USerStorageXML &xml, const std::string &component_name, int channel_index, int &return_value)
601
602
603
604
605
606
607
608
609
{
 return_value=2001;
 return "";
}
// --------------------------

}
#endif