本文目錄一覽:
用objective-c語言實現一個消息中心(NSnotificationcenter)功能
對象之間進行通信最基本的方式就是消息傳遞,在Cocoa中提供Notification
Center機制來完成這一任務。其主要作用就是負責在任意兩個對象之間進行通信。使用方法很簡單,如下幾個步驟即可:
假設A與B之間進行通信,B來觸發事件,A接受該事件,並作出響應。
1)
A編寫自定義的消息響應函數update
2)
A向消息中心註冊,[NSNotificationCenter
defaultCenter]
addObserver:
self
selector:@selector(update)
name:@”update”
object:nil]
3)
B觸發事件[[NSNotificationCenter
defaultCenter]
postNotificationName:@”update”
object:nil]
每一個進程都有一個默認的NSNotificationCenter,可以通過類方法defaultCenter獲取該消息中心的實例。消息中心可以處理同一進程中不同對象之間的消息。如果要在同一台機器上進行進程間的通信,需要使用NSDistributedNotificationCenter。
消息中心以同步的方式將消息分發到所有的觀察者中,換言之,直到所有的觀察者都收到消息並處理完畢以後,控制權才會回到調用者的手裡。如果需要非同步的處理消息,需要使用通知隊列NSNotificationQueue。
在多線程程序中,通知會被分發到每一個發起消息的線程中,這可能與觀察者註冊時所在的線程已經不是同一線程。
實例:
@implementation
TestClass
–
(void)
dealloc
{
//
If
you
don’t
remove
yourself
as
an
observer,
the
Notification
Center
//
will
continue
to
try
and
send
notification
objects
to
the
deallocated
//
object.
[[NSNotificationCenter
defaultCenter]
removeObserver:self];
[super
dealloc];
}
–
(id)
init
{
self
=
[super
init];
if
(!self)
return
nil;
//
Add
this
instance
of
TestClass
as
an
observer
of
the
TestNotification.
//
We
tell
the
notification
center
to
inform
us
of
“TestNotification”
//
notifications
using
the
receiveTestNotification:
selector.
By
//
specifying
object:nil,
we
tell
the
notification
center
that
we
are
not
//
interested
in
who
posted
the
notification.
If
you
provided
an
actual
//
object
rather
than
nil,
the
notification
center
will
only
notify
you
//
when
the
notification
was
posted
by
that
particular
object.
[[NSNotificationCenter
defaultCenter]
addObserver:self
selector:@selector(receiveTestNotification:)
name:@”TestNotification”
object:nil];
return
self;
}
–
(void)
receiveTestNotification:(NSNotification
*)
notification
{
//
[notification
name]
should
always
be
@”TestNotification”
//
unless
you
use
this
method
for
observation
of
other
notifications
//
as
well.
if
([[notification
name]
isEqualToString:@”TestNotification”])
NSLog
(@”Successfully
received
the
test
notification!”);
}
@end
關於mosquitto怎麼用c語言實現消息的訂閱和發送(mqtt)
1.目標:測試Mosquitto使用MQTT協議發消息的相關性能指標,包含發送速度,並發負載能力,資源佔用,消息到達率。
2.MQTT協議簡介:
1).建立長連接。客戶端發起請求和服務端建立長連接,建立成功後,服務端會返回ACK(CONNACK)
2).客戶端訂閱:客戶端發起訂閱,訂閱成功後,服務端會返回ACK(SUBACK)
3).發消息:發布者會給服務端發消息,服務端在把消息給合適的客戶端。
Qos=0(服務質量):客戶端消息收到後,不會發出ACK給服務端(PUBACK)。
Qos =1:服務端會發ACK給發布者,客戶端收到消息後會發ACK給服務端。
4).取消訂閱:客戶端發起取消訂閱,服務端返回ACK(UNSUBACK)
5)PingreqPingresp:客戶端和服務端會保持心跳。
3.存在問題:
1. 如何模擬出40W的用戶
2. 如何長連接,訂閱,發消息,取消訂閱,Pingreq行為如何實現。
4. python開源庫 Mosquitto.py,解決所有問題
1. 模擬40W用戶
a)可以使用虛擬機和Mosquitto.py實現,具體為:一般一台虛擬機最多是6W+的模擬數據(需要修改句柄數,我使用5W),方法是Client_id可以簡單的做出5W個來,然後調用Mosquitto裡面的connect方法,建立長連接。準備8台虛擬機就可以實現40W客戶端的模擬
2.行為的模擬
a)訂閱:Mosquitto.subscribe / 發消息:Mosquitto.publish / 取消訂閱:Mosquitto.unsubscribe。 簡單一句話 Mosquitto庫實現所有的行為.
5. 指標:發送速度,到達率,並發負載,資源佔用。
a. 發送速度:服務端日誌記錄,分析解決
b. 到達率: 1.客戶端記錄下收到消息,分析計算。2.計算服務端收到的PUBACK(客戶端收到消息返回的ACK),進行計算
c. 並發負載:5W 用戶不斷增加,注意觀察服務端的負載情況。
e.資源佔用:其實主要是cpu/mem/帶寬:cpu多核的話需要觀察top命令下的_id欄位, mem可以觀察free -h命令的剩餘mem, 帶寬可以使用mpstat進行觀察
6. 可以遇見問題:
a. 模擬客戶端的虛擬機需要修改句柄數才能支持5W的客戶端模擬數量
b. 要先吃透MQTT協議的流程,吃透了進行測試會非常的方便
c. Clear session,設置為true則不為客戶端保留休息,設置為false保留消息。其實就是客戶端離線後在連接上可以收到之前推出的消息。
雜誌訂閱管理的c語言
#includestdio.h
#include “string.h”
#include “stdlib.h”
#include “conio.h”
#define max 20
struct magazine
{
char name[11];
char sex[6];
char addr[11];
char phone_number[12];
char unit_price[5];
char amount[4];
char time_limit[11];
}
order[max];void save(int n);
int load_record();
void search();
void printf_n(int n);
void printf_one(int i);
void input(int i);
void statistic();
void add();
void menu();void main()
{
FILE *fp;
fp=fopen(“record.txt”,”w”);/*在C:\Documents and Settings\Administrator里建立一個”record.txt”記事本*/
fclose(fp);
menu();
}void menu()
{
// void clrscr();
int w,n;
do
{
//clrscr();
printf(“\n”);
printf(“\t\t* * * * * * * * * * * * * * * * * * * * * * * * *\n”);
printf(“\t\t \n”);
printf(“\t\t \n”);
printf(“\t\t 歡迎來到雜誌訂閱系統! \n”);
printf(“\t\t \n”);
printf(“\t\t \n”);
printf(“\t\t* * * * * * * * * * * * * * * * * * * * * * * * *\n”);
printf(“\n\n\t\t *********************************************\n\n”);
printf(“\t\t 1:add_new_subscriber\n”);
printf(“\t\t 2:browse\n”);
printf(“\t\t 3:statistic\n”);
printf(“\t\t 4:exit\n\n”);
printf(“\t\t *********************************************\n\n”);
printf(” Choice your number(1-4):[ ]\b\b”);
scanf(“%d”,n);
if(n1||n4) w=1;
else w=0;
}
while (w==1);
/***************選擇功能****************/
switch(n)
{
case 1:add();break; /*增加新訂戶記錄*/
case 2:search();break; /*查找過期的訂戶記錄並刪除*/
case 3:statistic();break; /*統計*/
case 4:break; /*退出*/
}
}
/*********************添加模塊************************/
void add()
{
int t,i,m;
system(“cls”);
t=load_record();
printf(“您需要添加多少用戶的信息?\n”);
printf(“輸入號碼:[ ]\b\b\b”);
scanf(“%d”,m);
for(i=0;im;i++)
{
printf(“\n輸入 %dth subscriber record.\n”,i+1);
input(t+i); /*調用輸入函數*/
}
save(t+m); /*調用保存函數 保存t+m組數據*/
system(“cls”) ; /*******清屏*********/
menu(); /*返回登陸界面*/
}
void input(int i)
{
printf(“請輸入:\n姓名 性別 地址 電話 雜誌單價 數量 訂閱期限\n”);
scanf(“%s%s%s%s%s%s%s”,order[i].name,order[i].sex,order[i].addr,order[i].phone_number,order[i].unit_price,order[i].amount,order[i].time_limit);
}
/**************************統計模塊****************************/
void statistic()
{
int t;
t=load_record();
printf(“訂閱者的數量[%d]\b\b\b”,t);
printf_n(t);
printf(“\n\n\n按任意鍵返回…\n\n”);
getch();
menu();
}void printf_one(int i) /*顯示一個儀器記錄的函數*/
{
printf(“%-10s %-5s %-10s %-11s %-4s %-3s %-10s\n\n”,order[i].name,order[i].sex,order[i].addr,order[i].phone_number,order[i].unit_price,order[i].amount,order[i].time_limit);
}void printf_n(int n) /*顯示n個儀器記錄的函數*/
{
int j;
system(“cls”); /**********清屏************/
printf(“姓名 性別 地址 電話號碼 價格 數量 時間期限\n\n”);
for(j=0;jn;j++)
{
if((j+1)%10==0) /*控制顯示,每屏顯示10行*/
{
printf(“\n\n按任意鍵繼續 …”);
getch();
puts(“\n\n”);
}
printf_one(j); /*調用顯示一個儀器記錄的函數*/
}
}
/***************查詢模塊****************/
void search()
{
FILE *fp;
char date[11],str[2];
int t,i,sum=0;
t=load_record();
system(“cls”);
printf(“請輸入日期:[ ]\b\b\b\b\b\b\b\b\b\b\b”);
scanf(“%s”,date);
printf(“\n”);
if((fp=fopen(“record.txt”,”w”))==NULL)/*以只寫方式打開*/
{
printf(“\n無法打開文件\n”);
exit(0);
}
system(“cls”);
printf(“\n”);
for(i=0;it;i++)
{
if(strcmp(order[i].time_limit,date)0)
{
sum++;
printf(“\n”);
printf_one(i); /**********把符合條件的記錄顯示出來*********/
printf(“\n\n記錄已過期!”);
printf(“\n\n你想要刪除此條信息嗎?(‘Y’or’N’):[ ]\b\b”);
scanf(“%s”,str);;
if(strcmp(str,”Y”)==0||strcmp(str,”y”)==0)
continue;
}
fwrite(order[i],sizeof(struct magazine),1,fp);
}
fclose(fp);
if(sum==0)
{
printf(“***************************************************\n\n”);
printf(“很遺憾! 無法找到您想要的條件\n\n”);
printf(“***************************************************\n\n”);
}
printf(“\n\n\n按任意鍵返回前一步…\n”);
getch();
menu();
}/****************************公共函數************************************/
int load_record()/***載入儀器信息並計算記錄儀器個數的函數***/
{
FILE *fp;
int i;
if((fp=fopen(“record.txt”,”r”))==NULL)/*以只讀方式打開*/
{
printf(“\n無法打開文件\n”);
exit(0);
}
for(i=0;!feof(fp);i++)/*從文件中讀入數據並保存在結構體數組中*/
fread(order[i],sizeof(struct magazine),1,fp);
fclose(fp);
return(i-1);
}void save(int n)/*n表示保存n組數據,m表示保存在哪個文件夾里*/
{
FILE *fp;
int i;
fp=fopen(“record.txt”,”w”);/*以只寫方式打開*/
if(fp==NULL)
{
printf(“\n無法打開文件\n”);
exit(0);
}
for(i=0;in;i++)
{
fwrite(order[i],sizeof(struct magazine),1,fp);
}
fclose(fp);
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/291103.html