弄Rpc有一段时间了,从定义IDL规则,自动生成存根代码给应用使用,相继做了c++,java,python,actionscript四个版本,且都都能互相调用,协议是自定义的二进制格式。
公司的项目要求我从事android的开发,编写一些小的应用,要与平台服务器通信。
与服务器的通信采用xml交换,之前做的二进制编码便不能使用。
不想很弱智的去重复的编解码xml消息格式,去编写socket通信代码,工作量和繁琐是自己不喜欢的,那只能让自己的rpc去支持xml格式。 www.2cto.com
这种支持我称之为半人工的,因为xml格式的tag定义必须在idl中出现
idl定义
1 struct Ts_heartbeat_p_User_t{
2string id;
3 };
4 //心跳
5
6 struct Ts_gps_p_GPS_t{
7float lon;
8float lat;
9float speed;
10float direction;
11inttime;
12 };
13
14 struct Ts_gps_p_Properties_t{
15string senderid;
16string target_type;
17string users;
18string groups;
19 };
20
21 struct Ts_verify_r_User_t{
22string id;
23 };
24
25 struct Ts_verify_r_Group_t{
26string id;
27string name;
28string type;
29 };
30
31 sequence<Ts_verify_r_Group_t> Ts_verify_r_Groups_t;
32
33 struct Ts_verify_r_Result_t{
34int code;
35string msg;
36Ts_verify_r_User_t user;
37Ts_verify_r_Groups_t groups;
38 };
39
40
41 struct Ts_verify_p_User_t{
42string token;
43 };
44
45 interface Ts{
46void heartbeat(Ts_heartbeat_p_User_t p);
47void gps(Ts_gps_p_GPS_t p,Ts_gps_p_Properties_t props);
48Ts_verify_r_Result_t verify(Ts_verify_p_User_t p);
49 };
50
idl定义了一个Ts的接口,内部有三个方法:heartbeat(),gps(),verify()
这些方法在服务器端已经实现,其他的struct类型定义了接口调用使用的参数和返回值类型 ,注意他们的格式:
struct Ts_verify_p_User_t{
string token;
};
这个类型将被解释成 <User token="..."/>
sequence<Ts_verify_r_Group_t> Ts_verify_r_Groups_t;
将被解释成
<Groups>
<Group id="" name="" type=""/>
...
</Groups>
努力不是白费的,python+ply实现了idl的语法解析,并生成java框架代码,并编写通信代码完成Rpc构造。
必须要注意Android不允许任何阻塞UI线程的操作,所以要实现通信必须在自己的线程中进行,我提供的Rpc调用模式只能是 oneway(单向调用) ,async(异步调用)
实例:
1 package test;
2
3
4 import sns_mobile.*;
5
6 import java.util.*;
7 import tce.*;
8
9 import java.nio.*;
10 import java.io.*;
11
12 public class SnsTestMain{
13TsProxy tsprx = null;
14String token="dBmPPfxxohFmXjx4SUv46BffzmeYitbjmzT2mRHLttGaau4kiTvKEOpW2BJE0IGLEBFZ4lE/KrMKl7qE6pNmaBUfbixMrPEG/1Xfsp36P4lmZHhQfkVgyGWa8yi5g/swbMpg8h2ybC2dQShR18VFtQkXGpEUg8VfV1Fb0UllN6j/umVaBBQX7lgqcf3zkIaGcOpvS7FB6snC/PWU1crCf2gHrgnOmZgIaKmBF14dkJsSoozRGKiIpiCjJ7mRZ05og67ZmFfOaH2EnaCo3G1pmQcez7MOqCtIvNpGo5yivmZ6GyMA2eSIlFmD2Z7ysCrxzAt92UUCNwb9mkKZrUthjA==";
15
16double locs[]={436914.309600,112259.854800,436914.309600,112259.847600,436914.298800,112259.847600,436914.298800,112259.844000,436914.291600,112259.844000,436914.288000,112259.847600,436914.298800,112259.847600,436914.298800,112259.844000,436914.306000,112259.836800,436914.309600,112259.836800,436914.316800,112259.826000,436914.324000,112259.811600,436914.334800,112259.793600,436914.345600,112259.775600,436914.352800,112259.775600,436914.360000,112259.775600,436914.370800,112259.764800,436914.378000,112259.757600,436914.381600,112259.739600,436914.381600,112259.754000,436914.381600,112259.808000,436914.352800,112259.883600,436914.316800,112260.024000,436914.280800,112260.186000,436914.244800,112260.376800,436914.234000,112260.600000,436914.201600,112260.834000,436914.165600,112261.086000,436914.118800,112261.359600,436914.075600,112261.654800,436914.036000,112261.942800,436914.000000,112262.230800,436913.910000,112262.562000,436913.841600,112262.907600,436913.794800,112263.285600,436913.733600,112263.660000,436913.676000,112264.038000,436913.614800,112264.401600,436913.553600,112264.776000,436913.517600,112265.128800,436913.445600,112265.463600,436913.373600,112265.805600,436913.334000,112266.180000,436913.290800,112266.540000,436913.244000,112266.910800,436913.244000,112266.910800,436913.139600,112267.695600,436913.139600,112267.695600,436913.139600,112267.695600,436913.056800,112268.440800,436913.002800,112268.793600,436912.923600,112269.081600,436912.920000,112269.276000,436912.938000,112269.376800,436912.995600,112269.369600,436913.013600,112269.366000,436913.020800,112269.394800,436913.013600,112269.423600,436913.010000,112269.441600,436913.020800,112269.459600,436913.010000,112269.495600,436913.010000,112269.513600,436913.002800,112269.531600,436912.992000,112269.546000,436912.977600,112269.564000,436912.974000,112269.567600,436912.966800,112269.582000,436912.956000,112269.585600,436912.948800,112269.592800,436912.941600,112269.600000,436912.938000,112269.610800,436912.930800,112269.618000,436912.923600,112269.621600,436912.923600,112269.636000,436912.923600,112269.639600,436912.923600,112269.639600,436912.923600,112269.639600,436912.920000,112269.646800,436912.920000,112269.654000,436912.920000,112269.657600,436912.912800,112269.664800,436912.920000,112269.664800,436912.912800,112269.672000,436912.912800,112269.675600,436912.912800,112269.682800,436912.912800,112269.693600,436912.912800,112269.693600,436912.905600,112269.700800,436912.905600,112269.700800,436912.905600,112269.700800,436912.902000,112269.711600,436912.902000,112269.711600,436912.902000,112269.726000,436912.894800,112269.736800,436912.894800,112269.747600,436912.887600,112269.772800,436912.884000,112269.801600,436912.884000,112269.801600,436912.858800,112269.852000,436912.851600,112269.888000,436912.833600,112269.924000,436912.815600,112269.963600,436912.797600,112270.032000,436912.768800,112270.104000,436912.768800,112270.104000,436912.707600,112270.500000,436912.660800,112270.719600,436912.617600,112270.942800,436912.560000,112271.205600,436912.527600,112271.508000,436912.455600,112271.824800,436912.372800,112272.148800,436912.311600,112272.498000,436912.236000,112272.850800,436912.149600,112273.218000,436912.059600,112273.588800,436911.966000,112273.959600,436911.861600,112274.344800,436911.753600,112274.715600,436911.642000,112275.100800,436911.642000,112275.100800,436911.346800,112275.838800,436911.202800,112276.173600,436911.051600,112276.476000,436910.896800,112276.785600,436910.860800,112277.055600,436910.796000,112277.271600,436910.727600,112277.469600,436910.670000,112277.620800,436910.634000,112277.721600,436910.644800,112277.746800,436910.619600,112277.757600,436910.616000,112277.764800,436910.616000,112277.764800,436910.619600,112277.782800,436910.616000,112277.782800,436910.616000,112277.782800,436910.608800,112277.775600,436910.608800,112277.772000,436910.608800,112277.772000,436910.608800,112277.772000,436910.601600,112277.772000,436910.598000,112277.764800,436910.583600,112277.757600,436910.583600,112277.754000,436910.580000,112277.746800,436910.580000,112277.739600,436910.580000,112277.739600,436910.572800,112277.728800,436910.565600,112277.721600,436910.562000,112277.736000,436910.554800,112277.736000,436910.547600,112277.736000,436910.536800,112277.721600,436910.529600,112277.721600,436910.518800,112277.721600,436910.508000,112277.703600,436910.500800,112277.710800,436910.500800,112277.721600,436910.500800,112277.721600,436910.490000,112277.721600,436910.490000,112277.728800,436910.490000,112277.728800,436910.482800,112277.728800,436910.482800,112277.721600,436910.475600,112277.718000,436910.464800,112277.718000,436910.446800,112277.710800,436910.436000,112277.703600,436910.418000,112277.703600,436910.403600,112277.703600,436910.385600,112277.692800,436910.382000,112277.700000,436910.374800,112277.692800,436910.364000,112277.685600,436910.356800,112277.685600,436910.356800,112277.685600,436910.349600,112277.685600,436910.349600,112277.685600,436910.346000,112277.692800,436910.346000,112277.692800,436910.338800,112277.692800,436910.338800,112277.692800,436910.338800,112277.692800,436910.328000,112277.685600,436910.328000,112277.685600,436910.320800,112277.685600,436910.313600,112277.685600,436910.310000,112277.682000,436910.310000,112277.682000,436910.310000,112277.682000,436910.302800,112277.674800,436910.302800,112277.667600,436910.295600,112277.667600,436910.292000,112277.664000,436910.292000,112277.664000,436910.292000,112277.667600,436910.277600,112277.667600,436910.266800,112277.667600,436910.259600,112277.667600,436910.248800,112277.674800,436910.241600,112277.674800,436910.238000,112277.674800,436910.223600,112277.667600,436910.220000,112277.667600,436910.220000,112277.667600,436910.212800,112277.667600,436910.220000,112277.674800,436910.220000,112277.674800,436910.223600,112277.682000,436910.223600,112277.685600,436910.220000,112277.700000,436910.202000,112277.703600,436910.176800,112277.739600,436910.140800,112277.790000,436910.122800,112277.862000,436910.079600,112277.901600,436910.043600,112277.973600,436910.007600,112278.042000,436909.953600,112278.132000,436909.914000,112278.207600,436909.647600,112278.402000,436909.528800,112278.924000,436909.410000,112279.230000,436909.276800,112279.518000,436909.143600,112279.770000,436909.071600,112279.968000,436908.996000,112280.166000,436908.898800,112280.385600,436908.808800,112280.634000,436908.754800,112280.904000,436908.682800,112281.220800,436908.610800,112281.490800,436908.513600,112281.807600,436908.351600,112282.239600,436908.207600,112282.653600,436908.063600,112283.056800,436907.898000,112283.445600,436907.746800,112283.802000,436907.746800,112283.802000,436907.343600,112284.496800,436907.188800,112284.813600,436907.052000,112285.116000,436906.947600,112285.422000,436906.857600,112285.720800,436906.720800,112286.055600,436906.720800,112286.055600,436906.440000,112286.674800,436906.440000,112286.674800,436906.288800,112287.042000,436906.288800,112287.042000,436906.000800,112287.934800,436905.864000,112288.330800,436905.712800,112288.701600,436905.558000,112289.061600,436905.414000,112289.410800,436905.288000,112289.745600,436905.162000,112290.084000,436905.036000,112290.408000,436905.039600,112290.699600,436905.057600,112291.005600,436905.093600,112291.200000,436905.324000,112291.326000,436905.478800,112291.473600,436905.756000,112291.506000,436906.054800,112291.506000,436906.353600,112291.524000,436906.677600,112291.542000,436907.008800,112291.563600,436907.358000,112291.588800,436907.728800,112291.596000,436908.114000,112291.596000,436908.520800,112291.596000,436908.960000,112291.560000,436908.960000,112291.560000,436909.863600,112291.480800,436909.863600,112291.480800,436910.320800,112291.437600,436910.320800,112291.437600,436911.246000,112291.380000,436911.246000,112291.380000,436911.246000,112291.380000,436912.149600,112291.362000,436912.578000,112291.380000,436912.948800,112291.398000,436913.283600,112291.416000,436913.578800,112291.419600,436913.820000,112291.426800,436914.010800,112291.426800,436914.172800,112291.437600,436914.324000,112291.444800,436914.435600,112291.437600,436914.514800,112291.434000,436914.576000,112291.437600,436914.622800,112291.434000,436914.648000,112291.434000,436914.658800,112291.444800,436914.666000,112291.444800,436914.669600,112291.452000,436914.669600,112291.455600,436914.676800,112291.452000,436914.676800,112291.452000,436914.687600,112291.452000,436914.694800,112291.452000,436914.694800,112291.452000,436914.694800,112291.444800,436914.702000,112291.452000,436914.702000,112291.444800,436914.702000,112291.452000,436914.694800,112291.452000,436914.694800,112291.452000,436914.687600,112291.455600,436914.687600,112291.470000,436914.687600,112291.473600,436914.684000,112291.488000,436914.676800,112291.491600,436914.676800,112291.506000,436914.669600,112291.516800,436914.669600,112291.524000,436914.669600,112291.524000,436914.666000,112291.527600,436914.666000,112291.534800,436914.658800,112291.534800,436914.651600,112291.542000,436914.648000,112291.545600,436914.640800,112291.545600,436914.640800,112291.552800,436914.633600,112291.545600,436914.630000,112291.542000,436914.630000,112291.545600,436914.666000,112291.545600,436914.756000,112291.545600,436914.900000,112291.542000,436914.900000,112291.542000,436915.342800,112291.578000,436915.342800,112291.578000,436915.342800,112291.578000,436915.929600,112291.632000,436916.271600,112291.650000,436916.628000,112291.668000,436916.998800,112291.686000,436917.402000,112291.707600,436917.816000,112291.740000,436918.230000,112291.768800,436918.654800,112291.794000,436919.094000,112291.833600,436919.544000,112291.887600,436919.986800,112291.966800,436920.400800,112292.046000,436920.771600,112292.139600,436921.131600,112292.218800,436921.488000,112292.308800,436921.848000,112292.398800,436922.200800,112292.481600,436922.532000,112292.560800,436922.830800,112292.632800,436923.118800,112292.715600,436923.406800,112292.805600,436923.712800,112292.902800,436924.054800,112292.992800,436924.432800,112293.082800,436924.821600,112293.190800,436925.235600,112293.316800,436925.235600,112293.316800,436926.142800,112293.568800,436926.636000,112293.702000,436927.125600,112293.864000,436927.611600,112294.018800,436928.112000,112294.170000,436928.619600,112294.332000,436928.619600,112294.332000,436929.616800,112294.666800,436930.113600,112294.857600,436930.588800,112295.037600,436931.031600,112295.199600,436931.424000,112295.361600,436931.787600,112295.505600,436932.090000,112295.638800,436932.334800,112295.754000,436932.540000,112295.847600,436932.705600,112295.934000,436932.820800,112295.988000,436932.892800,112296.027600,436932.918000,112296.042000,436932.921600,112296.042000,436932.921600,112296.045600,436932.928800,112296.042000,436932.928800,112296.045600,436932.936000,112296.042000,436932.936000,112296.045600,436932.928800,112296.045600,436932.928800,112296.060000,436932.921600,112296.070800,436932.921600,112296.070800,436932.928800,112296.063600,436932.939600,112296.052800,436932.936000,112296.063600,436932.928800,112296.078000,436932.928800,112296.070800,436932.936000,112296.070800,436932.936000,112296.070800,436932.936000,112296.078000,436932.939600,112296.070800,436932.946800,112296.063600,436932.954000,112296.060000,436932.954000,112296.060000,436932.946800,112296.070800,436932.954000,112296.078000,436932.946800,112296.078000,436932.939600,112296.096000,436932.939600,112296.096000,436932.939600,112296.081600,436932.939600,112296.078000,436932.939600,112296.078000,436932.939600,112296.070800,436932.939600,112296.063600,436932.939600,112296.078000,436932.939600,112296.078000,436932.928800,112296.106800,436932.928800,112296.106800,436932.918000,112296.117600,436932.918000,112296.117600,436932.918000,112296.117600,436932.921600,112296.124800,436932.910800,112296.142800,436932.903600,112296.135600,436932.903600,112296.132000,436932.900000,112296.153600,436932.892800,112296.160800,436932.892800,112296.168000,436932.892800,112296.168000,436932.885600,112296.171600,436932.882000,112296.186000,436932.892800,112296.196800,436932.946800,112296.214800,436933.047600,112296.243600,436933.206000,112296.297600,436933.404000,112296.384000,436933.630800,112296.492000,436933.890000,112296.621600,436934.188800,112296.765600,436934.188800,112296.765600,436934.847600,112297.096800,436935.196800,112297.251600,436935.538800,112297.413600,436935.880800,112297.593600,436936.248000,112297.752000,436936.618800,112297.953600,436937.007600,112298.176800,436937.400000,112298.385600,436937.400000,112298.385600,436938.249600,112298.824800,436938.732000,112299.040800,436939.218000,112299.282000,436939.707600,112299.519600,436940.211600,112299.760800,436940.722800,112300.005600,436941.226800,112300.254000,436941.730800,112300.516800,436942.242000,112300.779600,436942.749600,112301.056800,436943.235600,112301.337600,436943.703600,112301.622000,436944.150000,112301.884800,436944.567600,112302.162000,436944.978000,112302.406800,436945.359600,112302.658800,436945.359600,112302.658800,436946.061600,112303.108800,436946.374800,112303.296000,436946.374800,112303.296000,436946.374800,112303.296000,436946.878800,112303.605600,436947.105600,112303.767600,436947.364800,112303.929600,436947.642000,112304.106000,436947.948000,112304.296800,436948.275600,112304.494800,436948.635600,112304.700000,436948.995600,112304.898000,436949.370000,112305.106800,436949.766000,112305.333600,436950.183600,112305.592800,436950.612000,112305.862800,436951.047600,112306.140000,436951.479600,112306.410000,436951.929600,112306.672800,436952.376000,112306.942800,436952.811600,112307.223600,436952.811600,112307.223600,436953.693600,112307.778000,436954.125600,112308.030000,436954.536000,112308.282000,436954.924800,112308.519600,436955.302800,112308.760800,436955.680800,112308.994800,436956.033600,112309.218000,436956.357600,112309.426800,436956.642000,112309.606800,436956.876000,112309.750800,436957.077600,112309.876800,436957.077600,112309.876800,436957.383600,112310.064000,436957.488000,112310.128800,436957.570800,112310.172000,436957.635600,112310.200800,436957.689600,112310.236800,436957.732800,112310.254800,436957.758000,112310.272800,436957.768800,112310.265600,436957.776000,112310.265600,436957.768800,112310.262000,436957.776000,112310.262000,436957.768800,112310.265600,436957.768800,112310.262000,436957.776000,112310.265600,436957.797600,112310.283600,436957.866000,112310.326800,436957.948800,112310.373600,436958.067600,112310.445600,436958.211600,112310.532000,436958.373600,112310.640000,436958.560800,112310.748000,436958.766000,112310.866800,436959.000000,112311.003600,436959.270000,112311.162000,436959.540000,112311.316800,436959.828000,112311.489600,436960.144800,112311.684000,436960.494000,112311.900000,436960.857600,112312.126800,436961.253600,112312.360800,436961.656800,112312.623600,436962.070800,112312.893600,436962.502800,112313.178000,436962.952800,112313.469600,436963.402800,112313.746800,436963.860000,112314.024000,436964.320800,112314.294000,436964.320800,112314.294000,436965.220800,112314.826800,436965.688800,112315.071600,436966.185600,112315.287600,436966.686000,112315.503600,436967.175600,112315.708800,436967.676000,112315.899600,436968.162000,112316.094000,436968.612000,112316.248800,436969.011600,112316.400000,436969.378800,112316.526000,436969.702800,112316.637600,436969.998000,112316.752800,436970.250000,112316.860800,436970.469600,112316.940000,436970.656800,112317.004800,436970.818800,112317.058800,436970.973600,112317.112800,436971.142800,112317.174000,436971.315600,112317.231600,436971.510000,112317.303600,436971.726000,112317.372000,436971.978000,112317.447600,436972.266000,112317.537600,436972.590000,112317.645600,436972.932000,112317.753600,436973.292000,112317.861600,436973.680800,112317.969600,436974.069600,112318.066800,436974.465600,112318.156800,436974.850800,112318.239600,436975.246800,112318.326000,436975.246800,112318.326000,436976.092800,112318.488000,436976.517600,112318.570800,436976.982000,112318.678800,436977.450000,112318.794000,436977.910800,112318.923600,436978.368000,112319.046000,436978.821600,112319.164800,436979.289600,112319.290800,436979.782800,112319.424000,436980.297600,112319.535600,436980.819600,112319.643600,436981.356000,112319.758800,436981.896000,112319.874000,436982.439600,112320.003600,436983.004800,112320.136800,436983.570000,112320.270000,436984.149600,112320.417600,436984.732800,112320.568800,436985.298000,112320.741600,436985.859600,112320.921600,436986.424800,112321.108800,436986.982800,112321.317600,436987.540800,112321.540800,436988.088000,112321.764000,436988.620800,112322.008800,436989.142800,112322.260800,436989.654000,112322.523600,436990.143600,112322.790000,436990.618800,112323.052800,436991.094000,112323.286800,436991.580000,112323.502800,436992.066000,112323.690000,436992.534000,112323.837600,436992.951600,112323.978000,436993.318800,112324.089600,436993.624800,112324.179600,436993.887600,112324.251600,436993.887600,112324.251600,436994.298000,112324.374000,436994.445600,112324.420800,436994.568000,112324.464000,436994.640000,112324.482000,436994.650800,112324.485600,436994.650800,112324.485600,436994.643600,112324.482000,436994.643600,112324.482000,436994.643600,112324.474800,436994.643600,112324.482000,436994.643600,112324.482000,436994.640000,112324.482000,436994.640000,112324.482000,436994.640000,112324.474800,436994.640000,112324.474800,436994.640000,112324.467600,436994.640000,112324.464000,436994.640000,112324.456800,436994.640000,112324.449600,436994.640000,112324.446000,436994.640000,112324.446000,436994.643600,112324.438800,436994.640000,112324.438800,436994.640000,112324.438800,436994.640000,112324.438800,436994.643600,112324.431600,436994.650800,112324.431600,436994.650800,112324.428000,436994.650800,112324.420800,436994.650800,112324.413600,436994.658000,112324.410000,436994.658000,112324.402800,436994.661600,112324.395600,436994.661600,112324.395600,436994.668800,112324.395600,436994.676000,112324.392000,436994.679600,112324.384800,436994.686800,112324.377600,436994.694000,112324.377600,436994.697600,112324.377600,436994.704800,112324.377600,436994.712000,112324.377600,436994.712000,112324.377600,436994.715600,112324.377600,436994.715600,112324.384800,436994.722800,112324.384800,436994.722800,112324.384800,436994.722800,112324.384800,436994.730000,112324.384800,436994.730000,112324.384800,436994.733600,112324.392000,436994.740800,112324.392000,436994.740800,112324.395600,436994.740800,112324.395600,436994.766000,112324.402800,436994.838000,112324.420800,436994.949600,112324.456800,436995.090000,112324.492800,436995.244800,112324.521600,436995.432000,112324.539600,436995.612000,112324.539600,436995.784800,112324.536000,436995.936000,112324.500000,436996.054800,112324.428000,436996.170000,112324.287600,436996.299600,112324.089600,436996.378800,112323.906000,436996.468800,112323.690000,436996.551600,112323.441600,436996.648800,112323.186000,436996.756800,112322.916000,436996.854000,112322.638800,436996.972800,112322.379600,436997.055600,112322.134800,436997.055600,112322.134800,436997.232000,112321.648800,436997.314800,112321.386000,436997.415600,112321.116000,436997.548800,112320.813600,436997.682000,112320.507600,436997.800800,112320.198000,436997.908800,112319.892000,436997.991600,112319.586000,436998.060000,112319.316000,436998.114000,112319.049600,436998.186000,112318.858800,436998.268800,112318.725600,436998.286800,112318.624800,436998.297600,112318.560000,436998.304800,112318.534800,436998.312000,112318.524000,436998.294000,112318.506000,436998.276000,112318.498800,436998.261600,112318.498800,436998.258000,112318.491600,436998.258000,112318.491600,436998.243600,112318.480800,436998.243600,112318.473600,436998.232800,112318.470000,436998.225600,112318.462800,436998.222000,112318.452000,436998.207600,112318.444800,436998.214800,112318.434000,436998.222000,112318.408800,436998.225600,112318.354800,436998.243600,112318.290000,436998.240000,112318.236000,436998.207600,112318.182000,436998.214800,112318.156800,436998.214800,112318.149600,436998.225600,112318.156800,436998.232800,112318.156800,436998.232800,112318.156800,436998.232800,112318.156800,436998.243600,112318.164000,436998.243600,112318.167600,436998.240000,112318.167600,436998.240000,112318.167600,436998.225600,112318.167600,436998.225600,112318.174800,436998.214800,112318.174800,436998.207600,112318.167600,436998.196800,112318.174800,436998.186000,112318.174800,436998.178800,112318.174800,436998.178800,112318.174800,436998.168000,112318.182000,436998.160800,112318.182000,436998.153600,112318.182000,436998.150000,112318.185600,436998.150000,112318.185600,436998.142800,112318.192800,436998.142800,112318.192800,436998.135600,112318.192800,436998.135600,112318.185600,436998.132000,112318.185600,436998.132000,112318.164000,436998.135600,112318.102800,436998.135600,112318.020000,436998.117600,112317.922800,436998.070800,112317.807600,436998.045600,112317.678000,436998.045600,112317.678000,436998.016800,112317.444000,436998.006000,112317.310800,436997.998800,112317.202800,436998.052800,112317.040800,436998.214800,112316.796000,436998.261600,112316.619600,436998.322800,112316.482800,436998.322800,112316.482800,436998.322800,112316.482800,436998.441600,112316.205600,436998.502800,112316.032800,436998.574800,112315.834800,436998.657600,112315.600800,436998.747600,112315.338000,436998.855600,112315.042800,436998.945600,112314.718800,436999.053600,112314.405600,436999.176000,112314.099600,436999.302000,112313.782800,436999.428000,112313.469600,436999.528800,112313.178000,436999.644000,112312.911600,436999.734000,112312.666800,436999.824000,112312.422000,436999.906800,112312.188000,437000.004000,112311.964800,437000.086800,112311.756000,437000.158800,112311.568800,437000.158800,112311.568800,437000.284800,112311.252000,437000.284800,112311.252000,437000.284800,112311.252000,437000.364000,112311.003600,437000.364000,112311.003600,437000.364000,112311.003600,437000.392800,112310.841600,437000.367600,112310.740800,437000.392800,112310.658000,437000.428800,112310.578800,437000.446800,112310.506800,437000.457600,112310.442000,437000.454000,112310.352000,437000.446800,112310.236800,437000.464800,112310.121600,437000.500800,112310.002800,437000.547600,112309.866000,437000.616000,112309.722000,437000.698800,112309.578000,437000.778000,112309.473600,437000.824800,112309.390800,437000.868000,112309.336800,437000.914800,112309.290000,437000.950800,112309.236000,437000.979600,112309.164000,437001.022800,112309.066800,437001.058800,112308.969600,437001.058800,112308.969600,437001.120000,112308.771600,437001.130800,112308.670800,437001.174000,112308.573600,437001.213600,112308.472800,437001.264000,112308.357600,437001.321600,112308.213600,437001.382800,112308.069600,437001.465600,112307.922000,437001.562800,112307.770800,437001.642000,112307.626800,437001.699600,112307.464800,437001.771600,112307.284800,437001.832800,112307.079600,437001.879600,112306.881600,437001.879600,112306.665600,437001.933600,112306.500000,437001.966000,112306.359600,437002.005600,112306.230000,437002.048800,112306.078800,437002.092000,112305.927600,437002.146000,112305.783600,437002.185600,112305.636000,437002.239600,112305.484800,437002.300800,112305.333600,437002.365600,112305.196800,437002.419600,112305.060000,437002.419600,112305.060000,437002.491600,112304.826000,437002.534800,112304.721600,437002.570800,112304.620800,437002.596000,112304.520000,437002.606800,112304.430000,437002.624800,112304.343600,437002.635600,112304.278800,437002.632000,112304.232000,437002.635600,112304.196000,437002.635600,112304.170800,437002.635600,112304.152800,437002.614000,112304.142000,437002.588800,112304.134800,437002.563600,112304.124000,437002.542000,112304.116800,437002.542000,112304.109600,437002.527600,112304.106000,437002.527600,112304.088000,437002.534800,112304.062800,437002.545600,112304.019600,437002.570800,112303.980000,437002.596000,112303.929600,437002.632000,112303.872000,437002.668000,112303.785600,437002.707600,112303.713600,437002.750800,112303.638000,437002.804800,112303.551600,437002.833600,112303.461600,437002.858800,112303.371600,437002.866000,112303.296000,437002.894800,112303.216800,437002.941600,112303.170000,437003.028000,112303.144800,437003.103600,112303.152000,437003.190000,112303.170000,437003.290800,112303.173600,437003.406000,112303.191600,437003.571600,112303.206000,437003.740800,112303.263600,437003.928000,112303.314000,437004.126000,112303.360800,437004.342000,112303.414800,437004.342000,112303.414800,437004.810000,112303.558800,437004.810000,112303.558800,437005.029600,112303.602000,437005.029600,112303.602000,437005.342800,112303.767600,437005.512000,112303.836000,437005.677600,112303.890000,437005.893600,112303.864800,437006.109600,112303.900800,437006.314800,112303.954800,437006.505600,112304.016000,437006.685600,112304.080800,437006.865600,112304.134800,437007.070800,112304.206800,437007.286800,112304.304000,437007.567600,112304.448000,437007.909600,112304.613600,437008.230000,112304.757600,437008.518000,112304.880000,437008.806000,112304.934000,437009.076000,112304.944800,437009.310000,112304.880000,437009.547600,112304.818800,437009.716800,112304.746800,437009.868000,112304.631600,437010.004800,112304.433600,437010.130800,112304.235600,437010.202800,112304.080800,437010.282000,112303.918800,437010.336000,112303.767600,437010.390000,112303.638000,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800,437010.444000,112303.504800};
17publicintii=100;
18
19
20Ts_verify_r_Result_t user_ok = null;
21
22Terminal termthis = null;
23RpcCommAdapter adapter = null;
24Thread thread;
25SnsTestMain(){
26
27
28thread = new Thread(new Runnable(){
29public void run(){
30try{
31while(true){
32Thread.sleep(2000);
33if(tsprx!=null){
34heartbeat();
35send_gps();
36}
37
38}
39}catch(Exception e){
40System.out.println(e.toString());
41System.out.println("thread exiting");
42}
43}
44
45});
46//thread.start();
47
48initServant();
49}
50
51void initServant(){
52adapter = RpcCommunicator.instance().createAdapter("termimal",RpcConsts.MSG_ENCODE_XML);
53termthis = new Terminal(){
54@Override
55public void hello(RpcContext ctx){
56
57}
58};
59adapter.addServant(termthis);
60}
61
62void heartbeat(){
63Ts_heartbeat_p_User_t user = new Ts_heartbeat_p_User_t();
64user.id = user_ok.user.id;
65tsprx.heartbeat_oneway(user);
66}
67
68
69void send_gps(){
70Ts_gps_p_GPS_t gps = new Ts_gps_p_GPS_t();
71gps.lon= Float.valueOf((float)121.2004);
72gps.lat= Float.valueOf((float)31.1234);
73gps.speed = (float)0.0;
74gps.direction = (float)35.5;
75gps.time = new Date().getSeconds();
76Ts_gps_p_Properties_t props = new Ts_gps_p_Properties_t();
77props.senderid = user_ok.user.id;
78props.target_type="2";
79props.users="1001,1002";
80props.groups="2001,2001";
81tsprx.gps_oneway(gps, props);
82
83}
84
85public Tc_redirect_r_Result_t test_redirect(){
86
87TcProxy tc = TcProxy.createWithXML("172.26.181.193", 8881); //8889
88Tc_redirect_p_User_t user = new Tc_redirect_p_User_t();
89user.token = token;
90Tc_redirect_r_Result_t r = null;
91try{
92tc.redirect_async(user, new Tc_AsyncCallBack(){
93@Override
94public void redirect(Tc_redirect_r_Result_t result,RpcProxyBase proxy){
95System.out.println(String.format("%s %d", result.tc_srv_ip,result.tc_srv_port));
96//到这里重定向调度okay
97test_verify(result.tc_srv_ip,result.tc_srv_port);
98}
99@Override
100protected void onError(String name,String msg,RpcProxyBase proxy){
101System.out.println(String.format("error: %s msg:%s",name,msg));
102}
103});
104//tc.destroy();
105}catch(Exception e){
106System.out.println(e.toString());
107return null;
108}
109return r;
110}
111
112public Tc_redirect_r_Result_t test_verify(String host,int port){
113tsprx = TsProxy.createWithXML(host, port);
114Ts_verify_p_User_t user = new Ts_verify_p_User_t();
115user.token = token;
116try{
117tsprx.verify_async(user, new Ts_AsyncCallBack(){
118@Override
119public void verify(Ts_verify_r_Result_t result,RpcProxyBase proxy){
120System.out.println(String.format("verify back() msg:%s code:%d gourps.length:%d", result.msg,result.code,result.groups.size()));
121user_ok = result;
122tsprx.conn.attachAdapter(adapter);
123send_gps();
124}
125});
126//ts.destroy();
127}catch(Exception e){
128System.out.println(e.toString());
129return null;
130}
131return null;
132}
133
134
135public static void test(){
136SnsTestMain sns = new SnsTestMain();
137sns.test_redirect();
138}
139
140public static void mainxxx(String[] args) {
141SnsTestMain.test();
142
143}
144
145 }
146
IDL 语法树结构 python+ ply
lexical.py
1 importsys
2
3 language = 'py'
4 arch = '32'
5
6 class SyntexTreeNode:
7def __init__(self,name):
8self.name = None
9
10 class Container:
11def __init__(self,name=''):
12 #SyntexTreeNode.__init__(self,name)
13self.children={}
14self.list=[]
15
16def addChild(self,c):
17#self.children.append(c)
18self.children[c.getName()] = c
19self.list.append(c)
20
21def createStruct(self,st):
22pass
23
24def createInterface(self,ifc):
25pass
26
27def createSequence(self,seq):
28pass
29
30def createDictionary(self,dict):
31pass
32
33def createEnumeration(self,enm):
34pass
35
36def createUnit(self,unit):
37pass
38
39
40
41
42
43 class Contained:
44def __init__(self,container=None):
45self.container =container
46
47 class TypeId:
48def __init__(self,type,id): #变量定义type id;
49self.type = type # int name, <int> is type, <name> is id
50self.id = id
51
52
53 class TypeBase:
54def __init__(self,name):
55self.name = name
56self.idx = 0 #索引,用于数据序列化时类型识别
57#self.type = name
58
59def getName(self):
60return self.name
61
62def getTypeDefaultValue(self):
63if language == 'py':
64return '%s()'%self.name
65if language =='as':
66return 'new %s()'%self.name
67if language =='cpp':
68return '%s()'%self.name
69if language =='java':
70return 'new %s()'%self.name
71
72def getMappingTypeName(self):
73r = self.name
74
75return r
76
77 class DataMember(Contained):
78def __init__(self,d,container): # d - type_id
79Contained.__init__(self,container)
80self.d = d
81self.name = d.id
82self.type = d.type
83
84 class Sequence(Contained,TypeBase):
85def __init__(self,name,type):
86TypeBase.__init__(self,name)
87self.type = type # sequence<type> name;
88self.valuetype = type
89
90def getTypeDefaultValue(self):
91if language == 'py':
92return '[]'
93if language == 'as':
94return 'new Array()'
95if language =='cpp':
96return 'std::vector< %s >()'%self.type.getMappingTypeName()
97if language =='java':
98return 'new Vector<%s>()'%self.type.getMappingTypeName()
99
100def getMappingTypeName(self):
101r = ''
102if language == 'as':
103r = 'Array'
104
105if language =='cpp':
106return 'std::vector< %s >'%self.type.getMappingTypeName()
107if language =='java':
108return 'Vector<%s>'%self.type.getMappingTypeName()
109
110return r
111
112 class Dictionary(Contained,TypeBase):
113def __init__(self,name,first,second):
114TypeBase.__init__(self,name)
115self.first = first
116self.second = second
117
118def getTypeDefaultValue(self):
119if language =='py':
120return '{}'
121if language == 'as':
122return 'new HashMap()'
123
124if language == 'cpp':
125#return 'boost::shared_ptr< std::map<%s,%s> >( new std::map<%s,%s>()) '%(self.first.name,self.second.name,self.first.name,self.second.name)
126 #return '%s::hash_type()'%self.name
127return 'std::map< %s,%s >()'%(self.first.getMappingTypeName(),self.second.getMappingTypeName())
128if language =='java':
129return 'new Hashtable<%s,%s>()'%(self.first.getMappingTypeName(),
130self.second.getMappingTypeName()
131)
132
133
134def getMappingTypeName(self):
135r = ''
136if language == 'as':
137r = 'HashMap'
138if language =='cpp':
139 #return 'boost::shared_ptr< std::map<%s,%s> >'%(self.first.name,self.second.name)
140 #return '%s::hash_type'%(self.name)
141r = 'std::map< %s,%s >'%(self.first.getMappingTypeName(),self.second.getMappingTypeName() )
142
143if language == 'java':
144r = 'Hashtable< %s,%s >'%(self.first.getMappingTypeName(),self.second.getMappingTypeName() )
145
146return r
147
148 class Enumeration(Contained,TypeBase):
149def __init__(self,name):
150TypeBase.__init__(name)
151
152 class OperateMember(Contained):
153def __init__(self,name,type,params):
154self.name = name
155self.type = type # callreturn[ typefoo(params) ]
156self.params = params
157pass
158
159 class Struct(Container,Contained,TypeBase):
160def __init__(self,name):
161Container.__init__(self)
162Contained.__init__(self)
163TypeBase.__init__(self,name)
164
165def createDataMember(self,dm):
166if self.children.has_key(dm.id):
167return False
168e = DataMember(dm,self)
169self.children[dm.id] = e
170self.list.append(e)
171return True
172
173
174
175 class Interface(Container,Contained,TypeBase):
176def __init__(self,name):
177Container.__init__(self)
178Contained.__init__(self)
179TypeBase.__init__(self,name)
180
181
182
183def createOperateMember(self,opm):
184
185if self.children.has_key(opm.name):
186return False
187self.children[opm.name] = opm
188self.list.append(opm)
189
190return True
191
192 class Unit(Container):
193def __init__(self):
194Container.__init__(self)
195pass
196
197
198 class Builtin(TypeBase):
199def __init__(self,type):
200TypeBase.__init__(self,type)
201self.type = type
202
203tables =[
204'byte',
205'bool',
206'short',
207'int',
208'long',
209'float',
210'double',
211'string',
212]
213
214@staticmethod
215def isBuiltinType(type):
216return Builtin.tables.count(type)
217
218def getTypeDefaultValue(self):
219r = 'None'
220type = self.type
221if type in ('byte','short','int','long'):
222r = '0'
223if language == 'java':
224if type == 'byte':
225#r= '%s.valueOf( (byte)0)'%self.getMappingTypeName()
226r = '0'
227else:
228r = '%s.valueOf(0)'%self.getMappingTypeName()
229
230elif type in ('float','double'):
231r = '0.0'
232if language == 'java':
233r = '%s.valueOf(0)'%self.getMappingTypeName()
234
235elif type in ('bool'):
236r = 'False'
237if language in ('as','cpp'):
238r = 'false'
239 #if language == 'cpp':
240 #r = 'false'
241if language == 'java':
242r = '%s.valueOf(false)'%self.getMappingTypeName()
243
244elif type in ('string'):
245r = "''"
246if language in ('as','cpp','java'):
247r ="\"\""
248 #if language =='cpp':
249 #r = "\"\""
250
251return r
252
253def getMappingTypeName(self):
254r = '-^|^*'*5
255
256if language == 'as':
257type = self.type
258if type in ('byte',) : #'bool'):
259r ='uint'
260if type in ('bool',):
261r = 'Boolean'
262if type in ('short','int'):
263r = 'int'
264elif type in ('float','long','double'):
265r = 'Number'
266elif type in ('string'):
267r = "String"
268elif type in ('void'):
269r ='void'
270
271if language == 'cpp':
272type = self.type
273
274if type in ('byte',) : #'bool'):
275r ='unsigned char'
276if type in ('bool',):
277r = 'bool'
278if type in ('short',):
279r ='short'
280if type in ('int',):
281r = 'int'
282elif type in ('float',):
283r = type
284elif type in ('long',):
285r = type
286if arch =='32':
287r = 'long long'
288elif type in ('double',):
289r = type
290elif type in ('string'):
291r = "std::string"
292elif type in ('void'):
293r ='void'
294
295if language == 'java':
296type = self.type
297
298if type in ('byte',) : #'bool'):
299r ='Byte'
300if type in ('bool',):
301r = 'Boolean'
302if type in ('short',):
303r ='Short'
304if type in ('int',):
305r = 'Integer'
306elif type in ('float',):
307r = 'Float'
308elif type in ('long',):
309r = 'Long'
310
311elif type in ('double',):
312r = 'Double'
313elif type in ('string'):
314r = "String"
315elif type in ('void'):
316r ='void'
317
318return r
319
320@staticmethod
321def id(name):
322pass
323
324@staticmethod
325def str(id_):
326pass
327
328 types_def={}
329 kwds=['struct',
330'interface',
331'sequence',
332'dictionary',
333'exception',
334'void'
335]
336
337 def getTypeDef(type):
338t = types_def.get(type,None)
339return t
340
341 #检测变量名称是否合法
342 def checkVariantName(name,all=True):
343if kwds.count(name):
344return False
345if all:
346if getTypeDef(name):
347return False
348return True
349
350 def initBuiltinTypes():
351for t in Builtin.tables:
352types_def[t] = Builtin(t)
353
354 types_def['void'] = Builtin('void')
355
356 initBuiltinTypes()
357
358 unit=None
继续贴 语法代码 grammar.py
1 #--coding:utf-8--
2
3
4 #scottshanghai china
5 # qq:24509826 msn: socketref@hotmail.com
6 #
7
8 importsys
9
10
11
12 if".."notinsys.path:sys.path.insert(0,"..")
13
14 import os,sys,os.path,struct,time,traceback,string
15
16 importply.lexaslex
17 import ply.yacc as yacc
18
19 from lexparser import *
20 import lexparser as myparser
21
22 '''
23 默认是SLR,我们也可以通过参数指定为 LALR(
24
25 idl
26 interface定义 函数参数保留名称: d,idx,m
27 '''
28
29
30 tokens=(
31'IDENTIFIER','STRUCT','NUMBER','INTERFACE',
32'SEQUENCE','DICTIONARY','EXCEPTION','COMMENTLINE',
33 #'VOID',
34
35)
36
37 def t_COMMENTLINE(t):
38'//.*\n'
39
40 def t_SEQUENCE(t):
41'sequence'
42return t
43
44 def t_DICTIONARY(t):
45'dictionary'
46return t
47
48
49 def t_STRUCT(t):
50'struct'
51return t
52
53
54 def t_INTERFACE(t):
55'interface'
56return t
57
58 #def t_VOID(t):
59 #'void'
60 #return t
61
62 def t_IDENTIFIER(t):
63'[A-Za-z_][A-Za-z0-9_]*'
64return t
65
66 t_NUMBER=r'\d+([uU]|[lL]|[uU][lL]|[lL][uU])?'
67
68 t_ignore="\t"
69
70 deft_newline(t):
71r'\n+'
72t.lexer.lineno+=1 #int(t.value) #.count("\n")
73
74
75 deft_error(t):
76print("Illegalcharacter'%s'"%t.value[0])
77t.lexer.skip(1)
78
79 literals = [ '{','}',';','(',')','<','>',',' ]
80
81 #Buildthelexer
82 lexer = lex.lex()
83
84 #literals = [ ':',',','(',')' ]
85
86
87
88 #lexer.input(data)
89
90 #while 1:
91 #tok = lexer.token()
92 #if not tok: break
93#print tok.type,tok.value
94 #sys.exit()
95
96
97 def p_start(t):
98'''start : definations
99'''
100
101defs = t[1]
102unit = Unit()
103
104defs.reverse()
105
106 #print defs
107for d in defs:
108unit.addChild( d )
109
110
111t[0] = unit
112
113#t[0] = t[1]
114#print t[0]
115
116 def p_definations(t):
117'''definations : defination '''
118
119t[0] = [t[1],]
120
121 def p_definations_2(t):
122'''definations : definationdefinations'''
123t[2].append(t[1])
124t[0]= t[2]
125
126 def p_defination(t):
127''' defination : struct_def ';'
128| interface_def ';'
129| sequence_def ';'
130| dictionary_def ';'
131|
132'''
133t[0]=t[1]
134
135
136 def p_sequence_def(t):
137'''
138sequence_def : SEQUENCE '<' type '>' IDENTIFIER
139'''
140 #print 'sequence..',t[3]
141name = t[5]
142
143if not checkVariantName(name,False):
144print 'error: sequence<%s>.%s illegal!'%(t[3],name)
145sys.exit()
146
147if getTypeDef(name):
148print 'error: line %s sequence (%s) has existed!'%(t.lineno(1),name)
149sys.exit() #sequence的类型名存在
150
151type_ = t[3]
152 #if not getTypeDef(type_):
153 #print 'error: sequence %s <%s> not defined!'%(name,type_)
154 #sys.exit()
155 #print 'xxx.',name,type_
156
157seq = Sequence(name,type_)
158types_def[name] = seq
159t[0] = seq
160
161 def p_dictionary_def(t):
162'''
163dictionary_def : DICTIONARY '<' type ',' type '>' IDENTIFIER
164'''
165first = t[3]
166second = t[5]
167name = t[7]
168 #print first,second,name
169
170if not checkVariantName(name,False):
171print 'error: dictionary < %s > illegal!'%(name)
172sys.exit()
173
174if getTypeDef(name):
175print 'error: line %s dictionary type (%s) has existed!'%(t.lineno(1),name)
176sys.exit() #sequence的类型名存在
177
178
179 #if not getTypeDef(first):
180 #print 'error: dictionary %s.first <%s> not defined!'%(name,first)
181 #sys.exit()
182 #
183 #if not getTypeDef(second):
184 #print 'error: dictionary %s.second <%s> not defined!'%(name,second)
185 #sys.exit()
186
187dict = Dictionary(name,first,second)
188types_def[name] = dict
189t[0] = dict
190
191 def p_interface_def(t):
192'''
193interface_def :INTERFACE IDENTIFIER '{' operatemembers '}'
194'''
195 #print t[1],t[2]
196id = t[2]
197type_ = getTypeDef(id)
198if type_:
199print 'error: interface name(%s) has existed!'%id
200sys.exit()
201
202ifc = Interface(id)
203opms = t[4]
204opms.reverse()
205#检测函数名称是否有重复
206 #print opms
207for opm in opms:
208if not ifc.createOperateMember(opm):
209
210print 'error: line %s createOperateMember failed! interface:: %s.%s'%( t.lineno(3),t[2],opm.name)
211sys.exit()
212 #print type(ifc.children)
213types_def[id] = ifc
214t[0] = ifc # reduce to syntax tree
215
216
217
218 def p_operatemembers(t):
219'''
220operatemembers : operatemember
221'''
222#print 'operatemember num:',len(t) # 1 means no operatemebmer
223 #if len(t) > 1:
224 #t[0] = t[1]
225 #else:
226 #t[0] = []
227t[0] = [t[1],]
228
229
230 def p_operatemembers_2(t):
231'''
232operatemembers : operatemember operatemembers
233'''
234if type(t[2]) != type([]):
235t[0] = [t[1],t[2]]
236else:
237t[2].append(t[1])
238t[0] = t[2]
239
240 def p_operatemember(t):
241'''
242operatemember : callreturn IDENTIFIER '(' operateparams ')' ';'
243
244'''
245params = t[4]
246params.reverse()# 这里必须进行倒置一下
247for p in params:
248if p.id in ('d','p','m','r','idx','size','cr','container','o','prx','m2'):
249p.id+='_'
250opm = OperateMember(t[2],t[1],params)
251t[0] = opm
252 #print 'x1.',opm
253
254 def p_operateparams(t):
255'''
256operateparams : type_id
257|
258'''
259if len(t) > 1:
260t[0] = [ t[1],]
261else:
262t[0] =[ ]
263
264
265 def p_operateparams_2(t):
266'''
267operateparams : type_id ',' operateparams
268'''
269
270t[3].append(t[1])
271t[0] = t[3]
272
273 def p_callreturn(t):
274'''
275callreturn : type
276'''
277t[0] = t[1]
278
279
280 def p_struct_def(t):
281''' struct_def : STRUCT IDENTIFIER '{' datamembers '}' '''
282id = t[2]
283
284type = getTypeDef(id)
285iftype:
286print 'error struct name:%s existed!'%(id)
287sys.exit()
288
289st= Struct(id)
290 #print t[4]
291t[4].reverse()
292for dm in t[4]:
293if not st.createDataMember(dm):
294print 'error: datamember<%s> name<%s> has existed!'%(id,dm.id)
295sys.exit()
296
297types_def[id] = st#注册数据类型到全局类型表
298#print types_def
299t[0] = st
300
301
302
303 def p_datamembers(t):
304'''
305datamembers :datamember
306'''
307#print t[1]
308#t[0] = t[1]
309t[0] = [t[1],]
310#print 'a1..'
311#print t[0]
312
313 def p_datamembers_2(t):
314'''
315datamembers :datamember datamembers
316'''
317if type(t[2]) != type([]):
318t[0] = [t[1],t[2]]
319else:
320t[2].append(t[1])
321t[0] = t[2]
322
323
324 def p_datamember(t):
325'''
326datamember : type_id ';'
327'''
328#print 'datamenber..'
329t[0] =t[1]
330
331 def p_type_id(t):
332'''
333type_id : type IDENTIFIER
334'''
335#print t[1],t[2]
336id = t[2]
337if not checkVariantName(id):
338print 'error: type_id.id<%s> illegal!'%(id)
339sys.exit()
340
341t[0] = TypeId(t[1],t[2])
342
343
344
345
346 def p_type(t):
347'''type : IDENTIFIER
348'''
349type = getTypeDef(t[1])
350
351if type:
352t[0] = type
353else:
354print 'error: line %s'%(t.lineno),' type:%s not existed!'%t[1]
355sys.exit()
356
357
358 def p_error(t):
359print("Syntax error at '%s'" % t)
360#print t.lineno,t.lexpos
361
362 yacc.yacc(debug=True,method="SLR")
363
364 data='''
365
366 sequence < int > IntList;
367
368 struct student {
369int x;
370int y;
371int z;
372double fractor;
373IntList ids;
374 };
375
376 interface booksystem{
377int test1(int age);
378int test2(int age);
379 };
380
381 struct dog{
382int name;
383string dog;
384int name2;
385student st1;
386 };
387
388
389
390 '''
391
392 data='''
393
394 struct animal{
395int x;
396 };
397
398 sequence < animal > IntList;
399
400 struct student {
401int x;
402int y;
403int z;
404double fractor;
405IntList ids;
406 };
407
408 interface booksystem{
409int test1(int age);
410int test2(int age);
411 };
412
413 sequence<IntList>XX;
414
415 dictionary<int,int> int2_t;
416
417 dictionary<int2_t,string> intstr_t;
418
419
420 '''
421
422 def filterComments(data):
423return data
424lines = data.split("\n")
425result=[]
426for line in lines:
427x = line.strip()
428if x and x[0] =='#':
429continue
430result.append(line)
431data = string.join(result,'\n')
432return data
433
434 def syntax_result(data):
435return yacc.parse( filterComments(data))