This instance will be upgraded to Heptapod 0.27.1 on 2021-12-08 at 22:00 UTC+1 (a few minutes of down time)

URpcDecoderCommon.cpp 14.8 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

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;
 }
vlad_burin's avatar
vlad_burin committed
118
 else
119
120
121
122
 if(cmd == "StartTraining")
 {
  return true;
 }
123
124
125
126
127
128
129
130
131
132
133
134
135
 else
 if(cmd == "RegisterMetadataReceiver")
 {
  return true;
 }
 else
 if(cmd == "UnRegisterMetadataReceiver")
 {
  return true;
 }
 else
 if(cmd == "LoadProject")
 {
136
  return true;
137
138
139
140
 }
 else
 if(cmd == "SaveProject")
 {
141
  return true;
142
143
144
145
146
147
148
149
150
151
152
153
 }
 else
 if(cmd == "ReadImageFromVideoSource")
 {
  return true;
 }
 else
 if(cmd == "StartVideoSource")
 {
  return true;
 }
 else
154
155
156
157
158
159
160
161
162
 if(cmd == "DeployProject")
 {
  return true;
 }
 else
 if(cmd == "GetDeploymentState")
 {
  return true;
 }
163
164
165
166
167
 else
 if(cmd == "GetLastError")
 {
  return true;
 }
Aivan_2008's avatar
Aivan_2008 committed
168
 else
Aivan_2008's avatar
Aivan_2008 committed
169
170
171
172
173
 if(cmd == "PrepareProject")
 {
  return true;
 }
 else
174
175
176
177
178
 if(cmd == "GetPreparationResult")
 {
  return true;
 }
 else
Aivan_2008's avatar
Aivan_2008 committed
179
 if(cmd == "OpenPreparedProject")
Aivan_2008's avatar
Aivan_2008 committed
180
181
182
 {
  return true;
 }
Aivan_2008's avatar
Aivan_2008 committed
183
184
 else
 if(cmd == "RunPreparedProject")
185
186
187
188
 {
  return true;
 }
 else
Aivan_2008's avatar
Aivan_2008 committed
189
 if(cmd == "GetCalculationState")
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
 {
  return true;
 } 
 else
 if(cmd == "FinishCalculation")
 {
  return true;
 }
 else
 if(cmd == "UploadCalculationResults")
 {
  return true;
 }
 else
 if(cmd == "GetUploadState")
 {
  return true;
 }
 else
 if(cmd == "CloseSolver")
210
211
 {
  return true;
Aivan_2008's avatar
Aivan_2008 committed
212
 }
213
 else
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
 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)
 {
  //  -    
234
  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());
235
236
237
238
239
240
241
242
243
244
245
246
247
248
  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)
 {
249
  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());
250
251
252
253
254
255
256
  return false;
 }

 cmd->IsProcessed=true;
 return true;
}

257
const char* URpcDecoderCommon::RemoteCall(const char *request, int &return_value, int &res_channel_index)
258
259
260
261
262
263
264
{
 return_value=INT_MAX;

 RDK::USerStorageXML xml,xml_data;

 xml.Load(request,"RpcRequest");

265
 int channel_index=-1;
266
267
 std::string cmd;
 std::string component_name;
268
 res_channel_index=0;
269
270
271
272
273
274
275
276


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

277
 if(!ExtractCC(xml,channel_index,component_name))
278
 {
279
  ExtractChannel(xml,channel_index);
280
281
282
  ExtractComponent(xml,component_name);
 }
  /*
283
 if((channel_index < 0 || channel_index >= GetNumEngines()) &&
284
285
286
287
288
289
	(cmd != "GetNumChannels" && cmd !="SetNumChannels"))
 {
  return_value=2001;
  return 0;
 }  */

290
 res_channel_index=channel_index;
291
292
293
294
295
296
297
298
299
300
301
 std::string response,temp;
 std::string name;

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

Aivan_2008's avatar
Aivan_2008 committed
428
429
   std::string le = GetApplication()->GetProjectDeployer()->GetLastError();

430
   std::stringstream ss;
431
   ss<<d_state<<"|-|"<<stage_pos<<"|-|"<<stage_max<<"|-|"<<le;
432
433
434
   response=ss.str();
   return_value=0;//UServerControlForm->SaveProject();
  }
435
436
437
438
  else
  if(cmd == "GetLastError")
  {
   std::string last_error=GetApplication()->GetProjectDeployer()->GetLastError();
Aivan_2008's avatar
Aivan_2008 committed
439
440
441
442
   response=last_error;
   return_value=0;
  }
  else
Aivan_2008's avatar
Aivan_2008 committed
443
  if(cmd == "PrepareProject")
Aivan_2008's avatar
Aivan_2008 committed
444
  {
Aivan_2008's avatar
Aivan_2008 committed
445
    std::string resp="";
446
    int rs = GetApplication()->GetProjectDeployer()->PrepareProject(resp);
Aivan_2008's avatar
Aivan_2008 committed
447
448
449
450
    response=resp;
    return_value=0;
  }
  else
451
452
453
454
455
  if(cmd == "GetPreparationResult")
  {
    std::string resp="";
    int rs = GetApplication()->GetProjectDeployer()->GetPreparationResult(resp);
    std::stringstream ss;
456
    ss<<rs<<"|-|"<<resp.c_str();
457
458
459
460
    response=ss.str();
    return_value=0;
  }
  else
Aivan_2008's avatar
Aivan_2008 committed
461
462
463
  if(cmd=="OpenPreparedProject")
  {
    std::string resp="";
464
    std::cerr<<"Open prepared project!\n";
Aivan_2008's avatar
Aivan_2008 committed
465
    int rs = GetApplication()->GetProjectDeployer()->OpenPreparedProject(resp);
466
    std::cerr<<"Project opened successful!\n";
Aivan_2008's avatar
Aivan_2008 committed
467
468
    response=resp;
    return_value=0;
469
  }
470
  else
Aivan_2008's avatar
Aivan_2008 committed
471
  if(cmd=="RunPreparedProject")
472
  {
Aivan_2008's avatar
Aivan_2008 committed
473
474
475
476
477
    int rs = GetApplication()->GetProjectDeployer()->RunPreparedProject();
    std::stringstream ss;
    ss<<rs;
    response = ss.str();
    return_value=0;
478
479
  }
  else
Aivan_2008's avatar
Aivan_2008 committed
480
  if(cmd == "GetCalculationState")
481
  {
Aivan_2008's avatar
Aivan_2008 committed
482
483
484
    /*std::string resp="";
    int rs = GetApplication()->GetProjectDeployer()->GetPreparationResult(resp);
    std::stringstream ss;
485
    ss<<rs<<"|-|"<<resp.c_str();
Aivan_2008's avatar
Aivan_2008 committed
486
487
488
489
490
    response=ss.str();*/
    //  :
    //"<calculation_state>|<capture_state>|<capture_frid>|<capture_maxfrid>|<capture_finished>|<message>"
    int calculation_state=-1;
    int capture_state=-1;
Aivan_2008's avatar
Aivan_2008 committed
491
492
    unsigned long long capture_frid=0;
    unsigned long long capture_maxfrid=0;
Aivan_2008's avatar
Aivan_2008 committed
493
494
    std::string message = "test state message";

495

Aivan_2008's avatar
Aivan_2008 committed
496
497
    //   -   
    ///   
498
    /// -1 -  
Aivan_2008's avatar
Aivan_2008 committed
499
500
501
    /// 0 - 
    /// 1 -  
    /// 2 -  ,   
502
    /// 3 -    
Aivan_2008's avatar
Aivan_2008 committed
503
504
505
506
507
508
509
510
511
512
    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;
513
            calculation_state = 3;
Aivan_2008's avatar
Aivan_2008 committed
514
515
516
517
518
519
520
521
522
        }
        else
        {
            if(calc_state==2)
            {
                message = "Calculation started but not active (2).";
            }
            else if(calc_state==1 || calc_state==0)
            {
523
524
525
526
                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
527
528
529
530
531
532
533
534
                if(!res_capstate)
                {
                    std::string err="";
                    err = GetApplication()->GetProjectDeployer()->GetLastError();
                    message = "Get capture state error: "+err;
                }
                else
                {
535
                    capture_state = cap_state_out;
Aivan_2008's avatar
Aivan_2008 committed
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
                    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;
553
    }
Aivan_2008's avatar
Aivan_2008 committed
554
555

    std::stringstream res_ss;
556
    res_ss<<calculation_state<<"|-|"<<capture_state<<"|-|"<<capture_frid<<"|-|"<<capture_maxfrid<<"|-|"<<message.c_str();
Aivan_2008's avatar
Aivan_2008 committed
557
558
    response = res_ss.str();
    return_value=0;
559
  }
560
561
562
563
564
565
  else
  if(cmd=="FinishCalculation")
  {
    bool rs = GetApplication()->GetProjectDeployer()->FinishCalculation();
    std::string le = GetApplication()->GetProjectDeployer()->GetLastError();
    std::stringstream ss;
566
    ss<<rs<<"|-|"<<le;
567
568
569
570
571
572
573
574
575
    response = ss.str();
    return_value=0;
  }
  else
  if(cmd=="UploadCalculationResults")
  {
    bool rs = GetApplication()->GetProjectDeployer()->UploadCalculationResults();
    std::string le = GetApplication()->GetProjectDeployer()->GetLastError();
    std::stringstream ss;
576
    ss<<rs<<"|-|"<<le;
577
578
579
580
581
582
583
584
585
    response = ss.str();
    return_value=0;
  }
  else
  if(cmd=="GetUploadState")
  {
    int us = GetApplication()->GetProjectDeployer()->GetUploadState();
    std::string le = GetApplication()->GetProjectDeployer()->GetLastError();
    std::stringstream ss;
586
    ss<<us<<"|-|"<<le;
587
588
589
590
591
592
593
594
595
    response = ss.str();
    return_value=0;
  }
  else
  if(cmd=="CloseSolver")
  {
    bool rs = GetApplication()->GetProjectDeployer()->CloseSolver();
    std::string le = GetApplication()->GetProjectDeployer()->GetLastError();
    std::stringstream ss;
596
    ss<<rs<<"|-|"<<le;
597
    response = ss.str();
Aivan_2008's avatar
Aivan_2008 committed
598
    exit(0);
599
600
    return_value=0;
  }
601
602
603
604
605
606
 }

 result.SelectRoot();
 if(!response.empty())
 {
  result.WriteString("Data",response);
607
//  MEngine_FreeBufString(channel_index,response);
608
609
610
611
 }
 else
  result.WriteString("Data","");
 result.WriteInteger("Res",return_value);
612
 MCore_LockChannel(0);
613
614
 std::string &buffer=GetEngine(0)->CreateTempString();
 result.Save(buffer);
615
 MCore_UnLockChannel(0);
616
617
618
619

 return buffer.c_str();
}

620
std::string URpcDecoderCommon::ARemoteCall(const std::string &cmd, RDK::USerStorageXML &xml, const std::string &component_name, int channel_index, int &return_value)
621
622
623
624
625
626
627
628
629
{
 return_value=2001;
 return "";
}
// --------------------------

}
#endif